-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathLcd.cpp
More file actions
129 lines (107 loc) · 2.98 KB
/
Lcd.cpp
File metadata and controls
129 lines (107 loc) · 2.98 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
#include "GrowduinoFirmware.h"
//#include <LiquidCrystal.h>
#ifdef DISPLAY_2004
#include <LiquidCrystal_I2C.h>
extern LiquidCrystal_I2C lcd;
#else
#include <Adafruit_MCP23017.h>
#include <Adafruit_RGBLCDShield.h>
extern Adafruit_RGBLCDShield lcd;
#endif
char lcd_lines[LCD_BUFFER_LINES][17];
int lcd_last_printed_line, inserted_lines;
long lastrun;
void lcd_setup() {
#ifdef DISPLAY_2004
lcd.init();
lcd.backlight();
#else
lcd.begin(16, 2);
lcd.setBacklight(0x07);
#endif
lcd_flush();
lcd_publish(F("Initialising LCD"));
lcd_tick();
lastrun = -1;
}
void lcd_publish(char * msg) {
// Inserts msg into buffer
SERIAL.print(F("lcd_publish: "));
SERIAL.println(msg);
if (inserted_lines < LCD_BUFFER_LINES) {
strlcpy((char * ) lcd_lines[inserted_lines], msg, 17);
inserted_lines += 1;
lastrun = -1;
} else {
lcd_print_immediate(F("lcd buf overflow"));
}
}
void lcd_publish(const __FlashStringHelper * msg) {
// inserts msg into buffer from flash
SERIAL.print(F("lcd_publish: "));
SERIAL.println(msg);
if (inserted_lines < LCD_BUFFER_LINES) {
strlcpy_P((char * ) lcd_lines[inserted_lines], (char *) msg, 17);
inserted_lines += 1;
lastrun = -1;
} else {
lcd_print_immediate(F("lcd buf overflow"));
}
}
void lcd_publish(const char * text, const char * format, int data) {
lcd_publish(text, format, data, 0);
}
void lcd_publish(const char * text, const char * format, int data, float divisor) {
char lcd_msg[18];
if (data == MINVALUE) {
snprintf(lcd_msg, 17, "%s read error", text);
} else {
if (divisor == 0) {
snprintf(lcd_msg, 17, format, text, data);
} else if (divisor < 1) {
snprintf(lcd_msg, 17, format, text, (int) (data / divisor));
} else {
snprintf(lcd_msg, 17, format, text, (int) (data / divisor), abs(data % (int) divisor));
}
}
lcd_publish(lcd_msg);
}
void lcd_print_immediate(const __FlashStringHelper * msg) {
// injects msg from flash just after buffer start and displays it
if (inserted_lines > 1) {
strlcpy((char * ) lcd_lines[0], lcd_lines[inserted_lines - 1], 17);
inserted_lines = 2;
} else {
inserted_lines += 1;
}
strlcpy_P((char * ) lcd_lines[inserted_lines - 1], (char *) msg, 17);
SERIAL.println(lcd_lines[inserted_lines - 1]);
lastrun = -1;
lcd_last_printed_line = 0;
lcd_tick();
}
void lcd_flush() {
// cleans buffer
lcd_last_printed_line = 0;
lcd.setCursor(0, 0);
for (int i = 0; i < LCD_BUFFER_LINES; i++) {
lcd_lines[i][0] = '\0';
}
inserted_lines = 0;
lastrun = -1;
}
void lcd_tick() {
long currrun = millis() / (1000 * LCD_DISPLAY_LINES);
if (currrun != lastrun) {
lastrun = currrun;
int lines_to_print = min(LCD_DISPLAY_LINES, inserted_lines);
lcd.clear();
for (int i = 0; i < lines_to_print; i++) {
lcd.setCursor(0, i);
lcd.print(lcd_lines[lcd_last_printed_line]);
lcd_last_printed_line += 1;
if (lcd_last_printed_line >= inserted_lines)
lcd_last_printed_line = 0;
}
}
}