[136] | 1 | /*
|
---|
| 2 | Copyright (c) 2013 Arduino LLC. All right reserved.
|
---|
| 3 |
|
---|
| 4 | This library is free software; you can redistribute it and/or
|
---|
| 5 | modify it under the terms of the GNU Lesser General Public
|
---|
| 6 | License as published by the Free Software Foundation; either
|
---|
| 7 | version 2.1 of the License, or (at your option) any later version.
|
---|
| 8 |
|
---|
| 9 | This library is distributed in the hope that it will be useful,
|
---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 12 | Lesser General Public License for more details.
|
---|
| 13 |
|
---|
| 14 | You should have received a copy of the GNU Lesser General Public
|
---|
| 15 | License along with this library; if not, write to the Free Software
|
---|
| 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
| 17 | */
|
---|
| 18 |
|
---|
| 19 | #include <Console.h>
|
---|
| 20 |
|
---|
| 21 | // Default constructor uses global Bridge instance
|
---|
| 22 | ConsoleClass::ConsoleClass() :
|
---|
| 23 | bridge(Bridge), inBuffered(0), inReadPos(0), inBuffer(NULL),
|
---|
| 24 | autoFlush(true)
|
---|
| 25 | {
|
---|
| 26 | // Empty
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | // Constructor with a user provided BridgeClass instance
|
---|
| 30 | ConsoleClass::ConsoleClass(BridgeClass &_b) :
|
---|
| 31 | bridge(_b), inBuffered(0), inReadPos(0), inBuffer(NULL),
|
---|
| 32 | autoFlush(true)
|
---|
| 33 | {
|
---|
| 34 | // Empty
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | ConsoleClass::~ConsoleClass() {
|
---|
| 38 | end();
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | size_t ConsoleClass::write(uint8_t c) {
|
---|
| 42 | if (autoFlush) {
|
---|
| 43 | uint8_t tmp[] = { 'P', c };
|
---|
| 44 | bridge.transfer(tmp, 2);
|
---|
| 45 | return 1;
|
---|
| 46 | } else {
|
---|
| 47 | outBuffer[outBuffered++] = c;
|
---|
| 48 | if (outBuffered == outBufferSize)
|
---|
| 49 | flush();
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | size_t ConsoleClass::write(const uint8_t *buff, size_t size) {
|
---|
| 54 | if (autoFlush) {
|
---|
| 55 | // TODO: do it in a more efficient way
|
---|
| 56 | uint8_t *tmp = new uint8_t[size + 1];
|
---|
| 57 | tmp[0] = 'P';
|
---|
| 58 | memcpy(tmp + 1, buff, size);
|
---|
| 59 | bridge.transfer(tmp, size + 1);
|
---|
| 60 | delete[] tmp;
|
---|
| 61 | return size;
|
---|
| 62 | } else {
|
---|
| 63 | while (size > 0) {
|
---|
| 64 | outBuffer[outBuffered++] = *buff++;
|
---|
| 65 | size--;
|
---|
| 66 | if (outBuffered == outBufferSize)
|
---|
| 67 | flush();
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | void ConsoleClass::flush() {
|
---|
| 73 | if (autoFlush)
|
---|
| 74 | return;
|
---|
| 75 |
|
---|
| 76 | bridge.transfer(outBuffer, outBuffered);
|
---|
| 77 | outBuffered = 1;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | void ConsoleClass::noBuffer() {
|
---|
| 81 | if (autoFlush)
|
---|
| 82 | return;
|
---|
| 83 | delete[] outBuffer;
|
---|
| 84 | autoFlush = true;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | void ConsoleClass::buffer(uint8_t size) {
|
---|
| 88 | noBuffer();
|
---|
| 89 | if (size == 0)
|
---|
| 90 | return;
|
---|
| 91 | outBuffer = new uint8_t[size + 1];
|
---|
| 92 | outBuffer[0] = 'P'; // WRITE tag
|
---|
| 93 | outBufferSize = size + 1;
|
---|
| 94 | outBuffered = 1;
|
---|
| 95 | autoFlush = false;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | bool ConsoleClass::connected() {
|
---|
| 99 | uint8_t tmp = 'a';
|
---|
| 100 | bridge.transfer(&tmp, 1, &tmp, 1);
|
---|
| 101 | return tmp == 1;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | int ConsoleClass::available() {
|
---|
| 105 | // Look if there is new data available
|
---|
| 106 | doBuffer();
|
---|
| 107 | return inBuffered;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | int ConsoleClass::read() {
|
---|
| 111 | doBuffer();
|
---|
| 112 | if (inBuffered == 0)
|
---|
| 113 | return -1; // no chars available
|
---|
| 114 | else {
|
---|
| 115 | inBuffered--;
|
---|
| 116 | return inBuffer[inReadPos++];
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | int ConsoleClass::peek() {
|
---|
| 121 | doBuffer();
|
---|
| 122 | if (inBuffered == 0)
|
---|
| 123 | return -1; // no chars available
|
---|
| 124 | else
|
---|
| 125 | return inBuffer[inReadPos];
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | void ConsoleClass::doBuffer() {
|
---|
| 129 | // If there are already char in buffer exit
|
---|
| 130 | if (inBuffered > 0)
|
---|
| 131 | return;
|
---|
| 132 |
|
---|
| 133 | // Try to buffer up to 32 characters
|
---|
| 134 | inReadPos = 0;
|
---|
| 135 | uint8_t tmp[] = { 'p', BUFFER_SIZE };
|
---|
| 136 | inBuffered = bridge.transfer(tmp, 2, inBuffer, BUFFER_SIZE);
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | void ConsoleClass::begin() {
|
---|
| 140 | bridge.begin();
|
---|
| 141 | end();
|
---|
| 142 | inBuffer = new uint8_t[BUFFER_SIZE];
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | void ConsoleClass::end() {
|
---|
| 146 | noBuffer();
|
---|
| 147 | if (inBuffer) {
|
---|
| 148 | delete[] inBuffer;
|
---|
| 149 | inBuffer = NULL;
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | ConsoleClass Console;
|
---|