--- s725xdump.c.orig	2010-04-30 16:34:37.583891747 +0200
+++ s725xdump.c	2010-05-01 14:23:36.599173230 +0200
@@ -35,7 +35,7 @@
 #include <limits.h>
 
 /* Print some extra debug information */
-//#define DEBUG 1
+#define DEBUG 1
 
 /* Add gnuplot headers and titles */
 #define GNUPLOT 1
@@ -146,12 +146,18 @@
 /* Get the number of files */
 int getfiles(int fd) {
 	char buf[32];
+	int upper, lower;
+
 	buf[0]=0x15;
 	send(fd, buf, 1, 0);
 	recv(fd, buf, 32, 0);
-	return buf[3];
+	// Now buf[3] has BCD file numbers
+	upper = buf[3] & 0xf0;
+	upper = upper >> 4;
+	lower = buf[3] & 0x0f;
+	return ( (upper * 10) + lower );
 }
-	
+
 /* Download the files and return the fd to the temp file */
 int dlfiles(int fd) {
 	char* fn;
@@ -222,8 +228,11 @@
 	char buf[1024];
 	char title[64];
 	int i, size, totalsize, laps, samplerate, ms, lastlap, filenum;
+	int manualLaps, units;
+	int flagSpeed1, flagSpeed2, flagPower, flagCadence, flagAltitude;
 	int samples;
 	int* laptimes=NULL;
+	int sampleSize, lapSize;
 
 	filenum=0;
 	totalsize=readshort(fd);
@@ -262,22 +271,69 @@
 		fprintf(stdout, "# AvgHR: %d\n", (unsigned char)buf[0]);
 		fprintf(stdout, "# MaxHR: %d\n", (unsigned char)buf[1]);
 
-		read(fd, buf, 6); size-=6;
+		read(fd, buf, 1); size-=1;
 		laps=(unsigned char)buf[0];
 		fprintf(stdout, "# Laps: %d\n", laps);
+		read(fd, buf, 1); size-=1;
+		manualLaps=(unsigned char)buf[0];
 		laptimes=calloc(laps+2, sizeof(int));
 		laptimes[0]=0;
 		laptimes[laps+1]=INT_MAX;
 		lastlap=0;
 
+		// Exercise mode and User ID
+		read(fd, buf, 2); size-=2;
+
+		// Units
 		read(fd, buf, 1); size-=1;
-		switch(buf[0]) {
+		units=(unsigned char)buf[0];
+
+		// Various optional recordings
+		read(fd, buf, 1); size-=1;
+		flagSpeed1=(unsigned char)buf[0] & 0x20;
+#ifdef DEBUG
+		fprintf(stdout, "# Speed (Bike 1): %d\n", flagSpeed1);
+#endif
+		flagSpeed2=(unsigned char)buf[0] & 0x10;
+#ifdef DEBUG
+		fprintf(stdout, "# Speed (Bike 2): %d\n", flagSpeed2);
+#endif
+		flagPower=(unsigned char)buf[0] & 0x08;
+#ifdef DEBUG
+		fprintf(stdout, "# Speed Power: %d\n", flagPower);
+#endif
+		flagCadence=(unsigned char)buf[0] & 0x04;
+#ifdef DEBUG
+		fprintf(stdout, "# Cadence: %d\n", flagCadence);
+#endif
+		flagAltitude=(unsigned char)buf[0] & 0x02;
+#ifdef DEBUG
+		fprintf(stdout, "# Altitude: %d\n", flagAltitude);
+#endif
+		lapSize = 6;
+		if (flagAltitude != 0) { lapSize += 5; }
+		if ((flagSpeed1 != 0) || (flagSpeed2 != 0)) {
+			if (flagCadence != 0) lapSize += 1;
+			if (flagPower != 0) lapSize += 4;
+			lapSize += 4; // Speed data
+			// Interval Training can affect lap size
+		}
+                sampleSize = 1;
+		if (flagAltitude != 0) sampleSize += 2;
+		if ((flagSpeed1 != 0) || (flagSpeed2 != 0)) {
+			if (flagAltitude != 0) sampleSize -= 1;
+			if (flagPower != 0) sampleSize += 4;
+			if (flagCadence != 0) sampleSize += 1;
+		}
+
+		read(fd, buf, 1); size-=1;
+                // & 0xf seems accurate.  The upper bits are used for something else
+		switch(buf[0] & 0xf) {
 			case 0: samplerate=5; break;
 			case 1: samplerate=15; break;
 			case 2: samplerate=60; break;
 			default: samplerate=1; break;
 		}
-
 		if(samplerate==1) 
 			fprintf(stdout, "# RecordingRate: unknown (%02hhx)\n", buf[0]);
 		else 
@@ -292,7 +348,7 @@
 		skipbytes(fd, 21); size-=21; 
 
 		for(i=0; i<laps; i++) {
-			read(fd, buf, 10); size-=10;
+			read(fd, buf, lapSize); size-=lapSize;
 			ms=0;
 			ms+=((buf[0]>>6)&0x03) * 100;
 			ms+=((buf[1]>>6)&0x03) * 400;
@@ -329,22 +385,27 @@
 		fprintf(stdout, "##End gnuplot section\n");
 #endif
 
-		if(size<0) { 
+		if(size<0) {
 			fprintf(stderr, "Parsing error, overshot size: %d\n", size);
 			return -1;
 		}
-		if(size%3 != 0) {
-			fprintf(stderr, "Parsing error, bytes%%3!=0: %d (trying anyways)\n",size);
+		if((size % sampleSize) != 0) {
+			fprintf(stderr, "Parsing error, bytes mod sampleSize !=0: %d (trying anyways)\n",size);
 		}
-		samples=size/3;
+		samples=size / sampleSize;
 
 		i=0;
-		fprintf(stdout, "## Second   Heartrate Unknown Unknown\n");
-		while(size>0) {
-			read(fd, buf, 3); size-=3;
+		fprintf(stdout, "## Second   Heartrate ");
+		if (flagAltitude != 0) fprintf(stdout, "Altitude ");
+		fprintf(stdout, "\n");
+		while(size > 0) {
+			read(fd, buf, sampleSize); size -= sampleSize;
 
 			samples--;
-			fprintf(stdout, "%d   %hhu %hhu %hhu\n", samples*samplerate, buf[0], buf[1], buf[2]);
+			fprintf(stdout, "%d   %hhu ", samples*samplerate, buf[0]);
+			if (flagAltitude != 0)
+				fprintf(stdout, "%d ", ( (int)(buf[2] & 0x1f) << 8 ) + buf[1]);
+			fprintf(stdout, "\n");
 			i++;
 		}
 
@@ -389,8 +450,9 @@
 	fprintf(stderr, "Connected!\n");
 
 	printtime(fd);
-//	files=getfiles(fd);
-//	fprintf(stderr, "There are %d files\n", files);
+        // I need the number of files.
+	files=getfiles(fd);
+	fprintf(stderr, "There are %d files\n", files);
 	files=dlfiles(fd);
 	parsefiles(files);
 
