![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
This program utilizes the mcr20 Thread Shield on the FRDM-K64F MCU which is a two-part workspace (HVAC Server (RX)/Probe(TX)) to handle low temperature events read at the probe(s) to prevent pipes from freezing.
Dependencies: fsl_phy_mcr20a fsl_smac mbed-rtos mbed
Fork of mcr20_wireless_uart by
Diff: main.cpp
- Revision:
- 29:be24c0c29f17
- Parent:
- 27:1eb29717bfd9
--- a/main.cpp Tue Aug 18 13:04:46 2015 +0000 +++ b/main.cpp Thu Mar 30 20:22:35 2017 +0000 @@ -40,14 +40,19 @@ #define gDefaultBaudRate_UART_c 115200UL +#define BUF_SIZE 3 + Serial uart(USBTX, USBRX); CircularBuffer uartBuf; + #ifdef VERBOSE static bool_t bCCAFailed; static bool_t bACKFailed; #endif uint32_t gTaskEventFlags; +int low_temp_flag = 0; +int heat_state = 0; static uint8_t gau8TxDataBuffer[gMaxSmacSDULength_c + sizeof(rxPacket_t)]; txPacket_t *gAppTxPacket; rxPacket_t *gAppRxPacket; @@ -71,25 +76,59 @@ extern smacErrors_t smacToAppMcpsSap(smacToAppDataMessage_t* pMsg, instanceId_t instance); DigitalOut led1(LED_GREEN); +DigitalOut led2(LED_RED); +DigitalOut led3(LED_BLUE); InterruptIn sw2(SW2); uint32_t button_pressed; +uint8_t timer; +char temp[] = {'1','0','0'}; /*initialize temp to safe value*/ Thread *thread2; Thread *eventsThread; +Thread *timerThread; void uartSetBaudRate(uint32_t b) { uart.baud(b); } +/*ISR for sw2*/ void sw2_press(void) { thread2->signal_set(0x1); + /*Toggle Blue LED*/ + led3 = !led3; + +} + +void timer_thread(void const *argument) +{ + while (true) { + + Thread::wait(1000); + /*Kick heat on (turn on Red LED) if temp drops below + threshold and heat is not already on*/ + if((atoi(temp) <= 37) && (heat_state != 1)) + { + low_temp_flag = 1; + eventsThread->signal_set(0x1); + } + + /*Normal temp, turn off heat*/ + if(atoi(temp) > 37) + { + /*Turn off Red LED*/ + led2 = 1; + + /*Set heat_state to off*/ + heat_state = 0; + } + } } void led_thread(void const *argument) { while (true) { - led1 = !led1; + //led1 = !led1; Thread::wait(200); } } @@ -108,23 +147,33 @@ while (true) { + /*Wait for signal*/ Thread::signal_wait(0x1); + + /*If packet has been sent, reenable recieving mode*/ if(gMcps_Cnf_EVENT_c == (gTaskEventFlags & gMcps_Cnf_EVENT_c)) { - //get back in RX + /*Reenable RX requests*/ MLMERXEnableRequest(gAppRxPacket, 0); - //uart.printf("McpsDataCnf: Packet sent\r\n"); } + /*If a packet has been recieved, rcvd gets data*/ if(gMcps_Ind_EVENT_c == (gTaskEventFlags & gMcps_Ind_EVENT_c)) { - rcvd = gAppRxPacket->smacPdu.smacPdu[0]; - - //get back in RX - //gAppRxPacket = (rxPacket_t*)MEM_BufferAlloc(gMaxSmacSDULength_c + sizeof(rxPacket_t)); - //gAppRxPacket->u8MaxDataLength = gMaxSmacSDULength_c; - uart.printf("%c", rcvd); + + for(int i = 0; i <= gAppRxPacket->u8DataLength; i++) + { + rcvd = gAppRxPacket->smacPdu.smacPdu[i]; + + /*Add received data to temp*/ + temp[i] = rcvd; + + } + + printf("Node Temp: %dF\n\r",atoi(temp)); + + /*Reenable RX requests*/ MLMERXEnableRequest(gAppRxPacket, 0); @@ -145,15 +194,45 @@ uart.printf("CcaCnf: \r\n"); } + /*If there is something on the buffer, load packet and send*/ if(gWUSelf_EVENT_c == (gTaskEventFlags & gWUSelf_EVENT_c)) { - if (buffer_Ok_c == uartBuf.getFromBuffer(&c)) + + /*Set Data Length to number of items on uartBuf*/ + gAppTxPacket->u8DataLength = uartBuf.getCount(); + + /*Load TX packets until uartBuf is empty*/ + for(int i = 0; uartBuf.getCount() > 0; i++) { - gAppTxPacket->smacPdu.smacPdu[0] = c; - gAppTxPacket->u8DataLength = 1; + uartBuf.getFromBuffer(&c); + gAppTxPacket->smacPdu.smacPdu[i] = c; + } + /*Disable RX requests (block incoming packets)*/ (void)MLMERXDisableRequest(); + /*Generate request to send data packet*/ (void)MCPSDataRequest(gAppTxPacket); - } + + printf("Data Sent\n\r"); + + /*Toggle Red LED after transmission*/ + led2 = !led2; + + } + + if(low_temp_flag) + { + printf("WARNING: Node detected temp below safe threshold\n\r"); + printf("Suspect Node temp: %dF\n\r",atoi(temp)); + printf("Turning on heat..\n\r"); + + /*Simulate heat by turning on red LED*/ + led2 = 0; + + /*Lower low_temp_flag*/ + low_temp_flag = 0; + + /*Set head_state to on*/ + heat_state = 1; } gTaskEventFlags = 0; @@ -161,11 +240,16 @@ } int main() -{ +{ + led1 = 1;/*Turn off Green LED*/ + led2 = 1;/*Turn off Red LED*/ + led3 = 1;/*Turn off Blue LED*/ + MEM_Init(); Thread thread(led_thread); thread2 = new Thread(button_thread); eventsThread = new Thread(events_thread); + timerThread = new Thread(timer_thread); Phy_Init(); InitSmac(); @@ -181,15 +265,13 @@ button_pressed = 0; sw2.fall(&sw2_press); while (true) - { - if(uart.readable()) - { - (void)uartBuf.addToBuffer(uart.getc()); - } + { + /*Set signal for eventsThread if anything on uartBuf to be sent*/ if ( uartBuf.getCount() ) { gTaskEventFlags |= gWUSelf_EVENT_c; eventsThread->signal_set(0x1); + } Thread::yield(); }