gpt4 book ai didi

c - 挂起轮询 GPIO

转载 作者:太空宇宙 更新时间:2023-11-04 10:53:49 25 4
gpt4 key购买 nike

我已经实现了这个功能来轮询 GPIO 开关,并在录制过程中点亮 LED。该开关用于开始录音。录制结束时 LED 熄灭。我发现代码在激活开关之间的短时间内有效。但是,如果我让程序长时间运行......开关开始失去功能。就像,当我拨动开关时,我不能再开始/停止录音了。

所以说我开始录制,然后等待很长一段时间,可能是 1/2 小时。当我切换到“关闭”时,它没有注册。但是,如果我做一个 cat ../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() 一次

关于c - 挂起轮询 GPIO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29647457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com