1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-03-14 11:29:26 +01:00

Updated Console class. New ConsoleAsciiTable example.

This commit is contained in:
Cristian Maglie 2013-05-22 19:17:58 +02:00
parent f3abbf60a8
commit 6e94316bbb
3 changed files with 132 additions and 39 deletions

View File

@ -20,14 +20,15 @@
// Default constructor uses global Bridge instance // Default constructor uses global Bridge instance
ConsoleClass::ConsoleClass() : ConsoleClass::ConsoleClass() :
bridge(Bridge), buffered(0), readPos(0), buffer(NULL) bridge(Bridge), inBuffered(0), inReadPos(0), inBuffer(NULL),
autoFlush(true)
{ {
// Empty // Empty
} }
// Constructor with a user provided BridgeClass instance // Constructor with a user provided BridgeClass instance
ConsoleClass::ConsoleClass(BridgeClass &_b) : ConsoleClass::ConsoleClass(BridgeClass &_b) :
bridge(_b), buffered(0), readPos(0), buffer(NULL), bridge(_b), inBuffered(0), inReadPos(0), inBuffer(NULL),
autoFlush(true) autoFlush(true)
{ {
// Empty // Empty
@ -49,18 +50,18 @@ size_t ConsoleClass::write(uint8_t c) {
} }
} }
size_t ConsoleClass::write(const uint8_t *buffer, size_t size) { size_t ConsoleClass::write(const uint8_t *buff, size_t size) {
if (autoFlush) { if (autoFlush) {
// TODO: do it in a more efficient way // TODO: do it in a more efficient way
uint8_t *tmp = new uint8_t[size+1]; uint8_t *tmp = new uint8_t[size+1];
tmp[0] = 'P'; tmp[0] = 'P';
memcpy(tmp+1, buffer, size); memcpy(tmp+1, buff, size);
bridge.transfer(tmp, size+1); bridge.transfer(tmp, size+1);
delete[] tmp; delete[] tmp;
return size; return size;
} else { } else {
while (size > 0) { while (size > 0) {
outBuffer[outBuffered++] = *buffer++; outBuffer[outBuffered++] = *buff++;
size--; size--;
if (outBuffered == outBufferSize) if (outBuffered == outBufferSize)
flush(); flush();
@ -76,20 +77,22 @@ void ConsoleClass::flush() {
outBuffered = 1; outBuffered = 1;
} }
void ConsoleClass::setBuffer(uint8_t size) { void ConsoleClass::noBuffer() {
if (size==0) { if (autoFlush)
if (!autoFlush) { return;
delete[] outBuffer; delete[] outBuffer;
autoFlush = true; autoFlush = true;
} }
} else {
if (autoFlush) void ConsoleClass::buffer(uint8_t size) {
setBuffer(0); noBuffer();
outBuffer = new uint8_t[size+1]; if (size==0)
outBuffer[0] = 'P'; // WRITE tag return;
outBufferSize = size+1; outBuffer = new uint8_t[size+1];
outBuffered = 1; outBuffer[0] = 'P'; // WRITE tag
} outBufferSize = size+1;
outBuffered = 1;
autoFlush = false;
} }
bool ConsoleClass::connected() { bool ConsoleClass::connected() {
@ -101,51 +104,49 @@ bool ConsoleClass::connected() {
int ConsoleClass::available() { int ConsoleClass::available() {
// Look if there is new data available // Look if there is new data available
doBuffer(); doBuffer();
return buffered; return inBuffered;
} }
int ConsoleClass::read() { int ConsoleClass::read() {
doBuffer(); doBuffer();
if (buffered == 0) if (inBuffered == 0)
return -1; // no chars available return -1; // no chars available
else { else {
buffered--; inBuffered--;
return buffer[readPos++]; return inBuffer[inReadPos++];
} }
} }
int ConsoleClass::peek() { int ConsoleClass::peek() {
doBuffer(); doBuffer();
if (buffered == 0) if (inBuffered == 0)
return -1; // no chars available return -1; // no chars available
else else
return buffer[readPos]; return inBuffer[inReadPos];
} }
void ConsoleClass::doBuffer() { void ConsoleClass::doBuffer() {
// If there are already char in buffer exit // If there are already char in buffer exit
if (buffered > 0) if (inBuffered > 0)
return; return;
// Try to buffer up to 32 characters // Try to buffer up to 32 characters
readPos = 0; inReadPos = 0;
uint8_t tmp[] = { 'p', BUFFER_SIZE }; uint8_t tmp[] = { 'p', BUFFER_SIZE };
buffered = bridge.transfer(tmp, 2, buffer, BUFFER_SIZE); inBuffered = bridge.transfer(tmp, 2, inBuffer, BUFFER_SIZE);
} }
void ConsoleClass::begin() { void ConsoleClass::begin() {
bridge.begin(); bridge.begin();
end(); end();
buffer = new uint8_t[BUFFER_SIZE]; inBuffer = new uint8_t[BUFFER_SIZE];
} }
void ConsoleClass::end() { void ConsoleClass::end() {
if (autoFlush) { noBuffer();
setBuffer(0); if (inBuffer) {
} delete[] inBuffer;
if (buffer) { inBuffer = NULL;
delete[] buffer;
buffer = NULL;
} }
} }

View File

@ -32,7 +32,8 @@ public:
void begin(); void begin();
void end(); void end();
void setBuffer(uint8_t size); void buffer(uint8_t size);
void noBuffer();
bool connected(); bool connected();
@ -52,10 +53,10 @@ private:
BridgeClass &bridge; BridgeClass &bridge;
void doBuffer(); void doBuffer();
uint8_t buffered; uint8_t inBuffered;
uint8_t readPos; uint8_t inReadPos;
static const int BUFFER_SIZE = 32; static const int BUFFER_SIZE = 32;
uint8_t *buffer; uint8_t *inBuffer;
bool autoFlush; bool autoFlush;
uint8_t outBuffered; uint8_t outBuffered;

View File

@ -0,0 +1,91 @@
/*
ASCII table
Prints out byte values in all possible formats:
* as raw binary values
* as ASCII-encoded decimal, hex, octal, and binary values
For more on ASCII, see http://www.asciitable.com and http://en.wikipedia.org/wiki/ASCII
The circuit: No external hardware needed.
created 2006
by Nicholas Zambetti
modified 9 Apr 2012
by Tom Igoe
modified 22 May 2013
by Cristian Maglie
This example code is in the public domain.
<http://www.zambetti.com>
*/
#include <Console.h>
void setup() {
//Initialize Console and wait for port to open:
Bridge.begin();
Console.begin();
// Uncomment the followinf line to enable buffering:
// - better transmission speed and efficiency
// - needs to call Console.flush() to ensure that all
// transmitted data is sent
//Console.buffer(64);
while (!Console) {
; // wait for Console port to connect.
}
// prints title with ending line break
Console.println("ASCII Table ~ Character Map");
}
// first visible ASCIIcharacter '!' is number 33:
int thisByte = 33;
// you can also write ASCII characters in single quotes.
// for example. '!' is the same as 33, so you could also use this:
//int thisByte = '!';
void loop() {
// prints value unaltered, i.e. the raw binary version of the
// byte. The Console monitor interprets all bytes as
// ASCII, so 33, the first number, will show up as '!'
Console.write(thisByte);
Console.print(", dec: ");
// prints value as string as an ASCII-encoded decimal (base 10).
// Decimal is the default format for Console.print() and Console.println(),
// so no modifier is needed:
Console.print(thisByte);
// But you can declare the modifier for decimal if you want to.
//this also works if you uncomment it:
// Console.print(thisByte, DEC);
Console.print(", hex: ");
// prints value as string in hexadecimal (base 16):
Console.print(thisByte, HEX);
Console.print(", oct: ");
// prints value as string in octal (base 8);
Console.print(thisByte, OCT);
Console.print(", bin: ");
// prints value as string in binary (base 2)
// also prints ending line break:
Console.println(thisByte, BIN);
// if printed last visible character '~' or 126, stop:
if(thisByte == 126) { // you could also use if (thisByte == '~') {
// This loop loops forever and does nothing
while(true) {
continue;
}
}
// go on to the next character
thisByte++;
}