/* user main function, called by AppFramework. */ OSStatus user_main( app_context_t * const app_context ) { user_log_trace(); OSStatus err = kUnknownErr; user_log("User main task start..."); err = user_uartInit(); require_noerr_action( err, exit, user_log("ERROR: user_uartInit err = %d.", err) ); user_log("start photo..."); unsigned char* image="hello world"; user_uartSend( image,strlen(image)); char aa[200]; memset(aa, '\0', 200); mico_thread_sleep(5); int len=user_uartRecv((unsigned char *)aa, 200); user_log("uart_data_recv: [%d][%.*s]", len, len,(unsigned char*)aa); user_log("end..."); #if (MICO_CLOUD_TYPE != CLOUD_DISABLED) /* start fogcloud msg handle task */ err = start_fog_msg_handler(app_context); require_noerr_action( err, exit, user_log("ERROR: start_fog_msg_handler err = %d.", err) ); /* start properties notify task(upload data) */ err = mico_start_properties_notify(app_context, service_table, MICO_PROPERTIES_NOTIFY_INTERVAL_MS, STACK_SIZE_NOTIFY_THREAD); require_noerr_action( err, exit, user_log("ERROR: mico_start_properties_notify err = %d.", err) ); #endif /* main loop for user display */ while(1){ // check every 1 seconds mico_thread_sleep(1); // system work state show on OLED system_state_display(app_context, &g_user_context); } exit: user_log("ERROR: user_main exit with err=%d", err); return err; }
OSStatus ControllerBusSend(ECBusCmd cmd, unsigned char *inData, unsigned int inDataLen) { unsigned char* inBuf; unsigned int bufLen = sizeof(SCBusHeader) + inDataLen; inBuf = malloc(bufLen); if(inBuf == NULL) { user_log("[ERR]ControllerBusSend: memory failed"); return kGeneralErr; } SCBusHeader* header = (SCBusHeader*)inBuf; header->magic = 0x5a; header->cmd = cmd; header->datalen = inDataLen; header->tail = 0xaa; uint8_t data_checksum = 0; data_checksum += header->magic; data_checksum += header->cmd; data_checksum += header->datalen >> 8; data_checksum += header->datalen & 0x00ff; data_checksum += header->tail; for(uint16_t idx = 0; idx < inDataLen; idx++) { data_checksum += *(inData + idx); } header->checksum = data_checksum; if(inData != NULL) { unsigned char* payload = inBuf + sizeof(SCBusHeader); memcpy(payload, inData, inDataLen); } if(kNoErr != user_uartSend(inBuf, bufLen)) { user_log("[ERR]ControllerBusSend: send failed"); return kGeneralErr; } user_log("[DBG]ControllerBusSend: send with following data"); print_serial_data(inBuf, bufLen); if(inBuf != NULL) free(inBuf); return kNoErr; }
// set function: send data to uart && OLED int uart_data_send(struct mico_prop_t *prop, void *arg, void *val, uint32_t val_len) { int ret = 0; OSStatus err = kUnknownErr; uint32_t send_len = 0; char string_display_on_oled[MAX_USER_UART_BUF_SIZE+3] = {'\0'}; // add CR/LF && '\0' to the end for display on uart if((NULL == val) || (NULL == val_len) || (NULL == prop) || (NULL == arg)){ return -1; } user_context_t *uct = (user_context_t*)arg; // display string on OLED (max_len=32bytes) send_len = ((val_len > prop->maxStringLen) ? prop->maxStringLen : val_len); properties_user_log("Show cloud msg on OLED: [%d][%*.s]", send_len, send_len, (char*)val); OLED_ShowString(OLED_DISPLAY_COLUMN_START, OLED_DISPLAY_ROW_2, (uint8_t*)"From Cloud: "); memset(string_display_on_oled, ' ', MAX_USER_UART_BUF_SIZE); // clean display char memcpy(string_display_on_oled, (uint8_t*)val, send_len); string_display_on_oled[MAX_USER_UART_BUF_SIZE] = '\0'; // display end OLED_ShowString(OLED_DISPLAY_COLUMN_START, OLED_DISPLAY_ROW_3, (uint8_t*)string_display_on_oled); ret = 0; // set always ok uct->status.oled_keep_s = 3; // display 3s // send data to uart, add CR/LF to the end for display. string_display_on_oled[send_len] = '\r'; string_display_on_oled[send_len+1] = '\n'; // add CR LF for display on uart string_display_on_oled[send_len+2] = '\0'; // string end err = user_uartSend((unsigned char*)string_display_on_oled, send_len+2); if(kNoErr == err){ properties_user_log("uart_data_send: [%d][%s]", send_len+2, (char*)string_display_on_oled); ret = 0; // set ok } else{ properties_user_log("ERROR: uart_data_send, err=%d.", err); ret = -1; // set failed } return ret; }
/* user main function, called by AppFramework after system init done && wifi * station on in user_main thread. */ OSStatus user_main( app_context_t * const app_context ) { user_log_trace(); OSStatus err = kUnknownErr; unsigned char rdata[64]; unsigned char sdata[64]; uint16_t datalen; require(app_context, exit); // platform initialize AaSysComInit(); AaSysLogInit(); // application initialize ControllerBusInit(); OuterTriggerInit(NULL); TemperatureInit(); // will be support in release 2 BatteryInit(); // reset f411 and wait until it startup ResetF411(); #if 1 MOInit(); #endif err = SntpInit(app_context); if(kNoErr != err) { AaSysLogPrint(LOGLEVEL_ERR, "SntpInit finished with err code %d", err); } else { AaSysLogPrint(LOGLEVEL_INF, "SntpInit success"); } #if 1 DeviceInit(app_context); HealthInit(app_context); LightsInit(app_context); MusicInit(app_context); // start the downstream thread to handle user command err = mico_rtos_create_thread(&user_downstrem_thread_handle, MICO_APPLICATION_PRIORITY, "user_downstream", user_downstream_thread, STACK_SIZE_USER_DOWNSTREAM_THREAD, app_context ); require_noerr_action( err, exit, user_log("ERROR: create user_downstream thread failed!") ); #endif user_log("[DBG]net_main: Appilcation Initialize success @"SOFTWAREVERSION); // user_main loop, update oled display every 1s while(1){ #if 1 mico_thread_sleep(MICO_WAIT_FOREVER); #else mico_thread_sleep(5); datalen = user_uartRecv(rdata, 5); if(datalen) { user_log("[DBG]user_main: Usart recevice datalen %d", datalen); user_log("[DBG]user_main: receive %.*s", datalen, rdata); } else { user_log("[DBG]user_main: Usart didn't recevice data"); } mico_thread_sleep(2); sprintf(sdata, "hello, world!\r\n"); user_uartSend(sdata, strlen(sdata)); #endif } exit: if(kNoErr != err){ user_log("[ERR]user_main: user_main thread exit with err=%d", err); } mico_rtos_delete_thread(NULL); // delete current thread return err; }