aboutsummaryrefslogtreecommitdiff
path: root/libraries/Bridge/Console.cpp
blob: 8607421f76149e3c7346e9df9cebe474bbc36c51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
  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() : 
  bridge(Bridge), inBuffered(0), inReadPos(0), inBuffer(NULL),
  autoFlush(true)
{
  // Empty
}

// Constructor with a user provided BridgeClass instance
ConsoleClass::ConsoleClass(BridgeClass &_b) : 
  bridge(_b), inBuffered(0), inReadPos(0), inBuffer(NULL),
  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();
  }
}

size_t ConsoleClass::write(const uint8_t *buff, size_t size) {
  if (autoFlush) {
    // TODO: do it in a more efficient way
    uint8_t *tmp = new uint8_t[size+1];
    tmp[0] = 'P';
    memcpy(tmp+1, buff, size);
    bridge.transfer(tmp, size+1);
    delete[] tmp;
    return size;
  } else {
    while (size > 0) {
      outBuffer[outBuffered++] = *buff++;
      size--;
      if (outBuffered == outBufferSize)
        flush();
    }
  }
}

void ConsoleClass::flush() {
  if (autoFlush)
    return;
  
  bridge.transfer(outBuffer, outBuffered);
  outBuffered = 1;
}

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;
}

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();
  return inBuffered;
}

int ConsoleClass::read() {
  doBuffer();
  if (inBuffered == 0)
    return -1; // no chars available
  else {
    inBuffered--;
    return inBuffer[inReadPos++];
  }
}

int ConsoleClass::peek() {
  doBuffer();
  if (inBuffered == 0)
    return -1; // no chars available
  else
    return inBuffer[inReadPos];
}

void ConsoleClass::doBuffer() {
  // If there are already char in buffer exit
  if (inBuffered > 0)
    return;

  // Try to buffer up to 32 characters
  inReadPos = 0;
  uint8_t tmp[] = { 'p', BUFFER_SIZE };
  inBuffered = bridge.transfer(tmp, 2, inBuffer, BUFFER_SIZE);
}

void ConsoleClass::begin() {
  bridge.begin();
  end();
  inBuffer = new uint8_t[BUFFER_SIZE];
}

void ConsoleClass::end() {
  noBuffer();
  if (inBuffer) {
    delete[] inBuffer;
    inBuffer = NULL;
  }
}

ConsoleClass Console;