]> sigrok.org Git - libsigrok.git/blobdiff - hardware/demo/demo.c
Move the probe naming to the creator of the device, and let each driver name its...
[libsigrok.git] / hardware / demo / demo.c
index 356a9bca5d6c8e4444e8eb3df085bdadb393431f..6605d1e6c321f6fe5be8998d65a4799a5594cce3 100644 (file)
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
-#include <sigrok.h>
-#include <sigrok-internal.h>
 #ifdef _WIN32
 #include <io.h>
 #include <fcntl.h>
 #define pipe(fds) _pipe(fds, 4096, _O_BINARY)
 #endif
 #include "config.h"
+#include "sigrok.h"
+#include "sigrok-internal.h"
 
 /* TODO: Number of probes should be configurable. */
 #define NUM_PROBES             8
@@ -73,7 +73,7 @@ struct databag {
        uint8_t thread_running;
        uint64_t samples_counter;
        int device_index;
-       gpointer session_device_id;
+       gpointer session_data;
        GTimer *timer;
 };
 
@@ -102,6 +102,18 @@ static const char *pattern_strings[] = {
        NULL,
 };
 
+static const char *probe_names[NUM_PROBES + 1] = {
+       "0",
+       "1",
+       "2",
+       "3",
+       "4",
+       "5",
+       "6",
+       "7",
+       NULL,
+};
+
 static uint8_t pattern_sigrok[] = {
        0x4c, 0x92, 0x92, 0x92, 0x64, 0x00, 0x00, 0x00,
        0x82, 0xfe, 0xfe, 0x82, 0x00, 0x00, 0x00, 0x00,
@@ -116,20 +128,21 @@ static uint8_t pattern_sigrok[] = {
 /* List of struct sr_device_instance, maintained by opendev()/closedev(). */
 static GSList *device_instances = NULL;
 static uint64_t cur_samplerate = SR_KHZ(200);
+static uint64_t period_ps = 5000000;
 static uint64_t limit_samples = 0;
 static uint64_t limit_msec = 0;
 static int default_pattern = PATTERN_SIGROK;
 static GThread *my_thread;
 static int thread_running;
 
-static void hw_stop_acquisition(int device_index, gpointer session_device_id);
+static void hw_stop_acquisition(int device_index, gpointer session_data);
 
 static int hw_init(const char *deviceinfo)
 {
        struct sr_device_instance *sdi;
 
        /* Avoid compiler warnings. */
-       deviceinfo = deviceinfo;
+       (void)deviceinfo;
 
        sdi = sr_device_instance_new(0, SR_ST_ACTIVE, DEMONAME, NULL, NULL);
        if (!sdi) {
@@ -145,7 +158,7 @@ static int hw_init(const char *deviceinfo)
 static int hw_opendev(int device_index)
 {
        /* Avoid compiler warnings. */
-       device_index = device_index;
+       (void)device_index;
 
        /* Nothing needed so far. */
 
@@ -155,7 +168,7 @@ static int hw_opendev(int device_index)
 static int hw_closedev(int device_index)
 {
        /* Avoid compiler warnings. */
-       device_index = device_index;
+       (void)device_index;
 
        /* Nothing needed so far. */
 
@@ -184,6 +197,9 @@ static void *hw_get_device_info(int device_index, int device_info_id)
        case SR_DI_NUM_PROBES:
                info = GINT_TO_POINTER(NUM_PROBES);
                break;
+       case SR_DI_PROBE_NAMES:
+               info = probe_names;
+               break;
        case SR_DI_SAMPLERATES:
                info = &samplerates;
                break;
@@ -201,7 +217,7 @@ static void *hw_get_device_info(int device_index, int device_info_id)
 static int hw_get_status(int device_index)
 {
        /* Avoid compiler warnings. */
-       device_index = device_index;
+       (void)device_index;
 
        return SR_ST_ACTIVE;
 }
@@ -217,13 +233,14 @@ static int hw_set_configuration(int device_index, int capability, void *value)
        char *stropt;
 
        /* Avoid compiler warnings. */
-       device_index = device_index;
+       (void)device_index;
 
        if (capability == SR_HWCAP_PROBECONFIG) {
                /* Nothing to do, but must be supported */
                ret = SR_OK;
        } else if (capability == SR_HWCAP_SAMPLERATE) {
                cur_samplerate = *(uint64_t *)value;
+               period_ps = 1000000000000 / cur_samplerate;
                sr_dbg("demo: %s: setting samplerate to %" PRIu64, __func__,
                       cur_samplerate);
                ret = SR_OK;
@@ -288,12 +305,10 @@ static void samples_generator(uint8_t *buf, uint64_t size, void *data)
                        *(buf + i) = i;
                break;
        case PATTERN_ALL_LOW: /* All probes are low */
-               for (i = 0; i < size; i++)
-                       *(buf + i) = 0x00;
+               memset(buf, 0x00, size);
                break;
        case PATTERN_ALL_HIGH: /* All probes are high */
-               for (i = 0; i < size; i++)
-                       *(buf + i) = 0xff;
+               memset(buf, 0xff, size);
                break;
        default:
                /* TODO: Error handling. */
@@ -350,15 +365,17 @@ static void thread_func(void *data)
 }
 
 /* Callback handling data */
-static int receive_data(int fd, int revents, void *user_data)
+static int receive_data(int fd, int revents, void *session_data)
 {
        struct sr_datafeed_packet packet;
-       char c[BUFSIZE];
+       struct sr_datafeed_logic logic;
+       static uint64_t samples_received = 0;
+       unsigned char c[BUFSIZE];
        gsize z;
 
        /* Avoid compiler warnings. */
-       fd = fd;
-       revents = revents;
+       (void)fd;
+       (void)revents;
 
        do {
                g_io_channel_read_chars(channels[0],
@@ -366,10 +383,14 @@ static int receive_data(int fd, int revents, void *user_data)
 
                if (z > 0) {
                        packet.type = SR_DF_LOGIC;
-                       packet.length = z;
-                       packet.unitsize = 1;
-                       packet.payload = c;
-                       sr_session_bus(user_data, &packet);
+                       packet.payload = &logic;
+                       packet.timeoffset =  samples_received * period_ps;
+                       packet.duration = z * period_ps;
+                       logic.length = z;
+                       logic.unitsize = 1;
+                       logic.data = c;
+                       sr_session_bus(session_data, &packet);
+                       samples_received += z;
                }
        } while (z > 0);
 
@@ -379,7 +400,7 @@ static int receive_data(int fd, int revents, void *user_data)
 
                /* Send last packet. */
                packet.type = SR_DF_END;
-               sr_session_bus(user_data, &packet);
+               sr_session_bus(session_data, &packet);
 
                return FALSE;
        }
@@ -387,7 +408,7 @@ static int receive_data(int fd, int revents, void *user_data)
        return TRUE;
 }
 
-static int hw_start_acquisition(int device_index, gpointer session_device_id)
+static int hw_start_acquisition(int device_index, gpointer session_data)
 {
        struct sr_datafeed_packet *packet;
        struct sr_datafeed_header *header;
@@ -400,7 +421,7 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id)
        }
 
        mydata->sample_generator = default_pattern;
-       mydata->session_device_id = session_device_id;
+       mydata->session_data = session_data;
        mydata->device_index = device_index;
        mydata->samples_counter = 0;
 
@@ -422,7 +443,7 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id)
        g_io_channel_set_buffered(channels[1], FALSE);
 
        sr_source_add(mydata->pipe_fds[0], G_IO_IN | G_IO_ERR, 40,
-                     receive_data, session_device_id);
+                     receive_data, session_data);
 
        /* Run the demo thread. */
        g_thread_init(NULL);
@@ -447,26 +468,26 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id)
        }
 
        packet->type = SR_DF_HEADER;
-       packet->length = sizeof(struct sr_datafeed_header);
-       packet->payload = (unsigned char *)header;
+       packet->payload = header;
+       packet->timeoffset = 0;
+       packet->duration = 0;
        header->feed_version = 1;
        gettimeofday(&header->starttime, NULL);
        header->samplerate = cur_samplerate;
-       header->protocol_id = SR_PROTO_RAW;
        header->num_logic_probes = NUM_PROBES;
        header->num_analog_probes = 0;
-       sr_session_bus(session_device_id, packet);
+       sr_session_bus(session_data, packet);
        g_free(header);
        g_free(packet);
 
        return SR_OK;
 }
 
-static void hw_stop_acquisition(int device_index, gpointer session_device_id)
+static void hw_stop_acquisition(int device_index, gpointer session_data)
 {
        /* Avoid compiler warnings. */
-       device_index = device_index;
-       session_device_id = session_device_id;
+       (void)device_index;
+       (void)session_data;
 
        /* Stop generate thread. */
        thread_running = 0;