forked from marcelstoer/nodemcu-max7219
-
Notifications
You must be signed in to change notification settings - Fork 0
/
max7219.lua
271 lines (229 loc) · 7.36 KB
/
max7219.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
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
--------------------------------------------------------------------------------
-- MAX7229 module for NodeMCU
-- SOURCE: https://github.com/marcelstoer/nodemcu-max7219
-- AUTHOR: marcel at frightanic dot com
-- LICENSE: http://opensource.org/licenses/MIT
--------------------------------------------------------------------------------
-- Set module name as parameter of require
local modname = ...
local M = {}
_G[modname] = M
--------------------------------------------------------------------------------
-- Local variables
--------------------------------------------------------------------------------
local debug = false
local numberOfModules
local numberOfColumns
-- ESP8266 pin which is connected to CS of the MAX7219
local slaveSelectPin
-- numberOfModules * 8 bytes for the char representation, left-to-right
local columns = {}
--------------------------------------------------------------------------------
-- Local/private functions
--------------------------------------------------------------------------------
local function sendByte(module, register, data)
-- out("module: " .. module .. " register: " .. register .. " data: " .. data)
spiRegister = {}
spiData = {}
-- set all to 0 by default
for i = 1, numberOfModules do
spiRegister[i] = 0
spiData[i] = 0
end
-- set the values for just the affected display
spiRegister[module] = register
spiData[module] = data
-- enble sending data
gpio.write(slaveSelectPin, gpio.LOW)
for i = 1, numberOfModules do
spi.send(1, spiRegister[i] * 256 + spiData[i])
end
-- make the chip latch data into the registers
gpio.write(slaveSelectPin, gpio.HIGH)
end
local function numberToTable(number, base, minLen)
local t = {}
repeat
local remainder = number % base
table.insert(t, 1, remainder)
number = (number - remainder) / base
until number == 0
if #t < minLen then
for i = 1, minLen - #t do table.insert(t, 1, 0) end
end
return t
end
local function rotate(char, rotateleft)
local matrix = {}
local newMatrix = {}
for _, v in ipairs(char) do table.insert(matrix, numberToTable(v, 2, 8)) end
if rotateleft then
for i = 8, 1, -1 do
local s = ""
for j = 1, 8 do
s = s .. matrix[j][i]
end
table.insert(newMatrix, tonumber(s, 2))
end
else
for i = 1, 8 do
local s = ""
for j = 8, 1, -1 do
s = s .. matrix[j][i]
end
table.insert(newMatrix, tonumber(s, 2))
end
end
return newMatrix
end
local function commit()
-- for every module (1 to numberOfModules) send registers 1 - 8
-- since Lua uses 1-based indexes it's a bit of a +-1 dance here, sample:
-- module: 1 register: 1 data: 64
-- module: 1 register: 2 data: 66
-- ...
-- module: 1 register: 8 data: 0
-- module: 2 register: 1 data: 98
-- ...
-- module: 2 register: 8 data: 0
-- module: 3 register: 1 data: 34
-- ...
-- module: 3 register: 8 data: 0
for i = 1, numberOfColumns do
local module = math.floor(((i - 1) / 8) + 1)
local register = math.floor(((i - 1) % 8) + 1)
sendByte(module, register, columns[i])
end
end
local function out(msg)
if debug then
print("[MAX7219] " .. msg)
end
end
--------------------------------------------------------------------------------
-- Public functions
--------------------------------------------------------------------------------
-- Configures both the SoC and the MAX7219 modules.
-- @param config table with the following keys (* = mandatory)
-- - numberOfModules*
-- - slaveSelectPin*, ESP8266 pin which is connected to CS of the MAX7219
-- - debug
function M.setup(config)
local config = config or {}
numberOfModules = assert(config.numberOfModules, "'numberOfModules' is a mandatory parameter")
slaveSelectPin = assert(config.slaveSelectPin, "'slaveSelectPin' is a mandatory parameter")
numberOfColumns = numberOfModules * 8
if config.debug then debug = config.debug end
out("number of modules: " .. numberOfModules .. ", SS pin: " .. slaveSelectPin)
local MAX7219_REG_DECODEMODE = 0x09
local MAX7219_REG_INTENSITY = 0x0A
local MAX7219_REG_SCANLIMIT = 0x0B
local MAX7219_REG_SHUTDOWN = 0x0C
local MAX7219_REG_DISPLAYTEST = 0x0F
spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 16, 8)
-- Must NOT be done _before_ spi.setup() because that function configures all HSPI* pins for SPI. Hence,
-- if you want to use one of the HSPI* pins for slave select spi.setup() would overwrite that.
gpio.mode(slaveSelectPin, gpio.OUTPUT)
gpio.write(slaveSelectPin, gpio.HIGH)
for i = 1, numberOfModules do
sendByte(i, MAX7219_REG_SCANLIMIT, 7)
sendByte(i, MAX7219_REG_DECODEMODE, 0x00)
sendByte(i, MAX7219_REG_DISPLAYTEST, 0)
sendByte(i, MAX7219_REG_INTENSITY, 1)
sendByte(i, MAX7219_REG_SHUTDOWN, 1)
end
M.clear()
end
function M.clear()
for i = 1, numberOfColumns do
columns[i] = 0
end
commit()
end
function M.write(chars, transformation)
local transformation = transformation or {}
local revByte
if transformation.invert == true then
revByte = require("reverseBytes")
end
local c = {}
for i = 1, #chars do
local char = chars[i]
if transformation.rotate ~= nil then
char = rotate(char, transformation.rotate == "left")
end
for k, v in ipairs(char) do
if transformation.invert == true then
-- module offset + inverted register + 1
-- to produce 8, 7 .. 1, 16, 15 ... 9, 24, 23 ...
local index = ((i - 1) * 8) + 8 - k + 1
c[index] = revByte.GetReverseByte(v)
else
table.insert(c, v)
end
end
end
if (revByte ~= nil) then
package.loaded[revByte] = nil
_G[revByte] = nil
revByte = nil
end
columns = c
commit()
end
-- Sets the brightness of the display.
-- intensity: 0x00 - 0x0F (0 - 15)
function M.setIntensity(intensity)
local MAX7219_REG_INTENSITY = 0x0A
for i = 1, numberOfModules do
sendByte(i, MAX7219_REG_INTENSITY, intensity)
end
end
-- Turns the display on or off.
-- shutdown: true=turn off, false=turn on
function M.shutdown(shutdown)
local MAX7219_REG_SHUTDOWN = 0x0C
for i = 1, numberOfModules do
if (shutdown) then
sendByte(i, MAX7219_REG_SHUTDOWN, 0)
else
sendByte(i, MAX7219_REG_SHUTDOWN, 1)
end
end
end
-- todo: add scrolling support
-- Writes the specified text to the 7-Segment display.
-- If rAlign is true, the text is written right-aligned on the display.
function M.write7segment(text, rAlign)
local tab = {}
local lenNoDots = text:gsub("%.", ""):len()
-- pad with spaces to turn off not required digits
if (lenNoDots < (8 * numberOfModules)) then
if (rAlign) then
text = string.rep(" ", (8 * numberOfModules) - lenNoDots) .. text
else
text = text .. string.rep(" ", (8 * numberOfModules) - lenNoDots)
end
end
local wasdot = false
local font7seg = require("font7seg")
for i = string.len(text), 1, -1 do
local currentChar = text:sub(i,i)
if (currentChar == ".") then
wasdot = true
else
if (wasdot) then
wasdot = false
-- take care of the decimal point
table.insert(tab, font7seg.GetChar(currentChar) + 0x80)
else
table.insert(tab, font7seg.GetChar(currentChar))
end
end
end
package.loaded[font7seg] = nil
_G[font7seg] = nil
font7seg = nil
max7219.write({ tab }, { invert = false })
end
return M