Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcelstoer/693939b08f1edc71bec3b485cd765c1d to your computer and use it in GitHub Desktop.
Save marcelstoer/693939b08f1edc71bec3b485cd765c1d 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 without the use of a 0|1 matrix
local rotate = function(char, rotateleft)
local tab = {}
local newTable = {}
local numberToString = function(number, base, minLen)
local s = ""
repeat
local remainder = number % base
s = remainder .. s
number = (number - remainder) / base
until number == 0
if #s < minLen then
-- pad the string with 0s
for i = 1, minLen - #s do s = "0" .. s end
end
return s
end
-- rather than creating a matrix this creates a table with 8 rows, each one is a binary string
for _, v in ipairs(char) do table.insert(tab, numberToString(v, 2, 8)) end
-- WARNING, left/right ignored here
-- rotate and turn binary string back to number
for i = 8, 1, -1 do
local s = ""
for j = 1, 8 do
local r = tab[j]
s = s .. r:sub(i, i)
end
table.insert(newTable, tonumber(s, 2))
end
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