libraries / Bridge / src / Console.cppon commit Added link to project report (97a3ba0)
   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
  22ConsoleClass::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
  30ConsoleClass::ConsoleClass(BridgeClass &_b) :
  31  bridge(_b), inBuffered(0), inReadPos(0), inBuffer(NULL),
  32  autoFlush(true)
  33{
  34  // Empty
  35}
  36
  37ConsoleClass::~ConsoleClass() {
  38  end();
  39}
  40
  41size_t ConsoleClass::write(uint8_t c) {
  42  if (autoFlush) {
  43    uint8_t tmp[] = { 'P', c };
  44    bridge.transfer(tmp, 2);
  45  } else {
  46    outBuffer[outBuffered++] = c;
  47    if (outBuffered == outBufferSize)
  48      flush();
  49  }
  50  return 1;
  51}
  52
  53size_t ConsoleClass::write(const uint8_t *buff, size_t size) {
  54  if (autoFlush) {
  55    uint8_t tmp[] = { 'P' };
  56    bridge.transfer(tmp, 1, buff, size, NULL, 0);
  57  } else {
  58    size_t sent = size;
  59    while (sent > 0) {
  60      outBuffer[outBuffered++] = *buff++;
  61      sent--;
  62      if (outBuffered == outBufferSize)
  63        flush();
  64    }
  65  }
  66  return size;
  67}
  68
  69void ConsoleClass::flush() {
  70  if (autoFlush)
  71    return;
  72
  73  bridge.transfer(outBuffer, outBuffered);
  74  outBuffered = 1;
  75}
  76
  77void ConsoleClass::noBuffer() {
  78  if (autoFlush)
  79    return;
  80  delete[] outBuffer;
  81  autoFlush = true;
  82}
  83
  84void ConsoleClass::buffer(uint8_t size) {
  85  noBuffer();
  86  if (size == 0)
  87    return;
  88  outBuffer = new uint8_t[size + 1];
  89  outBuffer[0] = 'P'; // WRITE tag
  90  outBufferSize = size + 1;
  91  outBuffered = 1;
  92  autoFlush = false;
  93}
  94
  95bool ConsoleClass::connected() {
  96  uint8_t tmp = 'a';
  97  bridge.transfer(&tmp, 1, &tmp, 1);
  98  return tmp == 1;
  99}
 100
 101int ConsoleClass::available() {
 102  // Look if there is new data available
 103  doBuffer();
 104  return inBuffered;
 105}
 106
 107int ConsoleClass::read() {
 108  doBuffer();
 109  if (inBuffered == 0)
 110    return -1; // no chars available
 111  else {
 112    inBuffered--;
 113    return inBuffer[inReadPos++];
 114  }
 115}
 116
 117int ConsoleClass::peek() {
 118  doBuffer();
 119  if (inBuffered == 0)
 120    return -1; // no chars available
 121  else
 122    return inBuffer[inReadPos];
 123}
 124
 125void ConsoleClass::doBuffer() {
 126  // If there are already char in buffer exit
 127  if (inBuffered > 0)
 128    return;
 129
 130  // Try to buffer up to 32 characters
 131  inReadPos = 0;
 132  uint8_t tmp[] = { 'p', BUFFER_SIZE };
 133  inBuffered = bridge.transfer(tmp, 2, inBuffer, BUFFER_SIZE);
 134}
 135
 136void ConsoleClass::begin() {
 137  bridge.begin();
 138  end();
 139  inBuffer = new uint8_t[BUFFER_SIZE];
 140}
 141
 142void ConsoleClass::end() {
 143  noBuffer();
 144  if (inBuffer) {
 145    delete[] inBuffer;
 146    inBuffer = NULL;
 147  }
 148}
 149
 150ConsoleClass Console;