@@ -21,20 +21,22 @@ std::map<rf24_gpio_pin_t, gpio_fd> cachedPins;
21
21
struct gpio_v2_line_request request;
22
22
struct gpio_v2_line_values data;
23
23
24
+ // initialize static member.
25
+ int GPIOChipCache::fd = -1 ;
26
+
24
27
void GPIOChipCache::openDevice ()
25
28
{
26
29
if (fd < 0 ) {
27
- fd = open (chip , O_RDONLY);
30
+ fd = open (RF24_LINUX_GPIO_CHIP , O_RDONLY);
28
31
if (fd < 0 ) {
29
32
std::string msg = " Can't open device " ;
30
- msg += chip ;
33
+ msg += RF24_LINUX_GPIO_CHIP ;
31
34
msg += " ; " ;
32
35
msg += strerror (errno);
33
36
throw GPIOException (msg);
34
37
return ;
35
38
}
36
39
}
37
- chipInitialized = true ;
38
40
}
39
41
40
42
void GPIOChipCache::closeDevice ()
@@ -75,31 +77,21 @@ GPIO::~GPIO()
75
77
76
78
void GPIO::open (rf24_gpio_pin_t port, int DDR)
77
79
{
78
- try {
79
- gpioCache.openDevice ();
80
- }
81
- catch (GPIOException& exc) {
82
- if (gpioCache.chipInitialized ) {
83
- throw exc;
84
- return ;
85
- }
86
- gpioCache.chip = " /dev/gpiochip0" ;
87
- gpioCache.openDevice ();
88
- }
80
+ gpioCache.openDevice ();
89
81
90
82
// get chip info
91
83
gpiochip_info info;
92
84
memset (&info, 0 , sizeof (info));
93
85
int ret = ioctl (gpioCache.fd , GPIO_GET_CHIPINFO_IOCTL, &info);
94
86
if (ret < 0 ) {
95
87
std::string msg = " Could not gather info about " ;
96
- msg += gpioCache. chip ;
88
+ msg += RF24_LINUX_GPIO_CHIP ;
97
89
throw GPIOException (msg);
98
90
return ;
99
91
}
100
92
101
93
if (port > info.lines ) {
102
- std::string msg = " pin number " + std::to_string (port) + " not available for " + gpioCache. chip ;
94
+ std::string msg = " pin number " + std::to_string (port) + " not available for " + RF24_LINUX_GPIO_CHIP ;
103
95
throw GPIOException (msg);
104
96
return ;
105
97
}
0 commit comments