Skip to content

Commit 87286c5

Browse files
committed
STM32: Ensure proper distribution of analogRead values (now 0<=analogRead()<1 as on other boards) and add docs (fix #2612)
1 parent 08d4369 commit 87286c5

File tree

4 files changed

+8
-3
lines changed

4 files changed

+8
-3
lines changed

ChangeLog

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
STM32: Revert hasSystemSlept=true before _WFI that caused jsTimer to get corrupted
1717
Pipboy: allow audio files to be played at the same time as (silent) video. add audioStopped/videoStopped events and audioStop method
1818
Pipboy: Pip.audioStartVar(wav,{overlap:true}) can now play sounds over the top of other sounds. audioRead now doesn't stop audio playback
19+
STM32: Ensure proper distribution of analogRead values (now `0<=analogRead()<1` as on other boards) and add docs (fix #2612)
1920

2021
2v25 : ESP32C3: Get analogRead working correctly
2122
Graphics: Adjust image alignment when rotating images to avoid cropping (fix #2535)

src/jswrap_io.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,14 @@ void jswrap_io_poke(JsVarInt addr, JsVar *data, int wordSize) {
172172
"params" : [
173173
["pin","pin",["The pin to use","You can find out which pins to use by looking at [your board's reference page](#boards) and searching for pins with the `ADC` markers."]]
174174
],
175-
"return" : ["float","The Analog Value of the Pin between 0(GND) and 1(VCC). See below."]
175+
"return" : ["float","The analog value of the `Pin` between 0(GND) and 1(VCC). See below."]
176176
}
177177
Get the analogue value of the given pin.
178178
179+
* The value is normally greater than or equal to 0, however in some cases nRF52-based boards can produce values
180+
less than 0 when the ADC voltage is slightly less than the chip's internal GND.
181+
* The value returned will always be *less* than 1, even when the ADC reads full range. For example a 12 bit ADC may return 4095 as a full-range value, but this is divided by 4096 to produce `analogRead`'s output value.
182+
179183
This is different to Arduino which only returns an integer between 0 and 1023
180184
181185
However only pins connected to an ADC will work (see the datasheet)

src/jswrap_pin.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ void jswrap_pin_pulse(JsVar *parent, bool value, JsVar *times) {
233233
"class" : "Pin",
234234
"name" : "analog",
235235
"generate" : "jswrap_pin_analog",
236-
"return" : ["float","The analog Value of the Pin between 0 and 1"]
236+
"return" : ["float","The analog value of the `Pin` between 0(GND) and 1(VCC)"]
237237
}
238238
(Added in 2v20) Get the analogue value of the given pin. See `analogRead` for more information.
239239
*/

targets/stm32/jshardware.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1900,7 +1900,7 @@ JsVarFloat jshPinAnalog(Pin pin) {
19001900
if (!jshGetPinStateIsManual(pin))
19011901
jshPinSetState(pin, JSHPINSTATE_ADC_IN);
19021902

1903-
return jshAnalogRead(pinInfo[pin].analog, false) / (JsVarFloat)65535;
1903+
return jshAnalogRead(pinInfo[pin].analog, false) / (JsVarFloat)65536;
19041904
}
19051905

19061906
/// Returns a quickly-read analog value in the range 0-65535

0 commit comments

Comments
 (0)