/** * Fade animation */ void animation_fade(void){ #define ANIM_FADE_INC 1 static uint8_t led_r = 0xff; static uint8_t led_g = 0x00; static uint8_t led_b = 0x00; // fade leds if( led_r == 0xff ){ if( led_b > 0 ) led_b -= ANIM_FADE_INC; else if( led_g < 0xff ) led_g += ANIM_FADE_INC; } if( led_g == 0xff ){ if( led_r > 0 ) led_r -= ANIM_FADE_INC; else if( led_b < 0xff ) led_b += ANIM_FADE_INC; } if( led_b == 0xff ){ if( led_g > 0 ) led_g -= ANIM_FADE_INC; else if( led_r < 0xff ) led_r += ANIM_FADE_INC; } // set color led_all_on(); led_set_colors(led_r, led_g, led_b, LED_N); led_set_colors(led_r, led_g, led_b, LED_O); led_set_colors(led_r, led_g, led_b, LED_S); led_set_colors(led_r, led_g, led_b, LED_W); }
static void led_rotation_blink_all(void) { static uint16_t count; count++; static const uint16_t count_max = 100; if (count % count_max < count_max / 2) { led_all_off(); } else { led_all_on(); } }
static void led_rotation_rotate_leds_triple(void) { static uint16_t count; count++; led_all_on(); static const uint16_t count_max = 100; if (count % count_max < count_max / 4) { CLEAR_LED(LED_1); } else if (count % count_max < count_max / 2) { CLEAR_LED(LED_2); } else if (count % count_max < 3 * count_max / 4) { CLEAR_LED(LED_3); } else { CLEAR_LED(LED_4); } }
void animation_lady(void){ static uint8_t led_r = ANIMATION_LADY_RED_LTH; static uint8_t led = LED_N; static uint8_t ledInc = TRUE; // set color led_all_on(); led_set_colors(led_r, 0x00, 0xff, led); // calculate new color if( !ledInc ){ led_r--; } else{ led_r++; } if( led_r <= ANIMATION_LADY_RED_LTH || led_r == 0xff ){ if( led == LED_N ){ led = LED_O; } else if( led == LED_O ){ led = LED_S; } else if( led == LED_S ){ led = LED_W; } else{ led = LED_N; if( ledInc ){ ledInc = FALSE; } else { ledInc = TRUE; } } if( ledInc ){ led_r = ANIMATION_LADY_RED_LTH; } else{ led_r = 0xff; } } }
/***************************************************************************** * * ReadCloudCommands * * \param None * * \return None * * \brief Reads the commands from Exosite cloud * *****************************************************************************/ void ReadCloudCommands(void) { char * pbuf = exo_buffer; DisplayLCD(LCD_LINE6, " Exosite "); DisplayLCD(LCD_LINE7, " Read "); if (Exosite_Read("led_ctrl", pbuf, EXO_BUFFER_SIZE)) { DisplayLCD(LCD_LINE8, " OK "); if (!strncmp(pbuf, "0", 1)) led_all_off(); else if (!strncmp(pbuf, "1", 1)) led_all_on(); } else show_status(); MSTimerDelay(500); return; }
/** * Rainbow animation */ void animation_rainbow(void){ static uint8_t led1[3] = {0xff, 0x00, 0x00}; static uint8_t led2[3] = {0x00, 0xff, 0x00}; static uint8_t led3[3] = {0xff, 0xff, 0x00}; static uint8_t led4[3] = {0x00, 0x00, 0xff}; static uint8_t step = 0x00; led_all_on(); switch(step){ case 0: led_set_colors(led1[0], led1[1], led1[2], LED_N); led_set_colors(led2[0], led2[1], led2[2], LED_O); led_set_colors(led3[0], led3[1], led3[2], LED_S); led_set_colors(led4[0], led4[1], led4[2], LED_W); break; case 64: led_set_colors(led1[0], led1[1], led1[2], LED_W); led_set_colors(led2[0], led2[1], led2[2], LED_N); led_set_colors(led3[0], led3[1], led3[2], LED_O); led_set_colors(led4[0], led4[1], led4[2], LED_S); break; case 128: led_set_colors(led1[0], led1[1], led1[2], LED_S); led_set_colors(led2[0], led2[1], led2[2], LED_W); led_set_colors(led3[0], led3[1], led3[2], LED_N); led_set_colors(led4[0], led4[1], led4[2], LED_O); break; case 192: led_set_colors(led1[0], led1[1], led1[2], LED_O); led_set_colors(led2[0], led2[1], led2[2], LED_S); led_set_colors(led3[0], led3[1], led3[2], LED_W); led_set_colors(led4[0], led4[1], led4[2], LED_N); break; } step++; }
int main(void) { HAL_Init(); led_all_init(); SystemClock_Config(); BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_EXTI); while (1) { isPressed = 0; demos[demoID](); demoID = (demoID + 1) % 2; isPressed = 0; led_all_on(); while (!isPressed); led_all_off(); } }
/** * Red pulsed animation */ void animation_red_pulsed(void){ static uint8_t led = 0x00; static uint8_t ledInc = TRUE; // set color led_all_on(); led_set_colors(led, 0, 0, LED_N); led_set_colors(led, 0, 0, LED_O); led_set_colors(led, 0, 0, LED_S); led_set_colors(led, 0, 0, LED_W); // increase/decrease counter if(ledInc) led++; else led--; // set increase/decrease if( led == 255 ) ledInc = FALSE; if( led == 0 ) ledInc = TRUE; }
int led_open(struct inode *node,struct file *myfile) { led_all_on(); return 0; }
void copy_to_udisk(void) { uint32_t i; uint8_t ucErr; led_all_on(); // beeper_on(); // delay_ms(200); // beeper_off(); /* 初始化CH374相关的GPIO和FSMC */ bsp_InitCH374(); CH374_EN; /* 检测CH374芯片ID */ if (ch374_DetectOk()) ; // printf("CH374T Detect Ok\r\n"); else printf("CH374T Detect Failed\r\n"); delay_ms(200); /* 延时200毫秒 */ /* 初始化CH374程序库和CH374芯片,操作成功返回0 */ StopIfError(CH374LibInit()); // Time_Display(RTC_GetCounter()); while (1) { /* 等待插入U盘 */ printf( "\r\n*******************************************\r\n" ); printf( "插入U盘\r\n" ); while (1){ delay_ms(10); /* 没必要频繁查询 */ if (CH374DiskConnect() == ERR_SUCCESS){ break; /* 查询方式: 检查磁盘是否连接并更新磁盘状态,返回成功说明连接 */ } } delay_ms(200); /* 延时,可选操作,有的USB存储器需要几十毫秒的延时 */ /* 检查U盘是否准备好,有些U盘不需要这一步,但是某些U盘必须要执行这一步才能工作 */ for (i = 0; i < 5; i ++ ){ /* 有的U盘总是返回未准备好,不过可以被忽略 */ delay_ms(100); if (CH374DiskReady( ) == ERR_SUCCESS) break; /* 查询磁盘是否准备好 */ } if (CH374DiskStatus <= DISK_CONNECT) continue; /* 支持USB-HUB */ /* 查询磁盘物理容量 */ { uint32_t uiSize = 0; ucErr = ch374_DiskSize(&uiSize); printf( "U盘容量 = %u MB \r\n正在拷贝数据至U盘......\n\r", uiSize >> 11); /* 显示为以MB为单位的容量*/ // led_all_off(); // bsp_LedOn(5); StopIfError(ucErr); } flash_to_udisk(); /* 开始拷贝数据 */ /* 等待U盘拔出 */ printf( "操作完成,拔出U盘\r\n" ); led_all_on(); while ( 1 ) { /* 采用查询方式检查磁盘是否连接并更新磁盘状态,返回成功说明连接 */ /* 支持USB-HUB */ delay_ms(10); /* 没必要频繁查询 */ if (CH374DiskConnect() != ERR_SUCCESS) break; } break; } }
/** * Function to process debugging module * Checks for debbunging commands and things like this */ void debug_process(void){ #ifdef CFG_SUART_RX // check for recieved commands if( suart_rxFlag ){ uint8_t cmd = suart_getc(); #if !defined(CFG_CODE_LEVEL_AVG) uint16_t tmp; #endif // process command switch(cmd){ case 'f': motor_set_speed(MOTOR_ADDR_L, 0x50, MOTOR_FORWARD); motor_set_speed(MOTOR_ADDR_R, 0x40, MOTOR_FORWARD); break; case'b': motor_set_speed(MOTOR_ADDR_L, 0x40, MOTOR_BACKWARD); motor_set_speed(MOTOR_ADDR_R, 0x43, MOTOR_BACKWARD); break; case 'z': motor_set_speed(MOTOR_ADDR_L, 0x00, MOTOR_BRAKE); motor_set_speed(MOTOR_ADDR_R, 0x00, MOTOR_BRAKE); break; #if !defined(CFG_CODE_LEVEL_AVG) case 'g': led_set_allcolors(); led_all_on(); break; case 'h': led_set_nocolors(); led_all_off(); break; #endif case 's': led_on(LED_STATUS); break; case 'r': led_off(LED_STATUS); break; case 'c': motor_set_speed(MOTOR_ADDR_L, MOTOR_SPEED_HALF, MOTOR_FORWARD); motor_set_speed(MOTOR_ADDR_R, MOTOR_SPEED_HALF, MOTOR_BACKWARD); break; case 'a': motor_set_speed(MOTOR_ADDR_L, MOTOR_SPEED_HALF, MOTOR_BACKWARD); motor_set_speed(MOTOR_ADDR_R, MOTOR_SPEED_HALF, MOTOR_FORWARD); break; #if !defined(CFG_CODE_LEVEL_AVG) case 'm': debug_send_c( motor_get_speed(MOTOR_ADDR_L), TRUE ); break; case 'n': debug_send_c( motor_get_speed(MOTOR_ADDR_R), TRUE ); break; case 'o': debug_send_c( motor_get_direction(MOTOR_ADDR_L), TRUE ); break; case 'p': debug_send_c( motor_get_direction(MOTOR_ADDR_R), TRUE ); break; case 'd': debug_send_c( motor_get_fault(MOTOR_ADDR_L), TRUE ); break; case 'e': debug_send_c( motor_get_fault(MOTOR_ADDR_R), TRUE ); break; case 'u': motor_clear_fault( MOTOR_ADDR_L ); break; case 'v': motor_clear_fault( MOTOR_ADDR_R ); break; #endif case 'i': debug_send_c( sys_robotID, TRUE ); break; case 'j': debug_send_msg( SYS_PUBLISHER, TRUE ); break; case 'k': debug_send_msg( SYS_VERSION, TRUE ); break; case 'l': debug_send_msg( SYS_NAME, TRUE ); break; case 'M': debug_send_c( motor_test(), TRUE ); break; #if !defined(CFG_CODE_LEVEL_AVG) case 'T': tmp = monitor_read_temp(MONITOR_ADDR_1); debug_send_c_wait( tmp>>8, FALSE ); debug_send_c_wait( tmp, FALSE ); tmp = monitor_read_temp(MONITOR_ADDR_2); debug_send_c_wait( tmp>>8, FALSE ); debug_send_c_wait( tmp, TRUE ); break; case 'U': tmp = monitor_read_voltage(MONITOR_ADDR_2, MONITOR_U1); debug_send_c_wait( tmp>>8, FALSE ); debug_send_c_wait( tmp, TRUE ); break; case 'V': tmp = monitor_read_voltage(MONITOR_ADDR_2, MONITOR_U4); debug_send_c_wait( tmp>>8, FALSE ); debug_send_c_wait( tmp, TRUE ); break; case 'W': tmp = monitor_read_voltage(MONITOR_ADDR_2, MONITOR_U2); debug_send_c_wait( tmp>>8, FALSE ); debug_send_c_wait( tmp, TRUE ); break; case 'X': tmp = monitor_read_voltage(MONITOR_ADDR_2, MONITOR_U3); debug_send_c_wait( tmp>>8, FALSE ); debug_send_c_wait( tmp, TRUE ); break; case 'I': tmp = monitor_read_current(MONITOR_ADDR_1, MONITOR_I1); debug_send_c_wait( tmp>>8, FALSE ); debug_send_c_wait( tmp, TRUE ); break; case 'J': tmp = monitor_read_current(MONITOR_ADDR_1, MONITOR_I2); debug_send_c_wait( tmp>>8, FALSE ); debug_send_c_wait( tmp, TRUE ); break; case 'A': tmp = monitor_read_voltage(MONITOR_ADDR_1, MONITOR_UVCC); debug_send_c_wait( tmp>>8, FALSE ); debug_send_c_wait( tmp, FALSE ); tmp = monitor_read_voltage(MONITOR_ADDR_2, MONITOR_UVCC); debug_send_c_wait( tmp>>8, FALSE ); debug_send_c_wait( tmp, TRUE ); break; case 'R': animation_set_mode(ANIMATION_MODE_RED_PULSED); break; case 'S': animation_set_mode(ANIMATION_MODE_STROBE); break; case 'F': animation_set_mode(ANIMATION_MODE_FADE); break; case 'L': animation_set_mode(ANIMATION_MODE_LADY); break; case 'N': animation_set_mode(ANIMATION_MODE_NONE); break; #endif case '?': debug_send_help(); break; default: debug_send_c( 0xff, TRUE ); break; } } #endif /* CFG_SUART_RX */ }
void App_WhiskerGW() { char pubTopic[100]; char pubMsg[250]; unsigned char msgType; int counter=0; char cntStr[20]; char commandBuffer[64]; int commandBufferPointer=0; led_all_off(); // Give the unit a little time to start up // (300 ms for GS1011 and 1000 ms for GS1500) MSTimerDelay(1000); NVSettingsLoad(&GNV_Setting); led_on(4); WIFI_init(1); // Show MAC address and Version led_on(5); WIFI_Associate(); led_on(6); DisplayLCD(LCD_LINE8, "Demo starting."); DisplayLCD(LCD_LINE3, (const uint8_t *)WifiMAC); // UART if(spiUartInitialize()!=0) { DisplayLCD(LCD_LINE7, "!! SPIUART_ERROR !!"); while(1) { led_all_on(); MSTimerDelay(250); led_all_off(); MSTimerDelay(250); } } while (1) { // Do we need to connect to the AP? if (!AtLibGs_IsNodeAssociated()) { led_off(6); WIFI_Associate(); led_on(6); } else { if(mqttConnected==0) App_ConnectMqtt(); int charCount = spiUartRxBytesAvailable(); while(charCount>0) { commandBuffer[commandBufferPointer++] = spiUartGetByte(); charCount--; if(charCount==0) commandBufferPointer=0; if(commandBufferPointer>4) { if(strstr(commandBuffer,"RMPU")==commandBuffer) { if(commandBufferPointer>24) { // process response char macStr[9]; char lenStr[5]; memcpy(macStr,&commandBuffer[6],8); macStr[8] = 0; memcpy(lenStr,&commandBuffer[4],2); lenStr[2]=0; int len = (int)strtol(lenStr,0,16); unsigned long mac = strtoul(macStr,NULL,16); WhiskerModule *wm = findModule(mac); if(wm!=0) { if(commandBufferPointer>len+16) { char rssiStr[3]; memcpy(rssiStr,&commandBuffer[commandBufferPointer-3],2); rssiStr[2]=0; int rssi = (int)strtol(rssiStr,0,16); if((rssi & 0x80) == 0x80) rssi-=256; int puMsgPointer=14; mqtt_initJsonMsg(pubMsg); mqtt_addStringValToMsg("Name",wm->Name,pubMsg,0); mqtt_addStringValToMsg("Mac",macStr,pubMsg,1); char rstr[8]; sprintf(rstr,"%d dbm",rssi); mqtt_addStringValToMsg("Rssi",rstr,pubMsg,1); sprintf(pubMsg+strlen(pubMsg),",\"Values\":{"); puMsgPointer=14; int comma=0; while(puMsgPointer < (commandBufferPointer-3)) { char cidStr[3]; memcpy(cidStr,&commandBuffer[puMsgPointer],2); puMsgPointer+=2; cidStr[2]=0; unsigned char cid=(unsigned char)strtol(cidStr,0,16); unsigned char channel = cid & 0x1f; char valStr[9]; long valInt; switch(cid) { case 0x21: //digital input memcpy(valStr,&commandBuffer[puMsgPointer],2); valStr[2]=0; puMsgPointer+=2; valInt = (int)strtol(valStr,0,16); if(valInt) mqtt_addStringValToMsg("DIN1","True",pubMsg,comma); else mqtt_addStringValToMsg("DIN1","False",pubMsg,comma); break; case 0x22: //digital input memcpy(valStr,&commandBuffer[puMsgPointer],2); valStr[2]=0; puMsgPointer+=2; valInt = (int)strtol(valStr,0,16); if(valInt) mqtt_addStringValToMsg("DIN2","True",pubMsg,comma); else mqtt_addStringValToMsg("DIN2","False",pubMsg,comma); break; case 0x43: //battery analog in memcpy(valStr,&commandBuffer[puMsgPointer],4); valStr[4]=0; puMsgPointer+=4; valInt = (int)strtol(valStr,0,16); mqtt_addIntValToMsg("Battery",valInt,pubMsg,comma); break; case 0x44: //battery analog in memcpy(valStr,&commandBuffer[puMsgPointer],4); valStr[4]=0; puMsgPointer+=4; valInt = (int)strtol(valStr,0,16); mqtt_addIntValToMsg("Temperature",valInt,pubMsg,comma); break; case 0x45: //battery analog in memcpy(valStr,&commandBuffer[puMsgPointer],4); valStr[4]=0; puMsgPointer+=4; valInt = (int)strtol(valStr,0,16); mqtt_addIntValToMsg("RH",valInt,pubMsg,comma); break; case 0x5d: //internal temperature memcpy(valStr,&commandBuffer[puMsgPointer],4); valStr[4]=0; puMsgPointer+=4; valInt = (int)strtol(valStr,0,16); mqtt_addIntValToMsg("IntTemp",valInt,pubMsg,comma); break; case 0x57: //air quality memcpy(valStr,&commandBuffer[puMsgPointer],4); valStr[4]=0; puMsgPointer+=4; valInt = (int)strtol(valStr,0,16); mqtt_addIntValToMsg("AirQual",valInt,pubMsg,comma); break; case 0x58: //air quality memcpy(valStr,&commandBuffer[puMsgPointer],4); valStr[4]=0; puMsgPointer+=4; valInt = (int)strtol(valStr,0,16); mqtt_addIntValToMsg("AirQual",valInt,pubMsg,comma); break; case 0x61: // digital counter input memcpy(valStr,&commandBuffer[puMsgPointer],4); valStr[4]=0; puMsgPointer+=4; valInt = (int)strtol(valStr,0,16); mqtt_addIntValToMsg("Count1",valInt,pubMsg,comma); break; case 0x62: // digital counter input memcpy(valStr,&commandBuffer[puMsgPointer],4); valStr[4]=0; puMsgPointer+=4; valInt = (int)strtol(valStr,0,16); mqtt_addIntValToMsg("Count2",valInt,pubMsg,comma); break; } comma=1; } sprintf(pubMsg+strlen(pubMsg),"}"); mqtt_finishJsonMsg(pubMsg); sprintf(pubTopic, "%s/%s", WIO_DOMAIN, macStr); int res=mqtt_publish(&broker, pubTopic, pubMsg,0)<0; if(res<0) mqttConnected=0; led_on(8); //spiUartResetFIFO(); } } } } } } // Send data if RSSIReading(); AtLibGs_WaitForTCPMessage(1000); led_off(7); led_off(8); if(G_receivedCount>0) { AtLibGs_ParseTCPData(G_received,G_receivedCount,&rxm); msgType = MQTTParseMessageType(rxm.message); switch(msgType) { case MQTT_MSG_SUBACK: // todo: display subscription acknowledgement break; case MQTT_MSG_PUBLISH: App_MQTTMsgPublished(); break; case MQTT_MSG_PUBACK: // todo: display publish acknowledgement break; default: break; } } counter++; sprintf(cntStr,"Counter=%d",counter); DisplayLCD(LCD_LINE6,cntStr); if(counter>30) { counter=0; sprintf(pubTopic, "%s/%s", WIO_DOMAIN, WifiMAC); mqtt_initJsonMsg(pubMsg); mqtt_addStringValToMsg("msg","status ok",pubMsg,0); mqtt_finishJsonMsg(pubMsg); int res1=mqtt_publish(&broker, pubTopic, pubMsg,0)<0; if(res1<0) mqttConnected=0; led_on(7); } } // Send data if END } }
/***************** The uNabto application logic ***************** * This is where the user implements his/her own functionality * to the device. When a Nabto message is received, this function * gets called with the message's request id and parameters. * Afterwards a user defined message can be sent back to the * requesting browser. ****************************************************************/ application_event_result application_event(application_request* request, buffer_read_t* read_buffer, buffer_write_t* write_buffer) { switch(request->queryId) { case 1: { /** Get acceleration data */ extern int16_t gAccData[3]; uint16_t acc_x; uint16_t acc_y; uint16_t acc_z; // Get accelerometer data and calculate yaw, pitch and roll with offset Accelerometer_Get(); acc_x = gAccData[0] + 0xFF; acc_y = gAccData[1] + 0xFF; acc_z = gAccData[2] + 0xFF; // Write back data if (!buffer_write_uint16(write_buffer, acc_x)) return AER_REQ_RSP_TOO_LARGE; if (!buffer_write_uint16(write_buffer, acc_y)) return AER_REQ_RSP_TOO_LARGE; if (!buffer_write_uint16(write_buffer, acc_z)) return AER_REQ_RSP_TOO_LARGE; return AER_REQ_RESPONSE_READY; } case 2: { /** Get temperature data */ uint16_t temp; temp = Temperature_Get(); // Write back data if (!buffer_write_uint16(write_buffer, temp)) return AER_REQ_RSP_TOO_LARGE; return AER_REQ_RESPONSE_READY; } case 3: { /** Get light level */ uint16_t light; light = LightSensor_Get(); // Write back data if (!buffer_write_uint16(write_buffer, light)) return AER_REQ_RSP_TOO_LARGE; return AER_REQ_RESPONSE_READY; } case 4: { /** Get potentiometer data */ uint32_t pot; pot = Potentiometer_Get(); // Write back data if (!buffer_write_uint32(write_buffer, pot)) return AER_REQ_RSP_TOO_LARGE; return AER_REQ_RESPONSE_READY; } case 5: { /** Get button status */ uint8_t button1; uint8_t button2; uint8_t button3; button1 = Switch1IsPressed(); button2 = Switch2IsPressed(); button3 = Switch3IsPressed(); // Write back data if (!buffer_write_uint8(write_buffer, button1)) return AER_REQ_RSP_TOO_LARGE; if (!buffer_write_uint8(write_buffer, button2)) return AER_REQ_RSP_TOO_LARGE; if (!buffer_write_uint8(write_buffer, button3)) return AER_REQ_RSP_TOO_LARGE; return AER_REQ_RESPONSE_READY; } case 6: { /** Get sound level */ uint32_t sound; sound = Microphone_Get(); // Write back data if (!buffer_write_uint32(write_buffer, sound)) return AER_REQ_RSP_TOO_LARGE; return AER_REQ_RESPONSE_READY; } case 7: { /** Set LED */ uint16_t led; // Read parameters in request if (!buffer_read_uint16(read_buffer, &led)) return AER_REQ_TOO_SMALL; if (led == 1) { led_all_on(); } else { led_all_off(); } // Write back data if (!buffer_write_uint16(write_buffer, led)) return AER_REQ_RSP_TOO_LARGE; return AER_REQ_RESPONSE_READY; } } return AER_REQ_INV_QUERY_ID; }