Skip to content

Commit 7377d9d

Browse files
KurtEsoburi
authored andcommitted
Implement analogReadResolution
Co-Authored-by: Kurt Eckhardt <kurte@rockisland.com> Signed-off-by: TOKITA Hiroshi <tokita.hiroshi@gmail.com>
1 parent 24fdac1 commit 7377d9d

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

cores/arduino/Arduino.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff 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

cores/arduino/zephyrCommon.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff 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+
471482
int 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

0 commit comments

Comments
 (0)