[PATCH 3/4] Cochran: change progress bar behaviour

John Van Ostrand john at vanostrand.com
Wed Nov 19 12:11:40 PST 2014


The progress bar now shows a single progress for all reads.
---
 src/cochran_commander.c | 69 ++++++++++++++++++++++++++-----------------------
 1 file changed, 37 insertions(+), 32 deletions(-)

diff --git a/src/cochran_commander.c b/src/cochran_commander.c
index fdb1390..838f423 100644
--- a/src/cochran_commander.c
+++ b/src/cochran_commander.c
@@ -116,7 +116,7 @@ cochran_packet (cochran_device_t *device, const unsigned char command[],
 		bytes_read += n;
 
 		if (device->progress) {
-			device->progress->current = bytes_read;
+			device->progress->current += n;
 			device_event_emit (abstract, DC_EVENT_PROGRESS, device->progress);
 		}
 	}
@@ -517,17 +517,6 @@ cochran_read_logbook (dc_device_t *abstract)
 		return DC_STATUS_NOMEMORY;
 	}
 
-	// Enable progress notifications.
-	device->progress = malloc(sizeof(dc_event_progress_t));
-	if (device->progress == NULL) {
-		ERROR (abstract->context, "Failed to allocate memory.");
-		return DC_STATUS_NOMEMORY;
-	}
-
-	device->progress->current = 0;
-	device->progress->maximum = d->logbook_size;
-	device_event_emit (abstract, DC_EVENT_PROGRESS, device->progress);
-
 	serial_sleep(device->port, 800);
 
 	// set back to 9600 baud
@@ -537,10 +526,6 @@ cochran_read_logbook (dc_device_t *abstract)
 	rc = cochran_commander_device_read(abstract, 0, d->logbook,
 		d->logbook_size);
 
-	device->progress->current = d->logbook_size;
-	device_event_emit (abstract, DC_EVENT_PROGRESS, device->progress);
-	free (device->progress);
-
 	return rc;
 }
 
@@ -632,17 +617,6 @@ cochran_read_samples(dc_device_t *abstract)
 			return DC_STATUS_NOMEMORY;
 		}
 
-		// Enable progress notifications.
-		device->progress = malloc(sizeof(dc_event_progress_t));
-		if (device->progress == NULL) {
-			ERROR (abstract->context, "Failed to allocate memory.");
-			return DC_STATUS_NOMEMORY;
-		}
-
-		device->progress->current = 0;
-		device->progress->maximum = d->sample_size;
-		device_event_emit (abstract, DC_EVENT_PROGRESS, device->progress);
-
 		serial_sleep(device->port, 800);
 
 		// set back to 9600 baud
@@ -652,14 +626,9 @@ cochran_read_samples(dc_device_t *abstract)
 		rc = cochran_commander_device_read (abstract, d->sample_data_offset,
 				d->sample, d->sample_size);
 		if (rc != DC_STATUS_SUCCESS) {
-			free (device->progress);
 			ERROR (abstract->context, "Failed to read the sample data.");
 			return rc;
 		}
-
-		device->progress->current = d->sample_size;
-		device_event_emit (abstract, DC_EVENT_PROGRESS, device->progress);
-		free (device->progress);
 	}
 
 	return DC_STATUS_SUCCESS;
@@ -674,12 +643,40 @@ cochran_commander_device_read_all (dc_device_t *abstract)
 	cochran_config_t *conf = &d->conf;
 
 	dc_status_t rc;
+	int sample_size;
+	int max_config, max_misc, max_logbook, max_sample;
+
+	if ((conf->model & 0xFFFF0000) == COCHRAN_MODEL_COMMANDER_FAMILY)
+		sample_size = 2;
+	else
+		sample_size = 3;
+
+	max_config = 512 * 4; 					// max config page size
+	max_misc = 1500;						// misc size
+	max_logbook = 1024 * conf->log_size; 	// max logbook size
+	max_sample = 1450 * 3600 * sample_size;	// max sample size
+
+	// Enable progress notifications.
+	device->progress = malloc(sizeof(dc_event_progress_t));
+	if (device->progress == NULL) {
+		ERROR (abstract->context, "Failed to allocate memory.");
+		return DC_STATUS_NOMEMORY;
+	}
+
+	device->progress->current = 0;
+	device->progress->maximum = max_config + max_misc + max_logbook
+		+ max_sample;
+	device_event_emit (abstract, DC_EVENT_PROGRESS, device->progress);
 
 	// Read config
 	rc = cochran_read_config(abstract);
 	if (rc != DC_STATUS_SUCCESS)
 		return rc;
 
+	// Update max based on what's read
+	device->progress->maximum -=  max_config - device->progress->current;
+	device_event_emit (abstract, DC_EVENT_PROGRESS, device->progress);
+
 	rc = cochran_read_misc(abstract);
 	if (rc != DC_STATUS_SUCCESS)
 		return rc;
@@ -693,6 +690,9 @@ cochran_commander_device_read_all (dc_device_t *abstract)
 	d->logbook_size = ((d->dive_count * conf->log_size) & 0xFFFFC000)
 		 + 0x4000;
 
+	device->progress->maximum -= max_logbook - d->logbook_size;
+	device_event_emit (abstract, DC_EVENT_PROGRESS, device->progress);
+
 	rc = cochran_read_logbook(abstract);
 	if (rc != DC_STATUS_SUCCESS)
 		return rc;
@@ -701,10 +701,15 @@ cochran_commander_device_read_all (dc_device_t *abstract)
 	cochran_find_fingerprint(abstract);	
 	cochran_get_sample_parms(abstract);
 
+	device->progress->maximum -= max_sample - d->sample_size;
+	device_event_emit (abstract, DC_EVENT_PROGRESS, device->progress);
+
 	rc = cochran_read_samples(abstract);
 	if (rc != DC_STATUS_SUCCESS)
 		return rc;
 
+	free (device->progress);
+
 	return DC_STATUS_SUCCESS;
 }
 
-- 
1.8.3.1



More information about the devel mailing list