Skip to content

Commit 22bf611

Browse files
committed
Identify analogRead() input type and get right pin
This first commit is only for the micro:bit board, but it is to be extended to the rest of the variants. The analogRead() argument can be the "ADC Channel" or the pin directly. Boards without the same type of pin organisation than the Uno (Analog pins in order after the digital pins) need a look up table to convert a channel value into the right Pin.
1 parent bf534ec commit 22bf611

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

cores/nRF5/wiring_analog_nRF51.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,16 @@ uint32_t analogRead( uint32_t ulPin )
119119
uint32_t resolution;
120120
int16_t value;
121121

122+
#ifdef ARDUINO_ADC_NON_STANDARD_PINS
123+
ulPin = _analogChannelPin(ulPin);
124+
#elif NUM_ANALOG_INPUTS > 0
125+
if (ulPin < A0) {
126+
ulPin += A0;
127+
}
128+
#else
129+
return 0;
130+
#endif
131+
122132
if (ulPin >= PINS_COUNT) {
123133
return 0;
124134
}

variants/BBCmicrobit/variant.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ extern "C"
4040
// LEDs
4141
#define PIN_LED (13)
4242
#define LED_BUILTIN PIN_LED
43-
// #define
4443

4544
// Buttons
4645
#define PIN_BUTTON_A (5)
@@ -65,6 +64,23 @@ static const uint8_t A4 = PIN_A4 ;
6564
static const uint8_t A5 = PIN_A5 ;
6665
#define ADC_RESOLUTION 10
6766

67+
// Non standard distribution of Analog Pins requires a lookup table
68+
#define ARDUINO_ADC_NON_STANDARD_PINS
69+
__inline__ uint32_t _analogChannelPin(uint32_t pin) __attribute__((always_inline));
70+
__inline__ uint32_t _analogChannelPin(uint32_t pin) {
71+
switch (pin) {
72+
case 0: return PIN_A0;
73+
case 1: return PIN_A1;
74+
case 2: return PIN_A2;
75+
case 3: return PIN_A3;
76+
case 4: return PIN_A4;
77+
case 5: return PIN_A5;
78+
case 10: return PIN_A5;
79+
// Otherwise non-analog pin so return out of range pin to exit early
80+
default: return PINS_COUNT;
81+
}
82+
}
83+
6884
/*
6985
* Serial interfaces
7086
*/

0 commit comments

Comments
 (0)