]> sigrok.org Git - libsigrok.git/commitdiff
demo: Get/Set measurement quantity for the analog channels.
authorFrank Stettner <redacted>
Wed, 20 Feb 2019 19:32:04 +0000 (20:32 +0100)
committerUwe Hermann <redacted>
Fri, 29 Mar 2019 15:15:37 +0000 (16:15 +0100)
src/hardware/demo/api.c
src/hardware/demo/protocol.c
src/hardware/demo/protocol.h

index 52f30d4f14746ee55546b288d4df31fc4b662a68..1f9b9d2c04892c988a5dbbbd104ec3027ad9d6b4 100644 (file)
@@ -5,6 +5,7 @@
  * Copyright (C) 2011 Olivier Fauchon <olivier@aixmarseille.com>
  * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
  * Copyright (C) 2015 Bartosz Golaszewski <bgolaszewski@baylibre.com>
+ * Copyright (C) 2019 Frank Stettner <frank-stettner@gmx.net>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -82,6 +83,7 @@ static const uint32_t devopts_cg_analog_group[] = {
 };
 
 static const uint32_t devopts_cg_analog_channel[] = {
+       SR_CONF_MEASURED_QUANTITY | SR_CONF_GET | SR_CONF_SET,
        SR_CONF_PATTERN_MODE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
        SR_CONF_AMPLITUDE | SR_CONF_GET | SR_CONF_SET,
 };
@@ -184,12 +186,15 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
                        /* Every channel gets a generator struct. */
                        ag = g_malloc(sizeof(struct analog_gen));
                        ag->ch = ch;
+                       ag->mq = SR_MQ_VOLTAGE;
+                       ag->mq_flags = SR_MQFLAG_DC;
+                       ag->unit = SR_UNIT_VOLT;
                        ag->amplitude = DEFAULT_ANALOG_AMPLITUDE;
                        sr_analog_init(&ag->packet, &ag->encoding, &ag->meaning, &ag->spec, 2);
                        ag->packet.meaning->channels = cg->channels;
-                       ag->packet.meaning->mq = SR_MQ_VOLTAGE;
-                       ag->packet.meaning->mqflags = SR_MQFLAG_DC;
-                       ag->packet.meaning->unit = SR_UNIT_VOLT;
+                       ag->packet.meaning->mq = ag->mq;
+                       ag->packet.meaning->mqflags = ag->mq_flags;
+                       ag->packet.meaning->unit = ag->unit;
                        ag->packet.encoding->digits = DEFAULT_ANALOG_ENCODING_DIGITS;
                        ag->packet.spec->spec_digits = DEFAULT_ANALOG_SPEC_DIGITS;
                        ag->packet.data = ag->pattern_data;
@@ -231,6 +236,7 @@ static int config_get(uint32_t key, GVariant **data,
        struct dev_context *devc;
        struct sr_channel *ch;
        struct analog_gen *ag;
+       GVariant *mq_arr[2];
        int pattern;
 
        if (!sdi)
@@ -256,6 +262,18 @@ static int config_get(uint32_t key, GVariant **data,
        case SR_CONF_AVG_SAMPLES:
                *data = g_variant_new_uint64(devc->avg_samples);
                break;
+       case SR_CONF_MEASURED_QUANTITY:
+               if (!cg)
+                       return SR_ERR_CHANNEL_GROUP;
+               /* Any channel in the group will do. */
+               ch = cg->channels->data;
+               if (ch->type != SR_CHANNEL_ANALOG)
+                       return SR_ERR_ARG;
+               ag = g_hash_table_lookup(devc->ch_ag, ch);
+               mq_arr[0] = g_variant_new_uint32(ag->mq);
+               mq_arr[1] = g_variant_new_uint64(ag->mq_flags);
+               *data = g_variant_new_tuple(mq_arr, 2);
+               break;
        case SR_CONF_PATTERN_MODE:
                if (!cg)
                        return SR_ERR_CHANNEL_GROUP;
@@ -297,6 +315,7 @@ static int config_set(uint32_t key, GVariant *data,
        struct dev_context *devc;
        struct analog_gen *ag;
        struct sr_channel *ch;
+       GVariant *mq_tuple_child;
        GSList *l;
        int logic_pattern, analog_pattern;
 
@@ -325,6 +344,21 @@ static int config_set(uint32_t key, GVariant *data,
                devc->avg_samples = g_variant_get_uint64(data);
                sr_dbg("Setting averaging rate to %" PRIu64, devc->avg_samples);
                break;
+       case SR_CONF_MEASURED_QUANTITY:
+               if (!cg)
+                       return SR_ERR_CHANNEL_GROUP;
+               for (l = cg->channels; l; l = l->next) {
+                       ch = l->data;
+                       if (ch->type != SR_CHANNEL_ANALOG)
+                               return SR_ERR_ARG;
+                       ag = g_hash_table_lookup(devc->ch_ag, ch);
+                       mq_tuple_child = g_variant_get_child_value(data, 0);
+                       ag->mq = g_variant_get_uint32(mq_tuple_child);
+                       mq_tuple_child = g_variant_get_child_value(data, 1);
+                       ag->mq_flags = g_variant_get_uint64(mq_tuple_child);
+                       g_variant_unref(mq_tuple_child);
+               }
+               break;
        case SR_CONF_PATTERN_MODE:
                if (!cg)
                        return SR_ERR_CHANNEL_GROUP;
index ce897ad903bc0790223bc5e82182b8f41cd6695b..8db7cbbfacaf3e85776a1bd0edaeee27e1989867 100644 (file)
@@ -5,6 +5,7 @@
  * Copyright (C) 2011 Olivier Fauchon <olivier@aixmarseille.com>
  * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
  * Copyright (C) 2015 Bartosz Golaszewski <bgolaszewski@baylibre.com>
+ * Copyright (C) 2019 Frank Stettner <frank-stettner@gmx.net>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -400,6 +401,80 @@ static void send_analog_packet(struct analog_gen *ag,
        packet.type = SR_DF_ANALOG;
        packet.payload = &ag->packet;
 
+       ag->packet.meaning->channels = g_slist_append(NULL, ag->ch);
+       ag->packet.meaning->mq = ag->mq;
+       ag->packet.meaning->mqflags = ag->mq_flags;
+
+       /* Set a unit for the given quantity */
+       if (ag->mq == SR_MQ_VOLTAGE)
+               ag->packet.meaning->unit = SR_UNIT_VOLT;
+       else if (ag->mq == SR_MQ_CURRENT)
+               ag->packet.meaning->unit = SR_UNIT_AMPERE;
+       else if (ag->mq == SR_MQ_RESISTANCE)
+               ag->packet.meaning->unit = SR_UNIT_OHM;
+       else if (ag->mq == SR_MQ_CAPACITANCE)
+               ag->packet.meaning->unit = SR_UNIT_FARAD;
+       else if (ag->mq == SR_MQ_TEMPERATURE)
+               ag->packet.meaning->unit = SR_UNIT_CELSIUS;
+       else if (ag->mq == SR_MQ_FREQUENCY)
+               ag->packet.meaning->unit = SR_UNIT_HERTZ;
+       else if (ag->mq == SR_MQ_DUTY_CYCLE)
+               ag->packet.meaning->unit = SR_UNIT_PERCENTAGE;
+       else if (ag->mq == SR_MQ_CONTINUITY)
+               ag->packet.meaning->unit = SR_UNIT_OHM;
+       else if (ag->mq == SR_MQ_PULSE_WIDTH)
+               ag->packet.meaning->unit = SR_UNIT_PERCENTAGE;
+       else if (ag->mq == SR_MQ_CONDUCTANCE)
+               ag->packet.meaning->unit = SR_UNIT_SIEMENS;
+       else if (ag->mq == SR_MQ_POWER)
+               ag->packet.meaning->unit = SR_UNIT_WATT;
+       else if (ag->mq == SR_MQ_GAIN)
+               ag->packet.meaning->unit = SR_UNIT_UNITLESS;
+       else if (ag->mq == SR_MQ_SOUND_PRESSURE_LEVEL)
+               ag->packet.meaning->unit = SR_UNIT_DECIBEL_SPL;
+       else if (ag->mq == SR_MQ_CARBON_MONOXIDE)
+               ag->packet.meaning->unit = SR_UNIT_CONCENTRATION;
+       else if (ag->mq == SR_MQ_RELATIVE_HUMIDITY)
+               ag->packet.meaning->unit = SR_UNIT_HUMIDITY_293K;
+       else if (ag->mq == SR_MQ_TIME)
+               ag->packet.meaning->unit = SR_UNIT_SECOND;
+       else if (ag->mq == SR_MQ_WIND_SPEED)
+               ag->packet.meaning->unit = SR_UNIT_METER_SECOND;
+       else if (ag->mq == SR_MQ_PRESSURE)
+               ag->packet.meaning->unit = SR_UNIT_HECTOPASCAL;
+       else if (ag->mq == SR_MQ_PARALLEL_INDUCTANCE)
+               ag->packet.meaning->unit = SR_UNIT_HENRY;
+       else if (ag->mq == SR_MQ_PARALLEL_CAPACITANCE)
+               ag->packet.meaning->unit = SR_UNIT_FARAD;
+       else if (ag->mq == SR_MQ_PARALLEL_RESISTANCE)
+               ag->packet.meaning->unit = SR_UNIT_OHM;
+       else if (ag->mq == SR_MQ_SERIES_INDUCTANCE)
+               ag->packet.meaning->unit = SR_UNIT_HENRY;
+       else if (ag->mq == SR_MQ_SERIES_CAPACITANCE)
+               ag->packet.meaning->unit = SR_UNIT_FARAD;
+       else if (ag->mq == SR_MQ_SERIES_RESISTANCE)
+               ag->packet.meaning->unit = SR_UNIT_OHM;
+       else if (ag->mq == SR_MQ_DISSIPATION_FACTOR)
+               ag->packet.meaning->unit = SR_UNIT_UNITLESS;
+       else if (ag->mq == SR_MQ_QUALITY_FACTOR)
+               ag->packet.meaning->unit = SR_UNIT_UNITLESS;
+       else if (ag->mq == SR_MQ_PHASE_ANGLE)
+               ag->packet.meaning->unit = SR_UNIT_DEGREE;
+       else if (ag->mq == SR_MQ_DIFFERENCE)
+               ag->packet.meaning->unit = SR_UNIT_UNITLESS;
+       else if (ag->mq == SR_MQ_COUNT)
+               ag->packet.meaning->unit = SR_UNIT_PIECE;
+       else if (ag->mq == SR_MQ_POWER_FACTOR)
+               ag->packet.meaning->unit = SR_UNIT_UNITLESS;
+       else if (ag->mq == SR_MQ_APPARENT_POWER)
+               ag->packet.meaning->unit = SR_UNIT_VOLT_AMPERE;
+       else if (ag->mq == SR_MQ_MASS)
+               ag->packet.meaning->unit = SR_UNIT_GRAM;
+       else if (ag->mq == SR_MQ_HARMONIC_RATIO)
+               ag->packet.meaning->unit = SR_UNIT_UNITLESS;
+       else
+               ag->packet.meaning->unit = SR_UNIT_UNITLESS;
+
        if (!devc->avg) {
                ag_pattern_pos = analog_pos % ag->num_samples;
                sending_now = MIN(analog_todo, ag->num_samples - ag_pattern_pos);
index 82fb5d23553829b62604ffcf94e5d67fd602c529..de0ac7a86078d125f1925c801638249bdff37a7c 100644 (file)
@@ -5,6 +5,7 @@
  * Copyright (C) 2011 Olivier Fauchon <olivier@aixmarseille.com>
  * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
  * Copyright (C) 2015 Bartosz Golaszewski <bgolaszewski@baylibre.com>
+ * Copyright (C) 2019 Frank Stettner <frank-stettner@gmx.net>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -132,6 +133,9 @@ static const char *analog_pattern_str[] = {
 
 struct analog_gen {
        struct sr_channel *ch;
+       enum sr_mq mq;
+       enum sr_mqflag mq_flags;
+       enum sr_unit unit;
        enum analog_pattern_type pattern;
        float amplitude;
        float pattern_data[ANALOG_BUFSIZE];