File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -104,6 +104,9 @@ enum analogPins {
104104 DT_FOREACH_PROP_ELEM (DT_PATH (zephyr_user), adc_pin_gpios, AN_ENUMS)
105105};
106106
107+ // We provide analogReadResolution APIs
108+ void analogReadResolution (int bits);
109+
107110#endif
108111
109112#ifdef CONFIG_DAC
Original file line number Diff line number Diff line change @@ -468,9 +468,20 @@ void analogReference(uint8_t mode) {
468468 }
469469}
470470
471+ // Note: We can not update the arduino_adc structure as it is read only...
472+ static int read_resolution = 10 ;
473+
474+ void analogReadResolution (int bits) {
475+ read_resolution = bits;
476+ }
477+
478+ int analogReadResolution () {
479+ return read_resolution;
480+ }
481+
471482int analogRead (pin_size_t pinNumber) {
472483 int err;
473- int16_t buf;
484+ uint16_t buf;
474485 struct adc_sequence seq = {.buffer = &buf, .buffer_size = sizeof (buf)};
475486 size_t idx = analog_pin_index (pinNumber);
476487
@@ -500,7 +511,17 @@ int analogRead(pin_size_t pinNumber) {
500511 return err;
501512 }
502513
503- return buf;
514+ /*
515+ * If necessary map the return value to the
516+ * number of bits the user has asked for
517+ */
518+ if (read_resolution == seq.resolution ) {
519+ return buf;
520+ }
521+ if (read_resolution < seq.resolution ) {
522+ return buf >> (seq.resolution - read_resolution);
523+ }
524+ return buf << (read_resolution - seq.resolution );
504525}
505526
506527#endif
You can’t perform that action at this time.
0 commit comments