| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <string.h>
- #define DEVICE_FILE "/dev/gpio_led"
- int main()
- {
- int fd;
- char write_buf[2];
- ssize_t written;
- // 打开设备文件
- fd = open(DEVICE_FILE, O_RDWR);
- if (fd < 0) {
- perror("open");
- return -1;
- }
- // 在死循环中交替写入 0 和 1,控制 LED
- int i = 0;
- while (1) {
- // 每次循环写入 1(开启 LED)或 0(关闭 LED)
- if (i % 2 == 0) {
- write_buf[0] = '1'; // LED 开
- } else {
- write_buf[0] = '0'; // LED 关
- }
- // 向设备写入数据
- written = write(fd, write_buf, 1);
- if (written < 0) {
- perror("write");
- close(fd);
- return -1;
- } else {
- printf("Wrote %c to the device\n", write_buf[0]);
- }
- // 延迟 1 秒钟
- sleep(1);
- i++;
- }
- // 关闭设备文件
- close(fd);
- return 0;
- }
|