按键led传感器等模块底层接口修改
authorkyz <kyz@kyz-vbox.myguest.virtualbox.org>
Fri, 16 May 2025 08:44:34 +0000 (16:44 +0800)
committerkyz <kyz@kyz-vbox.myguest.virtualbox.org>
Fri, 16 May 2025 08:44:34 +0000 (16:44 +0800)
app/src/am_main.c
app/src/button.c
app/src/iot.c
app/src/iot_report.c
app/src/lcd.c
app/src/sensor.c

index 93ce64898385aee047a013b6e2adb024da76f3e8..254726e137a55cb2953ccc6db4f6933cf4eeefd6 100644 (file)
@@ -102,6 +102,21 @@ int32_t alarm_stop( am_dev_t *dev  )
     return 0;
 }
 
+void remote_jbkzq_power( int on )
+{
+    g_dev.dev_ctrl.controller_power = on?1:0;
+}
+
+void remote_jbq_power( int on )
+{
+    g_dev.dev_ctrl.speaker_power = on?1:0;
+}
+
+void remote_alarm( int alarm )
+{
+    alarm_start( &g_dev, alarm );
+}
+
 int32_t device_disp_info_update( am_dev_t *dev )
 {
     dev->disp.page = 0; //page_main
@@ -114,12 +129,12 @@ int32_t device_disp_info_update( am_dev_t *dev )
     dev->disp.sensor[4] = 0.0; //A
     dev->disp.sensor[5] = 0.0; //P
     dev->disp.sensor[6] = 0.0; //V
-    dev->disp.sensor[7] = 0.0; //T
-    dev->disp.sensor[8] = 0.0; //H
+    dev->disp.sensor[7] = g_dev.sensor_data.temperature; //T
+    dev->disp.sensor[8] = g_dev.sensor_data.humidity; //H
     dev->disp.sensor[9] = dev->sensor_data.sound_pressure; //noise dB
     dev->disp.sound_type = 0; //dev->sensor_data.sound_alarm; //sound type
-    dev->disp.pwr[0] = 1; //alarm speaker power on
-    dev->disp.pwr[1] = 1; //alarm controller power on
+    dev->disp.pwr[0] = g_dev.dev_ctrl.speaker_power; //alarm speaker power on
+    dev->disp.pwr[1] = g_dev.dev_ctrl.controller_power; //alarm controller power on
     return 0;
 }
 
@@ -127,6 +142,7 @@ int32_t lcd_write_page( int page );
 int32_t lcd_write_alarm( int alarm, int alarm_time );
 int32_t lcd_write_date_time( void );
 int32_t lcd_write_sensor_info( int sensor, float data );
+int32_t lcd_write_power_info( int spk_pwr, int ctrl_pwr );
 int32_t lcd_write_sound_type( int sound );
 int32_t lcd_write_wh_info( wh_info_t *info );
 
@@ -142,7 +158,9 @@ int32_t device_lcd_refresh( am_dev_t *dev )
 
     ///lcd_write_page( dev->disp.page );
     lcd_write_alarm( dev->disp.alarm, dev->disp.alarm_time );
-
+    lcd_write_power_info( dev->disp.pwr[0], dev->disp.pwr[1] );
+    lcd_write_sensor_info( 7, dev->disp.sensor[7] );
+    lcd_write_sensor_info( 8, dev->disp.sensor[8] );
     lcd_write_sensor_info( 9, dev->disp.sensor[9] );
     lcd_write_sound_type( dev->disp.sound_type );
 
@@ -330,7 +348,7 @@ int main( int argc, char ** argv )
     device_button_init( dev );
     device_led_init( dev );
     device_lcd_init( dev );
-    ///device_sensor_init( dev );
+    device_sensor_init( dev );
     ///sensor_board_init( dev );
 
     iot_init( dev );
index 152cddd3b0b9e6ff4bcae1098714ea83ccd3e572..a176273cf9861925eacc3d1cabe629e7a76933f2 100644 (file)
 #define SINGLE_BTN_CB_MAX 50
 #define COMB_BTN_CB_MAX 50
 
+/*
+预警 early_key          KEY_F1
+灾警 disaster_key       KEY_F2
+空袭 airstrike_key      KEY_F3
+解警 jiejing_key        KEY_F4
+解灾 relief_key         KEY_F5
+解锁 unlock_key         KEY_F6
+停止 stop_key           KEY_F7
+*/
+
 typedef struct{
     uint32_t code;
     uint32_t event;
@@ -51,6 +61,15 @@ extern int32_t is_btn_idle( void );
 
 uint32_t button_code( uint32_t key_code )
 {
+    if( key_code == KEY_F1 ) return BUTTON_1;  //yx
+    if( key_code == KEY_F3 ) return BUTTON_2;  //kx
+    if( key_code == KEY_F4 ) return BUTTON_3;  //jc
+    if( key_code == KEY_F2 ) return BUTTON_4;  //zj
+    if( key_code == KEY_F5 ) return BUTTON_5;  //jz
+    if( key_code == KEY_F7 ) return BUTTON_6;  //stop
+    if( key_code == KEY_F6 ) return BUTTON_7;  //unlock
+    //if( key_code == KEY_ ) return BUTTON_8; //powerkey
+
     if( key_code == KEY_1 )      return BUTTON_1;
     else if( key_code == KEY_2 ) return BUTTON_2;
     else if( key_code == KEY_3 ) return BUTTON_3;
@@ -95,13 +114,14 @@ void btn_data_ready_cb( uv_poll_t *handle, int status, int events )
                 code = ev[i].code;
                 value = ev[i].value;
 
-                if( type==EV_KEY && code >= KEY_1 && code <= KEY_8  ){
+                if( (type==EV_KEY && code >= KEY_1 && code <= KEY_8)
+                        ||(type==EV_KEY && code >= KEY_F1 && code <= KEY_F7)  ){
                     btn_scan = 1;
                     if( value == 0 ){
-                        p_btn->pressed_code &= ~( 1<< (code - KEY_1 ) );
+                        p_btn->pressed_code &= ~( button_code(code) );
                         //printf( "btn up...code:%x\n", pressed_code );
                     }else if( value == 1  ){
-                        p_btn->pressed_code |= ( 1<< (code - KEY_1 ) );
+                        p_btn->pressed_code |= ( button_code(code) );
                         //printf( "btn down...code:%x\n", pressed_code );
                     }
 
index 672dbe216598fd78b11adff5e45cd52e67d3cba2..2a178dcd8339d4a3db8d20aa39a614e4457ee2c7 100644 (file)
@@ -131,7 +131,7 @@ void connect_timer_cb( uv_timer_t * handle )
                 if( g_dev.idbm ){
                     p_iot->connected = 2; //tx connect req again
                     //rpt start
-                    uv_timer_start( &s_iot.iot_timer, rpt_timer_cb, 100, 5000 );
+                    uv_timer_start( &s_iot.iot_timer, rpt_timer_cb, 100, 5000*3 );
                 }else{
                     p_iot->connected = 0; //tx connect req again
                     uv_timer_start( &s_iot.iot_timer, connect_timer_cb, 2000, 0 );
index cf2e1bdc27d38ef31507d8948941ad9bda2384c6..6faad4b3ba8d87e61eb10ba4b7c889e8f2122f58 100644 (file)
@@ -324,12 +324,17 @@ cJSON* wrap_wh_event_json( int whbz, char*whid, char *rpt_time )
 {
     cJSON *event;
 
+    char * ts_str;
+    char buf[40];
+
+    ts_str = rpt_time?rpt_time:time_str( buf, time(NULL));
+
     if(( event = cJSON_CreateObject() )){
 
         cJSON_AddStringToObject( event,  "type", "e99" );
         cJSON_AddNumberToObject( event,  "whbz", whbz );
         cJSON_AddStringToObject( event,  "whid", whid );
-        cJSON_AddStringToObject( event,  "time", rpt_time );
+        cJSON_AddStringToObject( event,  "time", ts_str );
     }
 
     return event;
@@ -373,10 +378,14 @@ cJSON* wrap_button_event_json( int button, char * rpt_time )
 //4 读写类型 type — — varchar 255 0 write:写入
 //注: 一条控制报文若同时控制多个操作,则有多个回复报文,一个回复报文对应一次设备操作。
 //
-cJSON* wrap_ctrl_resp_json( char* ccode, char * device, char *status, char*tagcode, int value, char*rpt_time, char* rw_type )
+cJSON* wrap_ctrl_resp_json( char* ccode, char * device, char *status, char*tagcode, char* value, char*rpt_time, char* rw_type )
 {
     cJSON *resp;
     cJSON *devices;
+    char * ts_str;
+    char buf[40];
+
+    ts_str = rpt_time?rpt_time:time_str( buf, time(NULL));
 
     if(( resp = cJSON_CreateObject() )){
         if(( devices = cJSON_AddObjectToObject( resp, "devices" ) )){
@@ -384,10 +393,10 @@ cJSON* wrap_ctrl_resp_json( char* ccode, char * device, char *status, char*tagco
             cJSON_AddStringToObject( devices,  "device", device );
             cJSON_AddStringToObject( devices,  "status", status );
             cJSON_AddStringToObject( devices,  "tagcode", tagcode );
-            cJSON_AddNumberToObject( devices,  "value", value );
+            cJSON_AddStringToObject( devices,  "value", value );
         }
 
-        cJSON_AddStringToObject( resp,  "time", rpt_time );
+        cJSON_AddStringToObject( resp,  "time", ts_str );
         cJSON_AddStringToObject( resp,  "type", rw_type);
     }
 
@@ -438,6 +447,30 @@ uint8_t * iot_wrap_alarm_event_pkt( void  )
     return pkt_data;
 }
 
+/*-------------------------------------------------------------------------------------------
+//button event pkt
+//-------------------------------------------------------------------------------------------*/
+uint8_t * iot_wrap_wh_event_pkt( int start  )
+{
+    uint8_t *pkt_data = NULL;
+    time_t ts;
+    char starttime[32];
+
+    cJSON *payload;
+    cJSON *root;
+
+    ts = time( NULL );
+    time_str( starttime, ts );
+
+    payload = wrap_wh_event_json( start?1:0, g_dev.wh_info.whid, NULL ); 
+    if( payload ){
+        root = rpt_pkt_encrypted( 1, payload );
+        pkt_data = cJSON_Print( root );
+        cJSON_Delete( root );
+    }
+    return pkt_data;
+}
+
 /*-------------------------------------------------------------------------------------------
 //button event pkt
 //-------------------------------------------------------------------------------------------*/
@@ -464,6 +497,10 @@ uint8_t * iot_wrap_button_event_pkt( int button  )
 
 extern void dev_wh_create( char * whid );
 extern void dev_wh_delete( char * whid );
+extern void remote_jbkzq_power( int on );
+extern void remote_jbkzq_power( int on );
+extern void remote_alarm( int alarm );
+
 char* handle_iot_rx_data( char * data, int sz )
 {
     cJSON *root;
@@ -486,6 +523,9 @@ char* handle_iot_rx_data( char * data, int sz )
     char * plaintext = NULL;
     char * ciphertext= NULL;
 
+    cJSON *resp = NULL;
+    char *s_resp = NULL;
+
     root = cJSON_Parse( data );
     printf( " ------------ handle_iot_rx_data, parse json, root:%p\n", root );
     if( !root ){
@@ -544,37 +584,46 @@ char* handle_iot_rx_data( char * data, int sz )
     printf( "device:\n");
     printf( "\tdevice: %s\n",  item_devices_device->valuestring);
     printf( "\ttagcode: %s\n", item_devices_tagcode->valuestring);
-    printf( "\tvalue: %d\n",   item_devices_value->valueint);
+    printf( "\tvalue: %s\n",   item_devices_value->valuestring);
 
     if( item_ccode && item_devices && item_time && item_type && item_devices_device && item_type && strcmp( item_type->valuestring, "write")== 0){
-       if( strcmp(item_devices_device->valuestring, "gddykz" )==0 ){
+        if( strcmp(item_devices_device->valuestring, "gddykz" )==0 ){
             //power ctrl
             if( strcmp( item_devices_tagcode->valuestring, "jbq" )== 0 ){
                 if( strcmp( item_devices_value->valuestring, "0" )== 0 ){
                     //JBQ PowerOff
                     printf( "JBQ PowerOff .....\n" );
+                    remote_jbq_power(0);
                 }else if( strcmp( item_devices_value->valuestring, "1" )== 0 ){
                     //JBQ PowerON
                     printf( "JBQ PowerOn .....\n" );
+                    remote_jbq_power(1);
                 }
             }else if( strcmp( item_devices_tagcode->valuestring, "jbkzq" )== 0 ){
                 if( strcmp( item_devices_value->valuestring, "0" )== 0 ){
                     //JBKZQ PowerOff
                     printf( "JBKZQ PowerOff .....\n" );
+                    remote_jbkzq_power(0);
                 }else if( strcmp( item_devices_value->valuestring, "1" )== 0 ){
                     //JBKZQ PowerON
                     printf( "JBKZQ PowerOn .....\n" );
+                    remote_jbkzq_power(1);
                 }
             }
-       }else if( strcmp(item_devices_device->valuestring, "alarm" )==0 ){
+
+            resp = wrap_ctrl_resp_json( item_ccode->valuestring, item_devices_device->valuestring, "true", item_devices_tagcode->valuestring, item_devices_value->valuestring, NULL, "write" );
+
+        }else if( strcmp(item_devices_device->valuestring, "alarm" )==0 ){
             //remote alarm
             printf( "\n\n---------alarm:%s\n", item_devices_value->valuestring );
-
-       }else if( strcmp(item_devices_device->valuestring, "upgrade" )==0 ){
+            resp = wrap_ctrl_resp_json( item_ccode->valuestring, item_devices_device->valuestring, "true", item_devices_tagcode->valuestring, item_devices_value->valuestring, NULL, "write" );
+            remote_alarm(item_devices_value->valuestring[0]-'0');
+        }else if( strcmp(item_devices_device->valuestring, "upgrade" )==0 ){
             //ota
             printf( "\n\n---------OTA, url:%s\n", item_devices_tagcode->valuestring );
             //TODO
-       }else if( strcmp(item_devices_device->valuestring, "xj" )==0 ){
+            resp = wrap_ctrl_resp_json( item_ccode->valuestring, item_devices_device->valuestring, "true", item_devices_tagcode->valuestring, item_devices_value->valuestring, NULL, "write" );
+        }else if( strcmp(item_devices_device->valuestring, "xj" )==0 ){
             //xj
             printf( "\n\n---------XJ, ID:%s, start:%s\n", item_devices_tagcode->valuestring, item_devices_value->valuestring );
 
@@ -583,20 +632,20 @@ char* handle_iot_rx_data( char * data, int sz )
             }else if( strcmp( item_devices_value->valuestring, "0") == 0 ){
                 dev_wh_delete( item_devices_tagcode->valuestring );
             }
-
-       }else if( strcmp(item_devices_device->valuestring, "lock" )==0 ){
+            resp = wrap_ctrl_resp_json( item_ccode->valuestring, item_devices_device->valuestring, "true", item_devices_tagcode->valuestring, item_devices_value->valuestring, NULL, "write" );
+        }else if( strcmp(item_devices_device->valuestring, "lock" )==0 ){
             // remote lock/unlock
-           printf( "remote %s !\n", strcmp(item_devices_value->valuestring,"1" )==0?"Lock":"Unlock" );
-       }
+            printf( "remote %s !\n", strcmp(item_devices_value->valuestring,"1" )==0?"Lock":"Unlock" );
+            resp = wrap_ctrl_resp_json( item_ccode->valuestring, item_devices_device->valuestring, "true", item_devices_tagcode->valuestring, item_devices_value->valuestring, NULL, "write" );
+        }
     }
 
-    //TODO resp cmd
-    cJSON*resp = wrap_ctrl_resp_json( item_ccode->valuestring, item_devices_device->valuestring, "status", item_devices_tagcode->valuestring, item_devices_value->valueint, "time", "write" );
     //tx resp
-    char * s_resp = cJSON_Print( resp );
-    printf( "resp str:%s\n", s_resp );
-    //free( s_resp );
-    cJSON_Delete( resp );
+    if( resp ){
+        s_resp = cJSON_Print( resp );
+        printf( "resp str:%s\n", s_resp );
+        cJSON_Delete( resp );
+    }
 
     cJSON_Delete( root );
 
index 9fbc5d756602da7e855a827f9a103247222a9985..1622373b7ef2ea92db5576ca94466a71db4b70d2 100644 (file)
@@ -52,8 +52,8 @@ lcd_t lcd_open( void )
     newtio.c_cflag &= ~PARENB;
     newtio.c_cflag &= ~CSTOPB;
 
-    cfsetispeed(&newtio, B9600);
-    cfsetospeed(&newtio, B9600);
+    cfsetispeed(&newtio, B115200);
+    cfsetospeed(&newtio, B115200);
 
     newtio.c_cc[VTIME]  = 0;
     newtio.c_cc[VMIN] = 0;
@@ -135,11 +135,10 @@ int32_t lcd_write_wh_info( wh_info_t *info )
     char cmd[100];
     char buf[50];
    
-            printf( "\n\n\n----lcd_write_wh_info--------- whid:%s, create_time:%s, status:%d\n", info->whid, ctime(&info->create_time), info->status );
     if( info->create_time==0 ){
         lcd_write_str( &s_lcd, "main.b1.picc=0\xFF\xFF\xFF" );
         lcd_write_str( &s_lcd, "wh.b0.picc=4\xFF\xFF\xFF" );
-printf( ">>>>>>  wh b0 picc -> 4\n" );
+
         lcd_write_str( &s_lcd, "wh.t0.txt=\"\"\xFF\xFF\xFF" );
         lcd_write_str( &s_lcd, "wh.t1.txt=\"\"\xFF\xFF\xFF" );
         lcd_write_str( &s_lcd, "wh.t2.txt=\"\"\xFF\xFF\xFF" );
@@ -148,7 +147,7 @@ printf( ">>>>>>  wh b0 picc -> 4\n" );
         if( info->status == 0 ){
             lcd_write_str( &s_lcd, "main.b1.picc=1\xFF\xFF\xFF" );
             lcd_write_str( &s_lcd, "wh.b0.picc=5\xFF\xFF\xFF" );
-printf( ">>>>>> wh b0 picc -> 5\n" );
+
 
             sprintf( cmd, "wh.t0.txt=\"%s\"\xFF\xFF\xFF", info->whid );
             lcd_write_str( &s_lcd, cmd );
@@ -161,7 +160,7 @@ printf( ">>>>>> wh b0 picc -> 5\n" );
         }else if( info->status == 1 ){
             lcd_write_str( &s_lcd, "main.b1.picc=2\xFF\xFF\xFF" );
             lcd_write_str( &s_lcd, "wh.b0.picc=6\xFF\xFF\xFF" );
-printf( ">>>>>>  wh b0 picc -> 6\n" );
+
             sprintf( cmd, "wh.t0.txt=\"%s\"\xFF\xFF\xFF", info->whid );
             lcd_write_str( &s_lcd, cmd );
             sprintf( cmd, "wh.t1.txt=\"%s\"\xFF\xFF\xFF", time_str(buf, info->create_time) );
@@ -173,7 +172,6 @@ printf( ">>>>>>  wh b0 picc -> 6\n" );
         }else if( info->status == 2 ){
             lcd_write_str( &s_lcd, "main.b1.picc=0\xFF\xFF\xFF" );
             lcd_write_str( &s_lcd, "wh.b0.picc=4\xFF\xFF\xFF" );
-printf( ">>>>>>  wh b0 picc -> 4\n" );
 
             sprintf( cmd, "wh.t0.txt=\"%s\"\xFF\xFF\xFF", info->whid );
             lcd_write_str( &s_lcd, cmd );
@@ -200,33 +198,53 @@ void dev_wh_delete( char * whid )
     memset( &g_dev.wh_info, 0, sizeof( g_dev.wh_info) );
     store_wh_info( &g_dev.wh_info );
     lcd_write_wh_info( &g_dev.wh_info );
+    //Todo: rpt resp to srv
 }
 
 void dev_wh_create( char * whid )
 {
-            strcpy( g_dev.wh_info.whid, whid );
-            g_dev.wh_info.status=0;
-            g_dev.wh_info.create_time = time(NULL);
-            g_dev.wh_info.start_time = 0;
-            g_dev.wh_info.end_time = 0;
-            store_wh_info( &g_dev.wh_info );
-            lcd_write_wh_info( &g_dev.wh_info );
+    strcpy( g_dev.wh_info.whid, whid );
+    g_dev.wh_info.status=0;
+    g_dev.wh_info.create_time = time(NULL);
+    g_dev.wh_info.start_time = 0;
+    g_dev.wh_info.end_time = 0;
+    store_wh_info( &g_dev.wh_info );
+    lcd_write_wh_info( &g_dev.wh_info );
+    //Todo: rpt resp to srv
 }
+
+uint8_t * iot_wrap_wh_event_pkt( int start  );
 void dev_wh_stop( void )
 {
-            g_dev.wh_info.status=2;
-            g_dev.wh_info.end_time = time(NULL);
-            store_wh_info( &g_dev.wh_info );
-            lcd_write_wh_info( &g_dev.wh_info );
+    char * pkt_str;
+
+    g_dev.wh_info.status=2;
+    g_dev.wh_info.end_time = time(NULL);
+    store_wh_info( &g_dev.wh_info );
+    lcd_write_wh_info( &g_dev.wh_info );
+    //Todo: wh pkt rpt to srv
+    pkt_str = iot_wrap_wh_event_pkt( 0 );
+        if( pkt_str ){
+            iot_tx_data( pkt_str, strlen( pkt_str ) );
+            free( pkt_str );
+        }
 }
 
 void dev_wh_start( void )
 {
-            g_dev.wh_info.status=1;
-            g_dev.wh_info.start_time = time(NULL);
-            g_dev.wh_info.end_time = 0;
-            store_wh_info( &g_dev.wh_info );
-            lcd_write_wh_info( &g_dev.wh_info );
+    char * pkt_str;
+
+    g_dev.wh_info.status=1;
+    g_dev.wh_info.start_time = time(NULL);
+    g_dev.wh_info.end_time = 0;
+    store_wh_info( &g_dev.wh_info );
+    lcd_write_wh_info( &g_dev.wh_info );
+    //Todo: wh pkt rpt to srv
+    pkt_str = iot_wrap_wh_event_pkt( 1 );
+        if( pkt_str ){
+            iot_tx_data( pkt_str, strlen( pkt_str ) );
+            free( pkt_str );
+        }
 }
 
 void lcd_resp_handle( uint8_t *lcd_recv_data, uint16_t lcd_recv_cnt )
@@ -270,10 +288,10 @@ void lcd_data_ready_cb( uv_poll_t *handle, int status, int events )
         nbytes = read( s_lcd.uart, lcd_recv_data+lcd_recv_cnt, sizeof(lcd_recv_data)-lcd_recv_cnt );
         lcd_recv_cnt += nbytes;
 
-        printf( "lcd uart read data %d Bytes\n", nbytes );
-        printf( "\n[" );
-        for( int i=0; i<lcd_recv_cnt; i++ ) printf( "%02X ", lcd_recv_data[i] );
-        printf( "]\n" );
+        //printf( "lcd uart read data %d Bytes\n", nbytes );
+        //printf( "\n[" );
+        //for( int i=0; i<lcd_recv_cnt; i++ ) printf( "%02X ", lcd_recv_data[i] );
+        //printf( "]\n" );
         lcd_resp_handle( lcd_recv_data, lcd_recv_cnt );
     }
     return ;
@@ -333,6 +351,20 @@ int32_t  lcd_write_alarm( int alarm, int alarm_time )
     return 0;
 }
 
+int32_t lcd_write_power_info( int spk_pwr, int ctrl_pwr )
+{
+    //spk pwr
+    if( spk_pwr )
+        lcd_write_str( &s_lcd, "main.t11.txt=\"开启\"\xFF\xFF\xFF" );
+    else
+        lcd_write_str( &s_lcd, "main.t11.txt=\"关闭\"\xFF\xFF\xFF" );
+
+    if( ctrl_pwr )
+        lcd_write_str( &s_lcd, "main.t21.txt=\"开启\"\xFF\xFF\xFF" );
+    else
+        lcd_write_str( &s_lcd, "main.t21.txt=\"关闭\"\xFF\xFF\xFF" );
+}
+
 int32_t  lcd_write_date_time( void )
 {
     return 0;
@@ -341,7 +373,11 @@ int32_t  lcd_write_date_time( void )
 int32_t  lcd_write_sensor_info( int sensor, float data )
 {
     char cmd[100];
-    if( sensor == 9 ){
+    if( sensor == 7 ){
+        sprintf( cmd, "main.t31.txt=\"%0.1f\"\xFF\xFF\xFF", data ); lcd_write( &s_lcd, cmd, strlen(cmd) );
+    }else if( sensor == 8 ){
+        sprintf( cmd, "main.t32.txt=\"%0.1f\"\xFF\xFF\xFF", data ); lcd_write( &s_lcd, cmd, strlen(cmd) );
+    }else if( sensor == 9 ){
         sprintf( cmd, "main.t33.txt=\"%0.1f\"\xFF\xFF\xFF", data ); lcd_write( &s_lcd, cmd, strlen(cmd) );
     }
     return 0;
index cf304099b23cf05b1c38ee4e6347af5f942e7ab1..98eef984abb243273d358f920241c7ac549fdd4b 100644 (file)
@@ -7,6 +7,8 @@
 #include <uv.h>
 
 #include "alarm_monitor.h"
+#define  IR_DEV_PATH "/dev/infrared"
+#define  TEM_SENSOR_DEV_PATH "/dev/cht11"
 
 static struct{
     uv_timer_t timer;
@@ -19,28 +21,110 @@ static struct{
     float dc_vol;
     float tem;
     float humidity;
+    int   ir;
 }s_sensor;
 
 //TODO:
-float read_temperature( void ){ return 0; }
-float read_humidity( void ){ return 0; }
+
 float read_ac_vol( int ch ){ return 0; }
 float read_ac_curr( int ch ){ return 0; }
 float read_dc_vol( void ){ return 0; }
 float read_dc_curr( void ){ return 0; }
 
+int read_temperature( float *temperature, float*humidity );
+int read_ir( int * ir );
+
+int read_ir( int * ir )
+{
+    int fd;
+    int ret = -1;
+    uint8_t data;
+
+    fd = open( IR_DEV_PATH, O_RDONLY );
+    if( fd < 0 ){
+        printf( "read temperature,  open dev[%s] ret:%d fail!\n", IR_DEV_PATH, fd );
+        return -1;
+    }
+
+    if( read( fd, &data, 1  ) == 1 ){
+        if( ir )
+            *ir = data;
+        ret = 0;
+    }
+    close(fd);
+
+    return ret;
+}
+
+int read_temperature( float *temperature, float*humidity )
+{
+    int fd;
+    uint8_t data[4];
+    float t,h;
+    int8_t s;
+    int8_t p;
+    int ret = -1;
+
+    t = 0.0f;
+    h = 0.0f;
+
+    fd = open( TEM_SENSOR_DEV_PATH, O_RDONLY );
+    if( fd < 0 ){
+        printf( "read temperature,  open dev[%s] ret:%d fail!\n", TEM_SENSOR_DEV_PATH, fd );
+        return -1;
+    }
+
+    if( read( fd, data, 4  ) == 4 ){
+        //8位整数+1位符号位+7位小数位
+        t = (float)data[0];
+        p = (uint8_t)data[1]&0x7f;
+
+        if(p>100 )
+            t += p/1000.0f;
+        else if( p>10 )
+            t += p/100.0f;
+        else 
+            t += p/10.0f;
+
+        if( data[1]&0x80 )
+            t = -t;
+
+        h = (float)data[2]; 
+        p = (uint8_t)data[3];
+        if(p>100 )
+            h += p/1000.0f;
+        else if( p>10 )
+            h += p/100.0f;
+        else 
+            h += p/10.0f;
+
+        if( temperature ) *temperature = t;
+        if( humidity ) *humidity = h; 
+        printf( "TEM:%.2f, %.2f\n", t, h );
+        ret = 0;
+    }
+    close(fd);
+
+    return ret;
+}
 
 void sensor_read_cb( uv_work_t *req, int status )
 {
     if( s_sensor.data_ready ){
+        g_dev.sensor_data.temperature = s_sensor.tem;
+        g_dev.sensor_data.humidity= s_sensor.humidity;
         s_sensor.data_ready = 0;
+        if( g_dev.sensor_data.ir != s_sensor.ir){
+            printf( "IR -> %d\n", s_sensor.ir);
+            g_dev.sensor_data.ir = s_sensor.ir;
+        }
     }
 }
 
 void sensor_read( uv_work_t *req )
 {
-    s_sensor.tem = read_temperature();
-    s_sensor.humidity = read_humidity();
+    read_ir( &s_sensor.ir );
+    read_temperature( & s_sensor.tem, &s_sensor.humidity );
     s_sensor.ac_curr[0] = read_ac_curr( 0 );
     s_sensor.ac_curr[1] = read_ac_curr( 1 );
     s_sensor.ac_vol[0] = read_ac_vol( 0 );
@@ -59,6 +143,6 @@ void sensor_timer_cb( uv_timer_t *handle )
 void device_sensor_init( am_dev_t *dev )
 {
     uv_timer_init( uv_default_loop(), &s_sensor.timer );
-    uv_timer_start( &s_sensor.timer, sensor_timer_cb, 100, 100 );
+    uv_timer_start( &s_sensor.timer, sensor_timer_cb, 100, 1000 );
 }