BufferedSoftSerials
Dependencies: Buffer SoftSerial1
Fork of BufferedSoftSerial by
BufferedSoftSerial.h@9:5b069a1896f9, 2014-07-05 (annotated)
- Committer:
- Sissors
- Date:
- Sat Jul 05 08:08:07 2014 +0000
- Revision:
- 9:5b069a1896f9
- Parent:
- 7:58d0adeb95aa
Changed destructor
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sissors | 6:4e3e18b41ea6 | 1 | |
Sissors | 6:4e3e18b41ea6 | 2 | /** |
Sissors | 6:4e3e18b41ea6 | 3 | * @file BufferedSoftSerial.h |
Sissors | 6:4e3e18b41ea6 | 4 | * @brief Software Buffer - Extends mbed Serial functionallity adding irq driven TX and RX |
Sissors | 6:4e3e18b41ea6 | 5 | * @author sam grove |
Sissors | 6:4e3e18b41ea6 | 6 | * @version 1.0 |
Sissors | 6:4e3e18b41ea6 | 7 | * @see |
Sissors | 6:4e3e18b41ea6 | 8 | * |
Sissors | 6:4e3e18b41ea6 | 9 | * Copyright (c) 2013 |
Sissors | 6:4e3e18b41ea6 | 10 | * |
Sissors | 6:4e3e18b41ea6 | 11 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Sissors | 6:4e3e18b41ea6 | 12 | * you may not use this file except in compliance with the License. |
Sissors | 6:4e3e18b41ea6 | 13 | * You may obtain a copy of the License at |
Sissors | 6:4e3e18b41ea6 | 14 | * |
Sissors | 6:4e3e18b41ea6 | 15 | * http://www.apache.org/licenses/LICENSE-2.0 |
Sissors | 6:4e3e18b41ea6 | 16 | * |
Sissors | 6:4e3e18b41ea6 | 17 | * Unless required by applicable law or agreed to in writing, software |
Sissors | 6:4e3e18b41ea6 | 18 | * distributed under the License is distributed on an "AS IS" BASIS, |
Sissors | 6:4e3e18b41ea6 | 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Sissors | 6:4e3e18b41ea6 | 20 | * See the License for the specific language governing permissions and |
Sissors | 6:4e3e18b41ea6 | 21 | * limitations under the License. |
Sissors | 6:4e3e18b41ea6 | 22 | */ |
Sissors | 6:4e3e18b41ea6 | 23 | |
Sissors | 6:4e3e18b41ea6 | 24 | #ifndef BUFFEREDSOFTSERIAL_H |
Sissors | 6:4e3e18b41ea6 | 25 | #define BUFFEREDSOFTSERIAL_H |
Sissors | 6:4e3e18b41ea6 | 26 | |
Sissors | 6:4e3e18b41ea6 | 27 | #include "mbed.h" |
Sissors | 6:4e3e18b41ea6 | 28 | #include "Buffer.h" |
Sissors | 6:4e3e18b41ea6 | 29 | #include "SoftSerial.h" |
Sissors | 6:4e3e18b41ea6 | 30 | |
Sissors | 6:4e3e18b41ea6 | 31 | /** A serial port (UART) for communication with other serial devices |
Sissors | 6:4e3e18b41ea6 | 32 | * |
Sissors | 6:4e3e18b41ea6 | 33 | * Can be used for Full Duplex communication, or Simplex by specifying |
Sissors | 6:4e3e18b41ea6 | 34 | * one pin as NC (Not Connected) |
Sissors | 6:4e3e18b41ea6 | 35 | * |
Sissors | 6:4e3e18b41ea6 | 36 | * This uses software serial emulation, regular serial pins are alot better, |
Sissors | 6:4e3e18b41ea6 | 37 | * however if you don't have spare ones, you can use this. It is advicable |
Sissors | 6:4e3e18b41ea6 | 38 | * to put the serial connection with highest speed to hardware serial. |
Sissors | 6:4e3e18b41ea6 | 39 | * |
Sissors | 6:4e3e18b41ea6 | 40 | * If you lack RAM memory you can also use SoftSerial without this buffer around it. |
Sissors | 6:4e3e18b41ea6 | 41 | * In that case it is fully blocking. |
Sissors | 6:4e3e18b41ea6 | 42 | * |
Sissors | 6:4e3e18b41ea6 | 43 | * Example: |
Sissors | 6:4e3e18b41ea6 | 44 | * @code |
Sissors | 6:4e3e18b41ea6 | 45 | * #include "mbed.h" |
Sissors | 6:4e3e18b41ea6 | 46 | * #include "BufferedSoftSerial.h" |
Sissors | 6:4e3e18b41ea6 | 47 | * |
Sissors | 6:4e3e18b41ea6 | 48 | * SoftSerial block(USBTX, USBRX); |
Sissors | 7:58d0adeb95aa | 49 | * BufferedSoftSerial buf(USBTX, USBRX); |
Sissors | 6:4e3e18b41ea6 | 50 | * |
Sissors | 6:4e3e18b41ea6 | 51 | * int main() |
Sissors | 6:4e3e18b41ea6 | 52 | * { |
Sissors | 6:4e3e18b41ea6 | 53 | * while(1) { |
Sissors | 6:4e3e18b41ea6 | 54 | * Timer s; |
Sissors | 6:4e3e18b41ea6 | 55 | * |
Sissors | 6:4e3e18b41ea6 | 56 | * s.start(); |
Sissors | 6:4e3e18b41ea6 | 57 | * buf.printf("Hello World - buffered\r\n"); |
Sissors | 6:4e3e18b41ea6 | 58 | * int buffered_time = s.read_us(); |
Sissors | 6:4e3e18b41ea6 | 59 | * wait(0.1f); // give time for the buffer to empty |
Sissors | 6:4e3e18b41ea6 | 60 | * |
Sissors | 6:4e3e18b41ea6 | 61 | * s.reset(); |
Sissors | 6:4e3e18b41ea6 | 62 | * block.printf("Hello World - blocking\r\n"); |
Sissors | 6:4e3e18b41ea6 | 63 | * int polled_time = s.read_us(); |
Sissors | 6:4e3e18b41ea6 | 64 | * s.stop(); |
Sissors | 6:4e3e18b41ea6 | 65 | * wait(0.1f); // give time for the buffer to empty |
Sissors | 6:4e3e18b41ea6 | 66 | * |
Sissors | 6:4e3e18b41ea6 | 67 | * buf.printf("printf buffered took %d us\r\n", buffered_time); |
Sissors | 6:4e3e18b41ea6 | 68 | * buf.printf("printf blocking took %d us\r\n", polled_time); |
Sissors | 6:4e3e18b41ea6 | 69 | * wait(5); |
Sissors | 6:4e3e18b41ea6 | 70 | * } |
Sissors | 6:4e3e18b41ea6 | 71 | * } |
Sissors | 6:4e3e18b41ea6 | 72 | * @endcode |
Sissors | 6:4e3e18b41ea6 | 73 | */ |
Sissors | 6:4e3e18b41ea6 | 74 | |
Sissors | 6:4e3e18b41ea6 | 75 | /** |
Sissors | 6:4e3e18b41ea6 | 76 | * @class BufferedSerial |
Sissors | 6:4e3e18b41ea6 | 77 | * @brief Software buffers and interrupt driven tx and rx for SoftSerial |
Sissors | 6:4e3e18b41ea6 | 78 | */ |
Sissors | 6:4e3e18b41ea6 | 79 | class BufferedSoftSerial : public SoftSerial |
Sissors | 6:4e3e18b41ea6 | 80 | { |
Sissors | 6:4e3e18b41ea6 | 81 | private: |
Sissors | 6:4e3e18b41ea6 | 82 | Buffer <char> _rxbuf; |
Sissors | 6:4e3e18b41ea6 | 83 | Buffer <char> _txbuf; |
Sissors | 6:4e3e18b41ea6 | 84 | |
Sissors | 6:4e3e18b41ea6 | 85 | void rxIrq(void); |
Sissors | 6:4e3e18b41ea6 | 86 | void txIrq(void); |
Sissors | 6:4e3e18b41ea6 | 87 | void prime(void); |
Sissors | 6:4e3e18b41ea6 | 88 | |
Sissors | 6:4e3e18b41ea6 | 89 | public: |
Sissors | 6:4e3e18b41ea6 | 90 | /** Create a BufferedSoftSerial port, connected to the specified transmit and receive pins |
Sissors | 6:4e3e18b41ea6 | 91 | * @param tx Transmit pin |
Sissors | 6:4e3e18b41ea6 | 92 | * @param rx Receive pin |
Sissors | 6:4e3e18b41ea6 | 93 | * @note Either tx or rx may be specified as NC if unused |
Sissors | 6:4e3e18b41ea6 | 94 | */ |
Sissors | 6:4e3e18b41ea6 | 95 | BufferedSoftSerial(PinName tx, PinName rx, const char* name=NULL); |
Sissors | 6:4e3e18b41ea6 | 96 | |
Sissors | 6:4e3e18b41ea6 | 97 | /** Check on how many bytes are in the rx buffer |
Sissors | 6:4e3e18b41ea6 | 98 | * @return 1 if something exists, 0 otherwise |
Sissors | 6:4e3e18b41ea6 | 99 | */ |
Sissors | 6:4e3e18b41ea6 | 100 | virtual int readable(void); |
Sissors | 6:4e3e18b41ea6 | 101 | |
Sissors | 6:4e3e18b41ea6 | 102 | /** Check to see if the tx buffer has room |
Sissors | 6:4e3e18b41ea6 | 103 | * @return 1 always has room and can overwrite previous content if too small / slow |
Sissors | 6:4e3e18b41ea6 | 104 | */ |
Sissors | 6:4e3e18b41ea6 | 105 | virtual int writeable(void); |
Sissors | 6:4e3e18b41ea6 | 106 | |
Sissors | 6:4e3e18b41ea6 | 107 | /** Get a single byte from the BufferedSoftSerial Port. |
Sissors | 6:4e3e18b41ea6 | 108 | * Should check readable() before calling this. |
Sissors | 6:4e3e18b41ea6 | 109 | * @return A byte that came in on the BufferedSoftSerial Port |
Sissors | 6:4e3e18b41ea6 | 110 | */ |
Sissors | 6:4e3e18b41ea6 | 111 | virtual int getc(void); |
Sissors | 6:4e3e18b41ea6 | 112 | |
Sissors | 6:4e3e18b41ea6 | 113 | /** Write a single byte to the BufferedSoftSerial Port. |
Sissors | 6:4e3e18b41ea6 | 114 | * @param c The byte to write to the BufferedSoftSerial Port |
Sissors | 6:4e3e18b41ea6 | 115 | * @return The byte that was written to the BufferedSoftSerial Port Buffer |
Sissors | 6:4e3e18b41ea6 | 116 | */ |
Sissors | 6:4e3e18b41ea6 | 117 | virtual int putc(int c); |
Sissors | 6:4e3e18b41ea6 | 118 | |
Sissors | 6:4e3e18b41ea6 | 119 | /** Write a string to the BufferedSoftSerial Port. Must be NULL terminated |
Sissors | 6:4e3e18b41ea6 | 120 | * @param s The string to write to the Serial Port |
Sissors | 6:4e3e18b41ea6 | 121 | * @return The number of bytes written to the Serial Port Buffer |
Sissors | 6:4e3e18b41ea6 | 122 | */ |
Sissors | 6:4e3e18b41ea6 | 123 | virtual int puts(const char *s); |
Sissors | 6:4e3e18b41ea6 | 124 | |
Sissors | 6:4e3e18b41ea6 | 125 | /** Write a formatted string to the BufferedSoftSerial Port. |
Sissors | 6:4e3e18b41ea6 | 126 | * @param format The string + format specifiers to write to the BufferedSoftSerial Port |
Sissors | 6:4e3e18b41ea6 | 127 | * @return The number of bytes written to the Serial Port Buffer |
Sissors | 6:4e3e18b41ea6 | 128 | */ |
Sissors | 6:4e3e18b41ea6 | 129 | virtual int printf(const char* format, ...); |
Sissors | 6:4e3e18b41ea6 | 130 | |
Sissors | 6:4e3e18b41ea6 | 131 | /** Write data to the BufferedSoftSerial Port |
Sissors | 6:4e3e18b41ea6 | 132 | * @param s A pointer to data to send |
Sissors | 6:4e3e18b41ea6 | 133 | * @param length The amount of data being pointed to |
Sissors | 6:4e3e18b41ea6 | 134 | * @return The number of bytes written to the Serial Port Buffer |
Sissors | 6:4e3e18b41ea6 | 135 | */ |
Sissors | 6:4e3e18b41ea6 | 136 | virtual ssize_t write(const void *s, std::size_t length); |
Sissors | 6:4e3e18b41ea6 | 137 | }; |
Sissors | 6:4e3e18b41ea6 | 138 | |
Sissors | 6:4e3e18b41ea6 | 139 | #endif |