Last active
October 22, 2016 16:09
-
-
Save miketweaver/978791efc8900f917f92f085bc7afd1b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Really random test code for the MAx7219. | |
-- Chain 7 segment display off dev board | |
-- Chain 8x8 matix off of 7 segment display. | |
-- run this code to get 87654321. Assuming the L to R digit layout is just the | |
-- dev boards we picked up. | |
-- Functions: | |
-- | |
-- DISPLAY.text(str) | |
-- Displays the text, takes a string. | |
-- | |
-- DISPLAY.text(str,num) | |
-- Displays the text in a scrolling pattern | |
-- Takes a string, and an integer (milliseconds) | |
local moduleName = ... | |
local DISPLAY = {} | |
local MAX7219_REG_NOOP = 0x00; | |
local MAX7219_REG_DIGIT0 = 0x01; | |
local MAX7219_REG_DIGIT1 = 0x02; | |
local MAX7219_REG_DIGIT2 = 0x03; | |
local MAX7219_REG_DIGIT3 = 0x04; | |
local MAX7219_REG_DIGIT4 = 0x05; | |
local MAX7219_REG_DIGIT5 = 0x06; | |
local MAX7219_REG_DIGIT6 = 0x07; | |
local MAX7219_REG_DIGIT7 = 0x08; | |
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; | |
-- Available Characters | |
-- | |
-- When adding characters | |
-- this chart by @Zevlag should help | |
-- | |
-- 64 -> ---- | |
-- 2 -> | | <- 32 | |
-- 1 -> ---- | |
-- 4 -> | | <- 16 | |
-- 8 -> ---- . <- 128 | |
-- | |
local char_matrix = {}; | |
char_matrix[" "] = 0; | |
char_matrix["-"] = 1; | |
char_matrix["_"] = 0 + 0 + 0 + 8 + 0 + 0 + 0 + 0; | |
char_matrix["."] = 0 + 0 + 0 + 0 + 0 + 0 + 0 + 128; | |
char_matrix["!"] = 0 + 0 + 0 + 0 + 16 + 32 + 0 + 128; | |
char_matrix["0"] = 0 + 2 + 4 + 8 + 16 + 32 + 64 + 0; | |
char_matrix["1"] = 0 + 0 + 0 + 0 + 16 + 32 + 0 + 0; | |
char_matrix["2"] = 1 + 0 + 4 + 8 + 0 + 32 + 64 + 0; | |
char_matrix["3"] = 1 + 0 + 0 + 8 + 16 + 32 + 64 + 0; | |
char_matrix["4"] = 1 + 2 + 0 + 0 + 16 + 32 + 0 + 0; | |
char_matrix["5"] = 1 + 2 + 0 + 8 + 16 + 0 + 64 + 0; | |
char_matrix["6"] = 1 + 2 + 4 + 8 + 16 + 0 + 64 + 0; | |
char_matrix["7"] = 0 + 0 + 0 + 0 + 16 + 32 + 64 + 0; | |
char_matrix["8"] = 1 + 2 + 4 + 8 + 16 + 32 + 64 + 0; | |
char_matrix["9"] = 1 + 2 + 0 + 8 + 16 + 32 + 64 + 0; | |
char_matrix["a"] = 1 + 2 + 4 + 0 + 16 + 32 + 64 + 0; | |
char_matrix["b"] = 1 + 2 + 4 + 8 + 16 + 0 + 0 + 0; | |
char_matrix["c"] = 1 + 0 + 4 + 8 + 0 + 0 + 0 + 0; | |
char_matrix["C"] = 0 + 2 + 4 + 8 + 0 + 0 + 64 + 0; | |
char_matrix["d"] = 1 + 0 + 4 + 8 + 16 + 32 + 0 + 0; | |
char_matrix["e"] = 1 + 2 + 4 + 8 + 0 + 0 + 64 + 0; | |
char_matrix["f"] = 1 + 2 + 4 + 0 + 0 + 0 + 64 + 0; | |
char_matrix["g"] = 1 + 2 + 0 + 8 + 16 + 32 + 64 + 0; | |
char_matrix["h"] = 1 + 2 + 4 + 0 + 16 + 0 + 0 + 0; | |
char_matrix["H"] = 1 + 2 + 4 + 0 + 16 + 32 + 0 + 0; | |
char_matrix["i"] = 0 + 2 + 4 + 0 + 0 + 0 + 0 + 0; | |
char_matrix["j"] = 0 + 0 + 4 + 8 + 16 + 32 + 0 + 0; | |
char_matrix["k"] = 1 + 2 + 4 + 0 + 16 + 32 + 0 + 0; | |
char_matrix["l"] = 0 + 2 + 4 + 8 + 0 + 0 + 0 + 0; | |
char_matrix["m"] = 0 + 0 + 4 + 0 + 16 + 0 + 64 + 0; | |
char_matrix["n"] = 1 + 0 + 4 + 0 + 16 + 0 + 0 + 0; | |
char_matrix["o"] = 0 + 2 + 4 + 8 + 16 + 32 + 64 + 0; | |
char_matrix["p"] = 1 + 2 + 4 + 0 + 0 + 32 + 64 + 0; | |
char_matrix["P"] = 1 + 2 + 4 + 0 + 0 + 32 + 64 + 0; | |
char_matrix["q"] = 1 + 2 + 0 + 0 + 16 + 32 + 64 + 0; | |
char_matrix["r"] = 1 + 0 + 4 + 0 + 0 + 0 + 0 + 0; | |
char_matrix["s"] = 1 + 2 + 0 + 8 + 16 + 0 + 64 + 0; | |
char_matrix["S"] = 1 + 2 + 0 + 8 + 16 + 0 + 64 + 0; | |
char_matrix["t"] = 1 + 2 + 4 + 8 + 0 + 0 + 0 + 0; | |
char_matrix["u"] = 0 + 2 + 4 + 8 + 16 + 32 + 0 + 0; | |
char_matrix["v"] = 0 + 0 + 4 + 8 + 16 + 0 + 0 + 0; | |
char_matrix["w"] = 0 + 2 + 0 + 8 + 0 + 32 + 0 + 0; | |
char_matrix["x"] = 1 + 2 + 4 + 0 + 16 + 32 + 0 + 0; | |
char_matrix["y"] = 1 + 2 + 0 + 8 + 16 + 32 + 0 + 0; | |
char_matrix["z"] = 1 + 0 + 4 + 8 + 16 + 32 + 64 + 0; | |
-- Phrases | |
local str_array = {'S4in7con','rocks ur',' socks !'}; | |
--- Send byte to specific chip | |
-- | |
-- @param chip which chip to send data. | |
-- @param data a number representing command+data. | |
local function sendchipbyte(chip, data) | |
if chip == 0 then | |
-- set same data for both chips | |
spi.set_mosi(1,0,16,data,data); | |
elseif chip == 1 then | |
-- set no-op (second chip) then data (first chip) | |
spi.set_mosi(1,0,16,0x0000,data); | |
elseif chip == 2 then | |
-- set data (2nd chip) then no-op (first chip) | |
spi.set_mosi(1,0,16,data,0x0000); | |
else | |
-- invalid choice send no-op. Should just do nothing here. | |
spi.set_mosi(1,0,16,0x0000,0x0000); | |
end | |
spi.transaction(1,0,0,0,0,32,0,0); | |
end | |
function DISPLAY.clear() | |
for i=1,8 do | |
spi.send(1,i*256+15); | |
tmr.delay(50); | |
end | |
end | |
function DISPLAY.setup(boot) | |
spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 32, 8); | |
--chipinit = {0x0C00,0x0B07,0x0F00,0x0A15,0x0C01,0x0000,0x0000,0x09FF} | |
-- chipinit = {0x0B07,0x09FF,0x0F00,0x0A15,0x0C01,0x0000,0x0000} | |
--spi.send(1,chipinit); | |
--if boot then | |
-- print("REBOOT") | |
--else | |
-- print("INIT") | |
local b = rtcmem.read32(0); | |
if file.exists("score.dsp") then | |
spi.set_mosi(1,0,16,0x0, 0x0C00); | |
spi.transaction(1,0,0,0,0,32,0,0); | |
spi.set_mosi(1,0,16,0x0, 0x0B07); | |
spi.transaction(1,0,0,0,0,32,0,0); | |
spi.set_mosi(1,0,16,0x0, 0x09FF); | |
spi.transaction(1,0,0,0,0,32,0,0); | |
spi.set_mosi(1,0,16,0x0, 0x0A00+b); | |
spi.transaction(1,0,0,0,0,32,0,0); | |
spi.set_mosi(1,0,16,0x0, 0x0C01); | |
spi.transaction(1,0,0,0,0,32,0,0); | |
tmr.stop(0); | |
file.open("score.dsp", "r") | |
DISPLAY.display(tonumber(file.read())) | |
file.close() | |
else | |
spi.set_mosi(1,0,16,0x0, 0x0C00); | |
spi.transaction(1,0,0,0,0,32,0,0); | |
spi.set_mosi(1,0,16,0x0, 0x0B07); | |
spi.transaction(1,0,0,0,0,32,0,0); | |
spi.set_mosi(1,0,16,0x0, 0x0900); | |
spi.transaction(1,0,0,0,0,32,0,0); | |
spi.set_mosi(1,0,16,0x0, 0x0A00+b); | |
spi.transaction(1,0,0,0,0,32,0,0); | |
spi.set_mosi(1,0,16,0x0, 0x0C01); | |
spi.transaction(1,0,0,0,0,32,0,0); | |
DISPLAY.phrases(str_array,10000); | |
--DISPLAY.phrase(); | |
end | |
end | |
function DISPLAY.brightness() | |
node.task.post(node.task.MEDIUM_PRIORITY, function() | |
http.get("http://10.90.0.16", "Accept: */*\r\n", function(code, data) | |
if (code == 200 ) then | |
print("SC+BRIGHT="..data) | |
b = tonumber(data) | |
if (b ~= nil and b <= 15) then | |
rtcmem.write32(0, b); | |
spi.send(1, 0x0A00 + b) | |
end | |
else | |
print('http error ' .. code) | |
end | |
end) | |
end) | |
end | |
function DISPLAY.saintcon() | |
DISPLAY.text("S4in7con"); | |
end | |
function DISPLAY.help() | |
DISPLAY.text("--Help--"); | |
end | |
function DISPLAY.display(num) | |
if tonumber(num) ~= 0 then | |
spi.send(1, {0x0C00, 0x09FF, 0x0C01}); | |
if string.len(num) <= 8 then | |
num = tonumber(num) | |
if num ~= nil then | |
file.open("score.dsp", "w") | |
file.write(num) | |
file.close() | |
DISPLAY.clear(); | |
local i = 8; | |
while num > 0 do | |
spi.set_mosi(1,0,16,0x0, i*256+(num % 10)); | |
spi.transaction(1,0,0,0,0,32,0,0); | |
num = (num - (num %10)) / 10; | |
i = i - 1; | |
end | |
end | |
end | |
end | |
end | |
function DISPLAY.text_scroll(str,speed) | |
local location = 0; | |
tmr.alarm(0,speed,tmr.ALARM_AUTO, function() | |
spi.send(1, {0x0900,0x0000,0x0000}); | |
spi.set_mosi(1,0,16,0x0, 0x0C00); | |
for i = 1, 8 do | |
boo = ( i + location - 1 ) % string.len(str) + 1; | |
letter = string.sub(str, boo, boo); | |
print ( tostring(boo) .. letter ); | |
spi.send(1, {i * 256 + char_matrix[letter]}); | |
end | |
location = ( location - 1 ) % string.len(str) + 2; | |
spi.set_mosi(1,0,16,0x0, 0x0C01); | |
end) | |
end | |
function DISPLAY.text(str) | |
local location = 0; | |
spi.send(1, {0x0900,0x0000,0x0000}); | |
spi.set_mosi(1,0,16,0x0, 0x0C00); | |
for i = 1, 8 do | |
boo = ( i + location - 1 ) % string.len(str) + 1; | |
letter = string.sub(str, boo, boo); | |
print ( tostring(boo) .. " " .. letter ); | |
spi.send(1, {i * 256 + char_matrix[letter]}); | |
end | |
location = ( location - 1 ) % string.len(str) + 2; | |
spi.set_mosi(1,0,16,0x0, 0x0C01); | |
end | |
function DISPLAY.phrases(phrase_array,delay) | |
local phrase_timer = tmr.create() | |
size = tablelength(phrase_array); | |
i = 2; | |
DISPLAY.text(phrase_array[i-1]); | |
print("next phrase in " .. delay .. " milliseconds"); | |
tmr.alarm(phrase_timer,delay,tmr.ALARM_AUTO, function() | |
DISPLAY.text(phrase_array[i]); | |
if i < size then i = i+1 else i = 1 end | |
end) | |
end | |
function DISPLAY.unload() | |
package.loaded[moduleName]=nil | |
moduleName = nil | |
DISPLAY = nil | |
end | |
function tablelength(T) | |
local count = 0 | |
for _ in pairs(T) do count = count + 1 end | |
return count | |
end | |
return DISPLAY |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment