1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- #include "plt.h"
- #define DEV_PATH "/sys/class/gpio/gpio129/value" // 输入输出电平值设备
- #define EXPORT_PATH "/sys/class/gpio/export" // GPIO设备导出设备
- #define DIRECT_PATH "/sys/class/gpio/gpio129/direction" // GPIO输入输出控制设备
- #define IN "in"
- #define GPIO "129" // GPIO5_1
- int fd_dev, fd_export, fd_dir, ret;
- int device_power_on = 1;
- int power_detection_init()
- {
- char buf[10];
- fd_export = open(EXPORT_PATH, O_WRONLY); // 打开GPIO设备导出设备
- if (fd_export < 0)
- {
- perror("open export:");
- return -1;
- }
- write(fd_export, GPIO, strlen(GPIO));
- fd_dev = open(DEV_PATH, O_RDWR); // 打开输入输出电平值设备
- if (fd_dev < 0)
- {
- perror("open gpio:");
- return -1;
- }
- fd_dir = open(DIRECT_PATH, O_RDWR); // 打开GPIO输入输出控制设备
- if (fd_dir < 0)
- {
- perror("open direction:");
- return -1;
- }
- strcpy(buf, IN);
- ret = write(fd_dir, buf, strlen(IN));
- if (ret < 0)
- {
- perror("write direction:");
- close(fd_export);
- close(fd_dir);
- close(fd_dev);
- return -1;
- }
- return 0;
- }
- int power_detection_exe()
- {
- static char last[10] = {0};
- static char buf[10] = {0};
- memset(buf, 0, sizeof(buf));
- lseek(fd_dev, 0, SEEK_SET);
- ret = read(fd_dev, buf, sizeof(buf)); // 读取GPIO5_1输入电平值
- if (ret < 0)
- {
- log_dbg("ERROR : failed to read power on/off detection");
- // close(fd_export);
- // close(fd_dir);
- // close(fd_dev);
- // return -1;
- }
- else
- {
- if (buf[0] == '1')
- {
- if (strcmp(buf, last) != 0)
- {
- strcpy(last, buf);
- tb_send_telemetry("power:1");
- log_dbg("Power on");
- }
- device_power_on = 1;
- }
- else
- {
- if (strcmp(buf, last) != 0)
- {
- strcpy(last, buf);
- tb_send_telemetry("power:0");
- log_dbg("Power off");
- }
- device_power_on = 0;
- }
- }
- }
|