File tree 2 files changed +25
-2
lines changed
2 files changed +25
-2
lines changed Original file line number Diff line number Diff line change @@ -101,6 +101,9 @@ enum digitalPins {
101
101
enum analogPins { DT_FOREACH_PROP_ELEM (DT_PATH (zephyr_user),
102
102
adc_pin_gpios, AN_ENUMS) };
103
103
104
+ // We provide analogReadResolution APIs
105
+ void analogReadResolution (int bits);
106
+
104
107
#endif
105
108
106
109
#ifdef CONFIG_DAC
Original file line number Diff line number Diff line change @@ -368,10 +368,24 @@ void analogReference(uint8_t mode)
368
368
}
369
369
}
370
370
371
+ // Note: We can not update the arduino_adc structure as it is read only...
372
+ static int read_resolution = 10 ;
373
+
374
+ void analogReadResolution (int bits)
375
+ {
376
+ read_resolution = bits;
377
+ }
378
+
379
+ int analogReadResolution ()
380
+ {
381
+ return read_resolution;
382
+ }
383
+
384
+
371
385
int analogRead (pin_size_t pinNumber)
372
386
{
373
387
int err;
374
- int16_t buf;
388
+ uint16_t buf;
375
389
struct adc_sequence seq = { .buffer = &buf, .buffer_size = sizeof (buf) };
376
390
size_t idx = analog_pin_index (pinNumber);
377
391
@@ -401,7 +415,13 @@ int analogRead(pin_size_t pinNumber)
401
415
return err;
402
416
}
403
417
404
- return buf;
418
+ /*
419
+ * If necessary map the return value to the
420
+ * number of bits the user has asked for
421
+ */
422
+ if (read_resolution == seq.resolution ) return buf;
423
+ if (read_resolution < seq.resolution ) return buf >> (seq.resolution - read_resolution);
424
+ return buf << (read_resolution - seq.resolution ) ;
405
425
}
406
426
407
427
#endif
You can’t perform that action at this time.
0 commit comments