Created
June 16, 2016 21:48
-
-
Save marcelstoer/e7eeb7989597ca20bd121d6754a33cac to your computer and use it in GitHub Desktop.
http://wp.me/pzoQb-sN shows how to draw to MAX7219 8x8 matrix displays, this gist shows how the convert the numbers while rotating
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
local numberToTable = function(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 | |
-- "pad" table with 0s | |
for i = 1, minLen - #t do table.insert(t, 1, 0) end | |
end | |
return t | |
end | |
local rotate = function(char, rotateleft) | |
local matrix = {} | |
local newMatrix = {} | |
for _, v in ipairs(char) do table.insert(matrix, numberToTable(v, 2, 8)) end | |
-- take the 0s and 1s from the matrix and turn them to a number via binary string *while* rotating | |
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 | |
-- char '1' in CP437 | |
local char = { 0x40, 0x42, 0x7F, 0x7F, 0x40, 0x40, 0x00, 0x00 } | |
-- transformation.rotate is nil, 'left' or 'right' | |
if transformation.rotate ~= nil then | |
char = rotate(char, transformation.rotate == "left") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment