Added a GPIO to power on/off for external I2C sensor(s) (with LEDs)

Dependencies:   UniGraphic mbed vt100

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers af_attriburtes.cpp Source File

af_attriburtes.cpp

00001 #include "mbed.h"
00002 #include <ctype.h>
00003 #include "af_attributes.h"
00004 #include "edge_time.h"
00005 #include "edge_sensor.h"
00006 #include "edge_accel.h"
00007 #include "edge_color.h"
00008 #include "edge_temp.h"
00009 #include "edge_pressure.h"
00010 #include "edge_mgr.h"
00011 #include "edge_reset_mgr.h"
00012 // #include "SO1602A.h"
00013 #include <ILI9341.h>
00014 #include "pending.h"
00015 
00016 // extern SO1602A *display ;
00017 extern ILI9341 *display ;
00018 extern pending_class *pending ;
00019 
00020 static const af_attribute_type af_attr[] = {
00021 /*     ID,                     Description,                Type,                Size */
00022     {  ATTR_SENSE_VAL,         "Sensor Value",             ATTRIBUTE_TYPE_UTF8S, 255 },
00023     {  ATTR_ACCEL_PRESENT,     "Accel Present",            ATTRIBUTE_TYPE_BOOLEAN, 1 },
00024     {  ATTR_ACCEL_ENABLE,      "Accel Enable",             ATTRIBUTE_TYPE_BOOLEAN, 1 },
00025     {  ATTR_ACCEL_INTERVAL,    "Accel Interval",           ATTRIBUTE_TYPE_SINT16,  2 },
00026     {  ATTR_ACCEL_VALUE,       "Accel Value",              ATTRIBUTE_TYPE_FIXED_15_16, 4},
00027 /* first color sensor (VEML6040) and LED set */    
00028     {  ATTR_COLOR0_PRESENT,    "Color1 Present",           ATTRIBUTE_TYPE_BOOLEAN, 1 },
00029     {  ATTR_COLOR0_ENABLE,     "Color1 Enable",            ATTRIBUTE_TYPE_BOOLEAN, 1 },
00030     {  ATTR_COLOR0_INTERVAL,   "Color1 Interval",          ATTRIBUTE_TYPE_SINT16,  2 },
00031     {  ATTR_COLOR0_TRIGMODE,   "Color1 Trigger Mode",      ATTRIBUTE_TYPE_SINT8,   1 },
00032     {  ATTR_COLOR0_ITIME,      "Color1 Integration Time",  ATTRIBUTE_TYPE_SINT8,   1 },    
00033     {  ATTR_COLOR0_CALIBRATE,  "Color1 Calibrate",         ATTRIBUTE_TYPE_BOOLEAN, 1 },
00034     {  ATTR_COLOR0_PWM_R,      "Color1 PWM R",             ATTRIBUTE_TYPE_SINT32,  4 },
00035     {  ATTR_COLOR0_PWM_G,      "Color1 PWM G",             ATTRIBUTE_TYPE_SINT32,  4 },
00036     {  ATTR_COLOR0_PWM_B,      "Color1 PWM B",             ATTRIBUTE_TYPE_SINT32,  4 },
00037     {  ATTR_COLOR0_PWM_PERIOD, "Color1 PWM Period",        ATTRIBUTE_TYPE_SINT16,  2 },
00038     {  ATTR_COLOR0_PWM_TARGET, "Color1 PWM Target",        ATTRIBUTE_TYPE_SINT16,  2 },
00039     {  ATTR_COLOR0_R_VALUE,    "Color1 R",                 ATTRIBUTE_TYPE_SINT16,  2 },
00040     {  ATTR_COLOR0_G_VALUE,    "Color1 G",                 ATTRIBUTE_TYPE_SINT16,  2 },
00041     {  ATTR_COLOR0_B_VALUE,    "Color1 B",                 ATTRIBUTE_TYPE_SINT16,  2 },
00042 /* second color sensor (VEML6040) and LED set */ 
00043     {  ATTR_COLOR1_PRESENT,    "Color2 Present",           ATTRIBUTE_TYPE_BOOLEAN, 1 },
00044     {  ATTR_COLOR1_ENABLE,     "Color2 Enable",            ATTRIBUTE_TYPE_BOOLEAN, 1 },
00045     {  ATTR_COLOR1_INTERVAL,   "Color2 Interval",          ATTRIBUTE_TYPE_SINT16,  2 },
00046     {  ATTR_COLOR1_TRIGMODE,   "Color2 Trigger Mode",      ATTRIBUTE_TYPE_SINT8,   1 },
00047     {  ATTR_COLOR1_ITIME,      "Color2 Integration Time",  ATTRIBUTE_TYPE_SINT8,   1 },
00048     {  ATTR_COLOR1_CALIBRATE,  "Color2 Calibrate",         ATTRIBUTE_TYPE_BOOLEAN, 1 },
00049     {  ATTR_COLOR1_PWM_R,      "Color2 PWM R",             ATTRIBUTE_TYPE_SINT32,  4 },
00050     {  ATTR_COLOR1_PWM_G,      "Color2 PWM G",             ATTRIBUTE_TYPE_SINT32,  4 },
00051     {  ATTR_COLOR1_PWM_B,      "Color2 PWM B",             ATTRIBUTE_TYPE_SINT32,  4 },
00052     {  ATTR_COLOR1_PWM_PERIOD, "Color2 PWM Period",        ATTRIBUTE_TYPE_SINT16,  2 },
00053     {  ATTR_COLOR1_PWM_TARGET, "Color2 PWM Target",        ATTRIBUTE_TYPE_SINT16,  2 },
00054     {  ATTR_COLOR1_R_VALUE,    "Color2 R",                 ATTRIBUTE_TYPE_SINT16,  2 },
00055     {  ATTR_COLOR1_G_VALUE,    "Color2 G",                 ATTRIBUTE_TYPE_SINT16,  2 },
00056     {  ATTR_COLOR1_B_VALUE,    "Color2 B",                 ATTRIBUTE_TYPE_SINT16,  2 },
00057 /* first temperature sensor (LM75B) */    
00058     {  ATTR_TEMP0_PRESENT,    "Temp0 Present",             ATTRIBUTE_TYPE_BOOLEAN, 1 },
00059     {  ATTR_TEMP0_ENABLE,     "Temp0 Enable",              ATTRIBUTE_TYPE_BOOLEAN, 1 },
00060     {  ATTR_TEMP0_INTERVAL,   "Temp0 Interval",            ATTRIBUTE_TYPE_SINT16,  2 },
00061     {  ATTR_TEMP0_VALUE,      "Temp0 Value",               ATTRIBUTE_TYPE_FIXED_15_16, 4},
00062 /* second temperature sensor (SMTC502AT/Before) */    
00063     {  ATTR_TEMP1_PRESENT,    "Temp1 Present",             ATTRIBUTE_TYPE_BOOLEAN, 1 },
00064     {  ATTR_TEMP1_ENABLE,     "Temp1 Enable",              ATTRIBUTE_TYPE_BOOLEAN, 1 },
00065     {  ATTR_TEMP1_INTERVAL,   "Temp1 Interval",            ATTRIBUTE_TYPE_SINT16,  2 },
00066     {  ATTR_TEMP1_VALUE,      "Temp1 Value",               ATTRIBUTE_TYPE_FIXED_15_16, 4},
00067 /* third temperature sensor (SMTC502AT/After) */    
00068     {  ATTR_TEMP2_PRESENT,    "Temp2 Present",             ATTRIBUTE_TYPE_BOOLEAN, 1 },
00069     {  ATTR_TEMP2_ENABLE,     "Temp2 Enable",              ATTRIBUTE_TYPE_BOOLEAN, 1 },
00070     {  ATTR_TEMP2_INTERVAL,   "Temp2 Interval",            ATTRIBUTE_TYPE_SINT16,  2 },
00071     {  ATTR_TEMP2_VALUE,      "Temp2 Value",               ATTRIBUTE_TYPE_FIXED_15_16, 4},
00072 /* fouth temperateure sensor (LM75B) */
00073     {  ATTR_TEMP3_PRESENT,    "Temp3 Present",             ATTRIBUTE_TYPE_BOOLEAN, 1 },
00074     {  ATTR_TEMP3_ENABLE,     "Temp3 Enable",              ATTRIBUTE_TYPE_BOOLEAN, 1 },
00075     {  ATTR_TEMP3_INTERVAL,   "Temp3 Interval",            ATTRIBUTE_TYPE_SINT16,  2 },
00076     {  ATTR_TEMP3_VALUE,      "Temp3 Value",               ATTRIBUTE_TYPE_FIXED_15_16, 4}, 
00077 /* Gas Pressure sensor (PSE530) */    
00078     {  ATTR_GAS_PRESENT,      "Gas Pressure Present",      ATTRIBUTE_TYPE_BOOLEAN, 1 },
00079     {  ATTR_GAS_ENABLE,       "Gas Pressure Enable",       ATTRIBUTE_TYPE_BOOLEAN, 1 },
00080     {  ATTR_GAS_INTERVAL,     "Gas Pressure Interval",     ATTRIBUTE_TYPE_SINT16,  2 },
00081     {  ATTR_GAS_VALUE,        "Gas Pressure Value",        ATTRIBUTE_TYPE_FIXED_15_16, 4}, 
00082     {  ATTR_GAS_THR_MODE,     "Gas Press Threshold Mode",  ATTRIBUTE_TYPE_SINT8,   1 },
00083     {  ATTR_GAS_THR_HIGH,     "Gas Press High Thresh",     ATTRIBUTE_TYPE_SINT16,  2 },
00084     {  ATTR_GAS_THR_LOW,      "Gas Press Low Thresh",      ATTRIBUTE_TYPE_SINT16,  2 },
00085 /* Software Reset Request */    
00086     {  ATTR_SOFTWARE_RESET,   "Software Reset",            ATTRIBUTE_TYPE_BOOLEAN, 1 },
00087     {  ATTR_DISPLAY_MODE,     "Display Mode",              ATTRIBUTE_TYPE_SINT8,   1 },
00088     {  ATTR_MCU_RESET_REASON, "MCU Reset Reason",          ATTRIBUTE_TYPE_UTF8S,  64 },
00089                    
00090     { ATTR_LED,               "LED",                       ATTRIBUTE_TYPE_SINT16,  2 },
00091     { ATTR_IO0,               "I/O 0",                     ATTRIBUTE_TYPE_SINT64,  8 },
00092     { ATTR_IO1,               "I/O 1",                     ATTRIBUTE_TYPE_SINT64,  8 },
00093     { ATTR_IO2,               "I/O 2",                     ATTRIBUTE_TYPE_SINT64,  8 },
00094     { ATTR_BUTTON,            "BUTTON",                    ATTRIBUTE_TYPE_BOOLEAN, 2 },
00095     { ATTR_IO3,               "I/O 3",                     ATTRIBUTE_TYPE_SINT64,  8 },
00096     { ATTR_BOOT_LOADER_VER,   "Bootloader Version",        ATTRIBUTE_TYPE_SINT64,  8 },
00097     { ATTR_BLE_STACK_VER,     "BLE Stack Version",         ATTRIBUTE_TYPE_SINT64,  8 },
00098     { ATTR_FW_APP_VER,        "FW Application Version",    ATTRIBUTE_TYPE_SINT64,  8 },
00099     { ATTR_DEVICE_DESC,       "Device Description",        ATTRIBUTE_TYPE_SINT64,  8 },
00100     { ATTR_WIFI_VER,          "Wi-Fi chip",                ATTRIBUTE_TYPE_SINT64,  8 },
00101     { ATTR_OFFLINE_SCHED,     "Offline Schedules enable",  ATTRIBUTE_TYPE_SINT16,  2 }, 
00102     { ATTR_SECURITY_ENABLED,  "Security Enabled",          ATTRIBUTE_TYPE_SINT8,   1 }, /* ? */
00103     { ATTR_UTC_OFFSET,        "UTC offset data",           ATTRIBUTE_TYPE_BYTES,   8 },
00104     { ATTR_CONFIGURES_SSID,   "Configured SSID",           ATTRIBUTE_TYPE_UTF8S,  10 }, /* ? */
00105     { ATTR_WIFI_BARS,         "Wi-Fi Bars",                ATTRIBUTE_TYPE_SINT8,   1 },
00106     { ATTR_WIFI_STDY_STATE,   "Wi-Fi Steady State",        ATTRIBUTE_TYPE_SINT8,   1 },
00107     { ATTR_COMMAND,           "Command",                   ATTRIBUTE_TYPE_BYTES,   8 }, /* ? */   
00108     { ATTR_ASR_STATE,         "ASR State",                 ATTRIBUTE_TYPE_SINT8,   1 },
00109     { ATTR_LOW_BATTERY,       "Low Battery Warning",       ATTRIBUTE_TYPE_SINT8,   1 },
00110     { ATTR_LINKED_TIMESTAMP,  "Linked Timestamp",          ATTRIBUTE_TYPE_SINT32,  4 },
00111     { ATTR_ATTR_ACK,          "Attribute ACK",             ATTRIBUTE_TYPE_SINT16,  8 },
00112     { ATTR_REBOOT_REASON,     "Reboot Reason",             ATTRIBUTE_TYPE_UTF8S, 100 },
00113     { ATTR_BLE_COMMS,         "BLE Comms",                 ATTRIBUTE_TYPE_BYTES,  12 },
00114     { ATTR_MCU_INTERFACE,     "MCU Interface",             ATTRIBUTE_TYPE_SINT8,   1 },
00115     { 0,                      0,                           0,                      0 }
00116 } ;
00117 
00118 int get_af_attr(uint16_t id) 
00119 {
00120     int i ;
00121     for (i = 0 ; af_attr[i].id != 0 ; i++ ) {
00122         if (id == af_attr[i].id) {
00123             break ;
00124         }
00125     }
00126     return (i) ;
00127 }
00128 
00129 void print_af_error(int resultCode) 
00130 {
00131     switch(resultCode) {
00132     case afSUCCESS:
00133         printf("Operation completed successfully\n") ;
00134         break ;
00135     case afERROR_NO_SUCH_ATTRIBUTE:
00136         printf("Request was made for unknown attribute id\n") ;
00137         break ;
00138     case afERROR_BUSY:
00139         printf("Request already in progress, try again\n") ;
00140         break ;
00141     case afERROR_INVALID_COMMAND:
00142         printf("Command could not be parsed\n") ;
00143         break ;
00144     case afERROR_QUEUE_OVERFLOW:
00145         printf("Queue is full\n") ;
00146         break ;
00147     case afERROR_QUEUE_UNDERFLOW:
00148         printf("Queue is empty\n") ;
00149         break ;
00150     case afERROR_INVALID_PARAM:
00151         printf("Bad input parameter\n") ;
00152         break ;
00153     default:
00154         printf("Unknown error code %d\n", resultCode) ;
00155         break ;
00156     }
00157 }
00158 
00159 void af_print_values(
00160     const uint8_t   requestId, 
00161     const uint16_t  attributeId,
00162     const uint16_t  valueLen,
00163     const uint8_t   *value
00164 ) 
00165 {
00166     int i, id ;
00167 
00168     id = get_af_attr(attributeId) ;
00169 
00170     if (af_attr[id].id  != 0) {
00171         printf(af_attr[id].description) ;
00172         printf(" : ") ;
00173         switch(af_attr[id].attribute_type) {
00174         case ATTRIBUTE_TYPE_BOOLEAN:
00175         case ATTRIBUTE_TYPE_SINT8: 
00176             if (valueLen >= 1) {
00177                 printf("%02X\n", value[0]) ;
00178             }
00179             break ;
00180         case ATTRIBUTE_TYPE_SINT16:
00181             if (valueLen >= 2) {
00182                 printf("%02X%02X\n", value[1], value[0]) ;
00183             }
00184             break ; 
00185         case ATTRIBUTE_TYPE_SINT32:
00186             if (valueLen >= 4) {
00187                 printf("%02X%02X%02X%02X\n",
00188                     value[3],value[2],value[1],value[0]) ;
00189             }
00190             break ;
00191         case ATTRIBUTE_TYPE_SINT64:
00192             if (valueLen >= 8) {
00193                 printf("%02X%02X %02X%02X %02X%02X %02X%02X\n",
00194                     value[7], value[6], value[5], value[4],
00195                     value[3], value[2], value[1], value[0]) ;
00196             }
00197             break ;
00198         case ATTRIBUTE_TYPE_UTF8S: 
00199             if (valueLen > 0) {
00200                 for (i = 0 ; i < valueLen ; i++) {
00201                     if (isprint(value[i])) {
00202                         printf("%c", value[i]) ;
00203                     } else if (value[i] == 0) { /* string terminator NULL */
00204                         break ;
00205                     } else {
00206                         printf("\'%02X\'",value[i]) ;
00207                     }
00208                 }
00209                 printf("\n") ;
00210             }
00211             break ;
00212         case ATTRIBUTE_TYPE_BYTES:
00213         default:
00214             if (valueLen > 0) {
00215                 for (i = 0 ; i < valueLen ; i++ ) {
00216                     printf("%02X ", value[i]) ;
00217                 }
00218                 printf("\n") ;
00219             }
00220             break ;
00221         }
00222     } else {
00223         if (valueLen > 0) {
00224             for (i = 0 ; i < valueLen ; i++ ) {
00225                 printf("%02X ", value[i]) ;
00226             }
00227             printf("\n") ;
00228         }
00229     }
00230 //    printf("\n") ;
00231 }
00232 
00233 void assignAttribute(
00234     const uint8_t   requestId, 
00235     const uint16_t  attributeId,
00236     const uint16_t  valueLen,
00237     const uint8_t   *value,
00238     bool fromRequest
00239 ) 
00240 {
00241     switch(attributeId) {
00242     case ATTR_LINKED_TIMESTAMP: /* timestamp */  
00243         set_time(valueLen, value) ; /* 68 us */
00244         if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ;
00245         printf("timestampe = ") ;
00246         print_date_wd(&current_time) ;
00247 //        print_time(&current_time) ;
00248         printf("\n") ;
00249         break ;
00250     case ATTR_SOFTWARE_RESET: /* software reset requested! */
00251         if (value[0]) {
00252             reset_watch_dog() ;
00253             printf("Software Reset Requested!\n") ;
00254             if (display != 0) {
00255                 display->cls() ;
00256                 display->locate(0,0) ;
00257                 display->printf("System Rebooting!") ;
00258             }
00259             reset_watch_dog() ;
00260             if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ;
00261             wait(0.5) ;
00262             reset_watch_dog() ;
00263             reboot_edge() ;
00264         }
00265         break ;
00266     case ATTR_DISPLAY_MODE:
00267         if (display_mode != value[0]) {
00268             display_mode = value[0] ;
00269             if (display) {
00270                 display->BusEnable(true) ;
00271                 display->cls() ;
00272                 display->BusEnable(false) ;
00273             }
00274 /* following function is only for testing color sensor power on/off */
00275 #if 0
00276             if (value[0] == DISPLAY_MODE_OFF) {
00277                 disable_color_sensor() ;
00278             } else {
00279                 enable_color_sensor() ;
00280             }
00281 #endif            
00282         }
00283         reset_watch_dog() ;
00284         switch(value[0]) {
00285         case DISPLAY_MODE_GAS: /* gas pressure monitor only */
00286             break ;
00287         case DISPLAY_MODE_SUMMARY: /* summary */
00288             break ;
00289         case DISPLAY_MODE_CHART: /* chart mode */
00290             if (display) {
00291                 draw_all_chart_frame() ;
00292             }
00293             break ;
00294         case DISPLAY_MODE_OFF: /* display off */
00295         default:
00296             display_mode = DISPLAY_MODE_OFF ;          
00297             break ;
00298         }
00299         if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ;
00300         break ;
00301     case ATTR_ACCEL_ENABLE: /* accel enable */
00302         if (sensor[SENSOR_ID_ACCEL]) {
00303             if (value[0]) {
00304                 sensor[SENSOR_ID_ACCEL]->reset() ;
00305                 sensor[SENSOR_ID_ACCEL]->enable() ;
00306             } else if (sensor[SENSOR_ID_ACCEL]){
00307                 sensor[SENSOR_ID_ACCEL]->disable() ;
00308             }
00309             if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ;
00310         }
00311         break ;
00312     case ATTR_ACCEL_INTERVAL:
00313         if (sensor[SENSOR_ID_ACCEL]) {
00314             sensor[SENSOR_ID_ACCEL]->setInterval((value[1] << 8) | value[0]) ;
00315             if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ;
00316         }
00317         break ;
00318     case ATTR_COLOR0_ENABLE: /* color0 enable */
00319         if (sensor[SENSOR_ID_COLOR1]) {
00320             if (value[0]) {
00321                 sensor[SENSOR_ID_COLOR1]->reset() ;
00322                 sensor[SENSOR_ID_COLOR1]->enable() ;
00323             } else {
00324                 sensor[SENSOR_ID_COLOR1]->disable() ;
00325             }
00326             if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ;
00327         }
00328         break ;
00329     case ATTR_COLOR0_INTERVAL:
00330         if (sensor[SENSOR_ID_COLOR1]) {
00331             sensor[SENSOR_ID_COLOR1]->setInterval((value[1] << 8) | value[0]) ;
00332             if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ;
00333         }
00334         break ;    
00335     case ATTR_COLOR0_TRIGMODE: /* color0 config */
00336         if (sensor[SENSOR_ID_COLOR1]) {
00337             uint8_t config = ((edge_color*)sensor[SENSOR_ID_COLOR1])->getConfig() & 0x70 ;
00338             if (value[0]) {
00339                 config = config | 0x06 ;
00340             } 
00341             ((edge_color*)sensor[SENSOR_ID_COLOR1])->setConfig(config) ;
00342             if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ;
00343         }
00344         break ;
00345     case ATTR_COLOR0_ITIME: /* color0 config */
00346         if (sensor[SENSOR_ID_COLOR1]) {
00347             uint8_t config = ((edge_color*)sensor[SENSOR_ID_COLOR1])->getConfig() & 0x07 ;
00348             config = (value[0] << 4) | config ;
00349             ((edge_color*)sensor[SENSOR_ID_COLOR1])->setConfig(config) ;
00350           if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ;
00351         }
00352         break ;    
00353     case ATTR_COLOR0_PWM_PERIOD: /* color0 pwm period */
00354         if (sensor[SENSOR_ID_COLOR1]) {
00355             ((edge_color*)sensor[SENSOR_ID_COLOR1])->set_pwm_period((value[1] << 8) | value[0]) ;
00356           if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ;
00357         }            
00358         break ;
00359     case ATTR_COLOR0_PWM_TARGET: /* color0 pwm calibration target */
00360         if (sensor[SENSOR_ID_COLOR1]) {
00361             color0_target[0] = (value[1] << 8) | value[0] ;
00362             color0_target[1] = color0_target[0] ;
00363             color0_target[2] = color0_target[1] ;
00364           if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ;
00365         }            
00366         break ;     
00367 #if 1 /* do not handle calibration twice */   
00368     case ATTR_COLOR0_CALIBRATE: /* calibrate color0 */
00369         if (sensor[SENSOR_ID_COLOR1] && value[0] && fromRequest) { /* do calibration */ 
00370             ((edge_color*)sensor[SENSOR_ID_COLOR1])->request_calibration() ;
00371         }
00372         break ;
00373 #endif
00374     case ATTR_COLOR0_PWM_R:
00375         if (sensor[SENSOR_ID_COLOR1]) {
00376             ((edge_color*)sensor[SENSOR_ID_COLOR1])->setPwmR( (value[1] << 8) | value[0] ) ;
00377 //            color0_pwm[0] = (value[1] << 8) | value[0] ;
00378         }
00379         if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ;       
00380         break ;
00381     case ATTR_COLOR0_PWM_G:
00382         if (sensor[SENSOR_ID_COLOR1]) {
00383             ((edge_color*)sensor[SENSOR_ID_COLOR1])->setPwmG( (value[1] << 8) | value[0] ) ;
00384 //            color0_pwm[1] = (value[1] << 8) | value[0] ;
00385         }
00386         if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ;       
00387         break ;
00388     case ATTR_COLOR0_PWM_B:
00389         if (sensor[SENSOR_ID_COLOR1]) {
00390             ((edge_color*)sensor[SENSOR_ID_COLOR1])->setPwmB( (value[1] << 8) | value[0] ) ;
00391 //            color0_pwm[2] = (value[1] << 8) | value[0] ;
00392         }
00393         if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ;       
00394         break ;
00395     case ATTR_COLOR1_ENABLE: /* color1 enable */
00396         if (sensor[SENSOR_ID_COLOR2]) {
00397             if (value[0]) {
00398                 sensor[SENSOR_ID_COLOR2]->reset() ;
00399                 sensor[SENSOR_ID_COLOR2]->enable() ;
00400             } else {
00401                 sensor[SENSOR_ID_COLOR2]->disable() ;
00402             }
00403             if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ;
00404         }
00405         break ;
00406     case ATTR_COLOR1_INTERVAL:
00407         if (sensor[SENSOR_ID_COLOR2]) {
00408             sensor[SENSOR_ID_COLOR2]->setInterval((value[1] << 8) | value[0]) ;
00409             if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ;
00410         }
00411         break ;    
00412     case ATTR_COLOR1_TRIGMODE: /* color0 config */
00413         if (sensor[SENSOR_ID_COLOR2]) {
00414             uint8_t config = ((edge_color*)sensor[SENSOR_ID_COLOR2])->getConfig() & 0x70 ;
00415             if (value[0]) {
00416                 config = config | 0x06 ;
00417             } 
00418             ((edge_color*)sensor[SENSOR_ID_COLOR2])->setConfig(config) ;
00419             if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ;
00420         }
00421         break ;
00422     case ATTR_COLOR1_ITIME: /* color0 config */
00423         if (sensor[SENSOR_ID_COLOR2]) {
00424             uint8_t config = ((edge_color*)sensor[SENSOR_ID_COLOR2])->getConfig() & 0x07 ;
00425             config = (value[0] << 4) | config ;
00426             ((edge_color*)sensor[SENSOR_ID_COLOR2])->setConfig(config) ;
00427             if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ;
00428         }
00429         break ;  
00430     case ATTR_COLOR1_PWM_PERIOD: /* color0 pwm period */
00431         if (sensor[SENSOR_ID_COLOR2]) {
00432             ((edge_color*)sensor[SENSOR_ID_COLOR2])->set_pwm_period((value[1] << 8) | value[0]) ;
00433             if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ;
00434         }            
00435         break ;
00436     case ATTR_COLOR1_PWM_TARGET: /* color0 pwm calibration target */
00437         if (sensor[SENSOR_ID_COLOR2]) {
00438             color1_target[0] = (value[1] << 8) | value[0] ;
00439             color1_target[1] = color1_target[0] ;
00440             color1_target[2] = color1_target[1] ;
00441             if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ;
00442         }            
00443         break ;   
00444 #if 1 /* do not handle calibration twice */
00445     case ATTR_COLOR1_CALIBRATE: /* calibrate color1 */
00446         if (sensor[SENSOR_ID_COLOR2] && value[0] && fromRequest) { /* do calibration! */
00447             ((edge_color*)sensor[SENSOR_ID_COLOR2])->request_calibration() ;
00448         }
00449         break ;
00450 #endif
00451     case ATTR_COLOR1_PWM_R:
00452         if (sensor[SENSOR_ID_COLOR2]) {
00453             ((edge_color*)sensor[SENSOR_ID_COLOR2])->setPwmR( (value[1] << 8) | value[0] ) ;
00454 //            color1_pwm[0] = (value[1] << 8) | value[0] ;
00455         }
00456         if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ;       
00457         break ;
00458     case ATTR_COLOR1_PWM_G:
00459         if (sensor[SENSOR_ID_COLOR2]) {
00460             ((edge_color*)sensor[SENSOR_ID_COLOR2])->setPwmG( (value[1] << 8) | value[0] ) ;
00461 //            color1_pwm[1] = (value[1] << 8) | value[0] ;
00462         }
00463         if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ;       
00464         break ;
00465     case ATTR_COLOR1_PWM_B:
00466         if (sensor[SENSOR_ID_COLOR2]) {
00467             ((edge_color*)sensor[SENSOR_ID_COLOR2])->setPwmB( (value[1] << 8) | value[0] ) ;
00468 //            color1_pwm[2] = (value[1] << 8) | value[0] ;
00469         }
00470         if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ;       
00471         break ;
00472     case ATTR_TEMP0_ENABLE: /* temp0 is used to control temp-sensors */
00473         if (sensor[SENSOR_ID_TEMP]) {
00474             if (value[0]) {
00475                 sensor[SENSOR_ID_TEMP]->reset() ;
00476                 sensor[SENSOR_ID_TEMP]->enable() ;
00477             } else {
00478                 sensor[SENSOR_ID_TEMP]->disable() ;
00479             }
00480             if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ;
00481         } 
00482         break ;
00483     case ATTR_TEMP0_INTERVAL:
00484         if (sensor[SENSOR_ID_TEMP]) {
00485             sensor[SENSOR_ID_TEMP]->setInterval((value[1] << 8) | value[0]) ;
00486             if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ;
00487         }
00488         break ; 
00489     case ATTR_TEMP3_ENABLE: /* temp3 enable */
00490         break ;
00491     case ATTR_GAS_ENABLE: /* pressure enable */
00492         if (sensor[SENSOR_ID_PRESS]) {
00493             if (value[0]) {
00494                 sensor[SENSOR_ID_PRESS]->reset() ;
00495                 sensor[SENSOR_ID_PRESS]->enable() ;
00496             } else {
00497                 sensor[SENSOR_ID_PRESS]->disable() ;
00498             }
00499             if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ;
00500         }   
00501         break ;
00502     case ATTR_GAS_INTERVAL:
00503         if (sensor[SENSOR_ID_PRESS]) {
00504             sensor[SENSOR_ID_PRESS]->setInterval((value[1] << 8) | value[0]) ;
00505             if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ;
00506         }
00507         break ; 
00508     case ATTR_GAS_THR_MODE:
00509         if (sensor[SENSOR_ID_PRESS]) {
00510             ((edge_pressure*)sensor[SENSOR_ID_PRESS])->set_thr_mode(value[0]) ;
00511             if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ;
00512         }
00513         break ;
00514     case ATTR_GAS_THR_HIGH:
00515         if (sensor[SENSOR_ID_PRESS]) {
00516             ((edge_pressure*)sensor[SENSOR_ID_PRESS])->set_thr_high((value[1] << 8) | value[0]) ;
00517             if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ;
00518         }
00519         break ;
00520     case ATTR_GAS_THR_LOW:
00521         if (sensor[SENSOR_ID_PRESS]) {
00522             ((edge_pressure*)sensor[SENSOR_ID_PRESS])->set_thr_low((value[1] << 8) | value[0]) ;
00523             if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ;
00524         }
00525         break ;               
00526     default:
00527         break ;    
00528     }
00529 }
00530 
00531 /*
00532  * Callback that allows ASR to request an MCU attribute be changed. 
00533  * You should define this function in your MCU firmware to perform application-specific actions 
00534  * your code must take (e.g., updating the state of the hardware), 
00535  * in light of the attribute value change.
00536 */
00537 void attributeChangeRequest(
00538     const uint8_t   requestId, 
00539     const uint16_t  attributeId,
00540     const uint16_t  valueLen,
00541     const uint8_t   *value
00542 ) 
00543 { 
00544     uint32_t timestamp = edge_time ;
00545     if ((pending != 0)&&(pending->request->requestId == requestId)) {
00546         pending->replied_time = timestamp ;
00547 
00548     }
00549     ts2time(timestamp, &current_time) ; /* 12 us */
00550     if (verbos) {
00551         print_time(&current_time) ;
00552         printf(" %5d ASR requested [%d] : ", attributeId, requestId) ;
00553         af_print_values(requestId, attributeId, valueLen, value) ;
00554     }
00555 
00556     assignAttribute(requestId, attributeId, valueLen, value, true) ;
00557 
00558 //    af_print_values(requestId, attributeId, valueLen, value) ;
00559     if ((pending != 0)&&(pending->request->requestId == requestId)) {
00560         printf("Request [%d] replied in %d sec!\n", requestId, pending->replied_time - pending->submit_time) ;
00561         delete pending ;
00562         pending = 0 ;
00563     }
00564 }
00565 
00566 /*
00567  * Application callback that allows afLib to notify that an attribute has changed. 
00568  * This method will be called in response to a getAttribute call from the application 
00569  * and whenever a ASR module attribute changes.
00570  */
00571 void attributeUpdatedReport(
00572     const uint8_t   requestId,
00573     const uint16_t  attributeId,
00574     const uint16_t  valueLen,
00575     const uint8_t   *value
00576 ) 
00577 {
00578     uint32_t timestamp = edge_time ;
00579     int result ;
00580     if ((pending != 0)&&(pending->request->requestId == requestId)) {
00581         pending->replied_time = timestamp ;
00582     }   
00583     ts2time(timestamp, &current_time) ; /* 12us */
00584     if (verbos) {
00585         print_time(&current_time) ;
00586         printf(" %5d ASR reported [%d]: ", attributeId, requestId) ;
00587         af_print_values(requestId, attributeId, valueLen, value) ;
00588     }
00589 
00590     switch(attributeId) {
00591     case ATTR_LINKED_TIMESTAMP:
00592         set_time(valueLen, value) ; /* 68 us */
00593         printf("timestampe = ") ;
00594         print_date_wd(&current_time) ;
00595 //        print_time(&current_time) ;
00596         printf("\n") ;
00597         break ;         
00598     case ATTR_WIFI_STDY_STATE:
00599         gConnected = false ;
00600         printf("WiFi Steady State: ") ;
00601         switch(value[0]) {
00602         case 0: printf("Not Connected\n")      ; break ;
00603         case 1: printf("Pending\n") ;            break ;
00604         case 2: 
00605             printf("Connected\n") ;       
00606             gConnected = true ; // the only case Connected state is OK   
00607             break ;
00608         case 3: printf("Unknown Failure\n") ;    break ;
00609         case 4: printf("Association Failed\n") ; break ;
00610         case 5: printf("Handshake Failed\n") ;   break ;
00611         case 6: printf("Echo Failed\n") ;        break ;
00612         case 7: printf("SSID Not Found\n") ;     break ;
00613         case 8: printf("NTP Failed\n") ;         break ;
00614         default: printf("Unknown [%d]\n", value[0]) ; break ;
00615         }
00616         break ;
00617     case ATTR_REBOOT_REASON:
00618         printf("Reboot Reason: ") ;
00619         switch(value[0]) {
00620         case 1: printf("Reset pin asserted\n") ; break ;
00621         case 2: printf("Watchdog reset\n") ;     break ;
00622         case 4: printf("Software reset\n") ;     break ;
00623         case 8: printf("CPU Lock up\n") ;        break ;
00624         default: printf("Unknown [%d]\n", value[0]) ;     break ;
00625         }
00626         if (reboot_requested) {
00627             printf("Unexpected ASR Reboot. Rebooting MCU.\n") ;
00628             wait_ms(100) ;
00629             reboot_edge() ;
00630         }
00631         break ; 
00632     case ATTR_MCU_INTERFACE:
00633         printf("MCU Interface: ") ;
00634         switch(value[0]) {
00635         case 0:  printf("No MCU\n") ;    break ;
00636         case 1:  printf("SPI Slave\n") ; break ;
00637         case 2:  printf("UART\n") ;      break ;
00638         default: printf("Unknown\n") ;   break ; 
00639         }
00640         break ;
00641     case AF_SYSTEM_ASR_STATE:
00642         printf("ASR state: ") ;
00643         switch(value[0]) {
00644         case MODULE_STATE_REBOOTED:
00645             gLinked = false ;
00646             printf("Rebooted\n") ;
00647 //            wait_ms(100) ; /* */
00648             if (edge_mgr_status == EDGE_MGR_RUNNING) {
00649                 result = afero->getAttribute(ATTR_REBOOT_REASON) ;
00650                 reboot_requested = true ;
00651 //                reboot_edge() ;
00652             }
00653             break ;
00654         case MODULE_STATE_LINKED:
00655             if (gLinked == false) { /* new link established */
00656                 result = afero->getAttribute(ATTR_LINKED_TIMESTAMP) ;
00657                 if (result != afSUCCESS) {
00658                     printf("getAttriute for ATTR_LINKED_TIMESTAMP failed\n") ;
00659                 }
00660             }
00661             gLinked = true ;
00662             printf("Linked\n") ;
00663             break ;
00664         case MODULE_STATE_UPDATING:
00665             gLinked = true ; 
00666             printf("Updating\n") ;
00667             if (display) {
00668                 display->cls() ;
00669                 display->locate(5, 5) ;
00670                 display->printf("FW Updating...") ;
00671             }
00672             break ;
00673         case MOUDLE_STATE_UPDATE_READY:
00674             gLinked = false ;
00675             printf("Update ready - rebooting\n") ;
00676             if (display) {
00677                 display->cls() ;
00678                 display->locate(5, 5) ;
00679                 display->printf("Rebooting...") ;
00680             }
00681             while(afero->setAttribute32(AF_SYSTEM_COMMAND, MODULE_COMMAND_REBOOT) != afSUCCESS) {
00682                 afero->loop() ;
00683                 wait_us(100) ;
00684             }
00685             reboot_edge() ;
00686             break ;
00687         default:
00688             break ;
00689         }
00690         break ;
00691     default:
00692         assignAttribute(requestId, attributeId, valueLen, value, false) ;
00693         break ;
00694     }      
00695     if ((pending != 0)&&(pending->request->requestId == requestId)) {
00696         printf("Request [%d] replied in %d sec!\n", requestId, pending->replied_time - pending->submit_time) ;
00697         delete pending ;
00698         pending = 0 ;
00699     }      
00700 }