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