2828#include < sys/types.h>
2929#include < unistd.h>
3030
31+ #include < algorithm>
3132#include < sstream>
3233#include < string>
3334#include < vector>
@@ -43,6 +44,7 @@ namespace gpio {
4344const char GPIODriver::GPIO_BASE_DIR [] = " /sys/class/gpio/gpio" ;
4445
4546using ola::thread::MutexLocker;
47+ using std::find;
4648using std::string;
4749using std::vector;
4850
@@ -121,7 +123,9 @@ bool GPIODriver::SetupGPIO() {
121123 * echo N > /sys/class/gpio/export
122124 * That requires root access.
123125 */
124- const string direction (" out" );
126+ const string normal_direction (" low" );
127+ const string inverted_direction (" high" );
128+ const string* direction = nullptr ;
125129 bool failed = false ;
126130 vector<uint16_t >::const_iterator iter = m_options.gpio_pins .begin ();
127131 for (; iter != m_options.gpio_pins .end (); ++iter) {
@@ -133,7 +137,12 @@ bool GPIODriver::SetupGPIO() {
133137 break ;
134138 }
135139
136- GPIOPin pin = {pin_fd, UNDEFINED , false };
140+ // Check if pin is in the inverted pin list
141+ bool inverted = (find (m_options.gpio_inverted_pins .begin (),
142+ m_options.gpio_inverted_pins .end (),
143+ (*iter)) != m_options.gpio_inverted_pins .end ());
144+
145+ GPIOPin pin = {pin_fd, UNDEFINED , false , inverted};
137146
138147 // Set dir
139148 str.str (" " );
@@ -143,7 +152,16 @@ bool GPIODriver::SetupGPIO() {
143152 failed = true ;
144153 break ;
145154 }
146- if (write (fd, direction.c_str (), direction.size ()) < 0 ) {
155+ // Assign correct initial state
156+ if (inverted) {
157+ direction = &inverted_direction;
158+ } else {
159+ direction = &normal_direction;
160+ }
161+
162+ OLA_DEBUG << " Configuring GPIO pin " << static_cast <int >(*iter)
163+ << " with " << ( inverted ? " inverted" : " normal" ) << " logic" ;
164+ if (write (fd, direction->c_str (), direction->size ()) < 0 ) {
147165 OLA_WARN << " Failed to enable output on " << str.str () << " : "
148166 << strerror (errno);
149167 failed = true ;
@@ -190,7 +208,14 @@ bool GPIODriver::UpdateGPIOPins(const DmxBuffer &dmx) {
190208
191209 // Change the pin state if required.
192210 if (action != NO_CHANGE ) {
193- char data = (action == TURN_ON ? ' 1' : ' 0' );
211+ char data;
212+ bool state = (action == TURN_ON );
213+ // Handle inverted logic appropriately
214+ if (m_gpio_pins[i].inverted ) {
215+ state = !state;
216+ }
217+ // Convert to char and write to sysfs
218+ data = (state ? ' 0' : ' 1' );
194219 if (write (m_gpio_pins[i].fd , &data, sizeof (data)) < 0 ) {
195220 OLA_WARN << " Failed to toggle GPIO pin " << i << " , fd "
196221 << static_cast <int >(m_gpio_pins[i].fd ) << " : "
0 commit comments