-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLCD.pde
170 lines (137 loc) · 4.2 KB
/
LCD.pde
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
class LCD {
Serial device;
color blue = #0041FF;
PFont consola;
private int nX = 16, nY = 4;
private String CLEAR_SCREEN = "/CLS/";
private String contents [];
private String lastPortContentString;
private String lastNonNullPortContentString;
private float pixelDimension;
private float pixelGap;
private float nPixelX = 5, nPixelY = 8;
private float boxW, boxH;
private float gap;
private float startX, startY;
private char bufferUntilChar = '\n';
private void init (int nX, int nY, Serial serial) {
device = serial;
device.bufferUntil(bufferUntilChar);
consola = createFont("data/fonts/consola.ttf", 10);
this.nX = nX;
this.nY = nY;
clear ();
pixelDimension = 3.2;
pixelGap = 1;
gap = pixelDimension;
boxW = pixelGap*(nPixelX - 1) + pixelDimension*nPixelX;
boxH = pixelGap*(nPixelY - 1) + pixelDimension*nPixelY;
startX = (width - boxW*nX - gap*(nX - 1))/2;
startY = (height - boxH*nY - gap*(nY - 1))/2;
}
private LCD (int nX, int nY, Serial serial) {
init (nX, nY, serial);
}
LCD (int nX, int nY, Serial serial, char bufferUntilChar) {
this.bufferUntilChar = bufferUntilChar;
init (nX, nY, serial);
}
void clear () {
contents = new String [nY];
String content = "";
for (int a = 0; a < nY; a ++) {
for (int b = 0; b < nX; b ++)
content += " ";
contents [a] = content;
}
}
void draw () {
background(blue);
strokeWeight(1);
stroke(255, 40);
noFill ();
textFont(consola);
textAlign(CENTER, CENTER);
textSize(boxH*0.8);
for (int b = 0; b < nY; b ++) {
for (int a = 0; a < nX; a ++) {
float x = startX + boxW*a + gap*a, y = startY + boxH*b + gap*b;
rect(x, y, boxW, boxH);
try {
char charToDisplay = contents [b].charAt(a);
if (str(charToDisplay).length() > 0) {
text(charToDisplay, x + boxW/2, y + boxH/2 - textDescent ()/2);
}
}
catch (Exception e) {
}
}
}
overlay();
}
void overlay () {
strokeWeight(pixelGap);
stroke(blue, 40);
for (int b = 0; b < nY; b ++) {
for (int a = 0; a < nX; a ++) {
float x = startX + boxW*a + gap*a, y = startY + boxH*b + gap*b;
for (int c = 1; c < nPixelY; c ++) {
line(x, y + c*pixelGap + c*pixelDimension, x + boxW, y + c*pixelGap + c*pixelDimension);
}
for (int c = 1; c < nPixelX; c ++) {
line(x + c*pixelGap + c*pixelDimension, y, x + c*pixelGap + c*pixelDimension, y + boxH);
}
}
}
}
void print (String text, int x, int y) {
String content = "";
if (x >= nX ||y >= nY)
return;
for (int a = 0; a < nX; a ++) {
if (a < x)
content += contents [y].charAt(a);
else {
int counter = 0;
for (int b = a; b < nX; b ++) {
if (counter < text.length ()) {
content += text.charAt(counter ++);
} else
content += contents [y].charAt (b);
}
break;
}
}
contents [y] = content;
}
void serialEvent () throws Exception {
String content = device.readString ();
lastPortContentString = content;
if (content != null) {
lastNonNullPortContentString = content;
content = content.replace(str(bufferUntilChar), "").replace("\r", "");
if (content.equals(lcd.CLEAR_SCREEN)) {
lcd.clear ();
} else if (content.contains("~")) {
String splitted [] = split(content, "~");
if (splitted.length < 3 || !content.contains(","))
return;
String text = splitted [1];
String location = splitted [2];
splitted = split(location, ",");
if (splitted.length > 2) {
int x, y;
x = Integer.parseInt(splitted [0]);
y = Integer.parseInt(splitted [1]);
lcd.print(text, x, y);
}
}
}
}
String lastPortContent () {
return lastPortContentString;
}
String lastNonNullPortContent () {
return lastNonNullPortContentString;
}
}