From 79ab93680cac8d2d930f580a772fb73237a5d742 Mon Sep 17 00:00:00 2001 From: Code1110 Date: Mon, 7 Nov 2016 23:29:03 +0100 Subject: [PATCH] Added support for 7-segment displays --- font7seg.lua | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++ max7219.lua | 41 +++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 font7seg.lua diff --git a/font7seg.lua b/font7seg.lua new file mode 100644 index 0000000..09b0aae --- /dev/null +++ b/font7seg.lua @@ -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 \ No newline at end of file diff --git a/max7219.lua b/max7219.lua index 0ef3471..1f9c0bd 100644 --- a/max7219.lua +++ b/max7219.lua @@ -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 @@ -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