gjyrjyykkudt

Dependencies:   mbed MCP23017

Committer:
M_souta
Date:
Mon Jan 20 08:46:24 2020 +0000
Revision:
0:db8d4af513c0
jjjjjj

Who changed what in which revision?

UserRevisionLine numberNew contents of line
M_souta 0:db8d4af513c0 1 #include "XBee.h"
M_souta 0:db8d4af513c0 2
M_souta 0:db8d4af513c0 3 #include <stdint.h>
M_souta 0:db8d4af513c0 4 #include "mbed.h"
M_souta 0:db8d4af513c0 5
M_souta 0:db8d4af513c0 6 namespace XBEE
M_souta 0:db8d4af513c0 7 {
M_souta 0:db8d4af513c0 8 Ticker xbee_timer;
M_souta 0:db8d4af513c0 9 Serial xbee_uart(XBEE_TX, XBEE_RX);
M_souta 0:db8d4af513c0 10 DigitalOut XBee_LED(LED1);
M_souta 0:db8d4af513c0 11
M_souta 0:db8d4af513c0 12 void uartUpdate(void);
M_souta 0:db8d4af513c0 13 void lostCheck(void);
M_souta 0:db8d4af513c0 14
M_souta 0:db8d4af513c0 15 namespace
M_souta 0:db8d4af513c0 16 {
M_souta 0:db8d4af513c0 17 ControllerData ctrData;
M_souta 0:db8d4af513c0 18 ControllerData keepCtrData;
M_souta 0:db8d4af513c0 19 const uint8_t defaultData[4] = CTR_DEFAULT_DATA;
M_souta 0:db8d4af513c0 20 const char check[] = "DT=";
M_souta 0:db8d4af513c0 21 volatile char packet[24];
M_souta 0:db8d4af513c0 22
M_souta 0:db8d4af513c0 23 bool controllerLost = false;
M_souta 0:db8d4af513c0 24 uint8_t timerCount = 0;
M_souta 0:db8d4af513c0 25 }
M_souta 0:db8d4af513c0 26
M_souta 0:db8d4af513c0 27 void Controller::Initialize(void) {
M_souta 0:db8d4af513c0 28 xbee_timer.attach(lostCheck, 0.025);
M_souta 0:db8d4af513c0 29 xbee_uart.baud(4800);
M_souta 0:db8d4af513c0 30 xbee_uart.attach(uartUpdate, Serial::RxIrq);
M_souta 0:db8d4af513c0 31 DataReset();
M_souta 0:db8d4af513c0 32 }
M_souta 0:db8d4af513c0 33
M_souta 0:db8d4af513c0 34 ControllerData* Controller::GetData(void) {
M_souta 0:db8d4af513c0 35 __disable_irq();
M_souta 0:db8d4af513c0 36 for(uint8_t i = 0; i < CTR_DATA_LENGTH; i++) keepCtrData.buf[i] = ctrData.buf[i];
M_souta 0:db8d4af513c0 37 __enable_irq();
M_souta 0:db8d4af513c0 38 return &keepCtrData;
M_souta 0:db8d4af513c0 39 }
M_souta 0:db8d4af513c0 40
M_souta 0:db8d4af513c0 41 void Controller::DataReset(void) {
M_souta 0:db8d4af513c0 42 __disable_irq();
M_souta 0:db8d4af513c0 43 for(uint8_t i = 0; i < CTR_DATA_LENGTH; i++) ctrData.buf[i] = defaultData[i];
M_souta 0:db8d4af513c0 44 __enable_irq();
M_souta 0:db8d4af513c0 45 }
M_souta 0:db8d4af513c0 46
M_souta 0:db8d4af513c0 47 bool Controller::CheckControllerLost(void) {
M_souta 0:db8d4af513c0 48 return controllerLost;
M_souta 0:db8d4af513c0 49 }
M_souta 0:db8d4af513c0 50
M_souta 0:db8d4af513c0 51 void uartUpdate(void) {
M_souta 0:db8d4af513c0 52 static bool phase = false;
M_souta 0:db8d4af513c0 53 static uint8_t count = 0;
M_souta 0:db8d4af513c0 54
M_souta 0:db8d4af513c0 55 char data = xbee_uart.getc();
M_souta 0:db8d4af513c0 56
M_souta 0:db8d4af513c0 57 if(phase)
M_souta 0:db8d4af513c0 58 {
M_souta 0:db8d4af513c0 59 packet[count] = data;
M_souta 0:db8d4af513c0 60 if(count < 2)
M_souta 0:db8d4af513c0 61 {
M_souta 0:db8d4af513c0 62 if(data != check[count])
M_souta 0:db8d4af513c0 63 {
M_souta 0:db8d4af513c0 64 phase = false;
M_souta 0:db8d4af513c0 65 controllerLost = true;
M_souta 0:db8d4af513c0 66 XBee_LED = LED_OFF;
M_souta 0:db8d4af513c0 67 }
M_souta 0:db8d4af513c0 68 }
M_souta 0:db8d4af513c0 69 else if(count == 8)
M_souta 0:db8d4af513c0 70 {
M_souta 0:db8d4af513c0 71 if(data != '\r')
M_souta 0:db8d4af513c0 72 {
M_souta 0:db8d4af513c0 73 phase = false;
M_souta 0:db8d4af513c0 74 controllerLost = true;
M_souta 0:db8d4af513c0 75 XBee_LED = LED_OFF;
M_souta 0:db8d4af513c0 76 }
M_souta 0:db8d4af513c0 77 else
M_souta 0:db8d4af513c0 78 {
M_souta 0:db8d4af513c0 79 ctrData.buf[0] = packet[4];
M_souta 0:db8d4af513c0 80 ctrData.buf[1] = packet[5];
M_souta 0:db8d4af513c0 81 ctrData.buf[2] = packet[6];
M_souta 0:db8d4af513c0 82 ctrData.buf[3] = packet[7];
M_souta 0:db8d4af513c0 83 phase = false;
M_souta 0:db8d4af513c0 84 timerCount = 0;
M_souta 0:db8d4af513c0 85 controllerLost = false;
M_souta 0:db8d4af513c0 86 XBee_LED = LED_ON;
M_souta 0:db8d4af513c0 87 }
M_souta 0:db8d4af513c0 88 }
M_souta 0:db8d4af513c0 89 count++;
M_souta 0:db8d4af513c0 90 }
M_souta 0:db8d4af513c0 91 else
M_souta 0:db8d4af513c0 92 {
M_souta 0:db8d4af513c0 93 if(data == '@')
M_souta 0:db8d4af513c0 94 {
M_souta 0:db8d4af513c0 95 count = 0;
M_souta 0:db8d4af513c0 96 phase = true;
M_souta 0:db8d4af513c0 97 }
M_souta 0:db8d4af513c0 98 }
M_souta 0:db8d4af513c0 99 }
M_souta 0:db8d4af513c0 100
M_souta 0:db8d4af513c0 101 void lostCheck(void) {
M_souta 0:db8d4af513c0 102 timerCount++;
M_souta 0:db8d4af513c0 103 if(timerCount == 2) XBee_LED = LED_OFF;
M_souta 0:db8d4af513c0 104 if(timerCount >= 20) {
M_souta 0:db8d4af513c0 105 controllerLost = true;
M_souta 0:db8d4af513c0 106 Controller::DataReset();
M_souta 0:db8d4af513c0 107 timerCount = 0;
M_souta 0:db8d4af513c0 108 XBee_LED = LED_OFF;
M_souta 0:db8d4af513c0 109 }
M_souta 0:db8d4af513c0 110 }
M_souta 0:db8d4af513c0 111 }
M_souta 0:db8d4af513c0 112