Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
serial.cpp@2:4fb5a8d15f9c, 2016-12-02 (annotated)
- Committer:
- kieftea
- Date:
- Fri Dec 02 19:15:25 2016 +0000
- Revision:
- 2:4fb5a8d15f9c
- Child:
- 3:655f4fed48d7
Update
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| kieftea | 2:4fb5a8d15f9c | 1 | #include "mbed.h" |
| kieftea | 2:4fb5a8d15f9c | 2 | #include "rtos.h" |
| kieftea | 2:4fb5a8d15f9c | 3 | #include <string> |
| kieftea | 2:4fb5a8d15f9c | 4 | #include <sstream> |
| kieftea | 2:4fb5a8d15f9c | 5 | |
| kieftea | 2:4fb5a8d15f9c | 6 | DigitalOut led1(LED1); |
| kieftea | 2:4fb5a8d15f9c | 7 | DigitalOut led2(LED2); |
| kieftea | 2:4fb5a8d15f9c | 8 | Serial pc(USBTX, USBRX); |
| kieftea | 2:4fb5a8d15f9c | 9 | Thread *RX_THREAD_POINTER; |
| kieftea | 2:4fb5a8d15f9c | 10 | string test; |
| kieftea | 2:4fb5a8d15f9c | 11 | string* paramArray = new string[25]; |
| kieftea | 2:4fb5a8d15f9c | 12 | int index=0; |
| kieftea | 2:4fb5a8d15f9c | 13 | |
| kieftea | 2:4fb5a8d15f9c | 14 | // Rx Interupt routine |
| kieftea | 2:4fb5a8d15f9c | 15 | void Rx_interrupt(void){ |
| kieftea | 2:4fb5a8d15f9c | 16 | pc.attach(NULL); // Disable Rx interrupt |
| kieftea | 2:4fb5a8d15f9c | 17 | (*RX_THREAD_POINTER).signal_set(0x1); // dereference of RX_THREAD_POINTER |
| kieftea | 2:4fb5a8d15f9c | 18 | } |
| kieftea | 2:4fb5a8d15f9c | 19 | |
| kieftea | 2:4fb5a8d15f9c | 20 | |
| kieftea | 2:4fb5a8d15f9c | 21 | // Read received chars from UART |
| kieftea | 2:4fb5a8d15f9c | 22 | void rx_thread(void const *argument){ |
| kieftea | 2:4fb5a8d15f9c | 23 | while (true) { |
| kieftea | 2:4fb5a8d15f9c | 24 | // Signal flags that are reported as event are automatically cleared. |
| kieftea | 2:4fb5a8d15f9c | 25 | Thread::signal_wait(0x1); |
| kieftea | 2:4fb5a8d15f9c | 26 | while (pc.readable()) { |
| kieftea | 2:4fb5a8d15f9c | 27 | //pc.putc(pc.getc()); // read data from UART |
| kieftea | 2:4fb5a8d15f9c | 28 | //pc.printf("\r\nInput: \r\n"); |
| kieftea | 2:4fb5a8d15f9c | 29 | |
| kieftea | 2:4fb5a8d15f9c | 30 | stringstream ss; |
| kieftea | 2:4fb5a8d15f9c | 31 | char temp; |
| kieftea | 2:4fb5a8d15f9c | 32 | temp = pc.getc(); |
| kieftea | 2:4fb5a8d15f9c | 33 | //pc.putc(temp); |
| kieftea | 2:4fb5a8d15f9c | 34 | while (temp != '*'){ |
| kieftea | 2:4fb5a8d15f9c | 35 | ss << temp; |
| kieftea | 2:4fb5a8d15f9c | 36 | temp = pc.getc(); |
| kieftea | 2:4fb5a8d15f9c | 37 | //pc.putc(temp); |
| kieftea | 2:4fb5a8d15f9c | 38 | } |
| kieftea | 2:4fb5a8d15f9c | 39 | ss >> test; |
| kieftea | 2:4fb5a8d15f9c | 40 | if(index<24){ |
| kieftea | 2:4fb5a8d15f9c | 41 | //in_Array[i] = temp; |
| kieftea | 2:4fb5a8d15f9c | 42 | //pc.printf("%s\r\n", test); |
| kieftea | 2:4fb5a8d15f9c | 43 | //pc.printf("\r\n"); |
| kieftea | 2:4fb5a8d15f9c | 44 | led2=!led2; |
| kieftea | 2:4fb5a8d15f9c | 45 | paramArray[index++] = test; |
| kieftea | 2:4fb5a8d15f9c | 46 | } else { |
| kieftea | 2:4fb5a8d15f9c | 47 | //i = 0; |
| kieftea | 2:4fb5a8d15f9c | 48 | pc.attach(&Rx_interrupt); |
| kieftea | 2:4fb5a8d15f9c | 49 | //return; |
| kieftea | 2:4fb5a8d15f9c | 50 | } |
| kieftea | 2:4fb5a8d15f9c | 51 | } |
| kieftea | 2:4fb5a8d15f9c | 52 | pc.attach(&Rx_interrupt); // Enable Rx interrupt |
| kieftea | 2:4fb5a8d15f9c | 53 | } |
| kieftea | 2:4fb5a8d15f9c | 54 | } |
| kieftea | 2:4fb5a8d15f9c | 55 | |
| kieftea | 2:4fb5a8d15f9c | 56 | |
| kieftea | 2:4fb5a8d15f9c | 57 | string* recieve(){ |
| kieftea | 2:4fb5a8d15f9c | 58 | paramArray = new string[24]; |
| kieftea | 2:4fb5a8d15f9c | 59 | index=0; |
| kieftea | 2:4fb5a8d15f9c | 60 | Thread t_rx(rx_thread); |
| kieftea | 2:4fb5a8d15f9c | 61 | RX_THREAD_POINTER = &t_rx; // Set thread pointer as globally-accessible |
| kieftea | 2:4fb5a8d15f9c | 62 | t_rx.set_priority(osPriorityHigh); |
| kieftea | 2:4fb5a8d15f9c | 63 | pc.attach(Rx_interrupt); |
| kieftea | 2:4fb5a8d15f9c | 64 | //pc.printf("\r\nStart\r\n"); |
| kieftea | 2:4fb5a8d15f9c | 65 | while(test != "DONE"){ |
| kieftea | 2:4fb5a8d15f9c | 66 | led1=!led1; |
| kieftea | 2:4fb5a8d15f9c | 67 | //pc.printf("%s\r\n",test); |
| kieftea | 2:4fb5a8d15f9c | 68 | Thread::wait(1000); |
| kieftea | 2:4fb5a8d15f9c | 69 | } |
| kieftea | 2:4fb5a8d15f9c | 70 | |
| kieftea | 2:4fb5a8d15f9c | 71 | return paramArray; |
| kieftea | 2:4fb5a8d15f9c | 72 | } |
| kieftea | 2:4fb5a8d15f9c | 73 | |
| kieftea | 2:4fb5a8d15f9c | 74 | void send(string input){ |
| kieftea | 2:4fb5a8d15f9c | 75 | while(pc.writeable()){ |
| kieftea | 2:4fb5a8d15f9c | 76 | pc.printf("%s\r\n", input); |
| kieftea | 2:4fb5a8d15f9c | 77 | } |
| kieftea | 2:4fb5a8d15f9c | 78 | } |