Skip to content

Commit

Permalink
Added support for 7-segment displays
Browse files Browse the repository at this point in the history
  • Loading branch information
Code1110 committed Nov 7, 2016
1 parent abfacb0 commit 79ab936
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
80 changes: 80 additions & 0 deletions font7seg.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
local modname = ...
local M = {}
_G[modname] = M

local digits = {}
digits[" "] = 0x00
digits["-"] = 0x01
digits["_"] = 0x08
digits["0"] = 0x7e
digits["1"] = 0x30
digits["2"] = 0x6d
digits["3"] = 0x79
digits["4"] = 0x33
digits["5"] = 0x5b
digits["6"] = 0x5f
digits["7"] = 0x70
digits["8"] = 0x7f
digits["9"] = 0x7b
digits["a"] = 0x7d
digits["b"] = 0x1f
digits["c"] = 0x0d
digits["d"] = 0x3d
digits["e"] = 0x6f
digits["f"] = 0x47
digits["g"] = 0x7b
digits["h"] = 0x17
digits["i"] = 0x10
digits["j"] = 0x18
--digits["k"] = 0x08 -- not supported
digits["l"] = 0x06
--digits["m"] = 0x08 -- not supported
digits["n"] = 0x15
digits["o"] = 0x1d
digits["p"] = 0x67
digits["q"] = 0x73
digits["r"] = 0x05
digits["s"] = 0x5b
digits["t"] = 0x0f
digits["u"] = 0x1c
digits["v"] = 0x1c
--digits["w"] = 0x08 -- not supported
--digits["x"] = 0x08 -- not supported
digits["y"] = 0x3b
digits["z"] = 0x6d
digits["A"] = 0x77
digits["B"] = 0x7f
digits["C"] = 0x4e
digits["D"] = 0x7e
digits["E"] = 0x4f
digits["F"] = 0x47
digits["G"] = 0x5e
digits["H"] = 0x37
digits["I"] = 0x30
digits["J"] = 0x38
--digits["K"] = 0x08 -- not supported
digits["L"] = 0x0e
--digits["M"] = 0x08 -- not supported
digits["N"] = 0x76
digits["O"] = 0x7e
digits["P"] = 0x67
digits["Q"] = 0x73
digits["R"] = 0x46
digits["S"] = 0x5b
digits["T"] = 0x0f
digits["U"] = 0x3e
digits["V"] = 0x3e
--digits["W"] = 0x08 -- not supported
--digits["X"] = 0x08 -- not supported
digits["Y"] = 0x3b
digits["Z"] = 0x6d
digits[","] = 0x80
digits["."] = 0x80
digits["°"] = 0x63

function M.GetChar(char)
local r = digits[char]
if (r == nil) then return digits["_"] else return r end
end

return M
41 changes: 41 additions & 0 deletions max7219.lua
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ function M.setIntensity(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

Expand All @@ -253,4 +255,43 @@ function M.shutdown(shutdown)
end


function M.write7segment(text)
local tab = {}

local lenNoDots = text:gsub("%.", ""):len()

-- pad with spaces to turn off not required digits
if (lenNoDots < (8 * numberOfModules)) then
text = text .. string.rep(" ", (8 * numberOfModules) - lenNoDots)
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

-- todo 1 table per module is required
max7219.write( { tab } , { invert = false })
end

return M

0 comments on commit 79ab936

Please sign in to comment.