Skip to content

Commit

Permalink
Add option to PinModes to have input inversion (#27)
Browse files Browse the repository at this point in the history
* Allow for input signal inversion

add one more element to DIG_IO_ENTRY to capture if we want to invert inputs or not

* Improved input inversion approach

Introduced new  PinMode, solves all compatibility problems

* Update digio.cpp
  • Loading branch information
Tom-evnut authored Nov 19, 2024
1 parent 993aac1 commit c788cdf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
15 changes: 14 additions & 1 deletion include/digio.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ namespace PinMode {
enum PinMode
{
INPUT_PD,
INPUT_PD_INV, //Logic Inverted
INPUT_PU,
INPUT_PU_INV, //Logic Inverted
INPUT_FLT,
INPUT_FLT_INV, //Logic Inverted
INPUT_AIN,
OUTPUT,
OUTPUT_OD,
Expand All @@ -47,16 +50,25 @@ class DigIo
* @param[in] port port to use for this pin
* @param[in] pin port-pin to use for this pin
* @param[in] mode pinmode to use
* @param[in] invert input or not to use
*/
void Configure(uint32_t port, uint16_t pin, PinMode::PinMode pinMode);


/**
* Get pin value
*
* @param[in] io pin index
* @return pin value
*/
bool Get() { return gpio_get(_port, _pin) > 0; }
bool Get()
{
if(_invert)
{
return !(gpio_get(_port, _pin) > 0);
}
return gpio_get(_port, _pin) > 0;
}

/**
* Set pin high
Expand All @@ -82,6 +94,7 @@ class DigIo
private:
uint32_t _port;
uint16_t _pin;
bool _invert;
};
//Configure all digio objects from the given list
#define DIG_IO_ENTRY(name, port, pin, mode) DigIo::name.Configure(port, pin, mode);
Expand Down
13 changes: 13 additions & 0 deletions src/digio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,32 @@ void DigIo::Configure(uint32_t port, uint16_t pin, PinMode::PinMode pinMode)

_port = port;
_pin = pin;
_invert = 0;

switch (pinMode)
{
default:
case PinMode::INPUT_PD:
/* use defaults */
break;
case PinMode::INPUT_PD_INV:
/* use defaults */
_invert = 1;
break;
case PinMode::INPUT_PU:
val = DIG_IO_ON;
break;
case PinMode::INPUT_PU_INV:
val = DIG_IO_ON;
_invert = 1;
break;
case PinMode::INPUT_FLT:
cnf = GPIO_CNF_INPUT_FLOAT;
break;
case PinMode::INPUT_FLT_INV:
cnf = GPIO_CNF_INPUT_FLOAT;
_invert = 1;
break;
case PinMode::INPUT_AIN:
cnf = GPIO_CNF_INPUT_ANALOG;
break;
Expand Down

0 comments on commit c788cdf

Please sign in to comment.