asdf
Dependencies: NokiaLCD XMIT_IR mbed
Fork of 4180_mP_WirelessPong_revC by
main.cpp@19:5a4be4519de5, 2012-10-06 (annotated)
- Committer:
- cmulady
- Date:
- Sat Oct 06 17:45:32 2012 +0000
- Revision:
- 19:5a4be4519de5
- Parent:
- 18:98839f78fc01
- Child:
- 20:a74fe4d43ec0
good stopping point. irtx works well enough
Who changed what in which revision?
User | Revision | Line number | New 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 | 19:5a4be4519de5 | 6 | #define FPS 15 |
cmulady | 19:5a4be4519de5 | 7 | #define PKT_RATE 15 |
cmulady | 1:9ba884d85ac6 | 8 | |
cmulady | 3:8e492eacd346 | 9 | /**************************************** |
cmulady | 3:8e492eacd346 | 10 | |=======================================| |
cmulady | 3:8e492eacd346 | 11 | |MBED Connections: | |
cmulady | 3:8e492eacd346 | 12 | | -p5 : DIO on Sparkfun Nokia LCD | |
cmulady | 3:8e492eacd346 | 13 | | -p7 : CLK on Sparkfun Nokia LCD | |
cmulady | 3:8e492eacd346 | 14 | | -p8 : CS on Sparkfun Nokia LCD | |
cmulady | 3:8e492eacd346 | 15 | | -p9 : RST on Sparkfun Nokia LCD | |
cmulady | 12:9acca1dd0e8e | 16 | | -p21: CTL on Sparkfun IR Xmitter | |
cmulady | 12:9acca1dd0e8e | 17 | | -p14: OUT on Sparkfun IR Rcvr | |
cmulady | 12:9acca1dd0e8e | 18 | | -p13: GND on Sparkfun IR Xmitter | |
cmulady | 3:8e492eacd346 | 19 | |=======================================| |
cmulady | 3:8e492eacd346 | 20 | ****************************************/ |
cmulady | 1:9ba884d85ac6 | 21 | |
cmulady | 13:17fd813ef7c9 | 22 | //Function Prototypes |
cmulady | 13:17fd813ef7c9 | 23 | void BlinkAlive(void const* arguments); |
cmulady | 13:17fd813ef7c9 | 24 | void UpdateLCD(void const* arguments); |
cmulady | 13:17fd813ef7c9 | 25 | void IRStuff(void const* arguments); |
cmulady | 13:17fd813ef7c9 | 26 | void ISR_UARTRX(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 | 19:5a4be4519de5 | 42 | IRTransmitter ir_tx(p13,p21,p14,4,&ISR_UARTRX); //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 | 17:f92e4a384637 | 52 | Mutex data_update_mutex; |
cmulady | 1:9ba884d85ac6 | 53 | |
cmulady | 13:17fd813ef7c9 | 54 | |
cmulady | 1:9ba884d85ac6 | 55 | |
cmulady | 1:9ba884d85ac6 | 56 | |
cmulady | 1:9ba884d85ac6 | 57 | int main() |
cmulady | 1:9ba884d85ac6 | 58 | { |
cmulady | 0:c8ddcaa575ba | 59 | |
cmulady | 3:8e492eacd346 | 60 | //LCD init |
cmulady | 0:c8ddcaa575ba | 61 | lcd.background(0x000000); |
cmulady | 1:9ba884d85ac6 | 62 | |
cmulady | 6:5563f0026858 | 63 | //PC serial init |
cmulady | 6:5563f0026858 | 64 | pc.baud(19200); |
cmulady | 7:c9ff6b5c8507 | 65 | pc.printf("Starting...\n\n"); |
cmulady | 6:5563f0026858 | 66 | |
cmulady | 16:bc0e213ba52d | 67 | //IR_TRX module |
cmulady | 16:bc0e213ba52d | 68 | ir_tx.set_debug_port(&pc); |
cmulady | 16:bc0e213ba52d | 69 | |
cmulady | 13:17fd813ef7c9 | 70 | |
cmulady | 9:3e4e9d6a8ad8 | 71 | //Variable Init |
cmulady | 9:3e4e9d6a8ad8 | 72 | for(int i=0; i<10; i++) packet_buff[i]=0; |
cmulady | 9:3e4e9d6a8ad8 | 73 | |
cmulady | 12:9acca1dd0e8e | 74 | //Threads init |
cmulady | 1:9ba884d85ac6 | 75 | Thread thread_blinkalive(BlinkAlive); |
cmulady | 1:9ba884d85ac6 | 76 | Thread thread_updatelcd(UpdateLCD); |
cmulady | 3:8e492eacd346 | 77 | Thread thread_irstuff(IRStuff); |
cmulady | 8:54dd4a3d0de9 | 78 | threadptr_irstuff = &thread_irstuff; |
cmulady | 1:9ba884d85ac6 | 79 | |
cmulady | 1:9ba884d85ac6 | 80 | |
cmulady | 1:9ba884d85ac6 | 81 | |
cmulady | 0:c8ddcaa575ba | 82 | while(1) { |
cmulady | 13:17fd813ef7c9 | 83 | |
cmulady | 12:9acca1dd0e8e | 84 | //Use main loop to set LCD framerate |
cmulady | 1:9ba884d85ac6 | 85 | thread_updatelcd.signal_set(0x1); |
cmulady | 19:5a4be4519de5 | 86 | thread_blinkalive.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 | 19:5a4be4519de5 | 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 | 19:5a4be4519de5 | 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 | |
cmulady | 9:3e4e9d6a8ad8 | 166 | |
cmulady | 13:17fd813ef7c9 | 167 | void BlinkAlive(void const* arguments) |
cmulady | 13:17fd813ef7c9 | 168 | { |
cmulady | 13:17fd813ef7c9 | 169 | while(true) { |
cmulady | 13:17fd813ef7c9 | 170 | |
cmulady | 13:17fd813ef7c9 | 171 | //Change LED1 state (debug) |
cmulady | 13:17fd813ef7c9 | 172 | (led1==0.0)?led1=0.02:led1=0.0; |
cmulady | 13:17fd813ef7c9 | 173 | |
cmulady | 13:17fd813ef7c9 | 174 | //Form a test packet and send it over IR transmitter |
cmulady | 13:17fd813ef7c9 | 175 | irdatOUT[0] = 0xA5; |
cmulady | 13:17fd813ef7c9 | 176 | irdatOUT[1] = ++irdata_out; |
cmulady | 13:17fd813ef7c9 | 177 | irdatOUT[2] = ~(irdata_out*2); |
cmulady | 13:17fd813ef7c9 | 178 | irdatOUT[3] = irdata_out*7+13; |
cmulady | 16:bc0e213ba52d | 179 | ir_tx.MakePacket(irdatOUT,4); |
cmulady | 13:17fd813ef7c9 | 180 | |
cmulady | 13:17fd813ef7c9 | 181 | //Roughly use to set rate of data packets per second |
cmulady | 19:5a4be4519de5 | 182 | //Thread::wait(100); |
cmulady | 19:5a4be4519de5 | 183 | Thread::signal_wait(0x1); |
cmulady | 13:17fd813ef7c9 | 184 | } |
cmulady | 13:17fd813ef7c9 | 185 | } |
cmulady | 13:17fd813ef7c9 | 186 | |
cmulady | 13:17fd813ef7c9 | 187 | |
cmulady | 13:17fd813ef7c9 | 188 | |
cmulady | 13:17fd813ef7c9 | 189 | |
cmulady | 12:9acca1dd0e8e | 190 | //Handle Reception of RX data (mail it to appropriate thread) |
cmulady | 14:68f7cefd7359 | 191 | void ISR_UARTRX() |
cmulady | 7:c9ff6b5c8507 | 192 | { |
cmulady | 12:9acca1dd0e8e | 193 | //get RX data (and prevent ISR from looping forever |
cmulady | 7:c9ff6b5c8507 | 194 | uint32_t RBR = LPC_UART1->RBR; |
cmulady | 18:98839f78fc01 | 195 | ir_tx.isr_rx(RBR); |
cmulady | 8:54dd4a3d0de9 | 196 | |
cmulady | 7:c9ff6b5c8507 | 197 | } |
cmulady | 7:c9ff6b5c8507 | 198 | |
cmulady | 18:98839f78fc01 | 199 | |
cmulady | 18:98839f78fc01 | 200 |