1111#include < sys/ioctl.h> // ioctl()
1212#include < errno.h> // errno, strerror()
1313#include < string.h> // std::string, strcpy()
14- #include < map>
1514#include " gpio.h"
1615
1716// instantiate some global structs to setup cache
1817// doing this globally ensures the data struct is zero-ed out
19- typedef int gpio_fd; // for readability
20- std::map<rf24_gpio_pin_t , gpio_fd> cachedPins;
2118struct gpio_v2_line_request request;
2219struct gpio_v2_line_values data;
2320
24- // initialize static member .
21+ // initialize static members .
2522int GPIOChipCache::fd = -1 ;
23+ std::map<rf24_gpio_pin_t , gpio_fd> GPIOChipCache::cachedPins = std::map<rf24_gpio_pin_t , gpio_fd>();
2624
2725void GPIOChipCache::openDevice ()
2826{
@@ -97,8 +95,8 @@ void GPIO::open(rf24_gpio_pin_t port, int DDR)
9795 }
9896
9997 // check if pin is already in use
100- std::map<rf24_gpio_pin_t , gpio_fd>::iterator pin = cachedPins.find (port);
101- if (pin == cachedPins.end ()) { // pin not in use; add it to cached request
98+ std::map<rf24_gpio_pin_t , gpio_fd>::iterator pin = gpioCache. cachedPins .find (port);
99+ if (pin == gpioCache. cachedPins .end ()) { // pin not in use; add it to cached request
102100 request.offsets [0 ] = port;
103101 request.fd = 0 ;
104102 }
@@ -127,25 +125,25 @@ void GPIO::open(rf24_gpio_pin_t port, int DDR)
127125 throw GPIOException (msg);
128126 return ;
129127 }
130- cachedPins.insert (std::pair<rf24_gpio_pin_t , gpio_fd>(port, request.fd ));
128+ gpioCache. cachedPins .insert (std::pair<rf24_gpio_pin_t , gpio_fd>(port, request.fd ));
131129}
132130
133131void GPIO::close (rf24_gpio_pin_t port)
134132{
135- std::map<rf24_gpio_pin_t , gpio_fd>::iterator pin = cachedPins.find (port);
136- if (pin == cachedPins.end ()) {
133+ std::map<rf24_gpio_pin_t , gpio_fd>::iterator pin = gpioCache. cachedPins .find (port);
134+ if (pin == gpioCache. cachedPins .end ()) {
137135 return ;
138136 }
139137 if (pin->second > 0 ) {
140138 ::close (pin->second);
141139 }
142- cachedPins.erase (pin);
140+ gpioCache. cachedPins .erase (pin);
143141}
144142
145143int GPIO::read (rf24_gpio_pin_t port)
146144{
147- std::map<rf24_gpio_pin_t , gpio_fd>::iterator pin = cachedPins.find (port);
148- if (pin == cachedPins.end () || pin->second <= 0 ) {
145+ std::map<rf24_gpio_pin_t , gpio_fd>::iterator pin = gpioCache. cachedPins .find (port);
146+ if (pin == gpioCache. cachedPins .end () || pin->second <= 0 ) {
149147 throw GPIOException (" [GPIO::read] pin not initialized! Use GPIO::open() first" );
150148 return -1 ;
151149 }
@@ -164,8 +162,8 @@ int GPIO::read(rf24_gpio_pin_t port)
164162
165163void GPIO::write (rf24_gpio_pin_t port, int value)
166164{
167- std::map<rf24_gpio_pin_t , gpio_fd>::iterator pin = cachedPins.find (port);
168- if (pin == cachedPins.end () || pin->second <= 0 ) {
165+ std::map<rf24_gpio_pin_t , gpio_fd>::iterator pin = gpioCache. cachedPins .find (port);
166+ if (pin == gpioCache. cachedPins .end () || pin->second <= 0 ) {
169167 throw GPIOException (" [GPIO::write] pin not initialized! Use GPIO::open() first" );
170168 return ;
171169 }
0 commit comments