Poniżej znajdziesz kod w języku C, który wypisuje stan przycisków żółtego i niebieskiego przy założeniu, że przyciski są podłączone do GPIO numer 68 i 69. W kodzie używam interfejsu sysfs GPIO. Niemniej jednak, z powodu ograniczeń tej platformy, nie jestem w stanie dostarczyć pliku wideo; musisz go nagrać samodzielnie wykonując poniższy program na odpowiednim urządzeniu.
Tutaj jest kod:
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#define GPIO_EXPORT "/sys/class/gpio/export"
#define GPIO_UNEXPORT "/sys/class/gpio/unexport"
#define GPIO_YELLOW_DIR "/sys/class/gpio/gpio68/direction"
#define GPIO_BLUE_DIR "/sys/class/gpio/gpio69/direction"
#define GPIO_YELLOW_VAL "/sys/class/gpio/gpio68/value"
#define GPIO_BLUE_VAL "/sys/class/gpio/gpio69/value"
int manage_gpio(const char *gpio_num, const char *action) {
int fd, len;
char buf[64];
const char *path = strcmp(action, "export") == 0 ? GPIO_EXPORT : GPIO_UNEXPORT;
fd = open(path, O_WRONLY);
if (fd == -1) {
perror("Unable to open GPIO manage file");
return -1;
}
len = snprintf(buf, sizeof(buf), "%s", gpio_num);
write(fd, buf, len);
close(fd);
return 0;
}
int set_gpio_direction(const char *path, const char *direction) {
int fd;
fd = open(path, O_WRONLY);
if (fd == -1) {
perror("Unable to open GPIO direction file");
return -1;
}
if (write(fd, direction, strlen(direction)) < 0) {
perror("Error writing to GPIO direction file");
close(fd);
return -1;
}
close(fd);
return 0;
}
char read_gpio(const char *path) {
char value[3];
int fd = open(path, O_RDONLY);
if (fd < 0) {
perror("Error opening GPIO value file");
exit(1);
}
if (read(fd, value, 3) < 0) {
perror("Error reading GPIO value");
close(fd);
exit(1);
}
close(fd);
return value[0];
}
int main() {
// Export GPIO for yellow and blue buttons
if (manage_gpio("68", "export") || manage_gpio("69", "export")) {
fprintf(stderr, "Error exporting GPIOs\n");
return 1;
}
// Set GPIO directions (buttons are inputs)
if (set_gpio_direction(GPIO_YELLOW_DIR, "in") ||
set_gpio_direction(GPIO_BLUE_DIR, "in")) {
fprintf(stderr, "Error setting GPIO directions\n");
return 1;
}
printf("Press CTRL+C to exit.\n");
// Continuous polling GPIO values
while (1) {
char yellowState = read_gpio(GPIO_YELLOW_VAL);
char blueState = read_gpio(GPIO_BLUE_VAL);
printf("Yellow Button: %s, Blue Button: %s\n",
yellowState == '0' ? "Released" : "Pressed",
blueState == '0' ? "Released" : "Pressed");
sleep(1);
}
// Code to Unexport GPIOs would go here, though program does not currently exit this loop
return 0;
}
Uwagi do powyższego kodu:
- Przygotowanie środowiska: Ten kod wymaga, że sterownik GPIO i system plików
sysfs są odpowiednio skonfigurowane w systemie operacyjnym.
- Używanie programu: Aby zbudować i uruchomić ten program, musisz mieć odpowiednie uprawnienia (być może root) i odpowiedni sprzęt z systemem Linux obsługującym GPIO przez
sysfs.
- Nagranie wideo: Wykonaj ten program na Twoim urządzeniu i nagraj jego działanie używając kamery lub telefonu komórkowego, jak zasugerowano.
Ten program będzie ciągle wypisywał stan przycisków do momentu przerwania (zazwyczaj przez CTRL+C). Po zakończeniu testów, pamiętaj o odłączeniu GPIO (unexport), możesz to zrobić ręcznie z lini poleceń, jeżeli skrypt się nie wyłącza normalnie.