Skip to content

Commit fa39df5

Browse files
committed
Root4root_VLC init
1 parent 733fcef commit fa39df5

File tree

8 files changed

+362
-0
lines changed

8 files changed

+362
-0
lines changed

README.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Root4root_VLC (Virtual Liquid Crystal)
2+
3+
Arduino 1602/1604 LCD Helper with state cache to reduce MCU->LCD transfer.
4+
5+
![Figure1](images/figure1.png)
6+
7+
### Available Methods:
8+
```cpp
9+
Root4root_VLC(T *lcdHandler, uint8_t chars, uint8_t lines, uint8_t offsetX, uint8_t offsetY);
10+
11+
void setLine(uint8_t line, bool resetX = true);
12+
void setCursor(uint8_t x = 0, uint8_t y = 0);
13+
void displayString(const char *str);
14+
void write(uint8_t symbol);
15+
void clear();
16+
17+
void setDelayTime(uint16_t delay);
18+
void resetDelayCounter();
19+
```
20+
21+
### Dependencies:
22+
```cpp
23+
//Root4root_VLC depends on methods from injected library:
24+
setCursor(uint8_t, uint8_t);
25+
write(uint8_t);
26+
```
27+
28+
### Example:
29+
30+
```cpp
31+
#include <Arduino.h>
32+
#include <LiquidCrystal_I2C.h>
33+
34+
#include "Root4root_VLC.h"
35+
36+
LiquidCrystal_I2C lcd(0x27, 16, 2);
37+
38+
Root4root_VLC<LiquidCrystal_I2C> vlc1(&lcd, 10, 2, 0, 0);
39+
Root4root_VLC<LiquidCrystal_I2C> vlc2(&lcd, 5, 1, 11, 0);
40+
Root4root_VLC<LiquidCrystal_I2C> vlc3(&lcd, 5, 1, 11, 1);
41+
42+
char container[6] = {0};
43+
44+
void setup()
45+
{
46+
lcd.init();
47+
lcd.backlight();
48+
}
49+
50+
void loop()
51+
{
52+
vlc1.displayString("vlc1");
53+
vlc1.setLine(1);
54+
vlc1.displayString("1111111111");
55+
delay(1000);
56+
57+
vlc2.displayString("vlc2");
58+
delay(1000);
59+
60+
sprintf(container,"%5s", "vlc3");
61+
vlc3.displayString(container);
62+
delay(1000); //Figure above
63+
64+
65+
vlc2.clear();
66+
delay(1000); //Figure below
67+
vlc3.clear();
68+
delay(1000);
69+
vlc1.clear();
70+
71+
delay(5000);
72+
}
73+
```
74+
75+
![Figure2](images/figure2.png)
76+
77+
### License:
78+
MIT
79+
### Author:
80+
Paul aka root4root \<root4root at gmail dot com><br/>
81+
**Any comments/suggestions are welcomed.**

Root4root_VLC.h

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
#ifndef _LIB_ROOT4ROOT_VLC_
2+
#define _LIB_ROOT4ROOT_VLC_
3+
4+
#include "Arduino.h"
5+
6+
template <typename T>
7+
class Root4root_VLC
8+
{
9+
public:
10+
Root4root_VLC(T *lcdHandler, uint8_t chars, uint8_t lines, uint8_t offsetX, uint8_t offsetY);
11+
~Root4root_VLC();
12+
13+
void setLine(uint8_t line, bool resetX = true);
14+
void setCursor(uint8_t x = 0, uint8_t y = 0);
15+
16+
void setDelayTime(uint16_t delay);
17+
void resetDelayCounter();
18+
19+
void displayString(const char *str, bool moveCursor = true);
20+
void write(uint8_t symbol);
21+
void clear();
22+
23+
private:
24+
char *lcdState = NULL;
25+
26+
uint8_t lcdLines{}; //number of lcd lines
27+
uint8_t lcdChars{}; //number of lcd chars in line
28+
29+
uint8_t offsetX{}; //Top-left corner of Virtual LCD
30+
uint8_t offsetY{}; //"-"
31+
32+
uint8_t internalCursorX{};
33+
uint8_t internalCursorY{};
34+
35+
uint32_t previuosChangeTime{};
36+
uint16_t displayDelay{};
37+
38+
T *lcdHandler{}; //NULL PTR
39+
};
40+
41+
template <typename T>
42+
Root4root_VLC<T>::Root4root_VLC(T *lcd, uint8_t chars, uint8_t lines, uint8_t offsetX, uint8_t offsetY):
43+
lcdHandler(lcd), lcdLines(lines), lcdChars(chars), offsetX(offsetX), offsetY(offsetY)
44+
{
45+
lcdState = new char[lines * chars + 1]();
46+
}
47+
48+
template <typename T>
49+
void Root4root_VLC<T>::setDelayTime(uint16_t delay) //Starting from 0
50+
{
51+
displayDelay = delay;
52+
}
53+
54+
template <typename T>
55+
void Root4root_VLC<T>::resetDelayCounter()
56+
{
57+
previuosChangeTime = 0;
58+
}
59+
60+
61+
template <typename T>
62+
void Root4root_VLC<T>::setLine(uint8_t line, bool resetX) //Starting from 0
63+
{
64+
if (line > lcdLines) {
65+
return;
66+
}
67+
68+
if (resetX) {
69+
internalCursorX = 0;
70+
}
71+
72+
internalCursorY = line;
73+
74+
lcdHandler->setCursor(offsetX + internalCursorX, offsetY + internalCursorY);
75+
}
76+
77+
template <typename T>
78+
void Root4root_VLC<T>::setCursor(uint8_t x, uint8_t y) //Internal function
79+
{
80+
if (x < lcdChars && y < lcdLines ) {
81+
this->internalCursorX = x;
82+
this->internalCursorY = y;
83+
}
84+
85+
lcdHandler->setCursor(offsetX + internalCursorX, offsetY + internalCursorY);
86+
}
87+
88+
template <typename T>
89+
void Root4root_VLC<T>::displayString(const char *str, bool moveCursor)
90+
{
91+
if (displayDelay > 0 && (millis() - previuosChangeTime) < displayDelay) {
92+
return;
93+
}
94+
95+
if (internalCursorX >= lcdChars) { return; }
96+
97+
previuosChangeTime = millis();
98+
99+
uint8_t start = internalCursorY * lcdChars + internalCursorX;
100+
uint8_t head = 0; //Gap sensor to determine if we need setCursor command.
101+
102+
if (str[0] != lcdState[start]) {
103+
lcdHandler->setCursor(offsetX + internalCursorX, offsetY + internalCursorY);
104+
}
105+
106+
uint8_t i = 0;
107+
108+
for ( ; i < lcdChars - internalCursorX; ++i) {
109+
110+
if (str[i] == '\0') {
111+
break;
112+
}
113+
114+
if (str[i] != lcdState[start + i]) {
115+
lcdState[start + i] = str[i];
116+
if (head != i) {
117+
lcdHandler->setCursor(offsetX + internalCursorX + i, offsetY + internalCursorY);
118+
head = i;
119+
}
120+
lcdHandler->write(str[i]);
121+
++head;
122+
}
123+
}
124+
125+
if (moveCursor) {
126+
internalCursorX += i;
127+
}
128+
}
129+
130+
template <typename T>
131+
void Root4root_VLC<T>::write(uint8_t symbol)
132+
{
133+
lcdHandler->write(symbol);
134+
}
135+
136+
template <typename T>
137+
void Root4root_VLC<T>::clear()
138+
{
139+
char container[lcdChars + 1] = {};
140+
uint8_t i = 0;
141+
142+
for (i = 0; i < lcdChars; ++i) {
143+
container[i] = ' ';
144+
}
145+
146+
for (i = 0; i < lcdLines; ++i) {
147+
setLine(i);
148+
149+
displayString(container, false);
150+
}
151+
152+
internalCursorX = 0;
153+
internalCursorY = 0;
154+
155+
memset(lcdState, 0, lcdLines * lcdChars + 1);
156+
}
157+
158+
template <typename T>
159+
Root4root_VLC<T>::~Root4root_VLC()
160+
{
161+
delete [] lcdState;
162+
}
163+
164+
#endif //_LIB_ROOT4ROOT_VLC_
165+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <Arduino.h>
2+
#include <LiquidCrystal_I2C.h>
3+
4+
#include <Root4root_VLC.h>
5+
6+
LiquidCrystal_I2C lcd(0x27, 16, 2);
7+
8+
Root4root_VLC<LiquidCrystal_I2C> vlc1(&lcd, 10, 2, 0, 0);
9+
Root4root_VLC<LiquidCrystal_I2C> vlc2(&lcd, 5, 1, 11, 0);
10+
Root4root_VLC<LiquidCrystal_I2C> vlc3(&lcd, 5, 1, 11, 1);
11+
12+
char container[6] = {0};
13+
14+
void setup()
15+
{
16+
lcd.init();
17+
lcd.backlight();
18+
}
19+
20+
void loop()
21+
{
22+
vlc1.displayString("vlc1");
23+
vlc1.setLine(1);
24+
vlc1.displayString("1111111111");
25+
delay(1000);
26+
27+
vlc2.displayString("vlc2");
28+
delay(1000);
29+
30+
sprintf(container,"%5s", "vlc3");
31+
vlc3.displayString(container);
32+
delay(1000);
33+
34+
35+
vlc2.clear();
36+
delay(1000);
37+
vlc3.clear();
38+
delay(1000);
39+
vlc1.clear();
40+
41+
delay(5000);
42+
}

images/figure1.png

3.26 KB
Loading

images/figure2.png

3.22 KB
Loading

index.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <Arduino.h>
2+
#include <LiquidCrystal_I2C.h>
3+
4+
#include "Root4root_VLC.h"
5+
6+
LiquidCrystal_I2C lcd(0x27, 16, 2);
7+
8+
Root4root_VLC<LiquidCrystal_I2C> vlc1(&lcd, 10, 2, 0, 0);
9+
Root4root_VLC<LiquidCrystal_I2C> vlc2(&lcd, 5, 1, 11, 0);
10+
Root4root_VLC<LiquidCrystal_I2C> vlc3(&lcd, 5, 1, 11, 1);
11+
12+
char container[6] = {0}; //_TEST\0
13+
14+
void setup()
15+
{
16+
lcd.init();
17+
lcd.backlight();
18+
}
19+
20+
void loop()
21+
{
22+
vlc1.displayString("vlc1");
23+
vlc1.setLine(1);
24+
vlc1.displayString("1111111111");
25+
delay(1000);
26+
27+
vlc2.displayString("vlc2");
28+
delay(1000);
29+
30+
sprintf(container,"%5s", "vlc3");
31+
vlc3.displayString(container);
32+
delay(1000);
33+
34+
35+
vlc2.clear();
36+
delay(1000);
37+
vlc3.clear();
38+
delay(1000);
39+
vlc1.clear();
40+
41+
delay(5000);
42+
}

keywords.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#######################################
2+
# Syntax Coloring Map For Root4root_VLC
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
Root4root_VLC KEYWORD1
10+
11+
#######################################
12+
# Methods and Functions (KEYWORD2)
13+
#######################################
14+
15+
displayString KEYWORD2
16+
clear KEYWORD2
17+
setLine KEYWORD2
18+
setCursor KEYWORD2
19+
displayLine KEYWORD2
20+
21+
#######################################
22+
# Constants (LITERAL1)
23+
#######################################

library.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=Root4root_VLC
2+
version=1.0.0
3+
author=Paul aka root4root
4+
maintainer=root4root <root4root@gmail.com>
5+
sentence=Arduino 1602/1604 display helper library (Virtual Liquid Crystal)
6+
paragraph=Arduino library for 1602/1604 display with state cache, wich helps reduce transfer between MCU and DISPLAY via I2C. Compatible with LiquidCrystal_I2C.
7+
category=Display
8+
url=https://github.com/root4root/Root4root_VLC
9+
architectures=*

0 commit comments

Comments
 (0)