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
MySoftSerial.cpp@19:822d93e0bf34, 2017-05-10 (annotated)
- Committer:
- Thomas Cauwelier
- Date:
- Wed May 10 18:21:14 2017 +0200
- Revision:
- 19:822d93e0bf34
- Parent:
- 17:8683abdc1912
- Child:
- 20:d2a8daeb6eb7
add dc correction
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Thomas Cauwelier |
15:75f9bd5d7659 | 1 | // |
| Thomas Cauwelier |
15:75f9bd5d7659 | 2 | // Created by thoma on 10-May-17. |
| Thomas Cauwelier |
15:75f9bd5d7659 | 3 | // |
| Thomas Cauwelier |
15:75f9bd5d7659 | 4 | |
| Thomas Cauwelier |
15:75f9bd5d7659 | 5 | #include "MySoftSerial.h" |
| Thomas Cauwelier |
17:8683abdc1912 | 6 | #include "Manchester.h" |
| Thomas Cauwelier |
15:75f9bd5d7659 | 7 | |
| Desertification | 16:1a0589d846bf | 8 | MySoftSerial::MySoftSerial(PinName TX, PinName RX, const char *name) : SoftSerial(TX, RX, name) { |
| Thomas Cauwelier |
17:8683abdc1912 | 9 | format(16, SoftSerial::None, 1); |
| Thomas Cauwelier |
19:822d93e0bf34 | 10 | timer.reset(); |
| Thomas Cauwelier |
19:822d93e0bf34 | 11 | timer.start(); |
| Thomas Cauwelier |
15:75f9bd5d7659 | 12 | } |
| Thomas Cauwelier |
15:75f9bd5d7659 | 13 | |
| Thomas Cauwelier |
15:75f9bd5d7659 | 14 | MySoftSerial::~MySoftSerial() { |
| Thomas Cauwelier |
15:75f9bd5d7659 | 15 | |
| Thomas Cauwelier |
15:75f9bd5d7659 | 16 | } |
| Thomas Cauwelier |
15:75f9bd5d7659 | 17 | |
| Thomas Cauwelier |
15:75f9bd5d7659 | 18 | int MySoftSerial::putc(int c) { |
| Thomas Cauwelier |
17:8683abdc1912 | 19 | c = Manchester::encode(c); |
| Thomas Cauwelier |
19:822d93e0bf34 | 20 | timer.stop(); |
| Thomas Cauwelier |
19:822d93e0bf34 | 21 | if(timer.read_us() > max_time_between_transmission_us){ |
| Thomas Cauwelier |
19:822d93e0bf34 | 22 | correct_dc_offset(); |
| Thomas Cauwelier |
19:822d93e0bf34 | 23 | } |
| Thomas Cauwelier |
15:75f9bd5d7659 | 24 | return _putc(c); |
| Thomas Cauwelier |
15:75f9bd5d7659 | 25 | } |
| Thomas Cauwelier |
15:75f9bd5d7659 | 26 | |
| Thomas Cauwelier |
15:75f9bd5d7659 | 27 | int MySoftSerial::getc() { |
| Thomas Cauwelier |
19:822d93e0bf34 | 28 | int character = _getc(); |
| Thomas Cauwelier |
19:822d93e0bf34 | 29 | timer.reset(); |
| Thomas Cauwelier |
19:822d93e0bf34 | 30 | timer.start(); |
| Thomas Cauwelier |
19:822d93e0bf34 | 31 | return Manchester::decode(character); |
| Thomas Cauwelier |
17:8683abdc1912 | 32 | } |
| Thomas Cauwelier |
17:8683abdc1912 | 33 | |
| Thomas Cauwelier |
17:8683abdc1912 | 34 | void MySoftSerial::prepare_tx(int c) { |
| Thomas Cauwelier |
17:8683abdc1912 | 35 | |
| Thomas Cauwelier |
17:8683abdc1912 | 36 | SoftSerial::prepare_tx(c); |
| Thomas Cauwelier |
15:75f9bd5d7659 | 37 | } |
| Thomas Cauwelier |
17:8683abdc1912 | 38 | |
| Thomas Cauwelier |
17:8683abdc1912 | 39 | void MySoftSerial::baud(int baudrate) { |
| Thomas Cauwelier |
17:8683abdc1912 | 40 | SoftSerial::baud(baudrate); |
| Thomas Cauwelier |
17:8683abdc1912 | 41 | } |
| Thomas Cauwelier |
17:8683abdc1912 | 42 | |
| Thomas Cauwelier |
17:8683abdc1912 | 43 | int MySoftSerial::readable() { |
| Thomas Cauwelier |
17:8683abdc1912 | 44 | return SoftSerial::readable(); |
| Thomas Cauwelier |
17:8683abdc1912 | 45 | } |
| Thomas Cauwelier |
17:8683abdc1912 | 46 | |
| Thomas Cauwelier |
17:8683abdc1912 | 47 | int MySoftSerial::writeable() { |
| Thomas Cauwelier |
17:8683abdc1912 | 48 | return SoftSerial::writeable(); |
| Thomas Cauwelier |
17:8683abdc1912 | 49 | } |
| Thomas Cauwelier |
19:822d93e0bf34 | 50 | |
| Thomas Cauwelier |
19:822d93e0bf34 | 51 | void MySoftSerial::correct_dc_offset() { |
| Thomas Cauwelier |
19:822d93e0bf34 | 52 | for (int i = 0; i < 1; ++i) { |
| Thomas Cauwelier |
19:822d93e0bf34 | 53 | tx->write(0); |
| Thomas Cauwelier |
19:822d93e0bf34 | 54 | wait_us(bit_period * 12); // send 12 zeros |
| Thomas Cauwelier |
19:822d93e0bf34 | 55 | tx->write(1); |
| Thomas Cauwelier |
19:822d93e0bf34 | 56 | wait_us(bit_period * 4); // send 4 ones |
| Thomas Cauwelier |
19:822d93e0bf34 | 57 | } |
| Thomas Cauwelier |
19:822d93e0bf34 | 58 | } |