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.
Dependencies: SoftSerial SDFileSystem mbed wave_player
main.cpp@17:8683abdc1912, 2017-05-10 (annotated)
- Committer:
- Thomas Cauwelier
- Date:
- Wed May 10 17:31:22 2017 +0200
- Revision:
- 17:8683abdc1912
- Parent:
- 16:1a0589d846bf
- Child:
- 18:3aeaa22b265d
add single char encode decode
encode and decode in MySoftSerial
set format in constructor
hide format method
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Desertification | 1:6b839581dc9f | 1 | |
Desertification | 1:6b839581dc9f | 2 | #include "mbed.h" |
Thomas Cauwelier |
6:0e1973fdfe28 | 3 | #include "rtos.h" |
Thomas Cauwelier |
9:d95984ce5de9 | 4 | #include "Manchester.h" |
Thomas Cauwelier |
12:a070de571ea1 | 5 | #include "SoftSerial.h" |
Thomas Cauwelier |
15:75f9bd5d7659 | 6 | #include "MySoftSerial.h" |
Thomas Cauwelier |
6:0e1973fdfe28 | 7 | |
Thomas Cauwelier |
12:a070de571ea1 | 8 | //Serial out(p9, p10, 3200); //p9 |
Thomas Cauwelier |
12:a070de571ea1 | 9 | //Serial in(p13, p14, 3200); //p14 |
Thomas Cauwelier |
12:a070de571ea1 | 10 | //Serial usb(USBTX,USBRX); |
Thomas Cauwelier |
12:a070de571ea1 | 11 | // |
Thomas Cauwelier |
12:a070de571ea1 | 12 | //char *read_2_bytes(Serial *serial, char out[2]) { |
Thomas Cauwelier |
12:a070de571ea1 | 13 | // int i = 0; |
Thomas Cauwelier |
12:a070de571ea1 | 14 | // while (true) { |
Thomas Cauwelier |
12:a070de571ea1 | 15 | // while (!serial->readable()); |
Thomas Cauwelier |
12:a070de571ea1 | 16 | // char c = (char) serial->getc(); |
Thomas Cauwelier |
12:a070de571ea1 | 17 | // if (c == 0b11100011) //no data flag |
Thomas Cauwelier |
12:a070de571ea1 | 18 | // continue; |
Thomas Cauwelier |
12:a070de571ea1 | 19 | // else { |
Thomas Cauwelier |
12:a070de571ea1 | 20 | // out[i] = c; |
Thomas Cauwelier |
12:a070de571ea1 | 21 | // i++; |
Thomas Cauwelier |
12:a070de571ea1 | 22 | // } |
Thomas Cauwelier |
12:a070de571ea1 | 23 | // |
Thomas Cauwelier |
12:a070de571ea1 | 24 | // if (i == 2) { |
Thomas Cauwelier |
12:a070de571ea1 | 25 | // break; |
Thomas Cauwelier |
12:a070de571ea1 | 26 | // } |
Thomas Cauwelier |
12:a070de571ea1 | 27 | // } |
Thomas Cauwelier |
12:a070de571ea1 | 28 | // return out; |
Thomas Cauwelier |
12:a070de571ea1 | 29 | //} |
Thomas Cauwelier |
12:a070de571ea1 | 30 | // |
Thomas Cauwelier |
12:a070de571ea1 | 31 | // |
Thomas Cauwelier |
12:a070de571ea1 | 32 | //void receive_uart() { |
Thomas Cauwelier |
12:a070de571ea1 | 33 | // while (true) { |
Thomas Cauwelier |
12:a070de571ea1 | 34 | // if (in.readable()) { |
Thomas Cauwelier |
12:a070de571ea1 | 35 | // char s[2]; |
Thomas Cauwelier |
12:a070de571ea1 | 36 | // read_2_bytes(&in, s); |
Thomas Cauwelier |
12:a070de571ea1 | 37 | // char dec[1]; |
Thomas Cauwelier |
12:a070de571ea1 | 38 | // bool success = Manchester::decode_manchester(s, 2, dec); |
Thomas Cauwelier |
12:a070de571ea1 | 39 | // printf(dec); |
Thomas Cauwelier |
12:a070de571ea1 | 40 | // if (!success) { |
Thomas Cauwelier |
12:a070de571ea1 | 41 | // printf("#"); |
Thomas Cauwelier |
12:a070de571ea1 | 42 | // } |
Thomas Cauwelier |
12:a070de571ea1 | 43 | // } else { |
Thomas Cauwelier |
12:a070de571ea1 | 44 | // Thread::yield(); |
Thomas Cauwelier |
12:a070de571ea1 | 45 | // } |
Thomas Cauwelier |
12:a070de571ea1 | 46 | // } |
Thomas Cauwelier |
12:a070de571ea1 | 47 | //} |
Thomas Cauwelier |
12:a070de571ea1 | 48 | // |
Thomas Cauwelier |
12:a070de571ea1 | 49 | //void send_uart(Serial *serial, const char *s, int size) { |
Thomas Cauwelier |
12:a070de571ea1 | 50 | // int size_m = size * 2; |
Thomas Cauwelier |
12:a070de571ea1 | 51 | // char encoded[size_m]; |
Thomas Cauwelier |
12:a070de571ea1 | 52 | // Manchester::encode_manchester(s, size, encoded); |
Thomas Cauwelier |
12:a070de571ea1 | 53 | // |
Thomas Cauwelier |
12:a070de571ea1 | 54 | // for (int i = 0; i < size_m; ++i) { |
Thomas Cauwelier |
12:a070de571ea1 | 55 | // while (!serial->writeable()); |
Thomas Cauwelier |
12:a070de571ea1 | 56 | // serial->putc(encoded[i]); |
Thomas Cauwelier |
12:a070de571ea1 | 57 | // } |
Thomas Cauwelier |
12:a070de571ea1 | 58 | //} |
Thomas Cauwelier |
12:a070de571ea1 | 59 | // |
Thomas Cauwelier |
12:a070de571ea1 | 60 | //void hardware_uart_test(){ |
Thomas Cauwelier |
12:a070de571ea1 | 61 | // |
Thomas Cauwelier |
12:a070de571ea1 | 62 | // |
Thomas Cauwelier |
12:a070de571ea1 | 63 | // printf("%s\r\n", "hello, term!"); |
Thomas Cauwelier |
12:a070de571ea1 | 64 | // |
Thomas Cauwelier |
12:a070de571ea1 | 65 | // Thread thread; |
Thomas Cauwelier |
12:a070de571ea1 | 66 | // thread.start(receive_uart); |
Thomas Cauwelier |
12:a070de571ea1 | 67 | // |
Thomas Cauwelier |
12:a070de571ea1 | 68 | // while (true) { |
Thomas Cauwelier |
12:a070de571ea1 | 69 | // char tosend[28]; |
Thomas Cauwelier |
12:a070de571ea1 | 70 | // Manchester::encode_manchester("hello, term\r\n", 14, tosend); |
Thomas Cauwelier |
12:a070de571ea1 | 71 | // send_uart(&out, tosend, 28); |
Thomas Cauwelier |
12:a070de571ea1 | 72 | // //wait(1); |
Thomas Cauwelier |
12:a070de571ea1 | 73 | // } |
Thomas Cauwelier |
12:a070de571ea1 | 74 | //} |
Desertification | 1:6b839581dc9f | 75 | |
Thomas Cauwelier |
13:c111ea1c5990 | 76 | void client(){ |
Thomas Cauwelier |
15:75f9bd5d7659 | 77 | MySoftSerial s(p18,p17); |
Desertification | 16:1a0589d846bf | 78 | s.baud(20000); |
Thomas Cauwelier |
17:8683abdc1912 | 79 | //s.format(16, SoftSerial::None, 1); |
Thomas Cauwelier |
13:c111ea1c5990 | 80 | while (true) { |
Thomas Cauwelier |
13:c111ea1c5990 | 81 | while (!s.readable()); |
Desertification | 14:e29cca7e64b2 | 82 | printf("got %i\r\n",s.getc()); |
Thomas Cauwelier |
13:c111ea1c5990 | 83 | } |
Thomas Cauwelier |
13:c111ea1c5990 | 84 | } |
Thomas Cauwelier |
13:c111ea1c5990 | 85 | |
Thomas Cauwelier |
13:c111ea1c5990 | 86 | void relay(){ |
Desertification | 16:1a0589d846bf | 87 | MySoftSerial s(p17,p18); |
Desertification | 16:1a0589d846bf | 88 | s.baud(20000); |
Thomas Cauwelier |
17:8683abdc1912 | 89 | //s.format(16, SoftSerial::None, 1); |
Desertification | 14:e29cca7e64b2 | 90 | int i = 0; |
Thomas Cauwelier |
13:c111ea1c5990 | 91 | while (true){ |
Desertification | 14:e29cca7e64b2 | 92 | wait(0.1); |
Desertification | 16:1a0589d846bf | 93 | for (int j = 0; j < 50; ++j) { |
Desertification | 16:1a0589d846bf | 94 | while(!s.writeable()); |
Desertification | 16:1a0589d846bf | 95 | //printf("put %i\r\n", 0b0101010100110011); |
Desertification | 16:1a0589d846bf | 96 | //s.putc(0b0101010100110011); |
Desertification | 16:1a0589d846bf | 97 | printf("put %i\r\n", i); |
Desertification | 16:1a0589d846bf | 98 | //s.putc(0b0000000000000000); |
Desertification | 16:1a0589d846bf | 99 | s.putc(0b0000000000001111); |
Desertification | 16:1a0589d846bf | 100 | i++; |
Desertification | 16:1a0589d846bf | 101 | } |
Thomas Cauwelier |
13:c111ea1c5990 | 102 | } |
Thomas Cauwelier |
13:c111ea1c5990 | 103 | } |
Thomas Cauwelier |
13:c111ea1c5990 | 104 | |
Thomas Cauwelier |
12:a070de571ea1 | 105 | #define RELAY 1 |
Thomas Cauwelier |
11:e462ce7a42b9 | 106 | |
Thomas Cauwelier |
13:c111ea1c5990 | 107 | Serial usb(USBTX,USBRX); |
Thomas Cauwelier |
13:c111ea1c5990 | 108 | |
Desertification | 1:6b839581dc9f | 109 | int main() { |
Thomas Cauwelier |
13:c111ea1c5990 | 110 | usb.baud(115200); |
Thomas Cauwelier |
12:a070de571ea1 | 111 | if (RELAY){ |
Thomas Cauwelier |
13:c111ea1c5990 | 112 | printf("relay\r\n"); |
Thomas Cauwelier |
13:c111ea1c5990 | 113 | relay(); |
Thomas Cauwelier |
12:a070de571ea1 | 114 | } else { |
Thomas Cauwelier |
13:c111ea1c5990 | 115 | printf("client\r\n"); |
Thomas Cauwelier |
13:c111ea1c5990 | 116 | client(); |
Thomas Cauwelier |
6:0e1973fdfe28 | 117 | } |
thoma@THOMAS-CAUWELIER.khbo.be | 4:a07612fbf279 | 118 | } |