-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFED4_Display.cpp
More file actions
631 lines (524 loc) · 16.9 KB
/
Copy pathFED4_Display.cpp
File metadata and controls
631 lines (524 loc) · 16.9 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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
#include "FED4.h"
#define SHARPMEM_BIT_WRITECMD (0x01) // 0x80 if writing, 0x00 if reading
#define SHARPMEM_BIT_VCOM (0x02) // ROM_IN pin state
#define SHARPMEM_BIT_CLEAR (0x04) // CL pin state
#ifndef _swap_int16_t
#define _swap_int16_t(a, b) \
{ \
int16_t t = a; \
a = b; \
b = t; \
}
#endif
// Add these lookup tables at the top of the file
static const uint8_t PROGMEM set[] = {1, 2, 4, 8, 16, 32, 64, 128},
clr[] = {(uint8_t)~1, (uint8_t)~2, (uint8_t)~4,
(uint8_t)~8, (uint8_t)~16, (uint8_t)~32,
(uint8_t)~64, (uint8_t)~128};
void FED4::updateDisplay() {
setFont(&FreeSans9pt7b);
setTextSize(1);
setTextColor(DISPLAY_BLACK);
// Check if this is ActivityMonitor program
if (program == "ActivityMonitor") {
displayActivityMonitor();
} else {
displayTask();
displayMouseId();
// draw line to split on screen text
//drawLine(0,59,168,59, DISPLAY_BLACK);
drawLine(0,60,168,60, DISPLAY_BLACK);
// draw screen elements
displayEnvironmental();
displayBattery();
displaySDCardStatus();
displayCounters();
displayIndicators();
displayDateTime();
}
refresh();
}
void FED4::displayActivityMonitor() {
// Use the same layout as normal FED4 display but replace counters and indicators
displayTask();
displayMouseId();
// draw line to split on screen text
drawLine(0,59,168,59, DISPLAY_BLACK);
drawLine(0,60,168,60, DISPLAY_BLACK);
// draw screen elements (same as normal display)
displayEnvironmental();
displayBattery();
displaySDCardStatus();
// Replace displayCounters() with activity information
displayActivityCounters();
displayDateTime();
}
void FED4::displayActivityCounters() {
setFont(&FreeSans9pt7b);
setTextSize(1);
setTextColor(DISPLAY_BLACK);
// Clear all counter value areas with one white rectangle (same as displayCounters)
fillRect(90, 68, 50, 78, DISPLAY_WHITE); // Clear area for all counter values
setCursor(6, 80);
print("Activity ");
setCursor(90, 80);
print(motionCount);
setCursor(6, 100);
print("Activity% ");
setCursor(90, 100);
// Display motion percentage (calculated in real-time by motion() and pollSensors())
printf("%.1f", motionPercentage);
setCursor(6, 120);
print("Seconds");
setCursor(90, 120);
// Initialize pollSensorsTimer if it hasn't been set yet
if (pollSensorsTimer == 0) {
pollSensorsTimer = millis();
}
//print elapsed seconds since pollSensorsTimer was reset
print((millis() - pollSensorsTimer) / 1000);
setCursor(6, 140);
print("Uptime(h)");
setCursor(90, 140);
// Calculate total uptime in hours with 2 decimal places
float uptimeHours = millis() / 1000.0 / 3600.0;
printf("%.2f", uptimeHours);
}
void FED4::displayTask() {
setFont(&FreeSans9pt7b);
setTextSize(1);
setTextColor(DISPLAY_BLACK);
if (program == "SequenceLearning") {
// Display sequence information
setCursor(6, 35);
print("Seq:");
// Clear area for sequence display
fillRect(40, 35, 120, 30, DISPLAY_WHITE);
if (currentSequence.length() > 0) {
setCursor(50, 35);
// Display each character in the sequence (show entire required sequence)
for (int i = 0; i < currentSequence.length(); i++) {
char c = currentSequence[i];
// Show the entire required sequence for the current level
if (i < currentSequenceLevel) {
// Required sequence items - all with black background, white text
fillRect(43 + (i * 12), 20, 19, 19, DISPLAY_BLACK);
setTextColor(DISPLAY_WHITE);
} else {
// Future level items - white background, black text
fillRect(43 + (i * 12), 20, 19, 19, DISPLAY_WHITE);
setTextColor(DISPLAY_BLACK);
}
setCursor(45 + (i * 12), 35);
print(c);
}
}
} else {
// Display regular program name (limited to first 8 characters)
setCursor(6, 35);
print("Task: ");
fillRect(50, 20, 110, 20, DISPLAY_WHITE); // Clear area for task name
String shortProgram = program;
if (shortProgram.length() > 8) {
shortProgram = shortProgram.substring(0, 8);
}
print(shortProgram);
}
}
void FED4::displayMouseId() {
setFont(&FreeSans9pt7b);
setTextSize(1);
setTextColor(DISPLAY_BLACK);
if (!sdCardAvailable) {
// Show SD card error instead of MouseID
setCursor(6, 54);
fillRect(6, 41, 120, 16, DISPLAY_WHITE); // Clear area for mouse ID and label
print("SD Card error!");
} else {
// Show normal MouseID
setCursor(6, 54);
print("MouseID: ");
char idStr[6];
int mouseIdNum = mouseId.toInt();
if (mouseIdNum == 0 && mouseId[0] != '0') {
// Handle invalid conversion - just display the original string truncated to 4 chars
snprintf(idStr, sizeof(idStr), "%.4s", mouseId.c_str());
} else {
snprintf(idStr, sizeof(idStr), "%04d", mouseIdNum % 10000);
}
fillRect(82, 41, 80, 16, DISPLAY_WHITE); // Clear area for mouse ID
print(idStr);
}
}
void FED4::displaySex(){
setFont(&FreeSans9pt7b);
setTextSize(1);
setTextColor(DISPLAY_BLACK);
setCursor(6, 71);
print("Sex: ");
fillRect(48, 58, 110, 16, DISPLAY_WHITE); // Clear area for sex name
print(sex);
}
void FED4::displayStrain(){
setFont(&FreeSans9pt7b);
setTextSize(1);
setTextColor(DISPLAY_BLACK);
setCursor(6, 89);
print("Strain: ");
fillRect(60, 76, 160, 16, DISPLAY_WHITE); // Clear area for strain name
print(strain);
}
void FED4::displayAge(){
setFont(&FreeSans9pt7b);
setTextSize(1);
setTextColor(DISPLAY_BLACK);
setCursor(6, 107);
print("Age:");
fillRect(42, 94, 160, 16, DISPLAY_WHITE); // Clear area for age name
print(age);
print(" months");
}
void FED4::displayEnvironmental(){
//try to make text inverse white on black
fillRect (0, 0, 144, 17, DISPLAY_BLACK);
setFont(&Org_01);
setTextSize(2);
setTextColor(DISPLAY_WHITE);
setCursor(5, 9);
print((int)temperature);
drawCircle(30, 3, 2, DISPLAY_WHITE);
drawCircle(31, 3, 2, DISPLAY_WHITE);
setCursor(35, 9);
print("C");
// Add speaker muted icon if audio is silenced
if (audioSilenced) {
setCursor(55, 9);
print("X"); // X means no audio
}
}
void FED4::displayBattery(){
//battery graphic
fillRect (80, 1, 18, 10, DISPLAY_WHITE); //body
fillRect (82, 3, 14, 6, DISPLAY_BLACK); //body
fillRect (99, 3, 2, 6, DISPLAY_WHITE); //terminal
fillRect (82, 2, (int)((cellVoltage)/7), 8, DISPLAY_WHITE); //fill
//battery text
setFont(&Org_01);
setTextSize(2);
setTextColor(DISPLAY_WHITE);
setCursor(105, 9);
print(cellVoltage, 1);
print("V");
}
void FED4::displaySDCardStatus() {
}
void FED4::displayCounters()
{
setFont(&FreeSans9pt7b);
setTextSize(1);
setTextColor(DISPLAY_BLACK);
// Clear all counter value areas with one white rectangle
fillRect(90, 68, 50, 78, DISPLAY_WHITE); // Clear area for all counter values
setCursor(30, 80);
print("Left: ");
setCursor(90, 80);
print(leftCount);
setCursor(30, 100);
print("Center: ");
setCursor(90, 100);
print(centerCount);
setCursor(30, 120);
print("Right: ");
setCursor(90, 120);
print(rightCount);
setCursor(30, 140);
print("Pellets:");
setCursor(90, 140);
print(pelletCount);
}
void FED4::displayIndicators(){
//TODO: add indicators for when touch flags are set
//Left
fillCircle(17, 75, 5, DISPLAY_WHITE);
drawCircle(17, 75, 5, DISPLAY_BLACK);
if (leftTouch) {
fillCircle(17, 75, 5, DISPLAY_BLACK);
}
//Center
fillCircle(17, 95, 5, DISPLAY_WHITE);
drawCircle(17, 95, 5, DISPLAY_BLACK);
if (centerTouch) {
fillCircle(17, 95, 5, DISPLAY_BLACK);
}
//Right
fillCircle(17, 115, 5, DISPLAY_WHITE);
drawCircle(17, 115, 5, DISPLAY_BLACK);
if (rightTouch) {
fillCircle(17, 115, 5, DISPLAY_BLACK);
}
//Pellets
fillCircle(17, 135, 5, DISPLAY_WHITE);
drawCircle(17, 135, 5, DISPLAY_BLACK);
if (pelletPresent) {
fillCircle(17, 135, 5, DISPLAY_BLACK);
}
}
void FED4::displayDateTime() {
setFont(&Org_01);
setTextSize(2);
setTextColor(DISPLAY_WHITE);
// Print date and time at bottom of the screen
fillRect(0, 146, 144, 22, DISPLAY_BLACK);
DateTime current = rtc.now();
// Buffer for formatting time
char timeStr[6]; // HH:MM\0
char dateStr[9]; // MM.DD.YY\0
// Format date string
snprintf(dateStr, sizeof(dateStr), "%02d.%02d.%02d",
current.month(),
current.day(),
current.year() - 2000);
// Format time string
snprintf(timeStr, sizeof(timeStr), "%02d:%02d",
current.hour(),
current.minute());
// Display formatted strings
setCursor(5, 160);
print(dateStr);
setCursor(94, 160);
print(timeStr);
}
// Displays a low battery warning with an icon and message
void FED4::displayLowBatteryWarning() {
clearDisplay();
setFont(&FreeSans9pt7b);
setTextSize(1);
setTextColor(DISPLAY_BLACK);
// Draw battery outline (x=40, y=40, width=60, height=30)
int bx = 40, by = 40, bw = 60, bh = 30;
drawRect(bx, by, bw, bh, DISPLAY_BLACK); // Battery body
fillRect(bx + bw, by + 8, 6, 14, DISPLAY_BLACK); // Battery terminal
// Draw empty battery (just outline, no fill)
// Draw exclamation mark inside battery
int ex = bx + bw/2 - 2;
int ey = by + 6;
fillRect(ex, ey, 4, 12, DISPLAY_BLACK); // vertical bar
fillRect(ex, ey + 16, 4, 4, DISPLAY_BLACK); // dot
// Draw warning text
setCursor(10, by + bh + 30);
print("LOW BATTERY");
setCursor(12, by + bh + 50);
print("Please charge!");
refresh();
}
/**
* Initializes the Sharp Memory Display
*
* @return bool - true if initialization successful, false if buffer allocation fails
*
* This function:
* - Sets up the display CS pin and SPI bit order
* - Allocates display buffer memory
* - Sends initial clear command to display
* - Configures default display settings (rotation, font, text properties)
* - Performs initial refresh
*/
bool FED4::initializeDisplay()
{
pinMode(DISPLAY_CS, OUTPUT);
digitalWrite(DISPLAY_CS, LOW); // Display inactive = LOW
SPI.setBitOrder(LSBFIRST);
// Initialize the display buffer
if (displayBuffer)
{
free(displayBuffer);
displayBuffer = nullptr;
}
uint16_t bufferSize = (DISPLAY_WIDTH * DISPLAY_HEIGHT) / 8;
displayBuffer = (uint8_t *)malloc(bufferSize);
if (!displayBuffer)
{
Serial.println("Failed to allocate display buffer");
return false;
}
memset(displayBuffer, 0xff, bufferSize);
vcom = false;
// Initialize display with a clear command
SPI.setBitOrder(LSBFIRST);
digitalWrite(DISPLAY_CS, HIGH); // Select display
uint8_t cmd = SHARPMEM_BIT_WRITECMD | SHARPMEM_BIT_CLEAR;
if (vcom)
cmd |= SHARPMEM_BIT_VCOM;
vcom = !vcom;
SPI.transfer(cmd);
SPI.transfer(0x00); // Required trailing byte
digitalWrite(DISPLAY_CS, LOW); // Deselect display
delay(1); // Give display time to process clear command
setRotation(2);
setFont(&FreeSans9pt7b);
setTextSize(1);
setTextColor(DISPLAY_BLACK);
setTextWrap(false);
refresh(); // Initial refresh to ensure display is ready
return true;
}
void FED4::sendDisplayCommand(uint8_t cmd)
{
SPI.setBitOrder(LSBFIRST);
digitalWrite(DISPLAY_CS, HIGH); // Select display (active HIGH)
// Toggle VCOM
cmd |= SHARPMEM_BIT_WRITECMD;
if (vcom)
{
cmd |= SHARPMEM_BIT_VCOM;
}
vcom = !vcom;
SPI.transfer(cmd);
digitalWrite(DISPLAY_CS, LOW); // Deselect display
}
void FED4::clearDisplay()
{
sendDisplayCommand(SHARPMEM_BIT_CLEAR);
delay(1); // Wait for the clear to complete
memset(displayBuffer, 0xff, (DISPLAY_WIDTH * DISPLAY_HEIGHT) / 8);
}
void FED4::refresh()
{
uint16_t i, currentline;
SPI.setBitOrder(LSBFIRST);
digitalWrite(DISPLAY_CS, HIGH);
uint8_t cmd = SHARPMEM_BIT_WRITECMD;
if (vcom)
{
cmd |= SHARPMEM_BIT_VCOM;
}
vcom = !vcom;
SPI.transfer(cmd);
uint8_t bytes_per_line = DISPLAY_WIDTH / 8;
uint16_t totalbytes = (DISPLAY_WIDTH * DISPLAY_HEIGHT) / 8;
// Use static buffer to avoid stack allocation on every call
static uint8_t lineBuffer[22]; // Maximum size needed: bytes_per_line + 2 = 18 + 2 = 20, rounded up to 22
// Send all lines
for (i = 0; i < totalbytes; i += bytes_per_line)
{
// Send address byte (line number)
currentline = ((i + 1) / (DISPLAY_WIDTH / 8)) + 1;
lineBuffer[0] = currentline;
// Copy display data for this line
memcpy(lineBuffer + 1, displayBuffer + i, bytes_per_line);
// Add end of line marker
lineBuffer[bytes_per_line + 1] = 0x00;
// Send the entire line at once
for (uint8_t j = 0; j < bytes_per_line + 2; j++)
{
SPI.transfer(lineBuffer[j]);
}
}
SPI.transfer(0x00);
digitalWrite(DISPLAY_CS, LOW);
}
void FED4::drawPixel(int16_t x, int16_t y, uint16_t color)
{
if ((x < 0) || (x >= DISPLAY_WIDTH) || (y < 0) || (y >= DISPLAY_HEIGHT))
return;
switch (rotation)
{
case 1:
_swap_int16_t(x, y);
x = DISPLAY_WIDTH - 1 - x;
break;
case 2:
x = DISPLAY_WIDTH - 1 - x;
y = DISPLAY_HEIGHT - 1 - y;
break;
case 3:
_swap_int16_t(x, y);
y = DISPLAY_HEIGHT - 1 - y;
break;
}
if (color)
{
displayBuffer[(y * DISPLAY_WIDTH + x) / 8] |= pgm_read_byte(&set[x & 7]);
}
else
{
displayBuffer[(y * DISPLAY_WIDTH + x) / 8] &= pgm_read_byte(&clr[x & 7]);
}
}
void FED4::startupAnimation(){
setTextSize(5);
setFont(&Org_01);
setTextColor(DISPLAY_BLACK);
const char* text = "FED4"; // Text to animate
int textWidth = 28; // Approximate width of each character in pixels
int textX = 144; // Start position off the screen (right side)
int mouseX = 0;
int centerX = (144 - strlen(text) * textWidth) / 2; // Center X position
int textY = 60; // Vertical height of the text
while (textX > centerX) {
// Clear only the buffer (not hardware) to prevent flickering
// Avoid clearDisplay() which sends hardware commands - just clear buffer directly
memset(displayBuffer, 0xff, (DISPLAY_WIDTH * DISPLAY_HEIGHT) / 8);
//draw FED4
fillRect(100, 92, 32, 20, DISPLAY_BLACK); //FED4
fillRect(112, 82, 16, 8, DISPLAY_BLACK); //hopper
fillCircle(108, 98, 3, DISPLAY_WHITE); //poke 1
fillCircle(124, 98, 3, DISPLAY_WHITE); //poke 2
fillCircle(116, 104, 2, DISPLAY_WHITE); //poke 3
// Draw the text sliding in from the right (single render to reduce flickering)
setCursor(textX, textY);
print(text);
// Move the text to the left
textX -= 2; // Adjust the speed by changing this value
mouseX += 1; // Adjust the speed of the mouse
fillRoundRect (mouseX + 25, 92, 15, 10, 7, DISPLAY_BLACK); //head
fillRoundRect (mouseX + 22, 90, 8, 5, 3, DISPLAY_BLACK); //ear
fillRoundRect (mouseX + 30, 94, 1, 1, 1, DISPLAY_WHITE); //eye
//movement of the mouse
if ((mouseX / 10) % 2 == 0) {
fillRoundRect (mouseX, 94, 32, 17, 10, DISPLAY_BLACK); //body
drawFastHLine(mouseX - 8, 95, 18, DISPLAY_BLACK); //tail
drawFastHLine(mouseX - 8, 96, 18, DISPLAY_BLACK);
drawFastHLine(mouseX - 14, 94, 8, DISPLAY_BLACK);
drawFastHLine(mouseX - 14, 95, 8, DISPLAY_BLACK);
fillRoundRect (mouseX + 22, 109, 8, 4, 3, DISPLAY_BLACK); //front foot
fillRoundRect (mouseX , 107, 8, 6, 3, DISPLAY_BLACK); //back foot
}
else {
fillRoundRect (mouseX + 2, 92, 30, 17, 10, DISPLAY_BLACK); //body
drawFastHLine(mouseX - 6, 101, 18, DISPLAY_BLACK); //tail
drawFastHLine(mouseX - 6, 100, 18, DISPLAY_BLACK);
drawFastHLine(mouseX - 12, 102, 8, DISPLAY_BLACK);
drawFastHLine(mouseX - 12, 101, 8, DISPLAY_BLACK);
fillRoundRect (mouseX + 15, 109, 8, 4, 3, DISPLAY_BLACK); //foot
fillRoundRect (mouseX + 8, 107, 8, 6, 3, DISPLAY_BLACK); //back foot
}
// Update the display
refresh();
delay(10); // Increased delay to reduce flickering (was 1ms, now 10ms)
}
// Display the text in the center and hold
setCursor(textX, textY);
print(text);
refresh();
setTextSize(1);
}
void FED4::displayAudio() {
setCursor(6, 125);
print("Audio: ");
print(audioSilenced ? "Off" : "On");
}
// Display initialization status message below startup animation
void FED4::displayInitStatus(const char* message) {
setFont(&FreeSans9pt7b);
setTextSize(1);
setTextColor(DISPLAY_BLACK);
// Clear area for status message (below FED4 logo, around y=100-130)
fillRect(0, 125, 144, 100, DISPLAY_WHITE);
// Display the initialization message
setCursor(6, 135);
print("Initializing: ");
setCursor(6, 158);
print(message);
refresh();
}