Found this helpful when constructing patterns for drawing with.
function utils.printPattern(rows_of_bits)
local pattern = {}
for k, row in ipairs(rows_of_bits) do
local bit_position = #row
local row_value = 0x00
for i, bit in ipairs(row) do
if bit == 1 then
row_value = row_value + (math.pow(2, bit_position)//2)
end
bit_position -= 1
end
pattern[#pattern + 1] = row_value
end
local print_string = "{ "
for i, v in pairs(pattern) do
print_string = print_string .. string.format("0x%02X", v)
if i < #pattern then
print_string = print_string .. ", "
end
end
print_string = print_string .. " }"
print(print_string)
end
Which allows me to prototype a pattern in code quick and output a value that I can pass into, say, playdate.graphics.setPattern(...)
.
utils.printPattern({
{1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}
})
Outputs:
{ 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00 }
If there is an easier way to do this, I will not be surprised.