-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduino_Code.ino
More file actions
428 lines (321 loc) · 10.8 KB
/
Copy pathArduino_Code.ino
File metadata and controls
428 lines (321 loc) · 10.8 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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//LCD dash firmware made by P-rth 7/12/23
//
/*
Curcuit :
A7 --> 5.6k ohms --> Groud
Reset --> 22uf capacitor --> switch -- Groud (turn off to upload code)
LDR (+) --> Vin / 5v
LDR (-) --> A7
Backlight + (LCD A) --> D10
Backlight - (LCD K) --> Groud
LCD RS --> 12
LCD Enable --> 11
LCD D4 --> 5
LCD D5 --> 4
LCD D6 --> 3
LCD D7 --> 2
*/
#include <LiquidCrystal.h>
#include <LcdProgressBarDouble.h>
#include <Smooth.h>
//============================
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
char appFromPC[20] = {0};
char oldappFromPC[20] = {0};
char timeFromPC[numChars] = {0};
int dayperFromPC = 0;
int cpuFromPC = 0; // variables to hold the parsed data
int memFromPC = 0;
boolean newData = false;
//============================
int backlight=10;
int y; // y is the real time backlight brightess using ldr
int backlight_bright = 150; // startup bright
int inputPin = A7;
#define SMOOTHED_SAMPLE_SIZE 200
Smooth average(SMOOTHED_SAMPLE_SIZE); // Smoothing average object
int sensorValue1 = 0;
int sample = 0; // Simulated moving sample
int sensorMin = 0; // 100% sensor value
int sensorMax = 250; // 0% sensor value
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // creating LCD instance
float bright_steps = 0;
//=================================================================
LcdProgressBarDouble lpg_day_bright(&lcd, 1, 8, 9); // -- creating Object lenght 8 row 2 col 9
LcdProgressBarDouble lpg_cpu_mem(&lcd, 1, 8); // -- creating
//=================================================================
unsigned long interval_timeout = 5000;
unsigned long previous_serial_Millis = 0;
#include <SoftwareReset.hpp>
//==================================================================
bool FirstStart = true;
int aniframe_num = 0;
unsigned long previousMillis = 0;
unsigned long previousMillis1 = 0;
const long interval = 100;
const long interval1 = 50;
int total_frames = 17;
bool anifwd = true;
String aniframe[17] = { "| ",
" \x01 ",
" - ",
" / ",
" | ",
" \x01 ",
" - ",
" / ", //loading animation frames
" | ",
" \x01 ",
" - ",
" / ",
" | ",
" \x01 ",
" - ",
" /",
" |", };
byte customBackslash[8] = {
0b00000,
0b10000,
0b01000,
0b00100,
0b00010,
0b00001,
0b00000,
0b00000
};
byte d_pg_char[8] = {
B00000,
B00000,
B00000,
B00000,
B00000,
B11000,
B11000,
B00000
};
//===============================================================
//================================================================
void setup() {
Serial.begin(2000000);
Serial.println("Enter data in format: <time[8],app[8],day[0-100],cpu[0-100],mem[0-100]>"); //start serial
lcd.begin(2,16);
lpg_day_bright.setMinValues(0);
lpg_cpu_mem.setMinValues(0);
lpg_day_bright.setMaxValues(100);
lpg_cpu_mem.setMaxValues(100);
lcd.createChar(1, customBackslash);
analogWrite(6,0); //cotrast pin
}
//===============================================================
char oldtempChars[numChars];
bool lcd_sens_updates = true;
void loop() {
recvWithStartEndMarkers();
if (FirstStart == false){
takereading();
if (lcd_sens_updates == true){
lpg_day_bright.draw(dayperFromPC, sensorValue1);
}
y = map(sensorValue1, 0, 100, 50, 255);
analogWrite(backlight, y);
}
if (newData == true) {
if (oldtempChars != tempChars) {
newdata();
strcpy(oldtempChars , tempChars);
}
}
else if (FirstStart == true && newData == false){
loadinganimation();
}
if (FirstStart == false && newData == false){
unsigned long cMillis = millis();
if (cMillis - previous_serial_Millis >= interval_timeout) {
// Serial.println("Resetting");
bright_steps = (150 - y)/16; //150 is the brightess target as it is the brightess o startup
for (byte j = 0; j < 16; ++j) {
y = y+bright_steps;
delay(100);
analogWrite(backlight,y);
lcd.scrollDisplayRight();
}
softwareReset::simple();
}
}
}
//================================================================
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//================================================================
void parseData() { // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars,","); // get the first part - the string
strcpy(timeFromPC, strtokIndx);
strtokIndx = strtok(NULL,","); //secod part of the string - the app
strcpy(appFromPC, strtokIndx);
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
dayperFromPC = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
cpuFromPC = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
memFromPC = atoi(strtokIndx); // convert this part to an integer
}
//================================================================
void showParsedData() {
Serial.print("timeFromPC : ");
Serial.println(timeFromPC);
Serial.print("appFromPC : ");
Serial.println(appFromPC);
Serial.print("day%FromPC : ");
Serial.println(dayperFromPC);
Serial.print("cpuFromPC : ");
Serial.println(cpuFromPC);
Serial.print("memFromPC : ");
Serial.println(memFromPC);
}
//==================================================================
void takereading() {
sample = analogRead(inputPin);
average += sample;
sensorValue1 = constrain((int) average(), sensorMin, sensorMax);
sensorValue1 = map(sensorValue1, sensorMin, sensorMax, 0, 100);
if (newData == true) {
Serial.println(sensorValue1);
}
}
//=================================================================
class RepeatedString : public Printable
{
public:
RepeatedString(const char * str, unsigned int count)
: _str(str), _count(count) {}
size_t printTo(Print& p) const
{
size_t sz = 0;
for (unsigned int i = 0; i < _count; i++)
sz += p.print(_str);
return sz;
}
private:
const char * _str;
unsigned int _count;
};
//=================================================================
void updatelcd() {
//lcd.clear();
lpg_cpu_mem.draw(cpuFromPC,memFromPC);
if (strlen(appFromPC) < 11)
{
lcd_sens_updates = true;
lcd.setCursor(11,0);
lcd.print(timeFromPC);
lpg_day_bright.draw(dayperFromPC, sensorValue1);
}
else if (strlen(appFromPC) >= 11){
if (lcd_sens_updates != false) {
lcd_sens_updates = false;
lcd.setCursor(11,0);
lcd.print(" ");
lcd.setCursor(9,1); //clear previous text for new layout
lcd.print(" ");
}
lcd.setCursor(10,1);
lcd.print(timeFromPC);
}
if (strcmp(oldappFromPC,appFromPC) != 0) {
lcd.home();
lcd.print(RepeatedString(" ",strlen(oldappFromPC))); //mask the old text
strcpy(oldappFromPC,appFromPC );
lcd.home();
lcd.print(appFromPC);
}
lcd.setCursor(8,1);
lcd.print("|");
}
//==================================================================
void loadinganimation(){
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// if frame less than total_frames add frame else set to 0
if (anifwd == true){
if (aniframe_num < total_frames) {
aniframe_num++;
} else {
anifwd = false;
}
}
else{
if (aniframe_num > 0) {
aniframe_num--;
} else {
anifwd = true;
}
}
lcd.home();
lcd.print("[- Connecting -]");
lcd.setCursor(0, 1);
lcd.print(aniframe[aniframe_num]);
}
if (currentMillis - previousMillis1 >= interval1) {
previousMillis1 = currentMillis;
if (backlight_bright > 5){
analogWrite(backlight,backlight_bright);
backlight_bright = backlight_bright-1;
}
}
}
//==================================================================
void newdata(){
previous_serial_Millis = millis();
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
parseData();
//showParsedData();
if (FirstStart == true){
takereading();
y = map(sensorValue1, 0, 100, 50, 255);
bright_steps = (y - backlight_bright)/16;
for (byte j = 0; j < 16; ++j) {
backlight_bright = backlight_bright+bright_steps;
delay(100);
analogWrite(backlight,backlight_bright);
lcd.scrollDisplayLeft();
}
lcd.createChar(1, d_pg_char);
lcd.clear();
analogWrite(6,30); //cotrast pin
FirstStart = false;
}
updatelcd();
newData = false;
}