Hi again.

Here's another patch that adds a sigrok_samplerate_parse() function to
the sigrok library, and uses it from mainwindow.cpp in the GUI to set
the current sample rate before starting capture.

For me, it pops up the capture dialog and renders a bunch of time
lines and other interesting-looking stuff in the main window.

Regards,

/Emil
diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp
index 57b4c6f..98f9df1 100644
--- a/gui/mainwindow.cpp
+++ b/gui/mainwindow.cpp
@@ -616,7 +616,13 @@ void MainWindow::on_action_Get_samples_triggered()
 		return;
 	}
 
-	/* TODO */
+	uint64_t rate = sigrok_samplerate_parse(ui->comboBoxSampleRate->currentText().toUtf8().constData());
+	if (rate > 0 && device->plugin->set_configuration(device->plugin_index,
+				  HWCAP_SAMPLERATE, &rate) != SIGROK_OK) {
+		qDebug("Failed to set sample rate.");
+		session_destroy();
+		return;
+	};
 
 	if (device->plugin->set_configuration(device->plugin_index,
                   HWCAP_PROBECONFIG, (char *)device->probes) != SIGROK_OK) {
diff --git a/libsigrok/output/common.c b/libsigrok/output/common.c
index cebddc0..c2fa896 100644
--- a/libsigrok/output/common.c
+++ b/libsigrok/output/common.c
@@ -59,3 +59,28 @@ char *sigrok_samplerate_string(uint64_t samplerate)
 	return o;
 }
 
+/** Try to parse a "natural" string representation of a sample rate back into number.
+ *
+ * E.g. a value of "3 MHz" will be converted to 3000000.
+ *
+ * @param str A string representation of a sample rate.
+ * @return The parsed out rate, or 0 on failure.
+*/
+uint64_t sigrok_samplerate_parse(const char *str)
+{
+	uint64_t value = 0;
+	char unit[8] = "";
+
+	if (sscanf(str, "%" SCNu64 "%3s", &value, unit) >= 1) {
+		if (unit[0]) {
+			if (strcmp(unit, "GHz") == 0)
+				value *= 1000000000;
+			else if (strcmp(unit, "MHz") == 0)
+				value *= 1000000;
+			else if (strcmp(unit, "kHz") == 0)
+				value *= 1000;
+		}
+	}
+	return value;
+}
+
diff --git a/libsigrok/sigrok.h b/libsigrok/sigrok.h
index ede379b..1a2b7e5 100644
--- a/libsigrok/sigrok.h
+++ b/libsigrok/sigrok.h
@@ -153,6 +153,7 @@ int filter_probes(int in_unitsize, int out_unitsize, int *probelist,
 		  uint64_t *length_out);
 
 char *sigrok_samplerate_string(uint64_t samplerate);
+uint64_t sigrok_samplerate_parse(const char *str);
 
 /*--- analyzer.c ------------------------------------------------------------*/
 
------------------------------------------------------------------------------

_______________________________________________
sigrok-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sigrok-devel

Reply via email to