![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Firmware for UT Robotex 2018 basketball robot
RFManager/RFManager.cpp@0:ef6268629f0b, 2018-09-28 (annotated)
- Committer:
- Reiko
- Date:
- Fri Sep 28 10:46:57 2018 +0000
- Revision:
- 0:ef6268629f0b
Firmware for UT Robotex 2018 basketball robot
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Reiko | 0:ef6268629f0b | 1 | //#include <TARGET_LPC1768/cmsis.h> |
Reiko | 0:ef6268629f0b | 2 | #include "RFManager.h" |
Reiko | 0:ef6268629f0b | 3 | |
Reiko | 0:ef6268629f0b | 4 | RFManager::RFManager(PinName txPinName, PinName rxPinName): |
Reiko | 0:ef6268629f0b | 5 | serial(txPinName, rxPinName), buf(64) { |
Reiko | 0:ef6268629f0b | 6 | |
Reiko | 0:ef6268629f0b | 7 | messageAvailable = false; |
Reiko | 0:ef6268629f0b | 8 | receiveCounter = 0; |
Reiko | 0:ef6268629f0b | 9 | shortCommandsEnabled = false; |
Reiko | 0:ef6268629f0b | 10 | shortCommandLength = 5; |
Reiko | 0:ef6268629f0b | 11 | longCommandLength = 12; |
Reiko | 0:ef6268629f0b | 12 | commandLength = longCommandLength; |
Reiko | 0:ef6268629f0b | 13 | |
Reiko | 0:ef6268629f0b | 14 | if (rxPinName == P2_1) { |
Reiko | 0:ef6268629f0b | 15 | serialId = 1; |
Reiko | 0:ef6268629f0b | 16 | } else if (rxPinName == P0_11) { |
Reiko | 0:ef6268629f0b | 17 | serialId = 2; |
Reiko | 0:ef6268629f0b | 18 | } else if (rxPinName == P0_1) { |
Reiko | 0:ef6268629f0b | 19 | serialId = 3; |
Reiko | 0:ef6268629f0b | 20 | } else { |
Reiko | 0:ef6268629f0b | 21 | serialId = 0; |
Reiko | 0:ef6268629f0b | 22 | } |
Reiko | 0:ef6268629f0b | 23 | |
Reiko | 0:ef6268629f0b | 24 | serial.attach(this, &RFManager::rxHandler); |
Reiko | 0:ef6268629f0b | 25 | } |
Reiko | 0:ef6268629f0b | 26 | |
Reiko | 0:ef6268629f0b | 27 | void RFManager::baud(int baudrate) { |
Reiko | 0:ef6268629f0b | 28 | serial.baud(baudrate); |
Reiko | 0:ef6268629f0b | 29 | } |
Reiko | 0:ef6268629f0b | 30 | |
Reiko | 0:ef6268629f0b | 31 | void RFManager::rxHandler(void) { |
Reiko | 0:ef6268629f0b | 32 | // Interrupt does not work with RTOS when using standard functions (getc, putc) |
Reiko | 0:ef6268629f0b | 33 | // https://developer.mbed.org/forum/bugs-suggestions/topic/4217/ |
Reiko | 0:ef6268629f0b | 34 | |
Reiko | 0:ef6268629f0b | 35 | while (serial.readable()) { |
Reiko | 0:ef6268629f0b | 36 | char c = serialReadChar(); |
Reiko | 0:ef6268629f0b | 37 | |
Reiko | 0:ef6268629f0b | 38 | if (receiveCounter < commandLength) { |
Reiko | 0:ef6268629f0b | 39 | if (receiveCounter == 0) { |
Reiko | 0:ef6268629f0b | 40 | // Do not continue before a is received |
Reiko | 0:ef6268629f0b | 41 | if (c == 'a') { |
Reiko | 0:ef6268629f0b | 42 | receiveBuffer[receiveCounter] = c; |
Reiko | 0:ef6268629f0b | 43 | receiveCounter++; |
Reiko | 0:ef6268629f0b | 44 | } |
Reiko | 0:ef6268629f0b | 45 | } else if (c == 'a' && !shortCommandsEnabled |
Reiko | 0:ef6268629f0b | 46 | || c == 'a' && shortCommandsEnabled && (receiveCounter < commandLength - 1) |
Reiko | 0:ef6268629f0b | 47 | ) { |
Reiko | 0:ef6268629f0b | 48 | // If a is received in the middle, assume some bytes got lost before and start from beginning |
Reiko | 0:ef6268629f0b | 49 | receiveCounter = 0; |
Reiko | 0:ef6268629f0b | 50 | |
Reiko | 0:ef6268629f0b | 51 | receiveBuffer[receiveCounter] = c; |
Reiko | 0:ef6268629f0b | 52 | receiveCounter++; |
Reiko | 0:ef6268629f0b | 53 | } else { |
Reiko | 0:ef6268629f0b | 54 | receiveBuffer[receiveCounter] = c; |
Reiko | 0:ef6268629f0b | 55 | receiveCounter++; |
Reiko | 0:ef6268629f0b | 56 | } |
Reiko | 0:ef6268629f0b | 57 | |
Reiko | 0:ef6268629f0b | 58 | if (receiveCounter == commandLength) { |
Reiko | 0:ef6268629f0b | 59 | receiveCounter = 0; |
Reiko | 0:ef6268629f0b | 60 | |
Reiko | 0:ef6268629f0b | 61 | for (unsigned int i = 0; i < commandLength; i++) { |
Reiko | 0:ef6268629f0b | 62 | buf.queue(receiveBuffer[i]); |
Reiko | 0:ef6268629f0b | 63 | } |
Reiko | 0:ef6268629f0b | 64 | |
Reiko | 0:ef6268629f0b | 65 | if (!messageAvailable) { |
Reiko | 0:ef6268629f0b | 66 | handleMessage(); |
Reiko | 0:ef6268629f0b | 67 | //break; |
Reiko | 0:ef6268629f0b | 68 | } |
Reiko | 0:ef6268629f0b | 69 | } |
Reiko | 0:ef6268629f0b | 70 | } |
Reiko | 0:ef6268629f0b | 71 | } |
Reiko | 0:ef6268629f0b | 72 | } |
Reiko | 0:ef6268629f0b | 73 | |
Reiko | 0:ef6268629f0b | 74 | bool RFManager::readable() { |
Reiko | 0:ef6268629f0b | 75 | return messageAvailable; |
Reiko | 0:ef6268629f0b | 76 | } |
Reiko | 0:ef6268629f0b | 77 | |
Reiko | 0:ef6268629f0b | 78 | char *RFManager::read() { |
Reiko | 0:ef6268629f0b | 79 | messageAvailable = false; |
Reiko | 0:ef6268629f0b | 80 | return receivedMessage; |
Reiko | 0:ef6268629f0b | 81 | } |
Reiko | 0:ef6268629f0b | 82 | |
Reiko | 0:ef6268629f0b | 83 | void RFManager::send(char *sendData) { |
Reiko | 0:ef6268629f0b | 84 | serialWrite(sendData, commandLength); |
Reiko | 0:ef6268629f0b | 85 | } |
Reiko | 0:ef6268629f0b | 86 | |
Reiko | 0:ef6268629f0b | 87 | void RFManager::send(char *sendData, int length) { |
Reiko | 0:ef6268629f0b | 88 | serialWrite(sendData, length); |
Reiko | 0:ef6268629f0b | 89 | } |
Reiko | 0:ef6268629f0b | 90 | |
Reiko | 0:ef6268629f0b | 91 | void RFManager::update() { |
Reiko | 0:ef6268629f0b | 92 | /*if (receiveCounter == commandLength) { |
Reiko | 0:ef6268629f0b | 93 | handleMessage(); |
Reiko | 0:ef6268629f0b | 94 | _callback.call(); |
Reiko | 0:ef6268629f0b | 95 | }*/ |
Reiko | 0:ef6268629f0b | 96 | |
Reiko | 0:ef6268629f0b | 97 | if (buf.available() >= commandLength) { |
Reiko | 0:ef6268629f0b | 98 | handleMessage(); |
Reiko | 0:ef6268629f0b | 99 | } |
Reiko | 0:ef6268629f0b | 100 | } |
Reiko | 0:ef6268629f0b | 101 | |
Reiko | 0:ef6268629f0b | 102 | void RFManager::handleMessage() { |
Reiko | 0:ef6268629f0b | 103 | if (messageAvailable) { |
Reiko | 0:ef6268629f0b | 104 | return; |
Reiko | 0:ef6268629f0b | 105 | } |
Reiko | 0:ef6268629f0b | 106 | |
Reiko | 0:ef6268629f0b | 107 | for (unsigned int i = 0; i < commandLength; i++) { |
Reiko | 0:ef6268629f0b | 108 | buf.dequeue(receivedMessage + i); |
Reiko | 0:ef6268629f0b | 109 | } |
Reiko | 0:ef6268629f0b | 110 | |
Reiko | 0:ef6268629f0b | 111 | receivedMessage[commandLength] = '\0'; |
Reiko | 0:ef6268629f0b | 112 | |
Reiko | 0:ef6268629f0b | 113 | /*receiveCounter = 0; |
Reiko | 0:ef6268629f0b | 114 | |
Reiko | 0:ef6268629f0b | 115 | memcpy(receivedMessage, receiveBuffer, sizeof(receiveBuffer));*/ |
Reiko | 0:ef6268629f0b | 116 | |
Reiko | 0:ef6268629f0b | 117 | messageAvailable = true; |
Reiko | 0:ef6268629f0b | 118 | } |
Reiko | 0:ef6268629f0b | 119 | |
Reiko | 0:ef6268629f0b | 120 | void RFManager::serialWrite(char *sendData, int length) { |
Reiko | 0:ef6268629f0b | 121 | int i = 0; |
Reiko | 0:ef6268629f0b | 122 | |
Reiko | 0:ef6268629f0b | 123 | while (i < length) { |
Reiko | 0:ef6268629f0b | 124 | if (serial.writeable()) { |
Reiko | 0:ef6268629f0b | 125 | serial.putc(sendData[i]); |
Reiko | 0:ef6268629f0b | 126 | } |
Reiko | 0:ef6268629f0b | 127 | i++; |
Reiko | 0:ef6268629f0b | 128 | } |
Reiko | 0:ef6268629f0b | 129 | } |
Reiko | 0:ef6268629f0b | 130 | |
Reiko | 0:ef6268629f0b | 131 | char RFManager::serialReadChar() { |
Reiko | 0:ef6268629f0b | 132 | if (serialId == 1) { |
Reiko | 0:ef6268629f0b | 133 | return LPC_UART1->RBR; |
Reiko | 0:ef6268629f0b | 134 | } |
Reiko | 0:ef6268629f0b | 135 | |
Reiko | 0:ef6268629f0b | 136 | if (serialId == 2) { |
Reiko | 0:ef6268629f0b | 137 | return LPC_UART2->RBR; |
Reiko | 0:ef6268629f0b | 138 | } |
Reiko | 0:ef6268629f0b | 139 | |
Reiko | 0:ef6268629f0b | 140 | if (serialId == 3) { |
Reiko | 0:ef6268629f0b | 141 | return LPC_UART3->RBR; |
Reiko | 0:ef6268629f0b | 142 | } |
Reiko | 0:ef6268629f0b | 143 | |
Reiko | 0:ef6268629f0b | 144 | return LPC_UART0->RBR; |
Reiko | 0:ef6268629f0b | 145 | } |
Reiko | 0:ef6268629f0b | 146 | |
Reiko | 0:ef6268629f0b | 147 | void RFManager::setShortCommandMode(bool isEnabled) { |
Reiko | 0:ef6268629f0b | 148 | shortCommandsEnabled = isEnabled; |
Reiko | 0:ef6268629f0b | 149 | receiveCounter = 0; |
Reiko | 0:ef6268629f0b | 150 | |
Reiko | 0:ef6268629f0b | 151 | if (isEnabled) { |
Reiko | 0:ef6268629f0b | 152 | commandLength = shortCommandLength; |
Reiko | 0:ef6268629f0b | 153 | } else { |
Reiko | 0:ef6268629f0b | 154 | commandLength = longCommandLength; |
Reiko | 0:ef6268629f0b | 155 | } |
Reiko | 0:ef6268629f0b | 156 | } |