asdf

Dependencies:   NokiaLCD XMIT_IR mbed

Fork of 4180_mP_WirelessPong_revC by Curtis Mulady

Committer:
cmulady
Date:
Sat Oct 06 17:36:47 2012 +0000
Revision:
18:98839f78fc01
Parent:
17:f92e4a384637
Child:
19:5a4be4519de5
about to remove tx interrupt handling

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cmulady 0:c8ddcaa575ba 1 #include "mbed.h"
cmulady 0:c8ddcaa575ba 2 #include "rtos.h"
cmulady 0:c8ddcaa575ba 3 #include "NokiaLCD.h"
cmulady 1:9ba884d85ac6 4 #include "XMIT_IR.h"
cmulady 0:c8ddcaa575ba 5
cmulady 18:98839f78fc01 6 #define FPS 10
cmulady 1:9ba884d85ac6 7
cmulady 3:8e492eacd346 8 /****************************************
cmulady 3:8e492eacd346 9 |=======================================|
cmulady 3:8e492eacd346 10 |MBED Connections: |
cmulady 3:8e492eacd346 11 | -p5 : DIO on Sparkfun Nokia LCD |
cmulady 3:8e492eacd346 12 | -p7 : CLK on Sparkfun Nokia LCD |
cmulady 3:8e492eacd346 13 | -p8 : CS on Sparkfun Nokia LCD |
cmulady 3:8e492eacd346 14 | -p9 : RST on Sparkfun Nokia LCD |
cmulady 12:9acca1dd0e8e 15 | -p21: CTL on Sparkfun IR Xmitter |
cmulady 12:9acca1dd0e8e 16 | -p14: OUT on Sparkfun IR Rcvr |
cmulady 12:9acca1dd0e8e 17 | -p13: GND on Sparkfun IR Xmitter |
cmulady 3:8e492eacd346 18 |=======================================|
cmulady 3:8e492eacd346 19 ****************************************/
cmulady 1:9ba884d85ac6 20
cmulady 13:17fd813ef7c9 21 //Function Prototypes
cmulady 13:17fd813ef7c9 22 void BlinkAlive(void const* arguments);
cmulady 13:17fd813ef7c9 23 void UpdateLCD(void const* arguments);
cmulady 13:17fd813ef7c9 24 void IRStuff(void const* arguments);
cmulady 13:17fd813ef7c9 25 void ISR_UARTRX(void);
cmulady 18:98839f78fc01 26 void ISR_UARTTX(void);
cmulady 13:17fd813ef7c9 27 char CheckPacket(char new_data, char* packet_buffer, char* data, int data_len);
cmulady 13:17fd813ef7c9 28 void MakePacket2(char* data,int len);
cmulady 13:17fd813ef7c9 29
cmulady 3:8e492eacd346 30 //Pin Setup
cmulady 13:17fd813ef7c9 31 PwmOut led1(LED1);
cmulady 13:17fd813ef7c9 32 PwmOut led2(LED2);
cmulady 3:8e492eacd346 33 DigitalOut led3(LED3);
cmulady 3:8e492eacd346 34 DigitalOut led4(LED4);
cmulady 0:c8ddcaa575ba 35 NokiaLCD lcd(p5, p7, p8, p9, NokiaLCD::LCD6610); // mosi, sclk, cs, rst, type
cmulady 13:17fd813ef7c9 36 Serial* devicerx;
cmulady 13:17fd813ef7c9 37
cmulady 13:17fd813ef7c9 38 //Serial devicetx(p13,NC);
cmulady 6:5563f0026858 39 Serial pc(USBTX,USBRX);
cmulady 5:2e08cc167fff 40 PwmOut IRLED_mod(p21);
cmulady 1:9ba884d85ac6 41
cmulady 18:98839f78fc01 42 IRTransmitter ir_tx(p13,p21,p14,4,&ISR_UARTRX,&ISR_UARTTX); //tx,pwm,size
cmulady 13:17fd813ef7c9 43
cmulady 3:8e492eacd346 44 //Global Vars
cmulady 12:9acca1dd0e8e 45 char text_buffer[32];
cmulady 6:5563f0026858 46 char irdatOUT[10];
cmulady 6:5563f0026858 47 char irdatIN[10];
cmulady 12:9acca1dd0e8e 48 char irdata_out=0;
cmulady 6:5563f0026858 49 char error_code=0;
cmulady 8:54dd4a3d0de9 50 Thread* threadptr_irstuff;
cmulady 9:3e4e9d6a8ad8 51 char packet_buff[10];
cmulady 10:4fcd5bdb9642 52 Mail<char,64> rx_data_mailbox;
cmulady 17:f92e4a384637 53 Mutex data_update_mutex;
cmulady 1:9ba884d85ac6 54
cmulady 13:17fd813ef7c9 55
cmulady 1:9ba884d85ac6 56
cmulady 1:9ba884d85ac6 57
cmulady 1:9ba884d85ac6 58 int main()
cmulady 1:9ba884d85ac6 59 {
cmulady 0:c8ddcaa575ba 60
cmulady 3:8e492eacd346 61 //LCD init
cmulady 0:c8ddcaa575ba 62 lcd.background(0x000000);
cmulady 1:9ba884d85ac6 63
cmulady 6:5563f0026858 64 //PC serial init
cmulady 6:5563f0026858 65 pc.baud(19200);
cmulady 7:c9ff6b5c8507 66 pc.printf("Starting...\n\n");
cmulady 6:5563f0026858 67
cmulady 16:bc0e213ba52d 68 //IR_TRX module
cmulady 16:bc0e213ba52d 69 ir_tx.set_debug_port(&pc);
cmulady 16:bc0e213ba52d 70
cmulady 13:17fd813ef7c9 71
cmulady 9:3e4e9d6a8ad8 72 //Variable Init
cmulady 9:3e4e9d6a8ad8 73 for(int i=0; i<10; i++) packet_buff[i]=0;
cmulady 9:3e4e9d6a8ad8 74
cmulady 12:9acca1dd0e8e 75 //Threads init
cmulady 1:9ba884d85ac6 76 Thread thread_blinkalive(BlinkAlive);
cmulady 1:9ba884d85ac6 77 Thread thread_updatelcd(UpdateLCD);
cmulady 3:8e492eacd346 78 Thread thread_irstuff(IRStuff);
cmulady 8:54dd4a3d0de9 79 threadptr_irstuff = &thread_irstuff;
cmulady 1:9ba884d85ac6 80
cmulady 1:9ba884d85ac6 81
cmulady 1:9ba884d85ac6 82
cmulady 0:c8ddcaa575ba 83 while(1) {
cmulady 13:17fd813ef7c9 84
cmulady 12:9acca1dd0e8e 85 //Use main loop to set LCD framerate
cmulady 1:9ba884d85ac6 86 thread_updatelcd.signal_set(0x1);
cmulady 1:9ba884d85ac6 87 Thread::wait(1000/FPS);
cmulady 1:9ba884d85ac6 88
cmulady 0:c8ddcaa575ba 89 }
cmulady 0:c8ddcaa575ba 90 }
cmulady 1:9ba884d85ac6 91
cmulady 1:9ba884d85ac6 92 void UpdateLCD(void const* arguments)
cmulady 1:9ba884d85ac6 93 {
cmulady 7:c9ff6b5c8507 94 while(true) {
cmulady 13:17fd813ef7c9 95
cmulady 12:9acca1dd0e8e 96 //Start flash LED
cmulady 13:17fd813ef7c9 97 led2 = 0.02;
cmulady 17:f92e4a384637 98 data_update_mutex.lock();
cmulady 17:f92e4a384637 99 char irdatbuff[4];
cmulady 17:f92e4a384637 100 for(int i=0; i<4; i++) irdatbuff[i] = irdatIN[i];
cmulady 17:f92e4a384637 101 data_update_mutex.unlock();
cmulady 17:f92e4a384637 102
cmulady 17:f92e4a384637 103 if(irdatbuff[0]!=0xA5 && irdatbuff[0]!=0x00) {
cmulady 17:f92e4a384637 104 lcd.locate(0,7);
cmulady 17:f92e4a384637 105 lcd.printf("Big Problem! 0x%02X",irdatbuff[0]);
cmulady 17:f92e4a384637 106 while(1);
cmulady 17:f92e4a384637 107 }
cmulady 17:f92e4a384637 108
cmulady 13:17fd813ef7c9 109
cmulady 12:9acca1dd0e8e 110 //Write debug text to screen
cmulady 1:9ba884d85ac6 111 lcd.locate(0,1);
cmulady 1:9ba884d85ac6 112 lcd.printf("Debug:");
cmulady 1:9ba884d85ac6 113 lcd.locate(0,3);
cmulady 1:9ba884d85ac6 114 time_t seconds = time(NULL);
cmulady 12:9acca1dd0e8e 115 strftime(text_buffer, 32, "%I:%M:%S %p\n", localtime(&seconds));
cmulady 12:9acca1dd0e8e 116 lcd.printf("%s", text_buffer);
cmulady 3:8e492eacd346 117 lcd.locate(0,4);
cmulady 6:5563f0026858 118 lcd.printf("IR_OUT=0x%02X,0x%02X", irdatOUT[0],irdatOUT[1]);
cmulady 3:8e492eacd346 119 lcd.locate(0,5);
cmulady 17:f92e4a384637 120 lcd.printf("IR_IN= 0x%02X,0x%02X", irdatbuff[0],irdatbuff[1]);
cmulady 6:5563f0026858 121 lcd.locate(0,6);
cmulady 6:5563f0026858 122 lcd.printf("Error= 0x%02X", error_code);
cmulady 3:8e492eacd346 123
cmulady 12:9acca1dd0e8e 124 //End - flash LED
cmulady 13:17fd813ef7c9 125 led2 = 0.0;
cmulady 1:9ba884d85ac6 126 //End - Sleep thread
cmulady 1:9ba884d85ac6 127 Thread::signal_wait(0x1);
cmulady 7:c9ff6b5c8507 128 }
cmulady 1:9ba884d85ac6 129 }
cmulady 1:9ba884d85ac6 130
cmulady 3:8e492eacd346 131 void IRStuff(void const* arguments)
cmulady 3:8e492eacd346 132 {
cmulady 3:8e492eacd346 133 while(true) {
cmulady 7:c9ff6b5c8507 134
cmulady 12:9acca1dd0e8e 135 //If data available - Print data directly to USB port (for debug)
cmulady 16:bc0e213ba52d 136
cmulady 16:bc0e213ba52d 137 char result;
cmulady 17:f92e4a384637 138 char temp_buff[4];
cmulady 17:f92e4a384637 139
cmulady 17:f92e4a384637 140 //buffer data into temp register
cmulady 17:f92e4a384637 141 result = ir_tx.ReadPacket(temp_buff,4);
cmulady 17:f92e4a384637 142
cmulady 16:bc0e213ba52d 143 if(result == 0x1) {
cmulady 18:98839f78fc01 144 //pc.printf("\n");
cmulady 17:f92e4a384637 145 //update data - mutex
cmulady 17:f92e4a384637 146 data_update_mutex.lock();
cmulady 17:f92e4a384637 147 for(int i=0; i<4; i++) {
cmulady 17:f92e4a384637 148 irdatIN[i] = temp_buff[i];
cmulady 17:f92e4a384637 149 }
cmulady 17:f92e4a384637 150 data_update_mutex.unlock();
cmulady 16:bc0e213ba52d 151 for(int i=0; i<4; i++) {
cmulady 18:98839f78fc01 152 //pc.printf("0x%02X.",irdatIN[i]);
cmulady 16:bc0e213ba52d 153 }
cmulady 16:bc0e213ba52d 154 }
cmulady 16:bc0e213ba52d 155
cmulady 15:21c512907cf6 156
cmulady 13:17fd813ef7c9 157
cmulady 12:9acca1dd0e8e 158 //Do not return until we have more data
cmulady 13:17fd813ef7c9 159 //Thread::signal_wait(0x1);
cmulady 16:bc0e213ba52d 160 Thread::wait(10);
cmulady 3:8e492eacd346 161 }
cmulady 4:8fdff78c13c8 162
cmulady 3:8e492eacd346 163 }
cmulady 3:8e492eacd346 164
cmulady 13:17fd813ef7c9 165 char CheckPacket(char new_data, char* packet_buffer, char* data, int data_len)
cmulady 6:5563f0026858 166 {
cmulady 13:17fd813ef7c9 167 //Requires a packet buffer of length 'data_len'+3.
cmulady 9:3e4e9d6a8ad8 168 //Shifts data and checks each 'set' for a valid packet.
cmulady 9:3e4e9d6a8ad8 169 //Once a valid packet is receievd, the data buffer is updated with new values.
cmulady 13:17fd813ef7c9 170 //returns success(0) or failure(error code)
cmulady 13:17fd813ef7c9 171
cmulady 9:3e4e9d6a8ad8 172
cmulady 9:3e4e9d6a8ad8 173 char check=0x0;
cmulady 9:3e4e9d6a8ad8 174 //Shift All data 1 cell over
cmulady 13:17fd813ef7c9 175 for(int i=0; i<data_len+2; i++) {
cmulady 9:3e4e9d6a8ad8 176 packet_buffer[i] = packet_buffer[i+1];
cmulady 9:3e4e9d6a8ad8 177 }
cmulady 13:17fd813ef7c9 178 packet_buffer[data_len+2] = new_data;
cmulady 9:3e4e9d6a8ad8 179
cmulady 9:3e4e9d6a8ad8 180 //check for valid packet
cmulady 13:17fd813ef7c9 181 if(packet_buffer[0]!=ASCII_STX)
cmulady 9:3e4e9d6a8ad8 182 return 0x1; //bad start byte
cmulady 13:17fd813ef7c9 183 if(packet_buffer[data_len+2]!=ASCII_ETX)
cmulady 13:17fd813ef7c9 184 return 0x2; //bad end byte
cmulady 9:3e4e9d6a8ad8 185
cmulady 9:3e4e9d6a8ad8 186 for(int i=1; i<data_len+1; i++) {
cmulady 9:3e4e9d6a8ad8 187 check^=packet_buffer[i];
cmulady 9:3e4e9d6a8ad8 188 }
cmulady 15:21c512907cf6 189 if(check!=packet_buffer[data_len+1]) {
cmulady 15:21c512907cf6 190 return 0x3; //bad checksum
cmulady 9:3e4e9d6a8ad8 191 }
cmulady 13:17fd813ef7c9 192
cmulady 9:3e4e9d6a8ad8 193 return 0;
cmulady 9:3e4e9d6a8ad8 194 }
cmulady 9:3e4e9d6a8ad8 195
cmulady 13:17fd813ef7c9 196 void BlinkAlive(void const* arguments)
cmulady 13:17fd813ef7c9 197 {
cmulady 13:17fd813ef7c9 198 while(true) {
cmulady 13:17fd813ef7c9 199
cmulady 13:17fd813ef7c9 200 //Change LED1 state (debug)
cmulady 13:17fd813ef7c9 201 (led1==0.0)?led1=0.02:led1=0.0;
cmulady 13:17fd813ef7c9 202
cmulady 13:17fd813ef7c9 203 //Form a test packet and send it over IR transmitter
cmulady 13:17fd813ef7c9 204 irdatOUT[0] = 0xA5;
cmulady 13:17fd813ef7c9 205 irdatOUT[1] = ++irdata_out;
cmulady 13:17fd813ef7c9 206 irdatOUT[2] = ~(irdata_out*2);
cmulady 13:17fd813ef7c9 207 irdatOUT[3] = irdata_out*7+13;
cmulady 13:17fd813ef7c9 208 //MakePacket2(irdatOUT,4);
cmulady 16:bc0e213ba52d 209 ir_tx.MakePacket(irdatOUT,4);
cmulady 13:17fd813ef7c9 210
cmulady 13:17fd813ef7c9 211 //Roughly use to set rate of data packets per second
cmulady 18:98839f78fc01 212 Thread::wait(1000);
cmulady 13:17fd813ef7c9 213 }
cmulady 13:17fd813ef7c9 214 }
cmulady 13:17fd813ef7c9 215
cmulady 13:17fd813ef7c9 216
cmulady 13:17fd813ef7c9 217
cmulady 13:17fd813ef7c9 218
cmulady 12:9acca1dd0e8e 219 //Handle Reception of RX data (mail it to appropriate thread)
cmulady 14:68f7cefd7359 220 void ISR_UARTRX()
cmulady 7:c9ff6b5c8507 221 {
cmulady 12:9acca1dd0e8e 222 //get RX data (and prevent ISR from looping forever
cmulady 7:c9ff6b5c8507 223 uint32_t RBR = LPC_UART1->RBR;
cmulady 18:98839f78fc01 224 ir_tx.isr_rx(RBR);
cmulady 8:54dd4a3d0de9 225
cmulady 7:c9ff6b5c8507 226 }
cmulady 7:c9ff6b5c8507 227
cmulady 18:98839f78fc01 228 //Handle Transmission of TX data (mail it to appropriate thread)
cmulady 18:98839f78fc01 229 void ISR_UARTTX()
cmulady 18:98839f78fc01 230 {
cmulady 18:98839f78fc01 231 //get RX data (and prevent ISR from looping forever
cmulady 18:98839f78fc01 232 //uint32_t RBR = LPC_UART1->RBR;
cmulady 18:98839f78fc01 233
cmulady 18:98839f78fc01 234 char data = ir_tx.isr_tx();
cmulady 18:98839f78fc01 235 LPC_UART1->THR = (uint32_t)data;
cmulady 18:98839f78fc01 236 //LPC_UART1->THR = '?';
cmulady 18:98839f78fc01 237
cmulady 18:98839f78fc01 238 }
cmulady 18:98839f78fc01 239