Skip to content

Instantly share code, notes, and snippets.

@marcelstoer
Last active June 16, 2016 21:41
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/446043f600362f685a9c128ce99351f9 to your computer and use it in GitHub Desktop.
Save marcelstoer/446043f600362f685a9c128ce99351f9 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 the maybe "traditional" way of rotating 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(matrix, rotateLeft)
local newMatrix = {}
for i = 1, #matrix do
for j = 1, #matrix[1] do
local newRowIndex, newColumnIndex
if rotateLeft then
newRowIndex = #matrix[1] - j + 1
newColumnIndex = i
else
newRowIndex = j
newColumnIndex = #matrix - i + 1
end
if newMatrix[newRowIndex] == nil then
newMatrix[newRowIndex] = {}
end
newMatrix[newRowIndex][newColumnIndex] = matrix[i][j]
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
local t = {}
-- create an 8x8 matrix with each cell being 0|1
for _, v in ipairs(char) do table.insert(t, number2table(v, 2, 8)) end
-- regular matrix rotation
local tt = rotate(t, transformation.rotate == "left")
char = {}
-- take each matrix row, concat the 0s and 1s to a binary string, then convert it to a number
for _, v in ipairs(tt) do table.insert(char, tonumber(table.concat(v), 2)) end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment