File tree Expand file tree Collapse file tree 2 files changed +25
-2
lines changed Expand file tree Collapse file tree 2 files changed +25
-2
lines changed Original file line number Diff line number Diff line change @@ -101,6 +101,9 @@ enum digitalPins {
101101enum analogPins { DT_FOREACH_PROP_ELEM (DT_PATH (zephyr_user),
102102 adc_pin_gpios, AN_ENUMS) };
103103
104+ // We provide analogReadResolution APIs
105+ void analogReadResolution (int bits);
106+
104107#endif
105108
106109#ifdef CONFIG_DAC
Original file line number Diff line number Diff line change @@ -368,10 +368,24 @@ void analogReference(uint8_t mode)
368368 }
369369}
370370
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+
371385int analogRead (pin_size_t pinNumber)
372386{
373387 int err;
374- int16_t buf;
388+ uint16_t buf;
375389 struct adc_sequence seq = { .buffer = &buf, .buffer_size = sizeof (buf) };
376390 size_t idx = analog_pin_index (pinNumber);
377391
@@ -401,7 +415,13 @@ int analogRead(pin_size_t pinNumber)
401415 return err;
402416 }
403417
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 ) ;
405425}
406426
407427#endif
You can’t perform that action at this time.
0 commit comments