-
Notifications
You must be signed in to change notification settings - Fork 3
/
lcd_hd44780.lua
179 lines (156 loc) · 4.66 KB
/
lcd_hd44780.lua
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
local lcd_hd44780 = {}
lcd_hd44780.__index = lcd_hd44780
lcd_hd44780.lines = {
0x80, 0xc0, 0x94, 0xd4
}
setmetatable(lcd_hd44780, {
__call = function (cls, ...)
return cls.new(...)
end,
})
function lcd_hd44780.new(width, height, drv, mode, cursor_blink, cursor_visible)
local self = setmetatable({}, lcd_hd44780)
self.width = width
self.height = height
self.x = 0
self.y = 0
self.cursor_blink = cursor_blink
self.cursor_visible = cursor_visible
self.drv = drv
self.mode = mode
return self
end
function lcd_hd44780:buffered()
self.buffer = {}
self.screen = {}
for x=0, self.width-1 do
self.buffer[x] = {}
self.screen[x] = {}
for y=0, self.height-1 do
self.buffer[x][y] = " "
self.screen[x][y] = " "
end
end
end
function lcd_hd44780:init()
if self.width == 0 or self.height == 0 then
print("Initialize via lcd(width, height, [pins]) or buffered(..)")
return
end
if self.mode == 'buffered' then lcd_hd44780.buffered(self) end
self.drv:init()
lcd_hd44780._init_lcd(self, 1)
if lcd_hd44780.has_two_e(self) then lcd_hd44780._init_lcd(self, 2) end
end
function lcd_hd44780:_init_lcd(enable)
self.drv:command4(3, enable)
tmr.delay(50)
self.drv:command4(3, enable)
tmr.delay(50)
self.drv:command4(3, enable)
tmr.delay(50)
self.drv:command4(2, enable)
self.drv:command(0x28, enable)
self.drv:command(0x08, enable)
self.drv:command(0x01, enable)
self.drv:command(0x06, enable)
self.drv:command(12 + (2*self.cursor_visible) + self.cursor_blink, enable)
end
function lcd_hd44780:_get_line_addr(line)
if line < 2 or lcd_hd44780.has_two_e(self) == false then
return self.lines[line + 1]
else
return self.lines[line - 1]
end
end
function lcd_hd44780:_add_x(len)
self.x = self.x + 1
if self.x >= self.width then
self.y = self.y + 1
self.x = 0
if self.y >= self.height then self.y = 0 end
self.drv:command(lcd_hd44780._get_line_addr(self, self.y), lcd_hd44780.detect_e(self))
end
end
function lcd_hd44780:write(chars)
if self.mode == 'direct' then
for i = 1, #chars do
self.drv:write(chars:sub(i,i), lcd_hd44780.detect_e(self))
lcd_hd44780._add_x(self, 1)
end
end
if self.mode == 'buffered' then lcd_hd44780._write_buffered(self, chars) end
end
function lcd_hd44780:_write_buffered(chars)
for i = 1, #chars do
self.buffer[self.x + i - 1][self.y] = chars:sub(i,i)
end
self.x = self.x + #chars
end
function lcd_hd44780:flush()
if self.mode == 'direct' then
print("Wrong mode for flush!")
end
current_xy = lcd_hd44780.get_xy(self)
last_x = -1
last_y = -1
for y=0, self.height-1 do
for x=0, self.width-1 do
if self.buffer[x][y] ~= self.screen[x][y] then
if last_x + 1 ~= x or last_y ~=y then
lcd_hd44780.set_xy(self, x, y)
end
last_x = x
last_y = y
self.drv:write(self.buffer[x][y]:sub(1,1), lcd_hd44780.detect_e(self))
self.screen[x][y] = self.buffer[x][y]
end
end
end
lcd_hd44780.set_xy(self, current_xy['x'], current_xy['y'])
end
function lcd_hd44780:set_xy(x, y)
self.x = x
self.y = y
self.drv:command(lcd_hd44780._get_line_addr(self, y) + x, lcd_hd44780.detect_e(self))
end
function lcd_hd44780:get_xy()
return {
x = self.x,
y = self.y
}
end
function lcd_hd44780:clear()
if self.mode == 'buffered' then
for x=0, self.width-1 do
self.buffer[x] = {}
for y=0, self.height-1 do
self.buffer[x][y] = " "
end
end
else
drv:command(0x01, lcd_hd44780.detect_e(self))
self.x = 0
self.y = 0
end
end
function lcd_hd44780:detect_e()
if lcd_hd44780.has_two_e(self) and self.y > 1 then return 2 else return 1 end
end
function lcd_hd44780:has_two_e()
if self.width >= 40 and self.height >= 3 then
return true
else
return false
end
end
function lcd_hd44780:setCustomChar(position, pattern)
drv:command(bit.bor(0x40, bit.lshift(position, 3)), lcd_hd44780.detect_e(self))
drv:setrs(true)
for key,value in pairs(pattern) do
drv:_write8(pattern[key], lcd_hd44780.detect_e(self))
end
drv:command(0x00, lcd_hd44780.detect_e(self), true)
lcd_hd44780.clear(self)
end
return lcd_hd44780