ECE 4180 - Final Project Team / Mbed 2 deprecated WalkieTalkie

Dependencies:   mbed 4DGL-uLCD-SE mbed-rtos nRF24L01P

Committer:
drechtmann3
Date:
Sun Apr 29 20:18:09 2018 +0000
Revision:
45:805f92c68d55
Parent:
44:a4f81588fcd5
Distributed mutex and uLCD commands to individual functions and an if statement in the main while loop

Who changed what in which revision?

UserRevisionLine numberNew contents of line
drechtmann3 34:d73e95bbdbed 1 #include "uLCD_4DGL.h"
Nurchu 0:c35b54fb9c3c 2 #include "mbed.h"
Nurchu 7:0ac1f1ca8aa6 3 #include "rtos.h"
Nurchu 20:e068469ffb89 4
Nurchu 2:dc046ff72566 5 #include "Microphone.h"
Nurchu 20:e068469ffb89 6 #include "Speaker.h"
drechtmann3 18:e2a8ea6f55d3 7 #include "HUD.h"
Nurchu 20:e068469ffb89 8 #include "nRF24L01P.h"
Nurchu 20:e068469ffb89 9
Nurchu 20:e068469ffb89 10 #include "CircularBuf.h"
Nurchu 20:e068469ffb89 11 #include "CircularBuf.cpp" // Hack to get templates to work
Nurchu 20:e068469ffb89 12
Nurchu 27:08d34e60b6d0 13 // How many times larger the buffer should be
Nurchu 33:5d86c111d9bc 14 #define FIFO_BUFFER_SCALE 250
Nurchu 27:08d34e60b6d0 15 // How maybe bytes to send over RF
Nurchu 27:08d34e60b6d0 16 #define DATA_PACKET_SIZE 32
Nurchu 27:08d34e60b6d0 17 // How quickly to sample the mic / play the speaker
jacksacane 40:4f7d95c68d29 18 #define SAMPLE_PERIOD 1/8000.0
Nurchu 20:e068469ffb89 19
Nurchu 20:e068469ffb89 20
Nurchu 27:08d34e60b6d0 21 Serial pc(USBTX, USBRX);
Nurchu 27:08d34e60b6d0 22 DigitalOut myled1(LED1); // Mic data sent over RF
Nurchu 27:08d34e60b6d0 23 DigitalOut myled2(LED2); // Speaker data recieved over RF
Nurchu 27:08d34e60b6d0 24 DigitalOut myled3(LED3); // Sampled mic / played the speaker
Nurchu 27:08d34e60b6d0 25 DigitalOut myled4(LED4); // Heartbeat
Nurchu 20:e068469ffb89 26
Nurchu 20:e068469ffb89 27 Speaker spkr(p18);
Nurchu 27:08d34e60b6d0 28 Microphone mic(p16);
drechtmann3 45:805f92c68d55 29 Mutex u;
Nurchu 27:08d34e60b6d0 30
drechtmann3 34:d73e95bbdbed 31 uLCD_4DGL uLCD(p28, p27, p29); // serial tx, serial rx, reset pin;
Nurchu 20:e068469ffb89 32
Nurchu 20:e068469ffb89 33 nRF24L01P my_nrf24l01p(p5, p6, p7, p8, p9, p10); // mosi, miso, sck, csn, ce, irq
Nurchu 27:08d34e60b6d0 34 CircularBuf<uint8_t> txbuff( FIFO_BUFFER_SCALE * DATA_PACKET_SIZE );
Nurchu 27:08d34e60b6d0 35 CircularBuf<uint8_t> rxbuff( FIFO_BUFFER_SCALE * DATA_PACKET_SIZE );
Nurchu 27:08d34e60b6d0 36 Ticker sampler; //10:41 am 4/20
Nurchu 20:e068469ffb89 37
Nurchu 29:0c6f3c0c992a 38 InterruptIn button(p12); //changed DitialIn to InterruptIn at 5:54 4/18/18
jacksacane 41:9ed924a1f2e0 39 BusIn channel(p21, p22, p23, p24, p25);
Nurchu 20:e068469ffb89 40
jacksacane 21:95009b231c1f 41 int rfFreq;
jacksacane 21:95009b231c1f 42 int dataRate;
jacksacane 21:95009b231c1f 43 unsigned long long rxAddr, txAddr;
jacksacane 40:4f7d95c68d29 44 int pipe = 0;
drechtmann3 45:805f92c68d55 45 int channelNum = -1;
jacksacane 21:95009b231c1f 46
jacksacane 21:95009b231c1f 47 enum operatingMode {
jacksacane 21:95009b231c1f 48 RECEIVE = 0,
jacksacane 21:95009b231c1f 49 TRANSMIT
jacksacane 21:95009b231c1f 50 };
jacksacane 21:95009b231c1f 51
jacksacane 21:95009b231c1f 52 operatingMode mode;
jacksacane 21:95009b231c1f 53
Nurchu 27:08d34e60b6d0 54 // Cheap nonbranching min function
drechtmann3 45:805f92c68d55 55 int min(int a, int b)
drechtmann3 45:805f92c68d55 56 {
Nurchu 27:08d34e60b6d0 57 return a * (int)(a <= b) + b * (int)(b < a);
Nurchu 27:08d34e60b6d0 58 }
drechtmann3 1:fc0a2c17e086 59
jacksacane 40:4f7d95c68d29 60 // Sets the channel of the RF device based on which swtiches are flipped
jacksacane 40:4f7d95c68d29 61 // by changing RX/TX pipes
Nurchu 27:08d34e60b6d0 62 // TODO: Make sure we don't have to restart the device or anything to change this
drechtmann3 45:805f92c68d55 63 void setChannel()
drechtmann3 45:805f92c68d55 64 {
jacksacane 44:a4f81588fcd5 65 int oldChannel = channelNum;
jacksacane 44:a4f81588fcd5 66 //int oldPipe = pipe;
jacksacane 44:a4f81588fcd5 67 //int width = 5;
jacksacane 40:4f7d95c68d29 68 switch (channel) {
jacksacane 40:4f7d95c68d29 69 case 0: // Channel 0
jacksacane 44:a4f81588fcd5 70 //rxAddr = txAddr = 0xC2C2C2C2C0;
jacksacane 44:a4f81588fcd5 71 //pipe = NRF24L01P_PIPE_P0;
jacksacane 44:a4f81588fcd5 72 channelNum = 0;
jacksacane 44:a4f81588fcd5 73 if (channelNum != oldChannel) {
jacksacane 44:a4f81588fcd5 74 //my_nrf24l01p.setRxAddress(rxAddr, width, pipe);
jacksacane 44:a4f81588fcd5 75 //my_nrf24l01p.setTxAddress(txAddr, width);
jacksacane 44:a4f81588fcd5 76 rfFreq = channelNum + NRF24L01P_MIN_RF_FREQUENCY;
jacksacane 44:a4f81588fcd5 77 my_nrf24l01p.setRfFrequency(rfFreq);
drechtmann3 45:805f92c68d55 78 u.lock();
drechtmann3 45:805f92c68d55 79 uLCD.locate(0, 0);
drechtmann3 45:805f92c68d55 80 uLCD.printf("Freq: %d MHz", rfFreq);
drechtmann3 45:805f92c68d55 81 uLCD.locate(0, 2);
drechtmann3 45:805f92c68d55 82 uLCD.printf("Channel: %d ", channelNum);
drechtmann3 45:805f92c68d55 83 uLCD.locate(0, 10);
drechtmann3 45:805f92c68d55 84 u.unlock();
jacksacane 41:9ed924a1f2e0 85 }
jacksacane 40:4f7d95c68d29 86 break;
jacksacane 40:4f7d95c68d29 87 case 1: // Channel 1
jacksacane 44:a4f81588fcd5 88 //rxAddr = txAddr = 0xC2C2C2C2C1;
jacksacane 44:a4f81588fcd5 89 //pipe = NRF24L01P_PIPE_P1;
jacksacane 44:a4f81588fcd5 90 channelNum = 1;
jacksacane 44:a4f81588fcd5 91 if (channelNum != oldChannel) {
jacksacane 44:a4f81588fcd5 92 //my_nrf24l01p.setRxAddress(rxAddr, width, pipe);
jacksacane 44:a4f81588fcd5 93 //my_nrf24l01p.setTxAddress(txAddr, width);
jacksacane 44:a4f81588fcd5 94 rfFreq = channelNum + NRF24L01P_MIN_RF_FREQUENCY;
jacksacane 44:a4f81588fcd5 95 my_nrf24l01p.setRfFrequency(rfFreq);
drechtmann3 45:805f92c68d55 96 uLCD.locate(0, 0);
drechtmann3 45:805f92c68d55 97 uLCD.printf("Freq: %d MHz", rfFreq);
drechtmann3 45:805f92c68d55 98 uLCD.locate(0, 2);
drechtmann3 45:805f92c68d55 99 uLCD.printf("Channel: %d ", channelNum);
drechtmann3 45:805f92c68d55 100 uLCD.locate(0, 10);
drechtmann3 45:805f92c68d55 101 u.unlock();
jacksacane 41:9ed924a1f2e0 102 }
jacksacane 40:4f7d95c68d29 103 break;
jacksacane 40:4f7d95c68d29 104 case 2: // Channel 2
jacksacane 44:a4f81588fcd5 105 //rxAddr = txAddr = 0xC2C2C2C2C2;
jacksacane 44:a4f81588fcd5 106 //pipe = NRF24L01P_PIPE_P2;
jacksacane 44:a4f81588fcd5 107 channelNum = 2;
jacksacane 44:a4f81588fcd5 108 if (channelNum != oldChannel) {
jacksacane 44:a4f81588fcd5 109 //my_nrf24l01p.setRxAddress(rxAddr, width, pipe);
jacksacane 44:a4f81588fcd5 110 //my_nrf24l01p.setTxAddress(txAddr, width);
jacksacane 44:a4f81588fcd5 111 rfFreq = channelNum + NRF24L01P_MIN_RF_FREQUENCY;
jacksacane 44:a4f81588fcd5 112 my_nrf24l01p.setRfFrequency(rfFreq);
drechtmann3 45:805f92c68d55 113 u.lock();
drechtmann3 45:805f92c68d55 114 uLCD.locate(0, 0);
drechtmann3 45:805f92c68d55 115 uLCD.printf("Freq: %d MHz", rfFreq);
drechtmann3 45:805f92c68d55 116 uLCD.locate(0, 2);
drechtmann3 45:805f92c68d55 117 uLCD.printf("Channel: %d ", channelNum);
drechtmann3 45:805f92c68d55 118 uLCD.locate(0, 10);
drechtmann3 45:805f92c68d55 119 u.unlock();
jacksacane 41:9ed924a1f2e0 120 }
jacksacane 40:4f7d95c68d29 121 break;
jacksacane 40:4f7d95c68d29 122 case 4: // Channel 3
jacksacane 44:a4f81588fcd5 123 //rxAddr = txAddr = 0xC2C2C2C2C3;
jacksacane 44:a4f81588fcd5 124 //pipe = NRF24L01P_PIPE_P3;
jacksacane 44:a4f81588fcd5 125 channelNum = 3;
jacksacane 44:a4f81588fcd5 126 if (channelNum != oldChannel) {
jacksacane 44:a4f81588fcd5 127 //my_nrf24l01p.setRxAddress(rxAddr, width, pipe);
jacksacane 44:a4f81588fcd5 128 //my_nrf24l01p.setTxAddress(txAddr, width);
jacksacane 44:a4f81588fcd5 129 rfFreq = channelNum + NRF24L01P_MIN_RF_FREQUENCY;
jacksacane 44:a4f81588fcd5 130 my_nrf24l01p.setRfFrequency(rfFreq);
drechtmann3 45:805f92c68d55 131 u.lock();
drechtmann3 45:805f92c68d55 132 uLCD.locate(0, 0);
drechtmann3 45:805f92c68d55 133 uLCD.printf("Freq: %d MHz", rfFreq);
drechtmann3 45:805f92c68d55 134 uLCD.locate(0, 2);
drechtmann3 45:805f92c68d55 135 uLCD.printf("Channel: %d ", channelNum);
drechtmann3 45:805f92c68d55 136 uLCD.locate(0, 10);
drechtmann3 45:805f92c68d55 137 u.unlock();
jacksacane 41:9ed924a1f2e0 138 }
jacksacane 40:4f7d95c68d29 139 break;
jacksacane 40:4f7d95c68d29 140 case 8: // Channel 4
jacksacane 44:a4f81588fcd5 141 //rxAddr = txAddr = 0xC2C2C2C2C4;
jacksacane 44:a4f81588fcd5 142 //pipe = NRF24L01P_PIPE_P4;
jacksacane 44:a4f81588fcd5 143 channelNum = 4;
jacksacane 44:a4f81588fcd5 144 if (channelNum != oldChannel) {
jacksacane 44:a4f81588fcd5 145 //my_nrf24l01p.setRxAddress(rxAddr, width, pipe);
jacksacane 44:a4f81588fcd5 146 //my_nrf24l01p.setTxAddress(txAddr, width);
jacksacane 44:a4f81588fcd5 147 rfFreq = channelNum + NRF24L01P_MIN_RF_FREQUENCY;
jacksacane 44:a4f81588fcd5 148 my_nrf24l01p.setRfFrequency(rfFreq);
drechtmann3 45:805f92c68d55 149 u.lock();
drechtmann3 45:805f92c68d55 150 uLCD.locate(0, 0);
drechtmann3 45:805f92c68d55 151 uLCD.printf("Freq: %d MHz", rfFreq);
drechtmann3 45:805f92c68d55 152 uLCD.locate(0, 2);
drechtmann3 45:805f92c68d55 153 uLCD.printf("Channel: %d ", channelNum);
drechtmann3 45:805f92c68d55 154 uLCD.locate(0, 10);
drechtmann3 45:805f92c68d55 155 u.unlock();
jacksacane 41:9ed924a1f2e0 156 }
jacksacane 40:4f7d95c68d29 157 break;
jacksacane 40:4f7d95c68d29 158 case 16: // Channel 5
jacksacane 44:a4f81588fcd5 159 //rxAddr = txAddr = 0xC2C2C2C2C5;
jacksacane 44:a4f81588fcd5 160 //pipe = NRF24L01P_PIPE_P5;
jacksacane 44:a4f81588fcd5 161 channelNum = 5;
jacksacane 44:a4f81588fcd5 162 if (channelNum != oldChannel) {
jacksacane 44:a4f81588fcd5 163 //my_nrf24l01p.setRxAddress(rxAddr, width, pipe);
jacksacane 44:a4f81588fcd5 164 //my_nrf24l01p.setTxAddress(txAddr, width);
jacksacane 44:a4f81588fcd5 165 rfFreq = channelNum + NRF24L01P_MIN_RF_FREQUENCY;
jacksacane 44:a4f81588fcd5 166 my_nrf24l01p.setRfFrequency(rfFreq);
drechtmann3 45:805f92c68d55 167 u.lock();
drechtmann3 45:805f92c68d55 168 uLCD.locate(0, 0);
drechtmann3 45:805f92c68d55 169 uLCD.printf("Freq: %d MHz", rfFreq);
drechtmann3 45:805f92c68d55 170 uLCD.locate(0, 2);
drechtmann3 45:805f92c68d55 171 uLCD.printf("Channel: %d ", channelNum);
drechtmann3 45:805f92c68d55 172 uLCD.locate(0, 10);
drechtmann3 45:805f92c68d55 173 u.unlock();
jacksacane 41:9ed924a1f2e0 174 }
jacksacane 40:4f7d95c68d29 175 break;
jacksacane 40:4f7d95c68d29 176 default:
jacksacane 40:4f7d95c68d29 177 break;
jacksacane 40:4f7d95c68d29 178 }
drechtmann3 45:805f92c68d55 179
drechtmann3 45:805f92c68d55 180 //pc.printf("Pipe = %d\r\n", pipe);
drechtmann3 45:805f92c68d55 181
Nurchu 27:08d34e60b6d0 182 // TODO: Don't force it to the default RF frequency
jacksacane 40:4f7d95c68d29 183 //channelNum = 2;
drechtmann3 45:805f92c68d55 184
jacksacane 40:4f7d95c68d29 185 //my_nrf24l01p.setRfFrequency(channelNum + NRF24L01P_MIN_RF_FREQUENCY);
Nurchu 27:08d34e60b6d0 186 }
drechtmann3 1:fc0a2c17e086 187
Nurchu 27:08d34e60b6d0 188 // Callback interrupt from the button to shift into transmit mode
drechtmann3 45:805f92c68d55 189 void enterTransmitMode()
drechtmann3 45:805f92c68d55 190 {
Nurchu 27:08d34e60b6d0 191 mode = TRANSMIT;
Nurchu 33:5d86c111d9bc 192 txbuff.clear();
drechtmann3 45:805f92c68d55 193 // u.lock();
drechtmann3 45:805f92c68d55 194 // uLCD.locate(0, 10);
drechtmann3 45:805f92c68d55 195 // uLCD.printf("Mode: Transmitting");
drechtmann3 45:805f92c68d55 196 // u.unlock();
Nurchu 27:08d34e60b6d0 197 }
drechtmann3 1:fc0a2c17e086 198
Nurchu 27:08d34e60b6d0 199 // Callback interrupt from the button to shift into receive mode
drechtmann3 45:805f92c68d55 200 void enterRecieveMode()
drechtmann3 45:805f92c68d55 201 {
Nurchu 27:08d34e60b6d0 202 mode = RECEIVE;
drechtmann3 34:d73e95bbdbed 203 rxbuff.clear();
drechtmann3 45:805f92c68d55 204 // u.lock();
drechtmann3 45:805f92c68d55 205 // uLCD.locate(0, 10);
drechtmann3 45:805f92c68d55 206 // uLCD.printf("Mode: Receiving");
drechtmann3 45:805f92c68d55 207 // u.unlock();
drechtmann3 10:a8fcfc869fa5 208 }
drechtmann3 1:fc0a2c17e086 209
Nurchu 27:08d34e60b6d0 210 // Called every SAMPLE_PERIOD ms to sample the mic or output data into the speaker
Nurchu 27:08d34e60b6d0 211 void sampleData()
Nurchu 12:efcfe4c0d9f2 212 {
Nurchu 27:08d34e60b6d0 213 // Depending on the mode, only sample the mic or output data to the speaker
Nurchu 27:08d34e60b6d0 214 if (mode == RECEIVE) {
Nurchu 27:08d34e60b6d0 215 // Get speaker sample from buffer
Nurchu 27:08d34e60b6d0 216 // If there is no data in the buffer, it will just output 0 to the write function
Nurchu 27:08d34e60b6d0 217 uint8_t speakerSample = 0;
Nurchu 27:08d34e60b6d0 218 rxbuff.pop(&speakerSample, 1);
drechtmann3 45:805f92c68d55 219
Nurchu 27:08d34e60b6d0 220 // Output into the actual speaker
Nurchu 27:08d34e60b6d0 221 spkr.write(speakerSample);
Nurchu 27:08d34e60b6d0 222 } else {
Nurchu 27:08d34e60b6d0 223 // Get mic sample and place into buffer
Nurchu 33:5d86c111d9bc 224 uint8_t micSample = mic.getData();
Nurchu 27:08d34e60b6d0 225 txbuff.push(&micSample, 1);
Nurchu 12:efcfe4c0d9f2 226
Nurchu 27:08d34e60b6d0 227 // Make sure the speaker is actually off
Nurchu 27:08d34e60b6d0 228 spkr.turnOff();
Nurchu 27:08d34e60b6d0 229 }
drechtmann3 45:805f92c68d55 230
Nurchu 27:08d34e60b6d0 231 // TODO: This will have to be removed later on once we actually crank up the sample rate
Nurchu 27:08d34e60b6d0 232 myled3 = !myled3;
Nurchu 27:08d34e60b6d0 233 }
drechtmann3 1:fc0a2c17e086 234
Nurchu 27:08d34e60b6d0 235 // Communicates to the other MBED using RF
Nurchu 27:08d34e60b6d0 236 void commThread()
Nurchu 27:08d34e60b6d0 237 {
Nurchu 27:08d34e60b6d0 238 // We want this in it's own thread so we don't have to worry about the
Nurchu 27:08d34e60b6d0 239 // timings screwing anything else up
Nurchu 27:08d34e60b6d0 240 // It can't be in an interrupt because of that
Nurchu 27:08d34e60b6d0 241 while (true) {
Nurchu 27:08d34e60b6d0 242 // Change what we are sending based on what mode we are in
Nurchu 27:08d34e60b6d0 243 if (mode == RECEIVE) {
Nurchu 27:08d34e60b6d0 244 // Make sure something is there to read
Nurchu 27:08d34e60b6d0 245 if (my_nrf24l01p.readable( NRF24L01P_PIPE_P0 )) {
Nurchu 27:08d34e60b6d0 246 uint8_t spkrPacket[DATA_PACKET_SIZE];
drechtmann3 45:805f92c68d55 247
Nurchu 27:08d34e60b6d0 248 // Remove entire packet of data from the bus
Nurchu 27:08d34e60b6d0 249 int numReceived = my_nrf24l01p.read( NRF24L01P_PIPE_P0, (char*) spkrPacket, DATA_PACKET_SIZE );
drechtmann3 45:805f92c68d55 250
Nurchu 27:08d34e60b6d0 251 // Place into buffer to play speaker in another thread
Nurchu 27:08d34e60b6d0 252 // Only place into the buffer the number of bytes received
Nurchu 27:08d34e60b6d0 253 rxbuff.push(spkrPacket, min(DATA_PACKET_SIZE, numReceived));
drechtmann3 45:805f92c68d55 254
jacksacane 40:4f7d95c68d29 255 //pc.printf("Receiviing....\n\r");
Nurchu 27:08d34e60b6d0 256 myled2 = !myled2;
Nurchu 27:08d34e60b6d0 257 }
Nurchu 27:08d34e60b6d0 258 } else { // mode == TRANSMIT
Nurchu 27:08d34e60b6d0 259 if (txbuff.size() >= DATA_PACKET_SIZE) {
Nurchu 27:08d34e60b6d0 260 uint8_t micPacket[DATA_PACKET_SIZE];
drechtmann3 45:805f92c68d55 261
Nurchu 27:08d34e60b6d0 262 // Pull an entire packet of data from the mic sample buffer
Nurchu 27:08d34e60b6d0 263 int numPopped = txbuff.pop(micPacket, DATA_PACKET_SIZE);
drechtmann3 45:805f92c68d55 264 // rxbuff.push(micPacket, DATA_PACKET_SIZE);
drechtmann3 45:805f92c68d55 265
Nurchu 27:08d34e60b6d0 266 // Send the entire buffer to the other device
Nurchu 27:08d34e60b6d0 267 // TODO: We just assume that DATA_PACKET_SIZE bytes were popped, this may
Nurchu 27:08d34e60b6d0 268 // not be the case
Nurchu 27:08d34e60b6d0 269 my_nrf24l01p.write( NRF24L01P_PIPE_P0, (char*) micPacket, DATA_PACKET_SIZE );
drechtmann3 34:d73e95bbdbed 270
Nurchu 27:08d34e60b6d0 271 myled1 = !myled1;
Nurchu 27:08d34e60b6d0 272 }
drechtmann3 1:fc0a2c17e086 273 }
drechtmann3 45:805f92c68d55 274
Nurchu 33:5d86c111d9bc 275 //pc.printf("TX Size %d RX Size%d Mode %d\n\r", txbuff.size(), rxbuff.size(), mode);
Nurchu 27:08d34e60b6d0 276 Thread::yield();
Nurchu 33:5d86c111d9bc 277 //Thread::wait(10);
Nurchu 12:efcfe4c0d9f2 278 }
Nurchu 12:efcfe4c0d9f2 279 }
drechtmann3 1:fc0a2c17e086 280
Nurchu 27:08d34e60b6d0 281 // Displays the current info to the LCD display
jacksacane 44:a4f81588fcd5 282 /*void lcdThread()
jacksacane 21:95009b231c1f 283 {
jacksacane 21:95009b231c1f 284 while (1) {
drechtmann3 34:d73e95bbdbed 285 uLCD.locate(0, 0);
jacksacane 41:9ed924a1f2e0 286 uLCD.printf("Freq: %d MHz", rfFreq);
drechtmann3 34:d73e95bbdbed 287 uLCD.locate(0, 2);
jacksacane 41:9ed924a1f2e0 288 uLCD.printf("Rate: %d kbps", dataRate);
drechtmann3 34:d73e95bbdbed 289 uLCD.locate(0, 4);
jacksacane 44:a4f81588fcd5 290 uLCD.printf("Addr: 0x%010llX", txAddr);
drechtmann3 34:d73e95bbdbed 291 uLCD.locate(0, 6);
jacksacane 44:a4f81588fcd5 292 //uLCD.printf("RX: 0x%010llX", rxAddr);
jacksacane 44:a4f81588fcd5 293 uLCD.printf("Channel: %d", channelNum);
jacksacane 44:a4f81588fcd5 294 uLCD.locate(0, 10);
drechtmann3 45:805f92c68d55 295
jacksacane 21:95009b231c1f 296 switch (mode) {
jacksacane 21:95009b231c1f 297 case RECEIVE:
jacksacane 41:9ed924a1f2e0 298 uLCD.printf("Mode: Receiving");
jacksacane 21:95009b231c1f 299 break;
jacksacane 21:95009b231c1f 300 case TRANSMIT:
jacksacane 44:a4f81588fcd5 301 uLCD.line(0, 8, 127, 8, BLACK);
jacksacane 21:95009b231c1f 302 uLCD.printf("Mode: Transmitting");
jacksacane 21:95009b231c1f 303 break;
jacksacane 21:95009b231c1f 304 }
drechtmann3 45:805f92c68d55 305
jacksacane 21:95009b231c1f 306 // Maybe add some graphics too idk
jacksacane 44:a4f81588fcd5 307 Thread::wait(1000);
jacksacane 21:95009b231c1f 308 }
jacksacane 44:a4f81588fcd5 309 }*/
jacksacane 21:95009b231c1f 310
Nurchu 12:efcfe4c0d9f2 311 int main()
Nurchu 12:efcfe4c0d9f2 312 {
jacksacane 44:a4f81588fcd5 313 //Thread lcd;
drechtmann3 45:805f92c68d55 314 Thread comm;
drechtmann3 45:805f92c68d55 315
Nurchu 27:08d34e60b6d0 316 // Set up the nrf24l01p
jacksacane 41:9ed924a1f2e0 317 my_nrf24l01p.setAirDataRate(2000); // 2Mbs
jacksacane 21:95009b231c1f 318 rfFreq = my_nrf24l01p.getRfFrequency();
jacksacane 21:95009b231c1f 319 dataRate = my_nrf24l01p.getAirDataRate();
jacksacane 21:95009b231c1f 320 rxAddr = my_nrf24l01p.getRxAddress();
jacksacane 21:95009b231c1f 321 txAddr = my_nrf24l01p.getTxAddress();
drechtmann3 45:805f92c68d55 322
Nurchu 27:08d34e60b6d0 323 my_nrf24l01p.setTransferSize(DATA_PACKET_SIZE);
jacksacane 21:95009b231c1f 324
jacksacane 21:95009b231c1f 325 my_nrf24l01p.setReceiveMode();
jacksacane 21:95009b231c1f 326 my_nrf24l01p.enable();
drechtmann3 45:805f92c68d55 327
jacksacane 40:4f7d95c68d29 328 pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", rfFreq );
jacksacane 40:4f7d95c68d29 329 pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() );
jacksacane 40:4f7d95c68d29 330 pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() );
jacksacane 40:4f7d95c68d29 331 pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", txAddr );
jacksacane 40:4f7d95c68d29 332 pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", rxAddr );
drechtmann3 45:805f92c68d55 333
Nurchu 27:08d34e60b6d0 334 pc.printf("Finished starting up....\n\r");
drechtmann3 45:805f92c68d55 335
jacksacane 22:e835b3490280 336 mode = RECEIVE;
drechtmann3 45:805f92c68d55 337
jacksacane 21:95009b231c1f 338 // Initialize the uLCD
drechtmann3 34:d73e95bbdbed 339 uLCD.baudrate(3000000);
drechtmann3 34:d73e95bbdbed 340 uLCD.background_color(BLACK);
drechtmann3 45:805f92c68d55 341
jacksacane 21:95009b231c1f 342 // Spawn threads
jacksacane 44:a4f81588fcd5 343 //lcd.start(lcdThread);
Nurchu 27:08d34e60b6d0 344 comm.start(commThread);
drechtmann3 45:805f92c68d55 345
Nurchu 27:08d34e60b6d0 346 // Setup the button to enter transmit mode when pushed down
Nurchu 27:08d34e60b6d0 347 // and recieve when release
jacksacane 28:9413eb50156d 348 button.mode(PullUp);
Nurchu 31:39d04aedc3e5 349 button.fall(&enterTransmitMode);
Nurchu 31:39d04aedc3e5 350 button.rise(&enterRecieveMode);
drechtmann3 45:805f92c68d55 351
Nurchu 27:08d34e60b6d0 352 // Setup sampler to sample at a specific frequency
Nurchu 31:39d04aedc3e5 353 sampler.attach(&sampleData, SAMPLE_PERIOD);
drechtmann3 45:805f92c68d55 354
drechtmann3 45:805f92c68d55 355 // Heartbeat thread
drechtmann3 45:805f92c68d55 356 u.lock();
drechtmann3 45:805f92c68d55 357 uLCD.locate(0, 0);
drechtmann3 45:805f92c68d55 358 uLCD.printf("Freq: %d MHz", rfFreq);
drechtmann3 45:805f92c68d55 359 uLCD.locate(0, 2);
drechtmann3 45:805f92c68d55 360 uLCD.printf("Channel: %d", channelNum);
drechtmann3 45:805f92c68d55 361 uLCD.locate(0, 4);
drechtmann3 45:805f92c68d55 362 uLCD.printf("Addr: 0x%010llX", txAddr);
drechtmann3 45:805f92c68d55 363 uLCD.locate(0, 6);
drechtmann3 45:805f92c68d55 364 uLCD.printf("RX: 0x%010llX", rxAddr);
drechtmann3 45:805f92c68d55 365 uLCD.locate(0, 10);
drechtmann3 45:805f92c68d55 366 uLCD.printf("Mode: Receiving");
drechtmann3 45:805f92c68d55 367 u.unlock();
drechtmann3 45:805f92c68d55 368 int oldmode = -1;
Nurchu 27:08d34e60b6d0 369
drechtmann3 1:fc0a2c17e086 370 while (1) {
Nurchu 27:08d34e60b6d0 371 myled4 = !myled4;
jacksacane 40:4f7d95c68d29 372 setChannel(); // Poll the DIP switch and change channels accordingly
drechtmann3 45:805f92c68d55 373 if (mode == 0 && not(oldmode == mode)) {
drechtmann3 45:805f92c68d55 374 uLCD.locate(6, 10);
drechtmann3 45:805f92c68d55 375 uLCD.printf("Rx");
drechtmann3 45:805f92c68d55 376 oldmode = mode;
drechtmann3 45:805f92c68d55 377 } else if (mode == 1 && not(oldmode == mode)) {
drechtmann3 45:805f92c68d55 378 uLCD.locate(6, 10);
drechtmann3 45:805f92c68d55 379 uLCD.printf("Tx");
drechtmann3 45:805f92c68d55 380 oldmode = mode;
drechtmann3 45:805f92c68d55 381 }
Nurchu 27:08d34e60b6d0 382 Thread::wait(100);
Nurchu 12:efcfe4c0d9f2 383 }
Nurchu 12:efcfe4c0d9f2 384 }