]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/demo/api.c
demo: introduce graycode generator mode
[libsigrok.git] / src / hardware / demo / api.c
index 4d95ddda3f13776ee30973445ed6e3e95d8fc521..d0abe4ff92e053f111c6cb44b7cc9e66f9aa0f50 100644 (file)
@@ -44,6 +44,12 @@ static const char *logic_pattern_str[] = {
        "all-low",
        "all-high",
        "squid",
+       "graycode",
+};
+
+static const uint32_t scanopts[] = {
+       SR_CONF_NUM_LOGIC_CHANNELS,
+       SR_CONF_NUM_ANALOG_CHANNELS,
 };
 
 static const uint32_t drvopts[] = {
@@ -52,11 +58,6 @@ static const uint32_t drvopts[] = {
        SR_CONF_OSCILLOSCOPE,
 };
 
-static const uint32_t scanopts[] = {
-       SR_CONF_NUM_LOGIC_CHANNELS,
-       SR_CONF_NUM_ANALOG_CHANNELS,
-};
-
 static const uint32_t devopts[] = {
        SR_CONF_CONTINUOUS,
        SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
@@ -119,6 +120,9 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
        devc->cur_samplerate = SR_KHZ(200);
        devc->num_logic_channels = num_logic_channels;
        devc->logic_unitsize = (devc->num_logic_channels + 7) / 8;
+       devc->all_logic_channels_mask = 1UL << 0;
+       devc->all_logic_channels_mask <<= devc->num_logic_channels;
+       devc->all_logic_channels_mask--;
        devc->logic_pattern = DEFAULT_LOGIC_PATTERN;
        devc->num_analog_channels = num_analog_channels;
 
@@ -197,8 +201,8 @@ static int dev_clear(const struct sr_dev_driver *di)
        return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
 }
 
-static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
-               const struct sr_channel_group *cg)
+static int config_get(uint32_t key, GVariant **data,
+       const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
 {
        struct dev_context *devc;
        struct sr_channel *ch;
@@ -257,16 +261,14 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s
        return SR_OK;
 }
 
-static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
-               const struct sr_channel_group *cg)
+static int config_set(uint32_t key, GVariant *data,
+       const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
 {
        struct dev_context *devc;
        struct analog_gen *ag;
        struct sr_channel *ch;
        GSList *l;
        int logic_pattern, analog_pattern;
-       unsigned int i;
-       const char *stropt;
 
        devc = sdi->priv;
 
@@ -293,21 +295,9 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd
        case SR_CONF_PATTERN_MODE:
                if (!cg)
                        return SR_ERR_CHANNEL_GROUP;
-               stropt = g_variant_get_string(data, NULL);
-               logic_pattern = analog_pattern = -1;
-               for (i = 0; i < ARRAY_SIZE(logic_pattern_str); i++) {
-                       if (!strcmp(stropt, logic_pattern_str[i])) {
-                               logic_pattern = i;
-                               break;
-                       }
-               }
-               for (i = 0; i < ARRAY_SIZE(analog_pattern_str); i++) {
-                       if (!strcmp(stropt, analog_pattern_str[i])) {
-                               analog_pattern = i;
-                               break;
-                       }
-               }
-               if (logic_pattern == -1 && analog_pattern == -1)
+               logic_pattern = std_str_idx(data, ARRAY_AND_SIZE(logic_pattern_str));
+               analog_pattern = std_str_idx(data, ARRAY_AND_SIZE(analog_pattern_str));
+               if (logic_pattern < 0 && analog_pattern < 0)
                        return SR_ERR_ARG;
                for (l = cg->channels; l; l = l->next) {
                        ch = l->data;
@@ -351,40 +341,18 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd
        return SR_OK;
 }
 
-static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
-               const struct sr_channel_group *cg)
+static int config_list(uint32_t key, GVariant **data,
+       const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
 {
        struct sr_channel *ch;
-       GVariant *gvar;
-       GVariantBuilder gvb;
-
-       if (key == SR_CONF_SCAN_OPTIONS) {
-               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
-                               scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
-               return SR_OK;
-       }
-
-       if (key == SR_CONF_DEVICE_OPTIONS && !sdi) {
-               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
-                               drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
-               return SR_OK;
-       }
-
-       if (!sdi)
-               return SR_ERR_ARG;
 
        if (!cg) {
                switch (key) {
+               case SR_CONF_SCAN_OPTIONS:
                case SR_CONF_DEVICE_OPTIONS:
-                       *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
-                                       devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
-                       break;
+                       return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
                case SR_CONF_SAMPLERATE:
-                       g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
-                       gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), samplerates,
-                                       ARRAY_SIZE(samplerates), sizeof(uint64_t));
-                       g_variant_builder_add(&gvb, "{sv}", "samplerate-steps", gvar);
-                       *data = g_variant_builder_end(&gvb);
+                       *data = std_gvar_samplerates_steps(ARRAY_AND_SIZE(samplerates));
                        break;
                default:
                        return SR_ERR_NA;
@@ -394,18 +362,12 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *
                switch (key) {
                case SR_CONF_DEVICE_OPTIONS:
                        if (ch->type == SR_CHANNEL_LOGIC)
-                               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
-                                               devopts_cg_logic, ARRAY_SIZE(devopts_cg_logic),
-                                               sizeof(uint32_t));
+                               *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_logic));
                        else if (ch->type == SR_CHANNEL_ANALOG) {
                                if (strcmp(cg->name, "Analog") == 0)
-                                       *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
-                                                       devopts_cg_analog_group, ARRAY_SIZE(devopts_cg_analog_group),
-                                                       sizeof(uint32_t));
+                                       *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog_group));
                                else
-                                       *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
-                                                       devopts_cg_analog_channel, ARRAY_SIZE(devopts_cg_analog_channel),
-                                                       sizeof(uint32_t));
+                                       *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog_channel));
                        }
                        else
                                return SR_ERR_BUG;
@@ -416,11 +378,9 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *
                                return SR_ERR_NA;
 
                        if (ch->type == SR_CHANNEL_LOGIC)
-                               *data = g_variant_new_strv(logic_pattern_str,
-                                               ARRAY_SIZE(logic_pattern_str));
+                               *data = g_variant_new_strv(ARRAY_AND_SIZE(logic_pattern_str));
                        else if (ch->type == SR_CHANNEL_ANALOG)
-                               *data = g_variant_new_strv(analog_pattern_str,
-                                               ARRAY_SIZE(analog_pattern_str));
+                               *data = g_variant_new_strv(ARRAY_AND_SIZE(analog_pattern_str));
                        else
                                return SR_ERR_BUG;
                        break;
@@ -444,6 +404,7 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi)
 
        devc = sdi->priv;
        devc->sent_samples = 0;
+       devc->sent_frame_samples = 0;
 
        /*
         * Determine the numbers of logic and analog channels that are
@@ -497,6 +458,9 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi)
 
        std_session_send_df_header(sdi);
 
+       if (SAMPLES_PER_FRAME > 0)
+               std_session_send_frame_begin(sdi);
+
        /* We use this timestamp to decide how many more samples to send. */
        devc->start_us = g_get_monotonic_time();
        devc->spent_us = 0;
@@ -508,6 +472,10 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi)
 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
 {
        sr_session_source_remove(sdi->session, -1);
+
+       if (SAMPLES_PER_FRAME > 0)
+               std_session_send_frame_end(sdi);
+
        std_session_send_df_end(sdi);
 
        return SR_OK;