Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: UniGraphic mbed vt100
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(¤t_time) ; 00247 // print_time(¤t_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 } 00275 reset_watch_dog() ; 00276 switch(value[0]) { 00277 case DISPLAY_MODE_GAS: /* gas pressure monitor only */ 00278 break ; 00279 case DISPLAY_MODE_SUMMARY: /* summary */ 00280 break ; 00281 case DISPLAY_MODE_CHART: /* chart mode */ 00282 if (display) { 00283 draw_all_chart_frame() ; 00284 } 00285 break ; 00286 case DISPLAY_MODE_OFF: /* display off */ 00287 default: 00288 display_mode = DISPLAY_MODE_OFF ; 00289 break ; 00290 } 00291 if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ; 00292 break ; 00293 case ATTR_ACCEL_ENABLE: /* accel enable */ 00294 if (sensor[0]) { 00295 if (value[0]) { 00296 sensor[0]->reset() ; 00297 sensor[0]->enable() ; 00298 } else if (sensor[0]){ 00299 sensor[0]->disable() ; 00300 } 00301 if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ; 00302 } 00303 break ; 00304 case ATTR_ACCEL_INTERVAL: 00305 if (sensor[0]) { 00306 sensor[0]->setInterval((value[1] << 8) | value[0]) ; 00307 if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ; 00308 } 00309 break ; 00310 case ATTR_COLOR0_ENABLE: /* color0 enable */ 00311 if (sensor[1]) { 00312 if (value[0]) { 00313 sensor[1]->reset() ; 00314 sensor[1]->enable() ; 00315 } else { 00316 sensor[1]->disable() ; 00317 } 00318 if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ; 00319 } 00320 break ; 00321 case ATTR_COLOR0_INTERVAL: 00322 if (sensor[1]) { 00323 sensor[1]->setInterval((value[1] << 8) | value[0]) ; 00324 if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ; 00325 } 00326 break ; 00327 case ATTR_COLOR0_TRIGMODE: /* color0 config */ 00328 if (sensor[1]) { 00329 uint8_t config = ((edge_color*)sensor[1])->getConfig() & 0x70 ; 00330 if (value[0]) { 00331 config = config | 0x06 ; 00332 } 00333 ((edge_color*)sensor[1])->setConfig(config) ; 00334 if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ; 00335 } 00336 break ; 00337 case ATTR_COLOR0_ITIME: /* color0 config */ 00338 if (sensor[1]) { 00339 uint8_t config = ((edge_color*)sensor[1])->getConfig() & 0x07 ; 00340 config = (value[0] << 4) | config ; 00341 ((edge_color*)sensor[1])->setConfig(config) ; 00342 if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ; 00343 } 00344 break ; 00345 case ATTR_COLOR0_PWM_PERIOD: /* color0 pwm period */ 00346 if (sensor[1]) { 00347 ((edge_color*)sensor[1])->set_pwm_period((value[1] << 8) | value[0]) ; 00348 if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ; 00349 } 00350 break ; 00351 case ATTR_COLOR0_PWM_TARGET: /* color0 pwm calibration target */ 00352 if (sensor[1]) { 00353 color0_target[0] = (value[1] << 8) | value[0] ; 00354 color0_target[1] = color0_target[0] ; 00355 color0_target[2] = color0_target[1] ; 00356 if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ; 00357 } 00358 break ; 00359 #if 1 /* do not handle calibration twice */ 00360 case ATTR_COLOR0_CALIBRATE: /* calibrate color0 */ 00361 if (sensor[1] && value[0] && fromRequest) { /* do calibration */ 00362 ((edge_color*)sensor[1])->request_calibration() ; 00363 } 00364 break ; 00365 #endif 00366 case ATTR_COLOR0_PWM_R: 00367 if (sensor[1]) { 00368 ((edge_color*)sensor[1])->setPwmR( (value[1] << 8) | value[0] ) ; 00369 // color0_pwm[0] = (value[1] << 8) | value[0] ; 00370 } 00371 if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ; 00372 break ; 00373 case ATTR_COLOR0_PWM_G: 00374 if (sensor[1]) { 00375 ((edge_color*)sensor[1])->setPwmG( (value[1] << 8) | value[0] ) ; 00376 // color0_pwm[1] = (value[1] << 8) | value[0] ; 00377 } 00378 if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ; 00379 break ; 00380 case ATTR_COLOR0_PWM_B: 00381 if (sensor[1]) { 00382 ((edge_color*)sensor[1])->setPwmB( (value[1] << 8) | value[0] ) ; 00383 // color0_pwm[2] = (value[1] << 8) | value[0] ; 00384 } 00385 if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ; 00386 break ; 00387 case ATTR_COLOR1_ENABLE: /* color1 enable */ 00388 if (sensor[2]) { 00389 if (value[0]) { 00390 sensor[2]->reset() ; 00391 sensor[2]->enable() ; 00392 } else { 00393 sensor[2]->disable() ; 00394 } 00395 if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ; 00396 } 00397 break ; 00398 case ATTR_COLOR1_INTERVAL: 00399 if (sensor[2]) { 00400 sensor[2]->setInterval((value[1] << 8) | value[0]) ; 00401 if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ; 00402 } 00403 break ; 00404 case ATTR_COLOR1_TRIGMODE: /* color0 config */ 00405 if (sensor[2]) { 00406 uint8_t config = ((edge_color*)sensor[2])->getConfig() & 0x70 ; 00407 if (value[0]) { 00408 config = config | 0x06 ; 00409 } 00410 ((edge_color*)sensor[2])->setConfig(config) ; 00411 if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ; 00412 } 00413 break ; 00414 case ATTR_COLOR1_ITIME: /* color0 config */ 00415 if (sensor[2]) { 00416 uint8_t config = ((edge_color*)sensor[2])->getConfig() & 0x07 ; 00417 config = (value[0] << 4) | config ; 00418 ((edge_color*)sensor[2])->setConfig(config) ; 00419 if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ; 00420 } 00421 break ; 00422 case ATTR_COLOR1_PWM_PERIOD: /* color0 pwm period */ 00423 if (sensor[2]) { 00424 ((edge_color*)sensor[2])->set_pwm_period((value[1] << 8) | value[0]) ; 00425 if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ; 00426 } 00427 break ; 00428 case ATTR_COLOR1_PWM_TARGET: /* color0 pwm calibration target */ 00429 if (sensor[2]) { 00430 color1_target[0] = (value[1] << 8) | value[0] ; 00431 color1_target[1] = color1_target[0] ; 00432 color1_target[2] = color1_target[1] ; 00433 if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ; 00434 } 00435 break ; 00436 #if 1 /* do not handle calibration twice */ 00437 case ATTR_COLOR1_CALIBRATE: /* calibrate color1 */ 00438 if (sensor[2] && value[0] && fromRequest) { /* do calibration! */ 00439 ((edge_color*)sensor[2])->request_calibration() ; 00440 } 00441 break ; 00442 #endif 00443 case ATTR_COLOR1_PWM_R: 00444 if (sensor[2]) { 00445 ((edge_color*)sensor[2])->setPwmR( (value[1] << 8) | value[0] ) ; 00446 // color1_pwm[0] = (value[1] << 8) | value[0] ; 00447 } 00448 if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ; 00449 break ; 00450 case ATTR_COLOR1_PWM_G: 00451 if (sensor[2]) { 00452 ((edge_color*)sensor[2])->setPwmG( (value[1] << 8) | value[0] ) ; 00453 // color1_pwm[1] = (value[1] << 8) | value[0] ; 00454 } 00455 if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ; 00456 break ; 00457 case ATTR_COLOR1_PWM_B: 00458 if (sensor[2]) { 00459 ((edge_color*)sensor[2])->setPwmB( (value[1] << 8) | value[0] ) ; 00460 // color1_pwm[2] = (value[1] << 8) | value[0] ; 00461 } 00462 if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ; 00463 break ; 00464 case ATTR_TEMP0_ENABLE: /* temp0 is used to control temp-sensors */ 00465 if (sensor[3]) { 00466 if (value[0]) { 00467 sensor[3]->reset() ; 00468 sensor[3]->enable() ; 00469 } else { 00470 sensor[3]->disable() ; 00471 } 00472 if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ; 00473 } 00474 break ; 00475 case ATTR_TEMP0_INTERVAL: 00476 if (sensor[3]) { 00477 sensor[3]->setInterval((value[1] << 8) | value[0]) ; 00478 if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ; 00479 } 00480 break ; 00481 case ATTR_TEMP3_ENABLE: /* temp3 enable */ 00482 break ; 00483 case ATTR_GAS_ENABLE: /* pressure enable */ 00484 if (sensor[4]) { 00485 if (value[0]) { 00486 sensor[4]->reset() ; 00487 sensor[4]->enable() ; 00488 } else { 00489 sensor[4]->disable() ; 00490 } 00491 if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ; 00492 } 00493 break ; 00494 case ATTR_GAS_INTERVAL: 00495 if (sensor[4]) { 00496 sensor[4]->setInterval((value[1] << 8) | value[0]) ; 00497 if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ; 00498 } 00499 break ; 00500 case ATTR_GAS_THR_MODE: 00501 if (sensor[4]) { 00502 ((edge_pressure*)sensor[4])->set_thr_mode(value[0]) ; 00503 if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ; 00504 } 00505 break ; 00506 case ATTR_GAS_THR_HIGH: 00507 if (sensor[4]) { 00508 ((edge_pressure*)sensor[4])->set_thr_high((value[1] << 8) | value[0]) ; 00509 if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ; 00510 } 00511 break ; 00512 case ATTR_GAS_THR_LOW: 00513 if (sensor[4]) { 00514 ((edge_pressure*)sensor[4])->set_thr_low((value[1] << 8) | value[0]) ; 00515 if (fromRequest) afero->setAttributeComplete(requestId, attributeId, valueLen, value) ; 00516 } 00517 break ; 00518 default: 00519 break ; 00520 } 00521 } 00522 00523 /* 00524 * Callback that allows ASR to request an MCU attribute be changed. 00525 * You should define this function in your MCU firmware to perform application-specific actions 00526 * your code must take (e.g., updating the state of the hardware), 00527 * in light of the attribute value change. 00528 */ 00529 void attributeChangeRequest( 00530 const uint8_t requestId, 00531 const uint16_t attributeId, 00532 const uint16_t valueLen, 00533 const uint8_t *value 00534 ) 00535 { 00536 uint32_t timestamp = edge_time ; 00537 if ((pending != 0)&&(pending->request->requestId == requestId)) { 00538 pending->replied_time = timestamp ; 00539 00540 } 00541 ts2time(timestamp, ¤t_time) ; /* 12 us */ 00542 if (verbos) { 00543 print_time(¤t_time) ; 00544 printf(" %5d ASR requested [%d] : ", attributeId, requestId) ; 00545 af_print_values(requestId, attributeId, valueLen, value) ; 00546 } 00547 00548 assignAttribute(requestId, attributeId, valueLen, value, true) ; 00549 00550 // af_print_values(requestId, attributeId, valueLen, value) ; 00551 if ((pending != 0)&&(pending->request->requestId == requestId)) { 00552 printf("Request [%d] replied in %d sec!\n", requestId, pending->replied_time - pending->submit_time) ; 00553 delete pending ; 00554 pending = 0 ; 00555 } 00556 } 00557 00558 /* 00559 * Application callback that allows afLib to notify that an attribute has changed. 00560 * This method will be called in response to a getAttribute call from the application 00561 * and whenever a ASR module attribute changes. 00562 */ 00563 void attributeUpdatedReport( 00564 const uint8_t requestId, 00565 const uint16_t attributeId, 00566 const uint16_t valueLen, 00567 const uint8_t *value 00568 ) 00569 { 00570 uint32_t timestamp = edge_time ; 00571 int result ; 00572 if ((pending != 0)&&(pending->request->requestId == requestId)) { 00573 pending->replied_time = timestamp ; 00574 } 00575 ts2time(timestamp, ¤t_time) ; /* 12us */ 00576 if (verbos) { 00577 print_time(¤t_time) ; 00578 printf(" %5d ASR reported [%d]: ", attributeId, requestId) ; 00579 af_print_values(requestId, attributeId, valueLen, value) ; 00580 } 00581 00582 switch(attributeId) { 00583 case ATTR_LINKED_TIMESTAMP: 00584 set_time(valueLen, value) ; /* 68 us */ 00585 printf("timestampe = ") ; 00586 print_date_wd(¤t_time) ; 00587 // print_time(¤t_time) ; 00588 printf("\n") ; 00589 break ; 00590 case ATTR_WIFI_STDY_STATE: 00591 gConnected = false ; 00592 printf("WiFi Steady State: ") ; 00593 switch(value[0]) { 00594 case 0: printf("Not Connected\n") ; break ; 00595 case 1: printf("Pending\n") ; break ; 00596 case 2: 00597 printf("Connected\n") ; 00598 gConnected = true ; // the only case Connected state is OK 00599 break ; 00600 case 3: printf("Unknown Failure\n") ; break ; 00601 case 4: printf("Association Failed\n") ; break ; 00602 case 5: printf("Handshake Failed\n") ; break ; 00603 case 6: printf("Echo Failed\n") ; break ; 00604 case 7: printf("SSID Not Found\n") ; break ; 00605 case 8: printf("NTP Failed\n") ; break ; 00606 default: printf("Unknown [%d]\n", value[0]) ; break ; 00607 } 00608 break ; 00609 case ATTR_REBOOT_REASON: 00610 printf("Reboot Reason: ") ; 00611 switch(value[0]) { 00612 case 1: printf("Reset pin asserted\n") ; break ; 00613 case 2: printf("Watchdog reset\n") ; break ; 00614 case 4: printf("Software reset\n") ; break ; 00615 case 8: printf("CPU Lock up\n") ; break ; 00616 default: printf("Unknown [%d]\n", value[0]) ; break ; 00617 } 00618 break ; 00619 case ATTR_MCU_INTERFACE: 00620 printf("MCU Interface: ") ; 00621 switch(value[0]) { 00622 case 0: printf("No MCU\n") ; break ; 00623 case 1: printf("SPI Slave\n") ; break ; 00624 case 2: printf("UART\n") ; break ; 00625 default: printf("Unknown\n") ; break ; 00626 } 00627 break ; 00628 case AF_SYSTEM_ASR_STATE: 00629 printf("ASR state: ") ; 00630 switch(value[0]) { 00631 case MODULE_STATE_REBOOTED: 00632 gLinked = false ; 00633 printf("Rebooted\n") ; 00634 wait_ms(100) ; 00635 if (edge_mgr_status == EDGE_MGR_RUNNING) { 00636 reboot_edge() ; 00637 } 00638 break ; 00639 case MODULE_STATE_LINKED: 00640 if (gLinked == false) { /* new link established */ 00641 result = afero->getAttribute(ATTR_LINKED_TIMESTAMP) ; 00642 if (result != afSUCCESS) { 00643 printf("getAttriute for ATTR_LINKED_TIMESTAMP failed\n") ; 00644 } 00645 } 00646 gLinked = true ; 00647 printf("Linked\n") ; 00648 break ; 00649 case MODULE_STATE_UPDATING: 00650 gLinked = true ; 00651 printf("Updating\n") ; 00652 if (display) { 00653 display->cls() ; 00654 display->locate(5, 5) ; 00655 display->printf("FW Updating...") ; 00656 } 00657 break ; 00658 case MOUDLE_STATE_UPDATE_READY: 00659 gLinked = false ; 00660 printf("Update ready - rebooting\n") ; 00661 if (display) { 00662 display->cls() ; 00663 display->locate(5, 5) ; 00664 display->printf("Rebooting...") ; 00665 } 00666 while(afero->setAttribute32(AF_SYSTEM_COMMAND, MODULE_COMMAND_REBOOT) != afSUCCESS) { 00667 afero->loop() ; 00668 wait_us(100) ; 00669 } 00670 reboot_edge() ; 00671 break ; 00672 default: 00673 break ; 00674 } 00675 break ; 00676 default: 00677 assignAttribute(requestId, attributeId, valueLen, value, false) ; 00678 break ; 00679 } 00680 if ((pending != 0)&&(pending->request->requestId == requestId)) { 00681 printf("Request [%d] replied in %d sec!\n", requestId, pending->replied_time - pending->submit_time) ; 00682 delete pending ; 00683 pending = 0 ; 00684 } 00685 }
Generated on Fri Jul 15 2022 06:01:06 by
1.7.2