ros melodic library with custom message
Dependents: Robot_team1_QEI_Douglas Robot_team1
ros_lib/BufferedSerial/BufferedSerial.cpp@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 | * @file BufferedSerial.cpp |
scarter1 | 0:020db18a476d | 3 | * @brief Software Buffer - Extends mbed Serial functionallity adding irq driven TX and RX |
scarter1 | 0:020db18a476d | 4 | * @author sam grove |
scarter1 | 0:020db18a476d | 5 | * @version 1.0 |
scarter1 | 0:020db18a476d | 6 | * @see |
scarter1 | 0:020db18a476d | 7 | * |
scarter1 | 0:020db18a476d | 8 | * Copyright (c) 2013 |
scarter1 | 0:020db18a476d | 9 | * |
scarter1 | 0:020db18a476d | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); |
scarter1 | 0:020db18a476d | 11 | * you may not use this file except in compliance with the License. |
scarter1 | 0:020db18a476d | 12 | * You may obtain a copy of the License at |
scarter1 | 0:020db18a476d | 13 | * |
scarter1 | 0:020db18a476d | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
scarter1 | 0:020db18a476d | 15 | * |
scarter1 | 0:020db18a476d | 16 | * Unless required by applicable law or agreed to in writing, software |
scarter1 | 0:020db18a476d | 17 | * distributed under the License is distributed on an "AS IS" BASIS, |
scarter1 | 0:020db18a476d | 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
scarter1 | 0:020db18a476d | 19 | * See the License for the specific language governing permissions and |
scarter1 | 0:020db18a476d | 20 | * limitations under the License. |
scarter1 | 0:020db18a476d | 21 | */ |
scarter1 | 0:020db18a476d | 22 | |
scarter1 | 0:020db18a476d | 23 | #include "BufferedSerial.h" |
scarter1 | 0:020db18a476d | 24 | #include <stdarg.h> |
scarter1 | 0:020db18a476d | 25 | |
scarter1 | 0:020db18a476d | 26 | BufferedSerial::BufferedSerial(PinName tx, PinName rx, uint32_t buf_size, uint32_t tx_multiple, const char* name) |
scarter1 | 0:020db18a476d | 27 | : RawSerial(tx, rx) , _rxbuf(buf_size), _txbuf((uint32_t)(tx_multiple*buf_size)) |
scarter1 | 0:020db18a476d | 28 | { |
scarter1 | 0:020db18a476d | 29 | RawSerial::attach(this, &BufferedSerial::rxIrq, Serial::RxIrq); |
scarter1 | 0:020db18a476d | 30 | this->_buf_size = buf_size; |
scarter1 | 0:020db18a476d | 31 | this->_tx_multiple = tx_multiple; |
scarter1 | 0:020db18a476d | 32 | return; |
scarter1 | 0:020db18a476d | 33 | } |
scarter1 | 0:020db18a476d | 34 | |
scarter1 | 0:020db18a476d | 35 | BufferedSerial::~BufferedSerial(void) |
scarter1 | 0:020db18a476d | 36 | { |
scarter1 | 0:020db18a476d | 37 | RawSerial::attach(NULL, RawSerial::RxIrq); |
scarter1 | 0:020db18a476d | 38 | RawSerial::attach(NULL, RawSerial::TxIrq); |
scarter1 | 0:020db18a476d | 39 | |
scarter1 | 0:020db18a476d | 40 | return; |
scarter1 | 0:020db18a476d | 41 | } |
scarter1 | 0:020db18a476d | 42 | |
scarter1 | 0:020db18a476d | 43 | int BufferedSerial::readable(void) |
scarter1 | 0:020db18a476d | 44 | { |
scarter1 | 0:020db18a476d | 45 | return _rxbuf.available(); // note: look if things are in the buffer |
scarter1 | 0:020db18a476d | 46 | } |
scarter1 | 0:020db18a476d | 47 | |
scarter1 | 0:020db18a476d | 48 | int BufferedSerial::writeable(void) |
scarter1 | 0:020db18a476d | 49 | { |
scarter1 | 0:020db18a476d | 50 | return 1; // buffer allows overwriting by design, always true |
scarter1 | 0:020db18a476d | 51 | } |
scarter1 | 0:020db18a476d | 52 | |
scarter1 | 0:020db18a476d | 53 | int BufferedSerial::getc(void) |
scarter1 | 0:020db18a476d | 54 | { |
scarter1 | 0:020db18a476d | 55 | return _rxbuf; |
scarter1 | 0:020db18a476d | 56 | } |
scarter1 | 0:020db18a476d | 57 | |
scarter1 | 0:020db18a476d | 58 | int BufferedSerial::putc(int c) |
scarter1 | 0:020db18a476d | 59 | { |
scarter1 | 0:020db18a476d | 60 | _txbuf = (char)c; |
scarter1 | 0:020db18a476d | 61 | BufferedSerial::prime(); |
scarter1 | 0:020db18a476d | 62 | |
scarter1 | 0:020db18a476d | 63 | return c; |
scarter1 | 0:020db18a476d | 64 | } |
scarter1 | 0:020db18a476d | 65 | |
scarter1 | 0:020db18a476d | 66 | int BufferedSerial::puts(const char *s) |
scarter1 | 0:020db18a476d | 67 | { |
scarter1 | 0:020db18a476d | 68 | if (s != NULL) { |
scarter1 | 0:020db18a476d | 69 | const char* ptr = s; |
scarter1 | 0:020db18a476d | 70 | |
scarter1 | 0:020db18a476d | 71 | while(*(ptr) != 0) { |
scarter1 | 0:020db18a476d | 72 | _txbuf = *(ptr++); |
scarter1 | 0:020db18a476d | 73 | } |
scarter1 | 0:020db18a476d | 74 | _txbuf = '\n'; // done per puts definition |
scarter1 | 0:020db18a476d | 75 | BufferedSerial::prime(); |
scarter1 | 0:020db18a476d | 76 | |
scarter1 | 0:020db18a476d | 77 | return (ptr - s) + 1; |
scarter1 | 0:020db18a476d | 78 | } |
scarter1 | 0:020db18a476d | 79 | return 0; |
scarter1 | 0:020db18a476d | 80 | } |
scarter1 | 0:020db18a476d | 81 | |
scarter1 | 0:020db18a476d | 82 | int BufferedSerial::printf(const char* format, ...) |
scarter1 | 0:020db18a476d | 83 | { |
scarter1 | 0:020db18a476d | 84 | char buffer[this->_buf_size]; |
scarter1 | 0:020db18a476d | 85 | memset(buffer,0,this->_buf_size); |
scarter1 | 0:020db18a476d | 86 | int r = 0; |
scarter1 | 0:020db18a476d | 87 | |
scarter1 | 0:020db18a476d | 88 | va_list arg; |
scarter1 | 0:020db18a476d | 89 | va_start(arg, format); |
scarter1 | 0:020db18a476d | 90 | r = vsprintf(buffer, format, arg); |
scarter1 | 0:020db18a476d | 91 | // this may not hit the heap but should alert the user anyways |
scarter1 | 0:020db18a476d | 92 | if(r > this->_buf_size) { |
scarter1 | 0:020db18a476d | 93 | error("%s %d buffer overwrite (max_buf_size: %d exceeded: %d)!\r\n", __FILE__, __LINE__,this->_buf_size,r); |
scarter1 | 0:020db18a476d | 94 | va_end(arg); |
scarter1 | 0:020db18a476d | 95 | return 0; |
scarter1 | 0:020db18a476d | 96 | } |
scarter1 | 0:020db18a476d | 97 | va_end(arg); |
scarter1 | 0:020db18a476d | 98 | r = BufferedSerial::write(buffer, r); |
scarter1 | 0:020db18a476d | 99 | |
scarter1 | 0:020db18a476d | 100 | return r; |
scarter1 | 0:020db18a476d | 101 | } |
scarter1 | 0:020db18a476d | 102 | |
scarter1 | 0:020db18a476d | 103 | ssize_t BufferedSerial::write(const void *s, size_t length) |
scarter1 | 0:020db18a476d | 104 | { |
scarter1 | 0:020db18a476d | 105 | if (s != NULL && length > 0) { |
scarter1 | 0:020db18a476d | 106 | const char* ptr = (const char*)s; |
scarter1 | 0:020db18a476d | 107 | const char* end = ptr + length; |
scarter1 | 0:020db18a476d | 108 | |
scarter1 | 0:020db18a476d | 109 | while (ptr != end) { |
scarter1 | 0:020db18a476d | 110 | _txbuf = *(ptr++); |
scarter1 | 0:020db18a476d | 111 | } |
scarter1 | 0:020db18a476d | 112 | BufferedSerial::prime(); |
scarter1 | 0:020db18a476d | 113 | |
scarter1 | 0:020db18a476d | 114 | return ptr - (const char*)s; |
scarter1 | 0:020db18a476d | 115 | } |
scarter1 | 0:020db18a476d | 116 | return 0; |
scarter1 | 0:020db18a476d | 117 | } |
scarter1 | 0:020db18a476d | 118 | |
scarter1 | 0:020db18a476d | 119 | |
scarter1 | 0:020db18a476d | 120 | void BufferedSerial::rxIrq(void) |
scarter1 | 0:020db18a476d | 121 | { |
scarter1 | 0:020db18a476d | 122 | // read from the peripheral and make sure something is available |
scarter1 | 0:020db18a476d | 123 | if(serial_readable(&_serial)) { |
scarter1 | 0:020db18a476d | 124 | _rxbuf = serial_getc(&_serial); // if so load them into a buffer |
scarter1 | 0:020db18a476d | 125 | } |
scarter1 | 0:020db18a476d | 126 | |
scarter1 | 0:020db18a476d | 127 | return; |
scarter1 | 0:020db18a476d | 128 | } |
scarter1 | 0:020db18a476d | 129 | |
scarter1 | 0:020db18a476d | 130 | void BufferedSerial::txIrq(void) |
scarter1 | 0:020db18a476d | 131 | { |
scarter1 | 0:020db18a476d | 132 | // see if there is room in the hardware fifo and if something is in the software fifo |
scarter1 | 0:020db18a476d | 133 | while(serial_writable(&_serial)) { |
scarter1 | 0:020db18a476d | 134 | if(_txbuf.available()) { |
scarter1 | 0:020db18a476d | 135 | serial_putc(&_serial, (int)_txbuf.get()); |
scarter1 | 0:020db18a476d | 136 | } else { |
scarter1 | 0:020db18a476d | 137 | // disable the TX interrupt when there is nothing left to send |
scarter1 | 0:020db18a476d | 138 | RawSerial::attach(NULL, RawSerial::TxIrq); |
scarter1 | 0:020db18a476d | 139 | break; |
scarter1 | 0:020db18a476d | 140 | } |
scarter1 | 0:020db18a476d | 141 | } |
scarter1 | 0:020db18a476d | 142 | |
scarter1 | 0:020db18a476d | 143 | return; |
scarter1 | 0:020db18a476d | 144 | } |
scarter1 | 0:020db18a476d | 145 | |
scarter1 | 0:020db18a476d | 146 | void BufferedSerial::prime(void) |
scarter1 | 0:020db18a476d | 147 | { |
scarter1 | 0:020db18a476d | 148 | // if already busy then the irq will pick this up |
scarter1 | 0:020db18a476d | 149 | if(serial_writable(&_serial)) { |
scarter1 | 0:020db18a476d | 150 | RawSerial::attach(NULL, RawSerial::TxIrq); // make sure not to cause contention in the irq |
scarter1 | 0:020db18a476d | 151 | BufferedSerial::txIrq(); // only write to hardware in one place |
scarter1 | 0:020db18a476d | 152 | RawSerial::attach(this, &BufferedSerial::txIrq, RawSerial::TxIrq); |
scarter1 | 0:020db18a476d | 153 | } |
scarter1 | 0:020db18a476d | 154 | |
scarter1 | 0:020db18a476d | 155 | return; |
scarter1 | 0:020db18a476d | 156 | } |
scarter1 | 0:020db18a476d | 157 | |
scarter1 | 0:020db18a476d | 158 |