2013-05-09 11:14:59 +02:00
|
|
|
/*
|
|
|
|
Copyright (c) 2013 Arduino LLC. All right reserved.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Console.h>
|
|
|
|
|
|
|
|
// Default constructor uses global Bridge instance
|
|
|
|
ConsoleClass::ConsoleClass() :
|
2013-05-22 19:17:58 +02:00
|
|
|
bridge(Bridge), inBuffered(0), inReadPos(0), inBuffer(NULL),
|
|
|
|
autoFlush(true)
|
2013-05-09 11:14:59 +02:00
|
|
|
{
|
|
|
|
// Empty
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constructor with a user provided BridgeClass instance
|
|
|
|
ConsoleClass::ConsoleClass(BridgeClass &_b) :
|
2013-05-22 19:17:58 +02:00
|
|
|
bridge(_b), inBuffered(0), inReadPos(0), inBuffer(NULL),
|
2013-05-09 11:14:59 +02:00
|
|
|
autoFlush(true)
|
|
|
|
{
|
|
|
|
// Empty
|
|
|
|
}
|
|
|
|
|
|
|
|
ConsoleClass::~ConsoleClass() {
|
|
|
|
end();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ConsoleClass::write(uint8_t c) {
|
|
|
|
if (autoFlush) {
|
|
|
|
uint8_t tmp[] = { 'P', c };
|
|
|
|
bridge.transfer(tmp, 2);
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
outBuffer[outBuffered++] = c;
|
|
|
|
if (outBuffered == outBufferSize)
|
|
|
|
flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-22 19:17:58 +02:00
|
|
|
size_t ConsoleClass::write(const uint8_t *buff, size_t size) {
|
2013-05-09 11:14:59 +02:00
|
|
|
if (autoFlush) {
|
|
|
|
// TODO: do it in a more efficient way
|
|
|
|
uint8_t *tmp = new uint8_t[size+1];
|
|
|
|
tmp[0] = 'P';
|
2013-05-22 19:17:58 +02:00
|
|
|
memcpy(tmp+1, buff, size);
|
2013-05-09 11:14:59 +02:00
|
|
|
bridge.transfer(tmp, size+1);
|
|
|
|
delete[] tmp;
|
|
|
|
return size;
|
|
|
|
} else {
|
|
|
|
while (size > 0) {
|
2013-05-22 19:17:58 +02:00
|
|
|
outBuffer[outBuffered++] = *buff++;
|
2013-05-09 11:14:59 +02:00
|
|
|
size--;
|
|
|
|
if (outBuffered == outBufferSize)
|
|
|
|
flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleClass::flush() {
|
|
|
|
if (autoFlush)
|
|
|
|
return;
|
|
|
|
|
|
|
|
bridge.transfer(outBuffer, outBuffered);
|
|
|
|
outBuffered = 1;
|
|
|
|
}
|
|
|
|
|
2013-05-22 19:17:58 +02:00
|
|
|
void ConsoleClass::noBuffer() {
|
|
|
|
if (autoFlush)
|
|
|
|
return;
|
|
|
|
delete[] outBuffer;
|
|
|
|
autoFlush = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleClass::buffer(uint8_t size) {
|
|
|
|
noBuffer();
|
|
|
|
if (size==0)
|
|
|
|
return;
|
|
|
|
outBuffer = new uint8_t[size+1];
|
|
|
|
outBuffer[0] = 'P'; // WRITE tag
|
|
|
|
outBufferSize = size+1;
|
|
|
|
outBuffered = 1;
|
|
|
|
autoFlush = false;
|
2013-05-09 11:14:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ConsoleClass::connected() {
|
|
|
|
uint8_t tmp = 'a';
|
|
|
|
bridge.transfer(&tmp, 1, &tmp, 1);
|
|
|
|
return tmp==1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ConsoleClass::available() {
|
|
|
|
// Look if there is new data available
|
|
|
|
doBuffer();
|
2013-05-22 19:17:58 +02:00
|
|
|
return inBuffered;
|
2013-05-09 11:14:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int ConsoleClass::read() {
|
|
|
|
doBuffer();
|
2013-05-22 19:17:58 +02:00
|
|
|
if (inBuffered == 0)
|
2013-05-09 11:14:59 +02:00
|
|
|
return -1; // no chars available
|
|
|
|
else {
|
2013-05-22 19:17:58 +02:00
|
|
|
inBuffered--;
|
|
|
|
return inBuffer[inReadPos++];
|
2013-05-09 11:14:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int ConsoleClass::peek() {
|
|
|
|
doBuffer();
|
2013-05-22 19:17:58 +02:00
|
|
|
if (inBuffered == 0)
|
2013-05-09 11:14:59 +02:00
|
|
|
return -1; // no chars available
|
|
|
|
else
|
2013-05-22 19:17:58 +02:00
|
|
|
return inBuffer[inReadPos];
|
2013-05-09 11:14:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleClass::doBuffer() {
|
|
|
|
// If there are already char in buffer exit
|
2013-05-22 19:17:58 +02:00
|
|
|
if (inBuffered > 0)
|
2013-05-09 11:14:59 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Try to buffer up to 32 characters
|
2013-05-22 19:17:58 +02:00
|
|
|
inReadPos = 0;
|
2013-05-09 11:14:59 +02:00
|
|
|
uint8_t tmp[] = { 'p', BUFFER_SIZE };
|
2013-05-22 19:17:58 +02:00
|
|
|
inBuffered = bridge.transfer(tmp, 2, inBuffer, BUFFER_SIZE);
|
2013-05-09 11:14:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleClass::begin() {
|
|
|
|
bridge.begin();
|
|
|
|
end();
|
2013-05-22 19:17:58 +02:00
|
|
|
inBuffer = new uint8_t[BUFFER_SIZE];
|
2013-05-09 11:14:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleClass::end() {
|
2013-05-22 19:17:58 +02:00
|
|
|
noBuffer();
|
|
|
|
if (inBuffer) {
|
|
|
|
delete[] inBuffer;
|
|
|
|
inBuffer = NULL;
|
2013-05-09 11:14:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ConsoleClass Console;
|