挂起轮询 GPIO
Hanging polling GPIO
我已经实现了这个功能来轮询 GPIO 开关,并在录制过程中点亮 LED。该开关用于开始录音。录制结束时 LED 熄灭。
我发现代码在激活开关之间的短时间内有效。但是,如果我长时间离开程序 运行.. 开关开始失去功能。比如,当我拨动开关时,我不能再 start/stop 录音了。
所以说我开始录音,然后等待很长一段时间,大概1/2小时。当我切换到 "Off" 时,它没有注册。
但是,如果我做一只猫 ../gpio57/value,它会给我 0。(1 正在记录)
这可能是什么问题?
void* pollGPIOSwitch(void* arg) {
//pthread_detach(pthread_self());
Vn200* vn200 = (Vn200*)arg;
int fd, LEDFd;
char buf[100];
char LEDbuf[100];
char value;
int videoRecError;
bool videoRecOn = false;
sprintf(buf, "/sys/class/gpio/gpio57/value");
//printf(buf);
while (KEEP_GOING) {
fd = open(buf,O_RDONLY);
lseek(fd,0,SEEK_SET); // -- move to beginning of file
read(fd,&value,1);
if (value=='0') {
printf("Switch is OFF\n");
if (videoRecOn) { // -- recording on, switch off, end recording
stopAllRecordings();
videoRecOn = false;
TriggerGPSINSThreadExit = 0; // -- reset variables
printf("Reset GPSINS Thread variables.\n");
// -- Set LED to off
sprintf(LEDbuf, "/sys/class/gpio/gpio56/value");
//printf(LEDbuf);
LEDFd = open(LEDbuf, O_WRONLY);
write(LEDFd,"0",2);
}
}
else if (!videoRecOn) { // -- recording off, switch on, start recording
printf("Switch is ON\n");
if (pthread_create(&GPSINSLoggingThread, NULL, runGPSINS,(void*) vn200) != 0) {
printf("Error: Fail to create runGPSINS thread\n");
}
videoRecError = startVideoRecording();
if (videoRecError == -1)
pthread_exit(&videoRecError);
videoRecOn = true;
// -- Set LED to on
sprintf(LEDbuf, "/sys/class/gpio/gpio56/value");
//printf(LEDbuf);
LEDFd = open(LEDbuf, O_WRONLY);
write(LEDFd,"1",2);
}
//fflush(stdout);
usleep(500000);
}
close(fd);
close(LEDFd);
printf("Exited Polling!");
}
你有
close(fd);
在 执行 open()
的循环之外。我认为您正在疯狂地泄漏文件描述符,最终导致 open()
失败,而您没有检测到。始终错误检查您的 I/O.
也许 lseek()
暗示您想在循环之前执行 open()
一次。
我已经实现了这个功能来轮询 GPIO 开关,并在录制过程中点亮 LED。该开关用于开始录音。录制结束时 LED 熄灭。 我发现代码在激活开关之间的短时间内有效。但是,如果我长时间离开程序 运行.. 开关开始失去功能。比如,当我拨动开关时,我不能再 start/stop 录音了。
所以说我开始录音,然后等待很长一段时间,大概1/2小时。当我切换到 "Off" 时,它没有注册。 但是,如果我做一只猫 ../gpio57/value,它会给我 0。(1 正在记录)
这可能是什么问题?
void* pollGPIOSwitch(void* arg) {
//pthread_detach(pthread_self());
Vn200* vn200 = (Vn200*)arg;
int fd, LEDFd;
char buf[100];
char LEDbuf[100];
char value;
int videoRecError;
bool videoRecOn = false;
sprintf(buf, "/sys/class/gpio/gpio57/value");
//printf(buf);
while (KEEP_GOING) {
fd = open(buf,O_RDONLY);
lseek(fd,0,SEEK_SET); // -- move to beginning of file
read(fd,&value,1);
if (value=='0') {
printf("Switch is OFF\n");
if (videoRecOn) { // -- recording on, switch off, end recording
stopAllRecordings();
videoRecOn = false;
TriggerGPSINSThreadExit = 0; // -- reset variables
printf("Reset GPSINS Thread variables.\n");
// -- Set LED to off
sprintf(LEDbuf, "/sys/class/gpio/gpio56/value");
//printf(LEDbuf);
LEDFd = open(LEDbuf, O_WRONLY);
write(LEDFd,"0",2);
}
}
else if (!videoRecOn) { // -- recording off, switch on, start recording
printf("Switch is ON\n");
if (pthread_create(&GPSINSLoggingThread, NULL, runGPSINS,(void*) vn200) != 0) {
printf("Error: Fail to create runGPSINS thread\n");
}
videoRecError = startVideoRecording();
if (videoRecError == -1)
pthread_exit(&videoRecError);
videoRecOn = true;
// -- Set LED to on
sprintf(LEDbuf, "/sys/class/gpio/gpio56/value");
//printf(LEDbuf);
LEDFd = open(LEDbuf, O_WRONLY);
write(LEDFd,"1",2);
}
//fflush(stdout);
usleep(500000);
}
close(fd);
close(LEDFd);
printf("Exited Polling!");
}
你有
close(fd);
在 执行 open()
的循环之外。我认为您正在疯狂地泄漏文件描述符,最终导致 open()
失败,而您没有检测到。始终错误检查您的 I/O.
也许 lseek()
暗示您想在循环之前执行 open()
一次。