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.
XBee.cpp
00001 #include "XBee.h" 00002 00003 #include <stdint.h> 00004 #include "mbed.h" 00005 00006 namespace XBEE 00007 { 00008 Ticker xbee_timer; 00009 Serial xbee_uart(XBEE_TX, XBEE_RX); 00010 DigitalOut XBee_LED(LED1); 00011 00012 void uartUpdate(void); 00013 void lostCheck(void); 00014 00015 namespace 00016 { 00017 ControllerData ctrData; 00018 ControllerData keepCtrData; 00019 const uint8_t defaultData[4] = CTR_DEFAULT_DATA; 00020 const char check[] = "DT="; 00021 volatile char packet[24]; 00022 00023 bool controllerLost = false; 00024 uint8_t timerCount = 0; 00025 } 00026 00027 void Controller::Initialize(void) { 00028 xbee_timer.attach(lostCheck, 0.025); 00029 xbee_uart.baud(4800); 00030 xbee_uart.attach(uartUpdate, Serial::RxIrq); 00031 DataReset(); 00032 } 00033 00034 ControllerData* Controller::GetData(void) { 00035 __disable_irq(); 00036 for(uint8_t i = 0; i < CTR_DATA_LENGTH; i++) keepCtrData.buf[i] = ctrData.buf[i]; 00037 __enable_irq(); 00038 return &keepCtrData; 00039 } 00040 00041 void Controller::DataReset(void) { 00042 __disable_irq(); 00043 for(uint8_t i = 0; i < CTR_DATA_LENGTH; i++) ctrData.buf[i] = defaultData[i]; 00044 __enable_irq(); 00045 } 00046 00047 bool Controller::CheckControllerLost(void) { 00048 return controllerLost; 00049 } 00050 00051 void uartUpdate(void) { 00052 static bool phase = false; 00053 static uint8_t count = 0; 00054 00055 char data = xbee_uart.getc(); 00056 00057 if(phase) 00058 { 00059 packet[count] = data; 00060 if(count < 2) 00061 { 00062 if(data != check[count]) 00063 { 00064 phase = false; 00065 controllerLost = true; 00066 XBee_LED = LED_OFF; 00067 } 00068 } 00069 else if(count == 8) 00070 { 00071 if(data != '\r') 00072 { 00073 phase = false; 00074 controllerLost = true; 00075 XBee_LED = LED_OFF; 00076 } 00077 else 00078 { 00079 ctrData.buf[0] = packet[4]; 00080 ctrData.buf[1] = packet[5]; 00081 ctrData.buf[2] = packet[6]; 00082 ctrData.buf[3] = packet[7]; 00083 phase = false; 00084 timerCount = 0; 00085 controllerLost = false; 00086 XBee_LED = LED_ON; 00087 } 00088 } 00089 count++; 00090 } 00091 else 00092 { 00093 if(data == '@') 00094 { 00095 count = 0; 00096 phase = true; 00097 } 00098 } 00099 } 00100 00101 void lostCheck(void) { 00102 timerCount++; 00103 if(timerCount == 2) XBee_LED = LED_OFF; 00104 if(timerCount >= 20) { 00105 controllerLost = true; 00106 Controller::DataReset(); 00107 timerCount = 0; 00108 XBee_LED = LED_OFF; 00109 } 00110 } 00111 } 00112
Generated on Fri Jul 15 2022 17:22:44 by
1.7.2