-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoot4root_VLC.h
148 lines (114 loc) · 3.37 KB
/
Root4root_VLC.h
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
#ifndef _LIB_ROOT4ROOT_VLC_
#define _LIB_ROOT4ROOT_VLC_
#include <Arduino.h>
template <typename T>
class Root4root_VLC
{
public:
Root4root_VLC(T *lcdHandler, uint8_t chars, uint8_t lines, uint8_t offsetX, uint8_t offsetY);
~Root4root_VLC();
void setLine(uint8_t line, bool resetX = true);
void setCursor(uint8_t x = 0, uint8_t y = 0);
void setDelayTime(uint16_t delay);
void resetDelayCounter();
void displayString(char *str);
void write(uint8_t symbol);
void clear();
private:
char *lcdState = NULL;
uint8_t lcdLines{}; //number of lcd lines
uint8_t lcdChars{}; //number of lcd chars in line
uint8_t offsetX{}; //Top-left corner of Virtual LCD
uint8_t offsetY{}; //"-"
uint8_t internalCursorX{};
uint8_t internalCursorY{};
uint32_t previuosChangeTime{};
uint16_t displayDelay{};
T *lcdHandler{}; //NULL PTR
};
template <typename T>
Root4root_VLC<T>::Root4root_VLC(T *lcd, uint8_t chars, uint8_t lines, uint8_t offsetX, uint8_t offsetY):
lcdHandler(lcd), lcdLines(lines), lcdChars(chars), offsetX(offsetX), offsetY(offsetY)
{
lcdState = new char[lines * chars + 1]();
}
template <typename T>
void Root4root_VLC<T>::setDelayTime(uint16_t delay) //Starting from 0
{
displayDelay = delay;
}
template <typename T>
void Root4root_VLC<T>::resetDelayCounter()
{
previuosChangeTime = 0;
}
template <typename T>
void Root4root_VLC<T>::setLine(uint8_t line, bool resetX) //Starting from 0
{
if (line > lcdLines) {
return;
}
if (resetX) {
internalCursorX = 0;
}
internalCursorY = line;
lcdHandler->setCursor(offsetX, offsetY + internalCursorY);
}
template <typename T>
void Root4root_VLC<T>::setCursor(uint8_t x, uint8_t y) //Internal function
{
if (x < lcdChars && y < lcdLines ) {
this->internalCursorX = x;
this->internalCursorY = y;
}
lcdHandler->setCursor(offsetX + internalCursorX, offsetY + internalCursorY);
}
template <typename T>
void Root4root_VLC<T>::displayString(char *str)
{
if (displayDelay > 0 && (millis() - previuosChangeTime) < displayDelay) {
return;
}
previuosChangeTime = millis();
uint8_t start = internalCursorY * lcdChars + internalCursorX;
uint8_t head = 0; //Gap sensor to determine if we need setCursor command.
if (str[0] != lcdState[start]) {
lcdHandler->setCursor(offsetX + internalCursorX, offsetY + internalCursorY);
}
for (uint8_t i = 0; i < lcdChars - internalCursorX; ++i) {
if (str[i] == '\0') { break; }
if (str[i] != lcdState[start + i]) {
lcdState[start + i] = str[i];
if (head != i) {
lcdHandler->setCursor(offsetX + internalCursorX + i, offsetY + internalCursorY);
head = i;
}
lcdHandler->write(str[i]);
++head;
}
}
}
template <typename T>
void Root4root_VLC<T>::write(uint8_t symbol)
{
lcdHandler->write(symbol);
}
template <typename T>
void Root4root_VLC<T>::clear()
{
char container[lcdChars + 1] = {};
uint8_t i = 0;
for (i = 0; i < lcdChars; ++i) {
container[i] = ' ';
}
for (i = 0; i < lcdLines; ++i) {
setLine(i);
displayString(container);
}
}
template <typename T>
Root4root_VLC<T>::~Root4root_VLC()
{
delete [] lcdState;
}
#endif //_LIB_ROOT4ROOT_LCDHELPER_