openwrt控制LED 按键 蜂鸣器 重启系统或者恢复出厂设置
1.通过脚本控制LED
echo "1" >/sys/class/leds/LEDNAME/brightness//LED亮
echo "0" >/sys/class/leds/LEDNAME/brightness//LED灭
//LED闪烁
echo "1" >/sys/class/leds/LEDNAME/brightness
echo "timer" >/sys/class/leds/LEDNAME/trigger
echo "200" >/sys/class/leds/LEDNAME/delay_off
echo "200" >/sys/class/leds/LEDNAME/delay_on
2.通过脚本控制蜂鸣器
#!/bin/sh
echo 0 > /sys/class/pwm/pwmchip0/export
echo 250000 > /sys/class/pwm/pwmchip0/pwm0/period
echo 125000 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle
echo 1 > /sys/class/pwm/pwmchip0/pwm0/enable
2.通过C程序检测按键,自己要交叉编译下载到目标版
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <limits.h>
#include <termios.h>
#include <errno.h>
#include <linux/input.h>
#define TOUCHKEY_TAG "TouchkeyDemo "
#define TOUCHKEY_FILE "/dev/input/event0"
int main(int argc, char **argv)
{
int nFd;
int nRet;
struct input_event *sInputEvent;
printf(" --> Make time: %s %s %s <-- \n", __DATE__, __TIME__, "Tony.Huang");
sInputEvent = malloc(sizeof(struct input_event));
nFd = open(TOUCHKEY_FILE, O_RDWR);
if(nFd < 0)
{
printf("open file %s error.\n", TOUCHKEY_FILE);
return -1;
}
else
{
printf("open file %s success.\n", TOUCHKEY_FILE);
}
int time = 0;
long unsigned int old_time=0;
int flagOld = 1;
while(1){
memset(sInputEvent, 0, sizeof(struct input_event));
nRet = read(nFd, sInputEvent, sizeof(struct input_event));
if(nRet < 0)
{
printf("read file %s error.\n", TOUCHKEY_FILE);
return nRet;
}
printf("type: 0x%x \n", sInputEvent->type);
printf("code: 0x%x \n", sInputEvent->code);
printf("value: 0x%x\n", sInputEvent->value);
printf("%d\t%d\n",sInputEvent->time.tv_usec,sInputEvent->time.tv_sec);
old_time = sInputEvent->time.tv_sec;
while(sInputEvent->value!=0x0){
memset(sInputEvent, 0, sizeof(struct input_event));
nRet = read(nFd, sInputEvent, sizeof(struct input_event));
time = sInputEvent->time.tv_sec-old_time;
}
printf("button press time---------:%d\n",time);
if(time>0&&time<=3){
printf("reboot\n");
system("reboot");
}else if(time>3){
printf("update file\n");
}
}
free(sInputEvent);
close(nFd);
return 0;
}