-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.ino
More file actions
197 lines (187 loc) · 3.78 KB
/
client.ino
File metadata and controls
197 lines (187 loc) · 3.78 KB
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
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define ADDR 0x27
#define WIDTH 16
#define HEIGHT 2
#define DEG_CHAR 0
#define CHARGING_CHAR 1
#define DISCHARGING_CHAR 2
#define PLAYING_CHAR 3
#define PAUSED_CHAR 4
#define ELLIPSIS_CHAR 5
#define ARROW_UP_CHAR 6
#define ARROW_DOWN_CHAR 7
#define STANDBY_THRESHOLD 3
LiquidCrystal_I2C lcd(ADDR, WIDTH, HEIGHT);
const byte degBytes[] =
{
B00110,
B01001,
B01001,
B00110,
B00000,
B00000,
B00000,
B00000
};
const byte chargingBytes[] =
{
B01010,
B01010,
B11111,
B11111,
B01110,
B00100,
B00100,
B00100
};
const byte dischargingBytes[] =
{
B01110,
B11111,
B10001,
B10001,
B10001,
B10001,
B10001,
B11111
};
const byte playingBytes[] =
{
B00000,
B11011,
B11011,
B11011,
B11011,
B11011,
B11011,
B00000
};
const byte pausedBytes[] =
{
B10000,
B11000,
B11100,
B11110,
B11110,
B11100,
B11000,
B10000
};
const byte ellipsisBytes[] =
{
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B10101,
B00000
};
const byte arrowUpBytes[] =
{
B00100,
B01110,
B11111,
B00100,
B00100,
B00100,
B00100,
B11111
};
const byte arrowDownBytes[] =
{
B00100,
B00100,
B00100,
B00100,
B11111,
B01110,
B00100,
B11111
};
int timesNotResponded = STANDBY_THRESHOLD;
void standby()
{
lcd.noBacklight();
lcd.setCursor(0, 0);
lcd.print("Device not ");
lcd.setCursor(0, 1);
lcd.print("connected ");
}
void writeLine(String line)
{
for (int n = 0; n < WIDTH; n++)
{
if (n >= line.length()) lcd.print(" ");
else if (n == WIDTH - 1 && n + 1 < line.length())
{
lcd.write(ELLIPSIS_CHAR);
return;
}
else
{
switch (line[n])
{
case '^':
lcd.write(DEG_CHAR); // Use 0xDF for the standard degree character
break;
case '`':
lcd.write(CHARGING_CHAR);
break;
case '&':
lcd.write(DISCHARGING_CHAR);
break;
case '#':
lcd.write(PLAYING_CHAR);
break;
case '$':
lcd.write(PAUSED_CHAR);
break;
case '[':
lcd.write(ARROW_UP_CHAR);
break;
case ']':
lcd.write(ARROW_DOWN_CHAR);
break;
default:
lcd.print(line[n]);
}
}
}
}
void setup()
{
lcd.init();
lcd.createChar(DEG_CHAR, degBytes);
lcd.createChar(CHARGING_CHAR, chargingBytes);
lcd.createChar(DISCHARGING_CHAR, dischargingBytes);
lcd.createChar(PLAYING_CHAR, playingBytes);
lcd.createChar(PAUSED_CHAR, pausedBytes);
lcd.createChar(ELLIPSIS_CHAR, ellipsisBytes);
lcd.createChar(ARROW_UP_CHAR, arrowUpBytes);
lcd.createChar(ARROW_DOWN_CHAR, arrowDownBytes);
Serial.begin(9600);
}
void loop()
{
if (Serial.available())
{
if (timesNotResponded >= STANDBY_THRESHOLD) lcd.backlight();
timesNotResponded = 0;
String data = Serial.readStringUntil('\n');
int separatorIndex = data.indexOf(';');
lcd.setCursor(0, 0);
String line1 = data.substring(0, separatorIndex);
writeLine(line1);
lcd.setCursor(0, 1);
String line2 = data.substring(separatorIndex + 1);
writeLine(line2);
}
else if (timesNotResponded == STANDBY_THRESHOLD) standby();
else timesNotResponded++;
/* It's better for this delay to be smaller than the delay of the server
since you have to consider the time it takes to write to the LCD */
delay(900);
}