-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathproxySerial.cpp
206 lines (158 loc) · 4.7 KB
/
proxySerial.cpp
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//
// 4D Systems μLCD-μLED-μVGA Serial_LCD Library Suite
// Arduino 0023 chipKIT MPIDE 0023 Library
// ----------------------------------
//
// Feb 01, 2012 release 106
// see README.txt
//
// © Rei VILO, 2010-2012
// CC = BY NC SA
// http://sites.google.com/site/vilorei/
// http://github.com/rei-vilo/Serial_LCD
//
//
// Based on
// 4D LABS PICASO-SGC Command Set
// Software Interface Specification
// Document Date: 1st March 2011
// Document Revision: 6.0
// http://www.4d-Labs.com
//
//
#include "WProgram.h"
#include "Stream.h"
#include "proxySerial.h"
// Utilities
String ftoa(float number, uint8_t precision, uint8_t size) {
// Based on mem, 16.07.2008
// http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num = 1207226548/6#6
// prints val with number of decimal places determine by precision
// precision is a number from 0 to 6 indicating the desired decimial places
// example: printDouble(3.1415, 2); // prints 3.14 (two decimal places)
// Added rounding, size and overflow #
// ftoa(343.1453, 2, 10) -> " 343.15"
// ftoa(343.1453, 4, 7) -> "# "
// avenue33, April 10th, 2010
String s = "";
// Negative
if (number < 0.0) {
s = "-";
number = -number;
}
float rounding = 0.5;
for (uint8_t i = 0; i < precision; ++i) rounding /= 10.0;
number += rounding;
s += String(int(number)); // prints the integer part
if(precision > 0) {
s += "."; // prints the decimal point
uint32_t frac;
uint32_t mult = 1;
uint8_t padding = precision -1;
while(precision--) mult *= 10;
frac = (number - (uint64_t)(number)) * mult;
uint32_t frac1 = frac;
while(frac1 /= 10) padding--;
while(padding--) s += "0";
s += String(frac, DEC) ; // prints the fractional part
}
if ( (size>0) & (s.length()>size) ) return("#");
while( s.length()<size ) s = " "+s;
return s;
}
String ttoa(float number, uint8_t precision, uint8_t size) {
// Automatic selection of the time unit: ms, s, mn, h
if (number<1000) {
// ms
return ftoa(number, 0, max(0, size-2)) + "ms";
}
else if (number<60000.0) {
// s = ms/1000
return ftoa(number*0.001, precision, max(0, size-1)) + "s";
}
else if (number<3600000.0) {
// mn = ms/1000/60
return ftoa(number*0.001/60.0, precision, max(0, size-2)) + "mn";
}
else {
// h = ms/1000/60/60
return ftoa(number*0.001/60.0/60.0, precision, max(0, size-1)) + "h";
}
}
String htoa(uint32_t number, uint8_t size) {
String s = "";
while ( number>0 ) {
s = String("0123456789abcdef")[number%16] + s;
number = number>>4;
}
if ( (size>0) && (s.length()>size) ) return("#");
while ( s.length()<size ) s = "0"+s;
return s;
}
// Object
ProxySerial::ProxySerial(Stream * port0) {
_proxyPort = port0;
}
void ProxySerial::_checkSpeed() {
// while(!(millis()-_millis > 3)); // time in ms
while((millis()-_millis < securityDelay)); // time in ms, the same !> = <
_millis=millis();
}
void ProxySerial::begin(uint16_t b) { // to be managed at serial port level
_checkSpeed();
// _proxyPort->begin(b);
}
void ProxySerial::setXY16(boolean b) {
// true = 16-bits coordinates for Picaso
// false = 8-bits coordinates for Goldelox
_XY16 = b;
}
void ProxySerial::print(int8_t i) {
_checkSpeed();
_proxyPort->print(i);
}
void ProxySerial::print(uint8_t ui) {
_checkSpeed();
_proxyPort->print(ui);
};
void ProxySerial::print(int16_t i) {
_checkSpeed();
_proxyPort->print(highByte(i));
_proxyPort->print(lowByte(i));
};
void ProxySerial::print(uint16_t ui) {
_checkSpeed();
_proxyPort->print(highByte(ui));
_proxyPort->print(lowByte(ui));
};
void ProxySerial::printXY(int16_t i) {
_checkSpeed();
if ( _XY16 ) _proxyPort->print(highByte(i));
_proxyPort->print(lowByte(i));
};
void ProxySerial::printXY(uint16_t ui) {
_checkSpeed();
if ( _XY16 ) _proxyPort->print(highByte(ui));
_proxyPort->print(lowByte(ui));
};
void ProxySerial::print(char c) {
_proxyPort->print((uint8_t)c);
};
void ProxySerial::print(String s) {
for (uint8_t i=0; i<s.length(); i++) {
// _checkSpeed();
_proxyPort->print(s.charAt(i));
}
}
uint8_t ProxySerial::read() {
_checkSpeed();
return _proxyPort->read();
}
int8_t ProxySerial::available() {
_checkSpeed();
return _proxyPort->available();
}
void ProxySerial::flush() {
_checkSpeed();
_proxyPort->flush();
}