proculus_display_pulga
Source/main.cpp@1:2e4f9cb1c3e9, 2020-04-27 (annotated)
- Committer:
- ruschigo
- Date:
- Mon Apr 27 17:40:57 2020 +0000
- Revision:
- 1:2e4f9cb1c3e9
- Child:
- 2:afae00ebf9ba
testing display with interrupt
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ruschigo | 1:2e4f9cb1c3e9 | 1 | #include "mbed.h" |
ruschigo | 1:2e4f9cb1c3e9 | 2 | #include "proculus_display.h" |
ruschigo | 1:2e4f9cb1c3e9 | 3 | |
ruschigo | 1:2e4f9cb1c3e9 | 4 | unsigned int sensor_open;//fim de curso fake |
ruschigo | 1:2e4f9cb1c3e9 | 5 | |
ruschigo | 1:2e4f9cb1c3e9 | 6 | #define SCREEN_CALIBRATING 0x14 |
ruschigo | 1:2e4f9cb1c3e9 | 7 | |
ruschigo | 1:2e4f9cb1c3e9 | 8 | uint16_t screen_id = 0x00; |
ruschigo | 1:2e4f9cb1c3e9 | 9 | |
ruschigo | 1:2e4f9cb1c3e9 | 10 | void serial_tx_irq(){ |
ruschigo | 1:2e4f9cb1c3e9 | 11 | debug_serial->puts("TX"); |
ruschigo | 1:2e4f9cb1c3e9 | 12 | } |
ruschigo | 1:2e4f9cb1c3e9 | 13 | |
ruschigo | 1:2e4f9cb1c3e9 | 14 | void serial_rx_irq(){ |
ruschigo | 1:2e4f9cb1c3e9 | 15 | int i = 0, j, k; |
ruschigo | 1:2e4f9cb1c3e9 | 16 | struct proculus_pkt pkt; |
ruschigo | 1:2e4f9cb1c3e9 | 17 | uint16_t data[255]; |
ruschigo | 1:2e4f9cb1c3e9 | 18 | char msg[100]; |
ruschigo | 1:2e4f9cb1c3e9 | 19 | //char rx_buffer[100]; |
ruschigo | 1:2e4f9cb1c3e9 | 20 | pkt.buffer = data; |
ruschigo | 1:2e4f9cb1c3e9 | 21 | int unknow = 0; |
ruschigo | 1:2e4f9cb1c3e9 | 22 | //wait(SERIAL_DELAY/1000); |
ruschigo | 1:2e4f9cb1c3e9 | 23 | sprintf(msg, "rx_to_rcv=%d",rx_to_rcv); |
ruschigo | 1:2e4f9cb1c3e9 | 24 | debug_serial->puts("_|_"); |
ruschigo | 1:2e4f9cb1c3e9 | 25 | debug_serial->puts(msg); |
ruschigo | 1:2e4f9cb1c3e9 | 26 | while(serial1.readable() || rx_to_rcv >0){ |
ruschigo | 1:2e4f9cb1c3e9 | 27 | //while(!serial1.readable()); |
ruschigo | 1:2e4f9cb1c3e9 | 28 | serial_rx_buffer[i] = serial1.getc(); |
ruschigo | 1:2e4f9cb1c3e9 | 29 | /*if(i == 0 && serial_rx_buffer[0] == 0x5a){//header packet start |
ruschigo | 1:2e4f9cb1c3e9 | 30 | i++; |
ruschigo | 1:2e4f9cb1c3e9 | 31 | rx_to_rcv--; |
ruschigo | 1:2e4f9cb1c3e9 | 32 | while(serial_rx_buffer[i] != 0xa5){ |
ruschigo | 1:2e4f9cb1c3e9 | 33 | if(serial1.readable()) |
ruschigo | 1:2e4f9cb1c3e9 | 34 | serial_rx_buffer[i] = serial1.getc(); |
ruschigo | 1:2e4f9cb1c3e9 | 35 | } |
ruschigo | 1:2e4f9cb1c3e9 | 36 | }*/ |
ruschigo | 1:2e4f9cb1c3e9 | 37 | i++; |
ruschigo | 1:2e4f9cb1c3e9 | 38 | /*THIS IS ALTERNATIVE TO SOLVE A BUG, BUT NOT FIX THE BUG |
ruschigo | 1:2e4f9cb1c3e9 | 39 | BUG: Uart interrupt is activates twice, then it read the header_h (5a) |
ruschigo | 1:2e4f9cb1c3e9 | 40 | close the interruption, open a new one starting from header_l (5a) |
ruschigo | 1:2e4f9cb1c3e9 | 41 | then nothing will work fine at all, creating the rx_to_rcv, i know how |
ruschigo | 1:2e4f9cb1c3e9 | 42 | many bytes should be read, then just stay here until it complete the |
ruschigo | 1:2e4f9cb1c3e9 | 43 | full packet*/ |
ruschigo | 1:2e4f9cb1c3e9 | 44 | rx_to_rcv --; |
ruschigo | 1:2e4f9cb1c3e9 | 45 | if(rx_to_rcv <= 0) |
ruschigo | 1:2e4f9cb1c3e9 | 46 | break; |
ruschigo | 1:2e4f9cb1c3e9 | 47 | } |
ruschigo | 1:2e4f9cb1c3e9 | 48 | #ifdef DEBUG_SERIAL |
ruschigo | 1:2e4f9cb1c3e9 | 49 | //debug_puts(rx_buffer, i);//print pure data to be read as hex |
ruschigo | 1:2e4f9cb1c3e9 | 50 | if(i>1){ |
ruschigo | 1:2e4f9cb1c3e9 | 51 | debug_serial->puts("_|_"); |
ruschigo | 1:2e4f9cb1c3e9 | 52 | raw_to_string(serial_rx_buffer, msg, i); |
ruschigo | 1:2e4f9cb1c3e9 | 53 | debug_serial->puts(msg); |
ruschigo | 1:2e4f9cb1c3e9 | 54 | debug_serial->puts("_|_"); |
ruschigo | 1:2e4f9cb1c3e9 | 55 | sprintf(msg, "i=%d",i); |
ruschigo | 1:2e4f9cb1c3e9 | 56 | debug_serial->puts(msg); |
ruschigo | 1:2e4f9cb1c3e9 | 57 | debug_serial->puts("_|_"); |
ruschigo | 1:2e4f9cb1c3e9 | 58 | } |
ruschigo | 1:2e4f9cb1c3e9 | 59 | |
ruschigo | 1:2e4f9cb1c3e9 | 60 | #endif |
ruschigo | 1:2e4f9cb1c3e9 | 61 | if(i>1 && rx_to_rcv == 0){ |
ruschigo | 1:2e4f9cb1c3e9 | 62 | if(serial_rx_buffer[3] >= 0x80 && serial_rx_buffer[3] <= 0x84) |
ruschigo | 1:2e4f9cb1c3e9 | 63 | //serial_to_proculus_pkt(&pkt); |
ruschigo | 1:2e4f9cb1c3e9 | 64 | { |
ruschigo | 1:2e4f9cb1c3e9 | 65 | j = 0; |
ruschigo | 1:2e4f9cb1c3e9 | 66 | k = 0; |
ruschigo | 1:2e4f9cb1c3e9 | 67 | pkt.header_h = serial_rx_buffer[0]; |
ruschigo | 1:2e4f9cb1c3e9 | 68 | pkt.header_l = serial_rx_buffer[1]; |
ruschigo | 1:2e4f9cb1c3e9 | 69 | pkt.count = serial_rx_buffer[2]; |
ruschigo | 1:2e4f9cb1c3e9 | 70 | pkt.cmd = serial_rx_buffer[3]; |
ruschigo | 1:2e4f9cb1c3e9 | 71 | if(pkt.cmd == R_CTRL_REG){ |
ruschigo | 1:2e4f9cb1c3e9 | 72 | pkt.address = serial_rx_buffer[4]; |
ruschigo | 1:2e4f9cb1c3e9 | 73 | pkt.lenght = serial_rx_buffer[5]; |
ruschigo | 1:2e4f9cb1c3e9 | 74 | while(j < pkt.lenght){ |
ruschigo | 1:2e4f9cb1c3e9 | 75 | pkt.buffer[i] = serial_rx_buffer[6 + j]; |
ruschigo | 1:2e4f9cb1c3e9 | 76 | j++; |
ruschigo | 1:2e4f9cb1c3e9 | 77 | } |
ruschigo | 1:2e4f9cb1c3e9 | 78 | } |
ruschigo | 1:2e4f9cb1c3e9 | 79 | else{ |
ruschigo | 1:2e4f9cb1c3e9 | 80 | pkt.address = serial_rx_buffer[4] << 8; |
ruschigo | 1:2e4f9cb1c3e9 | 81 | pkt.address |= serial_rx_buffer[5]; |
ruschigo | 1:2e4f9cb1c3e9 | 82 | pkt.lenght = serial_rx_buffer[6]; |
ruschigo | 1:2e4f9cb1c3e9 | 83 | while(j < pkt.lenght){ |
ruschigo | 1:2e4f9cb1c3e9 | 84 | pkt.buffer[j] = serial_rx_buffer[7 + k] << 8; |
ruschigo | 1:2e4f9cb1c3e9 | 85 | pkt.buffer[j] |= serial_rx_buffer[8 + k]; |
ruschigo | 1:2e4f9cb1c3e9 | 86 | j++; |
ruschigo | 1:2e4f9cb1c3e9 | 87 | k += 2; |
ruschigo | 1:2e4f9cb1c3e9 | 88 | } |
ruschigo | 1:2e4f9cb1c3e9 | 89 | } |
ruschigo | 1:2e4f9cb1c3e9 | 90 | unknow=0; |
ruschigo | 1:2e4f9cb1c3e9 | 91 | } |
ruschigo | 1:2e4f9cb1c3e9 | 92 | else |
ruschigo | 1:2e4f9cb1c3e9 | 93 | unknow = 1;//received something unknow |
ruschigo | 1:2e4f9cb1c3e9 | 94 | } |
ruschigo | 1:2e4f9cb1c3e9 | 95 | |
ruschigo | 1:2e4f9cb1c3e9 | 96 | if(unknow ==0){ |
ruschigo | 1:2e4f9cb1c3e9 | 97 | //sprintf(msg, "rx_buffer[0]=%x|i=%d", rx_buffer[0],i); |
ruschigo | 1:2e4f9cb1c3e9 | 98 | //debug_serial->puts(msg); |
ruschigo | 1:2e4f9cb1c3e9 | 99 | |
ruschigo | 1:2e4f9cb1c3e9 | 100 | if(serial_rx_buffer[3] == R_VP){ |
ruschigo | 1:2e4f9cb1c3e9 | 101 | //if(pkt.address == 0x32){ |
ruschigo | 1:2e4f9cb1c3e9 | 102 | //data[0] = data[0] + 1; |
ruschigo | 1:2e4f9cb1c3e9 | 103 | //proculus_set_vp(0x5, 10, pkt.buffer); |
ruschigo | 1:2e4f9cb1c3e9 | 104 | //} |
ruschigo | 1:2e4f9cb1c3e9 | 105 | } |
ruschigo | 1:2e4f9cb1c3e9 | 106 | if(pkt.cmd == R_CTRL_REG){// |
ruschigo | 1:2e4f9cb1c3e9 | 107 | //if(serial_rx_buffer[3] == R_CTRL_REG){//from a read control register comand |
ruschigo | 1:2e4f9cb1c3e9 | 108 | //sprintf(msg, "pkt.address=%x",pkt.address ); |
ruschigo | 1:2e4f9cb1c3e9 | 109 | //sprintf(msg, "serial_rx_buffer[4]=%x",serial_rx_buffer[4]); |
ruschigo | 1:2e4f9cb1c3e9 | 110 | //debug_serial->puts(msg); |
ruschigo | 1:2e4f9cb1c3e9 | 111 | //debug_serial->puts("_|_"); |
ruschigo | 1:2e4f9cb1c3e9 | 112 | if(pkt.address == PIC_ID_H){// |
ruschigo | 1:2e4f9cb1c3e9 | 113 | //if(serial_rx_buffer[4] == PIC_ID_H){//read screen cmd |
ruschigo | 1:2e4f9cb1c3e9 | 114 | //screen_id = (serial_rx_buffer[6] << 8 ); |
ruschigo | 1:2e4f9cb1c3e9 | 115 | //screen_id |= serial_rx_buffer[7]; |
ruschigo | 1:2e4f9cb1c3e9 | 116 | screen_id = (pkt.buffer[0] << 8 ); |
ruschigo | 1:2e4f9cb1c3e9 | 117 | screen_id |= pkt.buffer[1]; |
ruschigo | 1:2e4f9cb1c3e9 | 118 | sprintf(msg, "screen_id=%x",screen_id); |
ruschigo | 1:2e4f9cb1c3e9 | 119 | debug_serial->puts(msg); |
ruschigo | 1:2e4f9cb1c3e9 | 120 | debug_serial->puts("_|_"); |
ruschigo | 1:2e4f9cb1c3e9 | 121 | if(screen_id == SCREEN_CALIBRATING) |
ruschigo | 1:2e4f9cb1c3e9 | 122 | jump_to_screen(0x46); |
ruschigo | 1:2e4f9cb1c3e9 | 123 | else |
ruschigo | 1:2e4f9cb1c3e9 | 124 | jump_to_screen(0); |
ruschigo | 1:2e4f9cb1c3e9 | 125 | }else |
ruschigo | 1:2e4f9cb1c3e9 | 126 | jump_to_screen(96); |
ruschigo | 1:2e4f9cb1c3e9 | 127 | } |
ruschigo | 1:2e4f9cb1c3e9 | 128 | } |
ruschigo | 1:2e4f9cb1c3e9 | 129 | else{ |
ruschigo | 1:2e4f9cb1c3e9 | 130 | debug_serial->puts("unknowPKT"); |
ruschigo | 1:2e4f9cb1c3e9 | 131 | debug_serial->puts("_|_"); |
ruschigo | 1:2e4f9cb1c3e9 | 132 | } |
ruschigo | 1:2e4f9cb1c3e9 | 133 | //sprintf(msg, "pkt.address=%x", serial_rx_buffer[1]); |
ruschigo | 1:2e4f9cb1c3e9 | 134 | //strcpy(serial_rx_buffer, "init"); |
ruschigo | 1:2e4f9cb1c3e9 | 135 | //debug_serial->puts(msg); |
ruschigo | 1:2e4f9cb1c3e9 | 136 | |
ruschigo | 1:2e4f9cb1c3e9 | 137 | } |
ruschigo | 1:2e4f9cb1c3e9 | 138 | |
ruschigo | 1:2e4f9cb1c3e9 | 139 | |
ruschigo | 1:2e4f9cb1c3e9 | 140 | //stop all measures, and stop any action |
ruschigo | 1:2e4f9cb1c3e9 | 141 | int system_stop(){ |
ruschigo | 1:2e4f9cb1c3e9 | 142 | return 0; |
ruschigo | 1:2e4f9cb1c3e9 | 143 | } |
ruschigo | 1:2e4f9cb1c3e9 | 144 | |
ruschigo | 1:2e4f9cb1c3e9 | 145 | int get_screen_at_irq(){ |
ruschigo | 1:2e4f9cb1c3e9 | 146 | int i = 0; |
ruschigo | 1:2e4f9cb1c3e9 | 147 | strcpy(serial_rx_buffer, ""); |
ruschigo | 1:2e4f9cb1c3e9 | 148 | while(serial1.readable()){//clean anything in buffer before send cmd |
ruschigo | 1:2e4f9cb1c3e9 | 149 | serial_rx_buffer[i] = serial1.getc(); |
ruschigo | 1:2e4f9cb1c3e9 | 150 | i++; |
ruschigo | 1:2e4f9cb1c3e9 | 151 | } |
ruschigo | 1:2e4f9cb1c3e9 | 152 | //serial1.attach(0); |
ruschigo | 1:2e4f9cb1c3e9 | 153 | while(rx_to_rcv); |
ruschigo | 1:2e4f9cb1c3e9 | 154 | proculus_get_ctrl(0x3, PIC_ID_H, 0x2); |
ruschigo | 1:2e4f9cb1c3e9 | 155 | //serial1.attach(&serial_rx_irq); |
ruschigo | 1:2e4f9cb1c3e9 | 156 | return 0; |
ruschigo | 1:2e4f9cb1c3e9 | 157 | } |
ruschigo | 1:2e4f9cb1c3e9 | 158 | |
ruschigo | 1:2e4f9cb1c3e9 | 159 | int main() |
ruschigo | 1:2e4f9cb1c3e9 | 160 | { |
ruschigo | 1:2e4f9cb1c3e9 | 161 | //simulate sensor "fim de curso" |
ruschigo | 1:2e4f9cb1c3e9 | 162 | sensor_open = 0; |
ruschigo | 1:2e4f9cb1c3e9 | 163 | |
ruschigo | 1:2e4f9cb1c3e9 | 164 | uint16_t data[100];//buffer to store vp datas |
ruschigo | 1:2e4f9cb1c3e9 | 165 | uint16_t ctrl_data[2];//buffer to store ctrl_reg datas |
ruschigo | 1:2e4f9cb1c3e9 | 166 | int i = 0x00, j=0; |
ruschigo | 1:2e4f9cb1c3e9 | 167 | struct proculus_pkt pkt; |
ruschigo | 1:2e4f9cb1c3e9 | 168 | //setup the device rx irq handler |
ruschigo | 1:2e4f9cb1c3e9 | 169 | serial1.attach(&serial_rx_irq, RawSerial::RxIrq); |
ruschigo | 1:2e4f9cb1c3e9 | 170 | //serial1->attach(&serial_tx_irq, RawSerial::TxIrq); |
ruschigo | 1:2e4f9cb1c3e9 | 171 | //serial1->attach(&serial_rx_irq); |
ruschigo | 1:2e4f9cb1c3e9 | 172 | //setup the baud rate to match with display |
ruschigo | 1:2e4f9cb1c3e9 | 173 | serial1.baud(115200); |
ruschigo | 1:2e4f9cb1c3e9 | 174 | #ifdef DEBUG_SERIAL |
ruschigo | 1:2e4f9cb1c3e9 | 175 | debug_serial->baud(115200); |
ruschigo | 1:2e4f9cb1c3e9 | 176 | debug_serial->puts("start"); |
ruschigo | 1:2e4f9cb1c3e9 | 177 | #endif |
ruschigo | 1:2e4f9cb1c3e9 | 178 | wait(3); |
ruschigo | 1:2e4f9cb1c3e9 | 179 | sensor_open = 0; |
ruschigo | 1:2e4f9cb1c3e9 | 180 | while(1) { |
ruschigo | 1:2e4f9cb1c3e9 | 181 | |
ruschigo | 1:2e4f9cb1c3e9 | 182 | if(!sensor_open){//are the device secure to operate? |
ruschigo | 1:2e4f9cb1c3e9 | 183 | get_screen_at_irq(); |
ruschigo | 1:2e4f9cb1c3e9 | 184 | wait(3); |
ruschigo | 1:2e4f9cb1c3e9 | 185 | }else{ |
ruschigo | 1:2e4f9cb1c3e9 | 186 | system_stop(); |
ruschigo | 1:2e4f9cb1c3e9 | 187 | jump_to_screen(SCREEN_CALIBRATING); |
ruschigo | 1:2e4f9cb1c3e9 | 188 | #ifdef DEBUG_CONSOLE |
ruschigo | 1:2e4f9cb1c3e9 | 189 | debug_serial->puts("Check if the device is properly closed"); |
ruschigo | 1:2e4f9cb1c3e9 | 190 | #endif |
ruschigo | 1:2e4f9cb1c3e9 | 191 | } |
ruschigo | 1:2e4f9cb1c3e9 | 192 | //if(sensor_open != 0)//simulates that the door is open for 3 seconds |
ruschigo | 1:2e4f9cb1c3e9 | 193 | //{ |
ruschigo | 1:2e4f9cb1c3e9 | 194 | // sensor_open--; |
ruschigo | 1:2e4f9cb1c3e9 | 195 | // wait(1); |
ruschigo | 1:2e4f9cb1c3e9 | 196 | //} |
ruschigo | 1:2e4f9cb1c3e9 | 197 | } |
ruschigo | 1:2e4f9cb1c3e9 | 198 | } |