/
PCA.cpp
365 lines (327 loc) · 8.11 KB
/
PCA.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
PCA85162.h -
Uses the Wire.h library
Created by Neal Meeker, February 25, 2013.
Created for TEC (The Everything Corp, LLC)
*/
#include <stdint.h>
#if (ARDUINO >= 100)
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <Wire.h>
#include "PCA.h"
//############################### Alfa Num Table #######################################
//void PCA::setTable(){
uint16_t alphaNumTable[] = {
0x0000, /* space 32*/
0x3C3C, /* ( 40*/
0xF00F, /* ) 41*/
0x00C0, /* - 45 */
//0x3000, /* . 46 set a different way... */
0x0CC3, /* / 47*/
0xFC3F, /* 0 */
0xC003, /* 1 */
0x3CCF, /* 2 */
0xF0CF, /* 3 */
0xC0F3, /* 4 */
0xF0FC, /* 5 */
0xFCFC, /* 6 */
0xC00F, /* 7 */
0xFCFF, /* 8 */
0xF0FF, /* 9 */
0xCCFF, /* a 65*/
0xFCF0, /* b */
0x3C3C, /* C */
0xFCC3, /* d */
0x3CFC, /* E */
0x0CFC, /* F */
0xF0FF, /* g */
0xCCF3, /* h */
0x0C30, /* i */
0xFC03, /* j */
0x3000, /* k.. No K */
0x3C30, /* L */
0x3000, /* M.. No M */
0xCCC0, /* N */
0xFC3F, /* O */
0x0CFF, /* P */
0x3000, /* q... No Q.. */
0x0CC0, /* r */
0xF0FC, /* s */
0x3000, /* t.. no t */
0xFC33, /* u */
0xFC00, /* v... Same as v? */
0x3000, /* w... No W... */
0x3000, /* x.. No X */
0xF0F3, /* y */
0x3CCF, /* z 90*/
0x3C3C, /* [ 91*/
0xC003 , /* \ ... shouldn't print.. 92*/
0xF00F, /* ] 93*/
0x3000, /* 94*/
0x3000, /* _ 95*/
0x0C30 /* | 124*/
};
//############################### Alfa Num Table #######################################
//####################################### Setting up i2c #######################
//Starts and sets up PCA chip to work with screen.
PCA::PCA()
{
//PCA(0b00111000); //defaul I2C address
}
void PCA::start()
{
start(0b00111000);
}
void PCA::start(byte i2c_addr)
{
_i2c_addr = i2c_addr; //Sets address for use later
//setTable();
posit = 0;
delay(2); //PCA needs to reset before initializing I2C.. takes a millesecond or 2
Wire.begin();
delay(2);
Wire.beginTransmission(_i2c_addr);
Wire.write(LCD_MODE_COMMAND | DISPLAY_ENABLE | LCD_MODE_SET_2MULTIPLEX);
//Chip Settings for Command Mode: Enabled and 2xMultiplex (DEFAULT)
//Sending this command to PCA chip sets the chip to receive data and display correctly
Wire.endTransmission();
}
//####################################### End Setting up i2c #######################
//############################################ Blink Mode ##############################
//PCA.Blink(1); = Fast Blink, 2 = Medium, 3 = Slow, 0 turns off screen, anything else turns off blink mode
void PCA::blink(uint8_t i)
{
Wire.beginTransmission(_i2c_addr);
if(i < 4)
{
Wire.write(Cont | BLINK_SELECT | ALTERNATE_BLINK | i); //Turn on Blink
}
else
{
Wire.write(((Cont | BLINK_SELECT) | ALTERNATE_BLINK) & 3); //Turn off Blink
}
Wire.endTransmission();
}
//######################################## End Blink Mode ##############################
//############################################ Draw Colon: #############################
//Turn Colon on or off
void PCA::drawColon(boolean state) {
if (state)
displayBuff[3] = displayBuff[3] | DECI;
else
displayBuff[3] = displayBuff[3] &= ~DECI;
}
//######################################## End Draw Colon: #############################
//########################################## Update Display ############################
//Updates the display to show the newest information
void PCA::updateDisplay(void){
Wire.beginTransmission(_i2c_addr);
Wire.write(0x00); //Set DataPointer to 0
if (colon == 1){
drawColon(1);
colon = 0;
}
else {
drawColon(0);
}
for (uint8_t i=0; i<4; i++) {
uint16_t tx = displayBuff[i];
Wire.write((unsigned char *)&tx, 2);
}
Wire.endTransmission();
posit = 0;
}
//###################################### End Update Display ############################
//######################################### Clear Display Buffer #######################
void PCA::clearBuff(void) {
for (uint8_t i=0; i<4; i++) {
displayBuff[i] = 0;
}
}
//##################################### End Clear Display Buffer #######################
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
//##################################### Printing Char ##################################
void PCA::print(char *str){
int x;
for (x = 0; str[x] != '\0'; x++)
{
print(str[x], posit);
}
}
void PCA::print(char *str, int pos){
int x;
for (x = 0; str[x] != '\0'; x++)
{
print(str[x], pos);
pos += 1;
}
}
void PCA::print(char c){
print(c, posit);
}
void PCA::print(char c, int pos){
if (c == 46){
if (pos < 4){
displayBuff[posit - 1] = displayBuff[posit - 1] | DECI;
}
}
else if(c == 32){
displayBuff[pos] = alphaNumTable[c - 32];
posit = pos + 1;
}
else if(c == 58){
colon = 1;
}
else if (c == 40){
displayBuff[pos] = alphaNumTable[c - 39];
posit = pos + 1;
}
else if (c == 41){
displayBuff[pos] = alphaNumTable[c - 39];
posit = pos + 1;
}
else if (c == 45){
displayBuff[pos] = alphaNumTable[c - 42];
posit = pos + 1;
}
else if ((c > 46) & (c < 58)){
displayBuff[pos] = alphaNumTable[c - 43];
posit = pos + 1;
}
else if((c > 64) & (c < 96)){
displayBuff[pos] = alphaNumTable[c - 50];
posit = pos + 1;
}
else if((c > 96) & (c < 126)){
displayBuff[pos] = alphaNumTable[c - 82];
posit = pos + 1;
}
else{
displayBuff[pos] = alphaNumTable[45];
posit = pos + 1;
}
}
//################################### End Printing Char ################################
//#################################### Print Num #######################################
void PCA::print(unsigned int ui){
print((int) ui);
}
void PCA::print( unsigned int ui, int pos){
print((int) ui, pos);
}
void PCA::print(int i){
// Handle negative numbers
if (i < 0.0)
{
print('-');
i = -i;
}
split(i,posit);
}
void PCA::println(int i){
int a = i / 1000;
int b = i % 1000;
b = b / 100;
int c = i % 100;
c = c / 10;
int d = i % 10;
print(a,0);
print(b,1);
print(c,2);
print(d,3);
}
// Split breaks the integer into single digits
void PCA::split(int i, int pos){
int arry[4];
int posi = pos;
int a = i / 1000;
int b = i % 1000;
b = b / 100;
int c = i % 100;
c = c / 10;
int d = i % 10;
arry[0] = d;
arry[1] = c;
arry[2] = b;
arry[3] = a;
boolean ready = false;
for (int x = 3; x >= 0; x--)
{
if (arry[x] > 0){
ready = true;
}
if (ready == true){
print(arry[x],posi);
posi++;
}
}
}
void PCA::print(int i, int pos){
if (i < 10){
displayBuff[pos] = alphaNumTable[i + 5];
posit = pos + 1;
}
else{
split(i, pos);
}
}
void PCA::print(long i){
print((int) i);
}
void PCA::print(long i,int pos){
print((int) i, pos);
}
void PCA::print(unsigned long i){
print((int) i);
}
void PCA::print(unsigned long i,int pos){
print((int) i, pos);
}
// digits would be the number of decimal places to round to
void PCA::print(float f, int digits){
printFloat((double) f, digits);
}
void PCA::print(float n){
printFloat((double) n, 2); //defaulted 2 decimal points
}
void PCA::print(double n){
printFloat(n,2);
}
void PCA::print(double n, int digits)
{
printFloat(n, digits);
}
//printFloat is used to handle floats & doubles
void PCA::printFloat(double number, uint8_t digits)
{
// Handle negative numbers
if (number < 0.0)
{
print('-');
number = -number;
}
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i=0; i<digits; ++i)
rounding /= 10.0;
number += rounding;
// Extract the integer part of the number and print it
unsigned int int_part = (unsigned int)number;
double remainder = number - (double)int_part;
print(int_part);
// Print the decimal point, but only if there are digits beyond
if (digits > 0)
print('.');
// Extract digits from the remainder one at a time
while (digits-- > 0)
{
remainder *= 10.0;
int toPrint = int(remainder);
print(toPrint);
remainder -= toPrint;
}
}
//################################# End Print Num ##########################################