Skip to content

Commit 563728e

Browse files
fauxparknoroadsleft
authored andcommitted
Change analogRead calls to analogReadPin (#9023)
* Change analogRead calls to analogReadPin * Add ChangeLog * Update docs, remove mention of `analogRead()` * Retarget changelog for next round
1 parent 970ad01 commit 563728e

File tree

5 files changed

+27
-25
lines changed

5 files changed

+27
-25
lines changed

docs/ChangeLog/20200829/PR9023.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Deprecation of `analogRead()`
2+
3+
[#9023](https://github.com/qmk/qmk_firmware/pull/9023)
4+
5+
This function takes Arduino pin numbers (eg. `9` vs. `B5`), which is at odds with the rest of the codebase. The replacement for this function is `analogReadPin()`.

docs/adc_driver.md

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ Then place this include at the top of your code:
4545

4646
Note that some of these pins are doubled-up on ADCs with the same channel. This is because the pins can be used for either ADC.
4747

48-
Also note that the F0 and F3 use different numbering schemes. The F0 has a single ADC and the channels are 0-based, whereas the F3 has 4 ADCs and the channels are 1 based. This is because the F0 uses the `ADCv1` implementation of the ADC, whereas the F3 uses the `ADCv3` implementation.
48+
Also note that the F0 and F3 use different numbering schemes. The F0 has a single ADC and the channels are 0-indexed, whereas the F3 has 4 ADCs and the channels are 1-indexed. This is because the F0 uses the `ADCv1` implementation of the ADC, whereas the F3 uses the `ADCv3` implementation.
4949

50-
|ADC|Channel|STM32F0XX|STM32F3XX|
50+
|ADC|Channel|STM32F0xx|STM32F3xx|
5151
|---|-------|---------|---------|
5252
|1 |0 |`A0` | |
5353
|1 |1 |`A1` |`A0` |
@@ -122,32 +122,29 @@ Also note that the F0 and F3 use different numbering schemes. The F0 has a singl
122122
|Function |Description |
123123
|----------------------------|-------------------------------------------------------------------------------------------------------------------|
124124
|`analogReference(mode)` |Sets the analog voltage reference source. Must be one of `ADC_REF_EXTERNAL`, `ADC_REF_POWER` or `ADC_REF_INTERNAL`.|
125-
|`analogRead(pin)` |Reads the value from the specified Arduino pin, eg. `4` for ADC6 on the ATmega32U4. |
126-
|`analogReadPin(pin)` |Reads the value from the specified QMK pin, eg. `F6` for ADC6 on the ATmega32U4. |
127-
|`pinToMux(pin)` |Translates a given QMK pin to a mux value. If an unsupported pin is given, returns the mux value for "0V (GND)". |
125+
|`analogReadPin(pin)` |Reads the value from the specified pin, eg. `F6` for ADC6 on the ATmega32U4. |
126+
|`pinToMux(pin)` |Translates a given pin to a mux value. If an unsupported pin is given, returns the mux value for "0V (GND)". |
128127
|`adc_read(mux)` |Reads the value from the ADC according to the specified mux. See your MCU's datasheet for more information. |
129128

130129
### ARM
131130

132-
Note that care was taken to match all of the functions used for AVR devices, however complications in the ARM platform prevent that from always being possible. For example, the `STM32` chips do not have assigned Arduino pins. We could use the default pin numbers, but those numbers change based on the package type of the device. For this reason, please specify your target pins with their identifiers (`A0`, `F3`, etc.). Also note that there are some variants of functions that accept the target ADC for the pin. Some pins can be used for multiple ADCs, and this specified can help you pick which ADC will be used to interact with that pin.
133-
134-
|Function |Description |
135-
|----------------------------|--------------------------------------------------------------------------------------------------------------------|
136-
|`analogReadPin(pin)` |Reads the value from the specified QMK pin, eg. `A0` for channel 0 on the STM32F0 and ADC1 channel 1 on the STM32F3. Note that if a pin can be used for multiple ADCs, it will pick the lower numbered ADC for this function. eg. `C0` will be channel 6 of ADC 1 when it could be used for ADC 2 as well.|
137-
|`analogReadPinAdc(pin, adc)`|Reads the value from the specified QMK pin and ADC, eg. `C0, 1` will read from channel 6, ADC 2 instead of ADC 1. Note that the ADCs are 0-indexed for this function.|
138-
|`pinToMux(pin)` |Translates a given QMK pin to a channel and ADC combination. If an unsupported pin is given, returns the mux value for "0V (GND)".|
139-
|`adc_read(mux)` |Reads the value from the ADC according to the specified pin and adc combination. See your MCU's datasheet for more information.|
131+
|Function |Description |
132+
|----------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
133+
|`analogReadPin(pin)` |Reads the value from the specified pin, eg. `A0` for channel 0 on the STM32F0 and ADC1 channel 1 on the STM32F3. Note that if a pin can be used for multiple ADCs, it will pick the lower numbered ADC for this function. eg. `C0` will be channel 6 of ADC 1 when it could be used for ADC 2 as well.|
134+
|`analogReadPinAdc(pin, adc)`|Reads the value from the specified pin and ADC, eg. `C0, 1` will read from channel 6, ADC 2 instead of ADC 1. Note that the ADCs are 0-indexed for this function. |
135+
|`pinToMux(pin)` |Translates a given pin to a channel and ADC combination. If an unsupported pin is given, returns the mux value for "0V (GND)". |
136+
|`adc_read(mux)` |Reads the value from the ADC according to the specified pin and ADC combination. See your MCU's datasheet for more information. |
140137

141138
## Configuration
142139

143140
## ARM
144141

145-
The ARM implementation of the ADC has a few additional options that you can override in your own keyboards and keymaps to change how it operates.
142+
The ARM implementation of the ADC has a few additional options that you can override in your own keyboards and keymaps to change how it operates. Please consult the corresponding `hal_adc_lld.h` in ChibiOS for your specific microcontroller for further documentation on your available options.
146143

147-
|`#define` |Type |Default |Description|
148-
|-------------------|------|---------------------|-----------|
149-
|ADC_CIRCULAR_BUFFER|`bool`|`false` |If `TRUE`, then the implementation will use a circular buffer.|
150-
|ADC_NUM_CHANNELS |`int` |`1` |Sets the number of channels that will be scanned as part of an ADC operation. The current implementation only supports `1`.|
151-
|ADC_BUFFER_DEPTH |`int` |`2` |Sets the depth of each result. Since we are only getting a 12-bit result by default, we set this to `2` bytes so we can contain our one value. This could be set to 1 if you opt for a 8-bit or lower result.|
152-
|ADC_SAMPLING_RATE |`int` |`ADC_SMPR_SMP_1P5` |Sets the sampling rate of the ADC. By default, it is set to the fastest setting. Please consult the corresponding `hal_adc_lld.h` in ChibiOS for your specific microcontroller for further documentation on your available options.|
153-
|ADC_RESOLUTION |`int` |`ADC_CFGR1_RES_12BIT`|The resolution of your result. We choose 12 bit by default, but you can opt for 12, 10, 8, or 6 bit. Please consult the corresponding `hal_adc_lld.h` in ChibiOS for your specific microcontroller for further documentation on your available options.|
144+
|`#define` |Type |Default |Description |
145+
|---------------------|------|---------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
146+
|`ADC_CIRCULAR_BUFFER`|`bool`|`false` |If `true`, then the implementation will use a circular buffer. |
147+
|`ADC_NUM_CHANNELS` |`int` |`1` |Sets the number of channels that will be scanned as part of an ADC operation. The current implementation only supports `1`. |
148+
|`ADC_BUFFER_DEPTH` |`int` |`2` |Sets the depth of each result. Since we are only getting a 12-bit result by default, we set this to 2 bytes so we can contain our one value. This could be set to 1 if you opt for an 8-bit or lower result.|
149+
|`ADC_SAMPLING_RATE` |`int` |`ADC_SMPR_SMP_1P5` |Sets the sampling rate of the ADC. By default, it is set to the fastest setting. |
150+
|`ADC_RESOLUTION` |`int` |`ADC_CFGR1_RES_12BIT`|The resolution of your result. We choose 12 bit by default, but you can opt for 12, 10, 8, or 6 bit. |

keyboards/handwired/promethium/config.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
150150
//#define NO_ACTION_FUNCTION
151151

152152
#define PS2_MOUSE_INIT_DELAY 2000
153-
#define BATTERY_PIN 9
154153
#define BATTERY_POLL 30000
155154
#define MAX_VOLTAGE 4.2
156155
#define MIN_VOLTAGE 3.2

keyboards/handwired/promethium/promethium.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "timer.h"
44
#include "matrix.h"
55
#include "musical_notes.h"
6+
#include "adafruit_ble.h"
67

78
float fauxclicky_pressed_note[2] = MUSICAL_NOTE(_A4, 0.0625);
89
float fauxclicky_released_note[2] = MUSICAL_NOTE(_A4, 0.0625);
@@ -11,7 +12,7 @@ float fauxclicky_beep_note[2] = MUSICAL_NOTE(_C6, 0.25);
1112
// cubic fit {3.3, 0}, {3.5, 2.9}, {3.6, 5}, {3.7, 8.6}, {3.8, 36}, {3.9, 62}, {4.0, 73}, {4.05, 83}, {4.1, 89}, {4.15, 94}, {4.2, 100}
1213

1314
uint8_t battery_level(void) {
14-
float voltage = analogRead(BATTERY_PIN) * 2 * 3.3 / 1024;
15+
float voltage = adafruit_ble_read_battery_voltage() * 2 * 3.3 / 1024;
1516
if (voltage < MIN_VOLTAGE) return 0;
1617
if (voltage > MAX_VOLTAGE) return 255;
1718
return (voltage - MIN_VOLTAGE) / (MAX_VOLTAGE - MIN_VOLTAGE) * 255;

tmk_core/protocol/lufa/adafruit_ble.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
#ifdef SAMPLE_BATTERY
4040
# ifndef BATTERY_LEVEL_PIN
41-
# define BATTERY_LEVEL_PIN 7
41+
# define BATTERY_LEVEL_PIN B5
4242
# endif
4343
#endif
4444

@@ -556,7 +556,7 @@ void adafruit_ble_task(void) {
556556
if (timer_elapsed(state.last_battery_update) > BatteryUpdateInterval && resp_buf.empty()) {
557557
state.last_battery_update = timer_read();
558558

559-
state.vbat = analogRead(BATTERY_LEVEL_PIN);
559+
state.vbat = analogReadPin(BATTERY_LEVEL_PIN);
560560
}
561561
#endif
562562
}

0 commit comments

Comments
 (0)