Monday, March 23, 2009

iPod lap timer parser

The iPod Nano is a beautiful thing.  That is unless you want to do something custom.  So it is with the lap timer in it.  There is no obvious way of synchronising the timer logs with the computer.  Even if there is a way in ITunes (I’m pretty sure there isn’t), the point is moot, as I don’t use ITunes (instead I’m using the wonderful ml_ipod plugin for Winamp).  This is “suboptimal” if you want to, for example, use the lap timer to chart your jogging progress over the course of a year.  You want to crunch those stats in Excel or some such, you certainly aren’t going to copy them from the screen.

The information on the web is a little sparse on this matter, but there are a number of applications that will let you do this.  The one that caught my eye was http://wafflesoftware.net/ipodtimer/ which luckily provided source code (Mac OS X only – aren’t we feeling exclusive).  A relatively trivial exercise in parsing.  The timer format was loosely described in the TimerFormat.txt in the zipped source.  At least detailed enough for me to whip up an implementation in C#.  Hence this post.

The underlying format is a binary file.  Time to dust off .NET’s BinaryReader.  The below method ReadTimerEntries(string filepath) will read in an iPod timer file (on my Nano it’s /IPod_Control/device/timer) and produce a list containing the date of the recording, and the associated laps.  From there you have all the info you need.  Anyway, it works for me, on an iPod Nano (3rd gen).  I don’t imagine that the file structure changes much though.

  1. public partial class Form1 : Form 
  2.     public Form1() 
  3.     { 
  4.         InitializeComponent(); 
  5.     } 
  6.  
  7.     private void button1_Click(object sender, EventArgs e) 
  8.     { 
  9.         OpenFileDialog ofd = new OpenFileDialog(); 
  10.         ofd.FileName = Application.StartupPath; 
  11.         DialogResult dr =ofd.ShowDialog(); 
  12.         if (dr == DialogResult.OK) 
  13.         { 
  14.             List<TimeEntry> entries = ReadTimerEntries(ofd.FileName); 
  15.             foreach (TimeEntry entry in entries) 
  16.             { 
  17.                 System.Diagnostics.Debug.WriteLine(entry.EntryDate.ToString()); 
  18.                 foreach (TimeSpan ts in entry.Laps) 
  19.                 { 
  20.                     System.Diagnostics.Debug.WriteLine(ts.ToString()); 
  21.                 } 
  22.             } 
  23.         } 
  24.     } 
  25.  
  26.     public List<TimeEntry> ReadTimerEntries(string filePath) 
  27.     { 
  28.         List<TimeEntry> results = new List<TimeEntry>(); 
  29.         //open the file 
  30.         var stream = File.OpenRead(filePath); 
  31.         BinaryReader br = new BinaryReader(stream); 
  32.         br.ReadBytes(4); //start file 
  33.         while (!br.ReadBytes(4).SequenceEqual(new byte[] { 0x13, 0, 0, 0xC0 })) 
  34.         { 
  35.             //first 4 bytes are already consumed 
  36.             ReadRound(br, results); 
  37.         } 
  38.         return results; 
  39.     } 
  40.  
  41.  
  42.     private void ReadRound(BinaryReader br, List<TimeEntry> itemList) 
  43.     {             
  44.         br.ReadBytes((16 * 5));// guff 
  45.          
  46.         br.ReadBytes(8);// start date 
  47.         byte seconds = br.ReadByte(); 
  48.         byte minutes = br.ReadByte(); 
  49.         byte hour = br.ReadByte(); 
  50.         byte day = br.ReadByte(); 
  51.         byte month = br.ReadByte(); 
  52.         int year = br.ReadInt16(); 
  53.         br.ReadByte(); //unknown use 
  54.         TimeEntry entry = new TimeEntry(); 
  55.         entry.EntryDate = new DateTime(year, month, day, hour, minutes, seconds); 
  56.         itemList.Add(entry); 
  57.         br.ReadBytes(4); //end date 
  58.  
  59.         br.ReadBytes(4); //start laps 
  60.         while (!br.ReadBytes(4).SequenceEqual(new byte[] { 0x10, 0, 0, 0xC0  })) 
  61.         { 
  62.             br.ReadBytes(4); //start lap 
  63.             int lapMilliseconds = br.ReadInt32(); //4 bytes 
  64.             TimeSpan lapTime = TimeSpan.FromMilliseconds(lapMilliseconds); 
  65.             entry.Laps.Add(new TimeSpan(0, 0, 0, 0, lapMilliseconds)); 
  66.             br.ReadBytes(4); //end lap 
  67.         } 
  68.         br.ReadBytes(4); //end round 
  69.     } 
  70.  
  71.  
  72. public class TimeEntry 
  73.     public DateTime EntryDate; 
  74.     public List<TimeSpan> Laps = new List<TimeSpan>(); 

Sunday, March 22, 2009

Terminator Salvation – coming to cinemas May 21

Ahh, the Terminator movie that we’ve always wanted – entirely set in the future, focusing on the war between man and machine.  I’ve always wondered when they would show the future they hinted at, a void slightly filled by the Terminator TV series (Sarah Connor Chronicles).  Another point of note is that this coming instalment is but the beginning of a Terminator revival – part of a new trilogy apparently.  And you can’t go wrong with Christian Bale as John Connor.

Thursday, March 5, 2009

Reading database tables in Powershell

Firstly, a correction.  Last time I posted about Powershell, I came up with something that did the job, but was, well, a tat long winded.  Kind of like building a skyscraper so that you can store garden tools in the basement.  Basically I didn’t realise that the Sort cmdlet already had a –Unique switch.  Thanks to Stephen Mills for pointing it out:

import-csv c:\mydata.csv | select Category, Subcategory | sort category, subcategory -Unique | Group-Object -Property category

Now to the business at hand – reading database tables.  Consider a situation where you’re asked to read in the data in a database table into some readable form, for reference, printing or whatnot.  Rather than using SQL Management Studio to query the results, selecting the entire results grid, copying it, pasting into Excel (if available on the same machine), or into a text file, then getting it into Excel – you could just use Powershell to connect to the DB and dump out the results into a HTML table.  Behold:

  1. # Parse Database tables 
  2. # This will connect to a database, do a "select *" on a table or view, and produce a html file with the data in a table. 
  3. # Note: don't forget to loosen up your execution policy "Set-ExecutionPolicy Unrestricted" 
  4. # and the connection string is ADO style : "Data Source=database;Initial Catalog=oesc_offerman;User Id =username;Password=password;Trusted_Connection=False;" 
  5.  
  6. param
  7. $connectionString = $(throw "Specify connection string" ), 
  8. $tableName = $(throw "Specify a table or view name"), 
  9. $outputPath =  $(throw "Specify output path"
  10.  
  11. echo ("Processing " + $tableName
  12. $table = new-object System.Data.DataTable; 
  13. $sqlConn = new-object System.Data.SqlClient.SqlConnection($connectionString); 
  14. $sqlConn.Open(); 
  15. $adapter = new-object System.Data.SqlClient.SqlDataAdapter(("select * from " +$tableName),$sqlConn); 
  16. $adapter.Fill($table); 
  17. $sqlConn.Close(); 
  18. $table.Rows | ConvertTo-Html | out-file $outputPath 

Wednesday, March 4, 2009

Fish blimp

Had to share this one – from a homemade airship competition, footage of a small blip that uses a tail fin for propulsion – it looks very graceful, especially for a blimp – sure it’s not encumbered, with no wind, but it’s still mesmerising to watch:


Air Art from flip on Vimeo.