]> sigrok.org Git - libsigrok.git/blobdiff - src/transform/scale.c
SR_DF_ANALOG_OLD and sr_datafeed_analog_old renames.
[libsigrok.git] / src / transform / scale.c
index e0ecd3de72907b56aa408f672d73eff312c28a84..47937be9e8985dfa5a84f5431e8da453cce88e4b 100644 (file)
@@ -26,7 +26,7 @@
 #define LOG_PREFIX "transform/scale"
 
 struct context {
-       double factor;
+       struct sr_rational factor;
 };
 
 static int init(struct sr_transform *t, GHashTable *options)
@@ -38,7 +38,8 @@ static int init(struct sr_transform *t, GHashTable *options)
 
        t->priv = ctx = g_malloc0(sizeof(struct context));
 
-       ctx->factor = g_variant_get_double(g_hash_table_lookup(options, "factor"));
+       g_variant_get(g_hash_table_lookup(options, "factor"), "(xt)",
+                       &ctx->factor.p, &ctx->factor.q);
 
        return SR_OK;
 }
@@ -48,10 +49,12 @@ static int receive(const struct sr_transform *t,
                struct sr_datafeed_packet **packet_out)
 {
        struct context *ctx;
-       const struct sr_datafeed_analog *analog;
+       const struct sr_datafeed_analog_old *analog_old;
+       const struct sr_datafeed_analog2 *analog2;
        struct sr_channel *ch;
        GSList *l;
        float *fdata;
+       float factor;
        int i, num_channels, c;
 
        if (!t || !t->sdi || !packet_in || !packet_out)
@@ -59,19 +62,25 @@ static int receive(const struct sr_transform *t,
        ctx = t->priv;
 
        switch (packet_in->type) {
-       case SR_DF_ANALOG:
-               analog = packet_in->payload;
-               fdata = (float *)analog->data;
-               num_channels = g_slist_length(analog->channels);
-               for (i = 0; i < analog->num_samples; i++) {
+       case SR_DF_ANALOG_OLD:
+               analog_old = packet_in->payload;
+               fdata = (float *)analog_old->data;
+               num_channels = g_slist_length(analog_old->channels);
+               factor = (float) ctx->factor.p / ctx->factor.q;
+               for (i = 0; i < analog_old->num_samples; i++) {
                        /* For now scale all values in all channels. */
-                       for (l = analog->channels, c = 0; l; l = l->next, c++) {
+                       for (l = analog_old->channels, c = 0; l; l = l->next, c++) {
                                ch = l->data;
                                (void)ch;
-                               fdata[i * num_channels + c] *= ctx->factor;
+                               fdata[i * num_channels + c] *= factor;
                        }
                }
                break;
+       case SR_DF_ANALOG2:
+               analog2 = packet_in->payload;
+               analog2->encoding->scale.p *= ctx->factor.p;
+               analog2->encoding->scale.q *= ctx->factor.q;
+               break;
        default:
                sr_spew("Unsupported packet type %d, ignoring.", packet_in->type);
                break;
@@ -104,9 +113,12 @@ static struct sr_option options[] = {
 
 static const struct sr_option *get_options(void)
 {
+       int64_t p = 1;
+       uint64_t q = 1;
+
        /* Default to a scaling factor of 1.0. */
        if (!options[0].def)
-               options[0].def = g_variant_ref_sink(g_variant_new_double(1.0));
+               options[0].def = g_variant_ref_sink(g_variant_new(("(xt"), &p, &q));
 
        return options;
 }