Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes analog read for pins A6 and A7 #338

Merged
merged 4 commits into from
Oct 27, 2024

Conversation

elral
Copy link
Collaborator

@elral elral commented Oct 18, 2024

Description of changes

ProMicro has some special pin mapping within pins_arduino.h through which analog pins A6 and A7 can not be adressed via there digital pin numbers. But Mobiflight does only use the digital pin numbers also for pins with analog readin function.
With this change it is checked if the digitla pin number is one of the two pin numbers and in this case a reampping is done so that the pin number fits to the required one.

Fixes #337

Copy link

Board and firmware folder for this pull request:
Mobiflight-Connector.zip

Copy link

Board and firmware folder for this pull request:
Mobiflight-Connector.zip

// therefore reading from A6 and A7 does not work
// via "digital" pins. See also pins_arduino.h
if (_pin == 4)
_pin = A6;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively we could have mapped the label A6 in the boards.json file, no?
That would leave this special treatment out here. My first thought was, that special handling for certain variants is not so great. i know we have done it for Pico, but that's an entirely different platform.

Copy link
Collaborator

@DocMoebiuz DocMoebiuz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we chat about this change and see if we want to keep it here?

Copy link

Board and firmware folder for this pull request:
Mobiflight-Connector.zip

@elral
Copy link
Collaborator Author

elral commented Oct 27, 2024

Summary from our chat on Discord:

Analog A6 is on pin 4. To use this pin 4 as analog in, the Arduino framework expects analogRead(A6). A6 is defined as 24 in the .h file. The analogRead() function subtracts 18 for pin numbers > 18. So I can also specify the value 6 as the pin number. But that is not pin 4 that I want to connect the potentiometer to.

Ax (x-18 if x>17) is equivalent to digital pin x, except for A6 and A7. Therefore you have to check if it is one of these two pins and then map it.

This special feature, that the digital pin number cannot be used for analogRead(), only exists with these two pins on the ProMicro.

I see no chance just to do it in the board.json file.

See also from arduino_pins.h:

// Mapping of analog pins as digital I/O
// A6-A11 share with digital pins
static const uint8_t A0 = 18;
static const uint8_t A1 = 19;
static const uint8_t A2 = 20;
static const uint8_t A3 = 21;
static const uint8_t A4 = 22;
static const uint8_t A5 = 23;
static const uint8_t A6 = 24;   // D4
static const uint8_t A7 = 25;   // D6
static const uint8_t A8 = 26;   // D8
static const uint8_t A9 = 27;   // D9
static const uint8_t A10 = 28;  // D10
static const uint8_t A11 = 29;  // D12

#define analogPinToChannel(P)  ( pgm_read_byte( analog_pin_to_channel_PGM + (P) ) )

//  _AVR_ATmega32U4 has an unusual mapping of pins to channels
extern const uint8_t PROGMEM analog_pin_to_channel_PGM[];
const uint8_t PROGMEM analog_pin_to_channel_PGM[] = {
    7,  // A0               PF7                 ADC7
    6,  // A1               PF6                 ADC6
    5,  // A2               PF5                 ADC5
    4,  // A3               PF4                 ADC4
    1,  // A4               PF1                 ADC1
    0,  // A5               PF0                 ADC0
    8,  // A6       D4      PD4                 ADC8
    10, // A7       D6      PD7                 ADC10
    11, // A8       D8      PB4                 ADC11
    12, // A9       D9      PB5                 ADC12
    13, // A10      D10     PB6                 ADC13
    9   // A11      D12     PD6                 ADC9
};

Are there anymore informations requuired to merge this Pullrequest?

@DocMoebiuz DocMoebiuz merged commit 1139657 into MobiFlight:main Oct 27, 2024
1 check passed
@elral elral deleted the fix_micro_analog_pins branch October 27, 2024 07:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Issues A6 and A7 with potentiometers on ProMicro
2 participants