Skip to content

RP2040: adapt NinaPins to be used as class #180

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

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/utility/nano_rp2040_support.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
* FUNCTION DEFINITION
******************************************************************************/

#ifdef NINA_PINS_AS_CLASS
#define VAL(x) x.get()
#else
#define VAL(x) static_cast<uint8_t>(x)
#endif

uint8_t toAnalogPin(NinaPin pin)
{
if (pin == A4) return 6; /* ADC1 - CH6 */
Expand All @@ -41,20 +47,20 @@ uint8_t toAnalogPin(NinaPin pin)

void pinMode(NinaPin pin, PinMode mode)
{
WiFiDrv::pinMode(static_cast<uint8_t>(pin), static_cast<uint8_t>(mode));
WiFiDrv::pinMode(VAL(pin), static_cast<uint8_t>(mode));
}

PinStatus digitalRead(NinaPin pin)
{
return WiFiDrv::digitalRead(static_cast<uint8_t>(pin));
return WiFiDrv::digitalRead(VAL(pin));
}

void digitalWrite(NinaPin pin, PinStatus value)
{
if (value == LOW)
WiFiDrv::digitalWrite(static_cast<uint8_t>(pin), 1);
WiFiDrv::digitalWrite(VAL(pin), 1);
else
WiFiDrv::digitalWrite(static_cast<uint8_t>(pin), 0);
WiFiDrv::digitalWrite(VAL(pin), 0);
}

int analogRead(NinaPin pin)
Expand All @@ -64,12 +70,16 @@ int analogRead(NinaPin pin)
if (adc_channel == 0xFF)
return 0;
else
#ifdef NINA_PINS_AS_CLASS
return WiFiDrv::analogRead(adc_channel) >> (12 - pin.analogReadResolution());
Copy link
Contributor

Choose a reason for hiding this comment

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

This line literally makes me want to 😢

#else
return WiFiDrv::analogRead(adc_channel);
#endif
}

void analogWrite(NinaPin pin, int value)
{
WiFiDrv::analogWrite(static_cast<uint8_t>(pin), static_cast<uint8_t>(value));
WiFiDrv::analogWrite(VAL(pin), static_cast<uint8_t>(value));
}

#endif /* ARDUINO_NANO_RP2040_CONNECT */