static void init_system_component() { // timer init_system_tick(); // ble BLE_Init(); //pwm PWM_Init(); PWM_Pin_Init(); //KEY key_io_init(); power_config_init(); }
void main() { Display_Init(); MCU_Init(); #ifdef INIT_BLE BLE_Init(); delay_ms(2000); #else RN_WAKE = 1; wait_response("CMD"); #endif DrawFrame(); InitTimer2(); while(1) { if(data_ready) { //If characteristic is configured as write //received messages come here Display_Message(); reset_buff(); } else { //Test: every 5sec increase baterry level (0 to 100%) //and send value via Bluetooth Low Energy if (tmr_flg) { batt_level++; if(batt_level > 100) { batt_level = 0; } Display_BatteryLevel(); if(RN_CONN) { //send battery level value if BLE connected shorttohex(batt_level, batt_level_txt); ltrim(batt_level_txt); ble2_write_server_characteristic_value_via_UUID("2A19",batt_level_txt); } tmr_flg = 0; } } } }
static void BleUartTask(void *pvParameters) { uint8_t buf[MAX_TX_MSG_SIZE]; uint8_t txBuf[MAX_TX_MSG_SIZE+sizeof("[Tx] ")+sizeof("AT+BLEUARTTX=\n")]; uint8_t res; CLS1_ConstStdIOType *io = CLS1_GetStdio(); int i; bool prevIsEnabled = FALSE; BLE_Init(); /* initialize BLE module, has to be done when interrupts are enabled */ FRTOS1_vTaskDelay(pdMS_TO_TICKS(2000)); /* wait so other tasks have time to write to the Shell */ if (res!=ERR_OK) { CLS1_SendStr("Failed changing LED activity\r\n", io->stdErr); } CLS1_SendStr("******************************************\r\n", io->stdOut); CLS1_SendStr("* Adafruit BLE UART CMD Mode Application *\r\n", io->stdOut); CLS1_SendStr("******************************************\r\n", io->stdOut); for(;;) { if (!prevIsEnabled && isEnabled) { /* enabled now */ prevIsEnabled = TRUE; BLE_Echo(FALSE); /* Disable command echo from Bluefruit */ CLS1_SendStr("Changing LED activity to MODE\r\n", io->stdOut); res = BLE_SendATCommandExpectedResponse("AT+HWMODELED=1\n", buf, sizeof(buf), "OK\r\n"); /* NOTE: "=MODE" failed! */ if (res!=ERR_OK) { CLS1_SendStr("Failed setting LED mode.\r\n", io->stdErr); } CLS1_SendStr("BLE UART enabled.\r\n", io->stdOut); } else if (prevIsEnabled && !isEnabled) { /* disabled now */ prevIsEnabled = FALSE; CLS1_SendStr("BLE UART disabled.\r\n", io->stdOut); } if (isEnabled) { while(isEnabled && !(isConnected=BLE_IsConnected())) { /* wait until connected */ CLS1_SendStr("Waiting for BLE UART connection...\r\n", io->stdOut); for(i=0;i<5 && isEnabled;i++) { FRTOS1_vTaskDelay(pdMS_TO_TICKS(1000)); LED1_Neg(); } } if (isConnected) { CLS1_SendStr("Connected!\r\n", io->stdOut); } while(isEnabled) { /* will break */ isConnected=BLE_IsConnected(); if (!isConnected) { CLS1_SendStr("Disconnected!\r\n", io->stdOut); break; /* get out of loop */ } if (txBuffer[0]!='\0') { /* have something to tx */ /* copy buffer */ taskENTER_CRITICAL(); UTIL1_strcpy(txBuf, sizeof(txBuf), "AT+BLEUARTTX="); UTIL1_strcat(txBuf, sizeof(txBuf), "[Tx] "); UTIL1_strcat(txBuf, sizeof(txBuf), txBuffer); txBuffer[0] = '\0'; taskEXIT_CRITICAL(); /* send tx string */ res = BLE_SendATCommandExpectedResponse(txBuf, buf, sizeof(buf), "OK\r\n"); if (res!=ERR_OK) { CLS1_SendStr("Failed to Tx string\r\n", io->stdErr); } } /* check Rx */ res = BLE_SendATCommandExpectedResponse("AT+BLEUARTRX\n", buf, sizeof(buf), "OK\r\n"); if (res==ERR_OK) { if (UTIL1_strncmp(buf, "OK\r\n", sizeof("OK\r\n")-1)==0) { /* only OK as response: no data */ } else { /* print response */ UTIL1_strCutTail(buf, "OK\r\n"); /* cut off the OK part */ CLS1_SendStr("[Rx] ", io->stdOut); CLS1_SendStr(buf, io->stdOut); } } FRTOS1_vTaskDelay(pdMS_TO_TICKS(50)); LED1_Neg(); } /* while */ } else { FRTOS1_vTaskDelay(pdMS_TO_TICKS(500)); LED1_Neg(); } //FRTOS1_vTaskDelay(pdMS_TO_TICKS(1000)); } }