From 99785d8f3e63d3f445f1c3660600629be1336436 Mon Sep 17 00:00:00 2001 From: kyz Date: Sat, 17 May 2025 16:38:59 +0800 Subject: [PATCH] modify led/key/alarm --- app/CMakeLists.txt | 3 +- app/S99_alarmmonitor | 4 +- app/{alarmmonitor_d => alarm_d} | 7 +- app/inc/{alarm_monitor.h => alarmmonitor.h} | 43 ++++- app/inc/button.h | 2 - app/inc/led.h | 2 +- app/src/am_main.c | 14 +- app/src/button.c | 16 +- app/src/button_scan.c | 2 +- app/src/config.c | 2 +- app/src/event_recorder.c | 2 +- app/src/iot.c | 2 +- app/src/iot_report.c | 2 +- app/src/lcd.c | 5 +- app/src/led.c | 29 ++- app/src/rtu_master.c | 2 +- app/src/rtu_slave.c | 2 +- app/src/sensor.c | 2 +- app/src/sensor_board.c | 2 +- app/src/spk.c | 184 ++++++++++++++++++++ app/src/tcp.c | 2 +- 21 files changed, 283 insertions(+), 46 deletions(-) rename app/{alarmmonitor_d => alarm_d} (87%) rename app/inc/{alarm_monitor.h => alarmmonitor.h} (85%) create mode 100644 app/src/spk.c diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 80e218b..74b0699 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -46,5 +46,6 @@ target_link_libraries( ) install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION /app) -install(FILES ${PROJECT_SOURCE_DIR}/alarmmonitor_d DESTINATION /app) +install(PROGRAMS ${PROJECT_SOURCE_DIR}/alarm_d DESTINATION /app) +install(PROGRAMS ${PROJECT_SOURCE_DIR}/S99_alarmmonitor DESTINATION /etc/init.d) install(FILES ${PROJECT_SOURCE_DIR}/log4crc DESTINATION /app) diff --git a/app/S99_alarmmonitor b/app/S99_alarmmonitor index 79ccb67..6d40f50 100755 --- a/app/S99_alarmmonitor +++ b/app/S99_alarmmonitor @@ -3,13 +3,11 @@ # Start init # -start() { - /bin/sh /app/alarmMonitor_d start & -} case "$1" in start) start + /app/alarm_d start & ;; stop) ;; diff --git a/app/alarmmonitor_d b/app/alarm_d similarity index 87% rename from app/alarmmonitor_d rename to app/alarm_d index a1a1528..2d1041b 100755 --- a/app/alarmmonitor_d +++ b/app/alarm_d @@ -4,13 +4,14 @@ mkdir -p /var/run/alarmmonitor # 定义守护进程的名称和运行路径 DAEMON_NAME="alarmmonitor" -#DAEMON_PATH="/app/" -DAEMON_PATH="/tmp/" ##debug path +DAEMON_PATH="../app/" +#DAEMON_PATH="/tmp/" ##debug path # 检查守护进程是否已经在运行 is_running() { - ps -ef | grep "./$DAEMON_NAME" | grep -v grep > /dev/null + pid=$(cat /var/run/alarmmonitor/alarmmonitor.pid) + ps -p $pid } diff --git a/app/inc/alarm_monitor.h b/app/inc/alarmmonitor.h similarity index 85% rename from app/inc/alarm_monitor.h rename to app/inc/alarmmonitor.h index def319c..7be66f3 100644 --- a/app/inc/alarm_monitor.h +++ b/app/inc/alarmmonitor.h @@ -1,5 +1,5 @@ -#ifndef __DA_DEVICE_H__ -#define __DA_DEVICE_H__ +#ifndef __ALARMMONITOR_H__ +#define __ALARMMONITOR_H__ #include #include @@ -24,16 +24,36 @@ //#include "led.h" //#include "sensor.h" +#define SW_VERSION "v1.0" + #ifdef __x86_64__ -//#warning "PC Version Debug" -#endif #define WH_INFO_PATH "./wh_info" #define IOT_CFG_PATH "./iot.cfg" #define DEV_CFG_PATH "./dev.cfg" #define FACTORY_CFG_PATH "./factory.cfg" +#define READ_UUID_CMD "echo -n pc12345678901234" + +#define BUTTON_DEVICE_PATH "/dev/input/event2" +#define LED_DEVICE_PATH_FMT "/sys/class/leds/%s/brightness" + +#define PID_FILE "/var/run/alarmmonitor/alarmmonitor.pid" + +#else + +#define WH_INFO_PATH "/userdata/wh_info" +#define IOT_CFG_PATH "/userdata/iot.cfg" +#define DEV_CFG_PATH "/userdata/dev.cfg" +#define FACTORY_CFG_PATH "/userdata/factory.cfg" #define READ_UUID_CMD "cat /proc/cpuinfo | grep Serial" +#define BUTTON_DEVICE_PATH "/dev/input/event1" +#define LED_DEVICE_PATH_FMT "/sys/class/leds/%s/brightness" + +#define PID_FILE "/var/run/alarmmonitor/alarmmonitor.pid" + +#endif + typedef struct{ char whid[80]; //wh id time_t create_time; @@ -185,6 +205,8 @@ int32_t load_factory_info( factory_info_t *cfg ); int32_t store_iot_cfg( iot_cfg_t *cfg ); int32_t load_iot_cfg( iot_cfg_t *cfg ); +int32_t load_wh_info( wh_info_t *out); +int32_t store_wh_info( wh_info_t *wh ); //---------------------------------------------------------------------------- //cfg info @@ -241,10 +263,21 @@ int event_recorder_dispall( void ); //iot void iot_init( am_dev_t *dev ); +void iot_tx_data( uint8_t *data, uint32_t sz ); + +//spk +void spk_init( void ); +int spk_alarm( alarm_type_t alarm ); + +//power ctrl +void remote_jbq_power( int on ); +void remote_jbkzq_power( int on ); extern am_dev_t g_dev; -#endif //__DA_DEVICE_H__ + + +#endif //__ALARMMONITOR_H__ //分区/目录 ////factory info diff --git a/app/inc/button.h b/app/inc/button.h index abea341..8bb392b 100644 --- a/app/inc/button.h +++ b/app/inc/button.h @@ -9,8 +9,6 @@ #include #include -#define BUTTON_DEVICE_PATH "/dev/input/event2" - typedef enum{ BUTTON_NONE = 0, BUTTON_1 = (1<<0), diff --git a/app/inc/led.h b/app/inc/led.h index b49ad54..11a5732 100644 --- a/app/inc/led.h +++ b/app/inc/led.h @@ -1,7 +1,7 @@ #ifndef __LED_H__ #define __LED_H__ -#include "alarm_monitor.h" +#include "alarmmonitor.h" #define LED_X(x) (1<<(x)) diff --git a/app/src/am_main.c b/app/src/am_main.c index 254726e..b4eeff5 100644 --- a/app/src/am_main.c +++ b/app/src/am_main.c @@ -6,11 +6,9 @@ #include #include -#include "alarm_monitor.h" +#include "alarmmonitor.h" #include "led.h" -#define PID_FILE "/var/run/alarmmonitor/alarmmonitor.pid" -#define SW_VERSION "v1.0" am_dev_t g_dev; @@ -93,6 +91,9 @@ int32_t alarm_start( am_dev_t *dev, uint32_t alarm ) else if( alarm == 5 ) led_on( LED_KEY_ALARM_5 ); } + + spk_alarm( dev->alarm.type ); + return 0; } @@ -346,11 +347,12 @@ int main( int argc, char ** argv ) read_device_uuid( dev ); device_button_init( dev ); - device_led_init( dev ); - device_lcd_init( dev ); - device_sensor_init( dev ); + ///device_led_init( dev ); + ///device_lcd_init( dev ); + ///device_sensor_init( dev ); ///sensor_board_init( dev ); + spk_init(); iot_init( dev ); event_recorder_init(); //init sqlite diff --git a/app/src/button.c b/app/src/button.c index a176273..9c68aa5 100644 --- a/app/src/button.c +++ b/app/src/button.c @@ -11,7 +11,7 @@ //#include #include -#include "alarm_monitor.h" +#include "alarmmonitor.h" #define SINGLE_BTN_CB_MAX 50 #define COMB_BTN_CB_MAX 50 @@ -106,6 +106,7 @@ void btn_data_ready_cb( uv_poll_t *handle, int status, int events ) if( nbytes < (int)sizeof(struct input_event )){ AM_LOGE( "Button read event err!expected %d bytes, got %d\n", (int) sizeof(struct input_event), nbytes ); + printf( "---------Button read event err!expected %d bytes, got %d\n", (int) sizeof(struct input_event), nbytes ); }else{ for (int i = 0; i < nbytes / sizeof(struct input_event); i++) { unsigned int type, code, value; @@ -119,13 +120,13 @@ void btn_data_ready_cb( uv_poll_t *handle, int status, int events ) btn_scan = 1; if( value == 0 ){ p_btn->pressed_code &= ~( button_code(code) ); - //printf( "btn up...code:%x\n", pressed_code ); + printf( "btn up...code:%x\n",p_btn->pressed_code ); }else if( value == 1 ){ p_btn->pressed_code |= ( button_code(code) ); - //printf( "btn down...code:%x\n", pressed_code ); + printf( "btn down...code:%x\n", p_btn->pressed_code ); } - //printf( "EV_KEY : KEY_%d, value:%d\n", code - KEY_1 +1, ev[i].value ); + printf( "EV_KEY : KEY_%d, value:%d\n", code - KEY_F1 +1, ev[i].value ); } } //for } @@ -285,8 +286,8 @@ button_event_cb_t find_comb_cb( btn_ctrl_t btn_ctrl, uint32_t code, uint32_t eve //----------------------------------------------------------------------------------------- //static const char *btn_dev_path = "/dev/input/by-path/platform-i8042-serio-0-event-kbd"; -//static const char *btn_dev_path = "/dev/input/by-path/platform-gpio_keys-event"; -static const char *btn_dev_path = "/dev/input/event2"; +//static const char *btn_dev_path = "/dev/input/by-path/platform-gpio-keys-event"; +static const char *btn_dev_path = "/dev/input/event1"; extern void button_event_cb( button_code_t btn_code, button_event_t btn_event, void* value ); @@ -294,13 +295,16 @@ void device_button_init( am_dev_t *dev ) { btn_ctrl_t ctrl; AM_LOGI( "btn init" ); + printf( "btn init\b" ); ctrl = am_button_open( (char*)btn_dev_path ); if( ctrl ){ dev->btn = ctrl; am_button_event_register( ctrl, 0x1FF, EVENT_SINGLE_CLICK|EVENT_DOUBLE_CLICK|EVENT_LONG_PRESS|EVENT_LONG_PRESS_UP, button_event_cb ); am_button_event_combonation_register( ctrl, BUTTON_7|BUTTON_8, EVENT_COMB_PRESS_5S, 0, button_event_cb ); + printf( "-------------------btn init ok\b" ); }else{ + printf( "-----------------------btn init fail\b" ); AM_LOGE( "am_button_open fail\n" ); } } diff --git a/app/src/button_scan.c b/app/src/button_scan.c index a4c49c3..782dad8 100644 --- a/app/src/button_scan.c +++ b/app/src/button_scan.c @@ -7,7 +7,7 @@ #include #include -#include "alarm_monitor.h" +#include "alarmmonitor.h" typedef struct { uint32_t st; //0:up idle, 1: pressed, 2:up wait 250ms for next pressed. 3: double_pressed 8:wait all key released diff --git a/app/src/config.c b/app/src/config.c index fcba028..765445a 100644 --- a/app/src/config.c +++ b/app/src/config.c @@ -8,7 +8,7 @@ #include #include -#include "alarm_monitor.h" +#include "alarmmonitor.h" #define SERIAL_DEF "12345678" #define HW_VER_DEF "V1.0" diff --git a/app/src/event_recorder.c b/app/src/event_recorder.c index 60d16b3..3e88632 100644 --- a/app/src/event_recorder.c +++ b/app/src/event_recorder.c @@ -1,4 +1,4 @@ -#include "alarm_monitor.h" +#include "alarmmonitor.h" #include #define EVENT_DB "event.db" diff --git a/app/src/iot.c b/app/src/iot.c index 2a178dc..94e7506 100644 --- a/app/src/iot.c +++ b/app/src/iot.c @@ -1,4 +1,4 @@ -#include "alarm_monitor.h" +#include "alarmmonitor.h" #include "am_tcp.h" #include "crc16.h" #include "cQueue.h" diff --git a/app/src/iot_report.c b/app/src/iot_report.c index 6faad4b..d63348a 100644 --- a/app/src/iot_report.c +++ b/app/src/iot_report.c @@ -1,4 +1,4 @@ -#include "alarm_monitor.h" +#include "alarmmonitor.h" #include "cJSON.h" extern void hex_to_ascii( uint8_t *out, uint8_t *hex, uint16_t hex_len ); diff --git a/app/src/lcd.c b/app/src/lcd.c index 1622373..fde171e 100644 --- a/app/src/lcd.c +++ b/app/src/lcd.c @@ -11,9 +11,10 @@ #include #include -#include "alarm_monitor.h" +#include "alarmmonitor.h" -#define LCD_UART_PATH "/dev/ttyUSB0" +//#define LCD_UART_PATH "/dev/ttyUSB0" +#define LCD_UART_PATH "/dev/ttyS4" typedef struct{ int uart; diff --git a/app/src/led.c b/app/src/led.c index 6d42c67..ae52c0d 100644 --- a/app/src/led.c +++ b/app/src/led.c @@ -1,10 +1,10 @@ -#include "alarm_monitor.h" +#include "alarmmonitor.h" static const char * led_dev_name[LED_NRS] = { - "early_led ", // 预警 - "disaster_led", // 灾警 + "early_led", // 预警 "airstrike_led", // 空袭 "jiejing_led", // 解警 + "disaster_led", // 灾警 "relief_led", // 解灾 "unlock_led", // 解锁 "stop_led", // 停止 @@ -26,12 +26,20 @@ int led_on( int led_id ) { led_t *p_led; + if( !s_led ) + return -1; + for( int i=0; iled[i]; p_led->on = LED_ON; printf( "LED[ %s ] --> ON\n", led_dev_name[i] ); - if(write( p_led->fd, "1", 1 ) < 0 ){ + if( p_led->fd >= 0 ){ + + if(write( p_led->fd, "1", 1 ) < 0 ){ + printf( "LED[ %s ] [%d] --> fail\n", led_dev_name[i], p_led->fd ); + }else + printf( "LED[ %s ] [%d] --> OK\n", led_dev_name[i], p_led->fd ); } } } @@ -42,12 +50,18 @@ int led_off( int led_id ) { led_t *p_led; + if( !s_led ) + return -1; + for( int i=0; iled[i]; p_led->on = LED_OFF; printf( "LED[ %s ] --> OFF\n", led_dev_name[i] ); - if(write( p_led->fd, "0", 1 ) < 0 ){ + if( p_led->fd >= 0 ){ + + if(write( p_led->fd, "0", 1 ) < 0 ){ + } } } } @@ -75,13 +89,14 @@ static int led_init( am_dev_t *dev, int led ) char path[50]; int fd; - sprintf( path, "/sys/class/leds/%s/brightness", led_dev_name[led] ); - fd = open( path, O_RDONLY ); + sprintf( path, LED_DEVICE_PATH_FMT, led_dev_name[led] ); + fd = open( path, O_WRONLY ); if( fd < 0 ){ //error printf( "open led[%s] fail!\n", path ); return -1; } + printf( "---------------open led[%s] ok, fd:%d!\n", path, fd ); s_led->led[led].fd = fd; return 0; diff --git a/app/src/rtu_master.c b/app/src/rtu_master.c index dfc74d8..af0b953 100644 --- a/app/src/rtu_master.c +++ b/app/src/rtu_master.c @@ -5,7 +5,7 @@ #include #include -#include "alarm_monitor.h" +#include "alarmmonitor.h" #define MB_RTU_PORT_PATH_1 "/dev/ttyUSB0" #define MB_RTU_PORT_PATH_2 "/dev/ttyUSB1" diff --git a/app/src/rtu_slave.c b/app/src/rtu_slave.c index e434c2f..10a15a5 100644 --- a/app/src/rtu_slave.c +++ b/app/src/rtu_slave.c @@ -3,7 +3,7 @@ ModBus 从站功能,作为报警控制器上位机通信接口 预留功能,暂未使用 */ -#include "alarm_monitor.h" +#include "alarmmonitor.h" #define RTU_PORT_PATH "/dev/ttyUSB0" #define RTU_PORT_BAUDRATE 115200 diff --git a/app/src/sensor.c b/app/src/sensor.c index 98eef98..d0c9e26 100644 --- a/app/src/sensor.c +++ b/app/src/sensor.c @@ -6,7 +6,7 @@ #include #include -#include "alarm_monitor.h" +#include "alarmmonitor.h" #define IR_DEV_PATH "/dev/infrared" #define TEM_SENSOR_DEV_PATH "/dev/cht11" diff --git a/app/src/sensor_board.c b/app/src/sensor_board.c index bf54db1..adc8e44 100644 --- a/app/src/sensor_board.c +++ b/app/src/sensor_board.c @@ -1,4 +1,4 @@ -#include "alarm_monitor.h" +#include "alarmmonitor.h" #include "am_tcp.h" #include "crc16.h" diff --git a/app/src/spk.c b/app/src/spk.c new file mode 100644 index 0000000..b81d6fe --- /dev/null +++ b/app/src/spk.c @@ -0,0 +1,184 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "alarmmonitor.h" + +#define SPK_UART_PATH "/dev/ttyUSB0" + +#define SPK_ALARM_STOP 0 +#define SPK_ALARM_PREV 1 + +/* +串口接PC抓取报警操作时控制器发送的数据,波特率9600 +警报启动操作:按“解锁”1秒,再按某一个“警报”1秒 +警报停止操作:按“停止”1秒 + +1. 预警 +[2025-03-05 11:41:28.090]# RECV HEX> +68 10 E1 5A 00 00 00 11 +[2025-03-05 11:41:28.585]# RECV HEX> +68 10 D1 2E 00 00 00 11 +2. 停止 +[2025-03-05 11:41:32.963]# RECV HEX> +68 10 E1 5A 00 00 00 11 +[2025-03-05 11:41:33.457]# RECV HEX> +68 10 E1 1F 00 00 00 11 +3. 空袭 +[2025-03-05 11:41:37.713]# RECV HEX> +68 10 E1 5A 00 00 00 11 +[2025-03-05 11:41:38.208]# RECV HEX> +68 10 D2 2D 00 00 00 11 +4. 停止 + +5. 解警 +[2025-03-05 11:41:48.060]# RECV HEX> +68 10 E1 5A 00 00 00 11 +[2025-03-05 11:41:48.554]# RECV HEX> +68 10 D3 2C 00 00 00 11 +6. 停止 + +7. 灾警 +[2025-03-05 11:41:56.511]# RECV HEX> +68 10 E1 5A 00 00 00 11 +[2025-03-05 11:41:57.005]# RECV HEX> +68 10 D4 2B 00 00 00 11 +8. 停止 + +9. 解灾/消防 +[2025-03-05 11:42:02.812]# RECV HEX> +68 10 E1 5A 00 00 00 11 +[2025-03-05 11:42:03.319]# RECV HEX> +68 10 D5 2A 00 00 00 11 + +*/ + +typedef struct{ + int uart; + uv_poll_t poll; + uv_timer_t timer; + uint8_t *tx_cmd; +}spk_ctrl_t; + +spk_ctrl_t s_spk; + +void spk_timer_cb( uv_timer_t *handle ); + +spk_ctrl_t *spk_uart_open( void ) +{ + struct termios newtio,oldtio; + int fd; + + fd = open( SPK_UART_PATH, O_RDWR | O_NOCTTY | O_NDELAY); + + if( fd < 0 ){ + perror("Failed to open Uart"); + goto error; + } + + if ( tcgetattr( fd, &oldtio) != 0) { + perror("SetupSerial 1"); + goto error; + } + + bzero( &newtio, sizeof( newtio ) ); + newtio.c_cflag |= CLOCAL | CREAD; + newtio.c_cflag &= ~CSIZE; + newtio.c_cflag |= CS8; + newtio.c_cflag &= ~PARENB; + newtio.c_cflag &= ~CSTOPB; + + cfsetispeed(&newtio, B9600); + cfsetospeed(&newtio, B9600); + + newtio.c_cc[VTIME] = 0; + newtio.c_cc[VMIN] = 0; + tcflush( fd, TCIFLUSH ); + if((tcsetattr( fd, TCSANOW,&newtio ) )!=0) + { + perror("com set error"); + return NULL; + } + s_spk.uart = fd; + +// uv_poll_init( uv_default_loop(), &s_spk.poll, s_spk.uart ); +// uv_poll_start( &s_spk.poll, UV_READABLE, spk_data_ready_cb ); + + uv_timer_init( uv_default_loop(), &s_spk.timer ); + uv_timer_start( &s_spk.timer, spk_timer_cb, 500, 0 ); + return (spk_ctrl_t*)&s_spk; +error: + return NULL; +} + +int spk_write( spk_ctrl_t *spk, char *tx_data, int tx_size ) +{ + int result; + + if( spk && spk->uart>=0){ + result = write( spk->uart, tx_data, tx_size); + if( result<0){ + perror("Failed to write to UART device"); + goto error;; + } + return 0; + } + return 0; +error: + return -1; +} +uint8_t cmd_start[8] = { 0x68, 0x10, 0xE1, 0x5A, 0x00, 0x00, 0x00, 0x11 }; + +uint8_t spk_alarm_cmds[6][8] = { +[ALARM_TYPE_0] = { 0x68, 0x10, 0xE1, 0x1F, 0x00, 0x00, 0x00, 0x11 }, //Stop +[ALARM_TYPE_1] = { 0x68, 0x10, 0xD1, 0x2E, 0x00, 0x00, 0x00, 0x11 }, //alarm 1 +[ALARM_TYPE_2] = { 0x68, 0x10, 0xD2, 0x2D, 0x00, 0x00, 0x00, 0x11 }, //alarm 2 +[ALARM_TYPE_3] = { 0x68, 0x10, 0xD3, 0x2C, 0x00, 0x00, 0x00, 0x11 }, //alarm 3 +[ALARM_TYPE_4] = { 0x68, 0x10, 0xD4, 0x2B, 0x00, 0x00, 0x00, 0x11 }, //alarm 4 +[ALARM_TYPE_5] = { 0x68, 0x10, 0xD5, 0x2A, 0x00, 0x00, 0x00, 0x11 } //alarm 5 +}; + +void spk_timer_cb( uv_timer_t *handle ) +{ + + if( s_spk.tx_cmd ){ + //printf( "Write SPK CMD[" ); for( int i=0; i<8; i++ ) printf( "%02X", s_spk.tx_cmd[i] ); printf( "]\n" ); + spk_write( &s_spk, s_spk.tx_cmd, 8 ); + s_spk.tx_cmd= NULL; + } +} + +int spk_alarm( alarm_type_t alarm ) +{ + //printf( "HW SPK_ALARM( %d ) ..............\n\n" ); + if( alarm < ALARM_TYPE_0 || alarm>ALARM_TYPE_5 ) + return -1; + if( s_spk.tx_cmd ) + return -1; + s_spk.tx_cmd = &spk_alarm_cmds[alarm][0]; + + //second cmd 1 + //printf( "Write SPK CMD[" ); for( int i=0; i<8; i++ ) printf( "%02X", cmd_start[i] ); printf( "]\n" ); + spk_write( &s_spk, cmd_start, 8 ); + //wait 0.5s to send cmd 2 + uv_timer_start( &s_spk.timer, spk_timer_cb, 500, 0 ); + +} + +void spk_init( void ) +{ + if( spk_uart_open() ){ + printf( "spk init, open uart ok!\n" ); + }else{ + printf( "spk init, open uart fail!\n" ); + } +} + diff --git a/app/src/tcp.c b/app/src/tcp.c index 8b8445e..7049c16 100644 --- a/app/src/tcp.c +++ b/app/src/tcp.c @@ -1,4 +1,4 @@ -#include "alarm_monitor.h" +#include "alarmmonitor.h" #include "am_tcp.h" /* typedef struct{ -- 2.34.1