Permalink
Browse files

Gpio class will now optionally avoid unexporting pin upon `close()`. …

…As a side effect this will fix #302: the issue was due to the pin being unexported by the dying Bela program, thus leaving bela-cape-btn to poll forever
  • Loading branch information...
giuliomoro committed Jun 24, 2017
1 parent 1313bb7 commit 8bc9adadd57badaf4569793ec2588c7831b45943
Showing with 13 additions and 5 deletions.
  1. +1 −1 core/PRU.cpp
  2. +12 −4 include/Gpio.h
View
@@ -393,7 +393,7 @@ int PRU::initialise(int pru_num, int frames_per_buffer, int spi_channels, int mu
pru_buffer_audio_dac[i] = 0;
if(capeButtonMonitoring){
belaCapeButton.open(BELA_CAPE_BUTTON_PIN, INPUT);
belaCapeButton.open(BELA_CAPE_BUTTON_PIN, INPUT, false);
}
// Allocate audio buffers
View
@@ -45,10 +45,11 @@ class Gpio{
*
* @param pin the GPIO pin ( 0 <= pin < 128)
* @param direction one of INPUT or OUTPUT
* @param unexport if `false`, it will not try to unexport the pin when calling `close()`
*
* @return 0 if success, -1 otherwise;
*/
int open(unsigned int pin, unsigned int direction){
int open(unsigned int pin, unsigned int direction, bool unexport = true){
if(pin >= 128){
return -1;
}
@@ -57,8 +58,13 @@ class Gpio{
}
oldPin = pin;
// gpio_export can fail if the pin has already been exported.
// We don't care.
gpio_export(pin);
// if that is the case, let's make a note of it so we do not
// unexport it when we are done.
int ret = gpio_export(pin);
if(ret == 0 && unexport)
shouldUnexport = true;
else
shouldUnexport = false;
if(gpio_set_dir(pin, direction) < 0){
return -1;
}
@@ -85,7 +91,8 @@ class Gpio{
* Closes a currently open GPIO
*/
void close(){
gpio_unexport(oldPin);
if(shouldUnexport)
gpio_unexport(oldPin);
::close(fd);
oldPin = -1;
fd = -1;
@@ -132,6 +139,7 @@ class Gpio{
return fd != -1;
}
private:
bool shouldUnexport;
int oldPin;
int fd;
uint32_t pinMask;

0 comments on commit 8bc9ada

Please sign in to comment.