Skip to content
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 72 additions & 7 deletions lcd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ local generate_line = function(s, ypos)
return texture
end

local generate_texture = function(lines)
local generate_text_texture = function(lines)
local texture = "[combine:"..LCD_WIDTH.."x"..LCD_WIDTH
local ypos = math.floor((LCD_WIDTH - LINE_HEIGHT*NUMBER_OF_LINES) / 2)
for i = 1, #lines do
Expand All @@ -171,6 +171,56 @@ local generate_texture = function(lines)
return texture
end

local number_to_color = function(num)
if type(num) ~= "number" or num >= 4096 or num < 0 then
return "#0000"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case of an error, it might make more sense to either:

  • fall back to 0xF00 (red = error)
  • or convert to 0x000 (black = error)
  • or pass tonumber(num) or 0 to the numeric computation below and let the user see when it looks strange.

Also: What if nan is passed to this function?

end

return string.format("#%X%X%X", num / 16^2, num / 16 % 16, num % 16 )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the string.format? Couldn't we just use 0x notation in Lua?

end

local generate_raster_texture = function(colors)
-- the LCD texture is 16x16, will upscale the resolution by 4
local total_width = 16 * 4
local w_padding = 1 * 4
local h_padding = 2 * 4

local width = total_width - w_padding * 2
local height = total_width - h_padding * 2

local texture = "[fill:"..total_width.."x"..total_width..":#0000"

-- the LCD's frame
-- top
texture = texture
.."^[fill:"..total_width.."x"..h_padding
..":0,0:#000"
-- bottom
texture = texture
.."^[fill:"..total_width.."x"..h_padding
..":0,"..total_width - h_padding..":#000"
-- left
texture = texture
.."^[fill:"..w_padding.."x"..total_width - h_padding
..":0,"..h_padding..":#000"
-- right
texture = texture
.."^[fill:"..w_padding.."x"..total_width - h_padding
..":".. total_width - w_padding ..","..h_padding..":#000"

if type(colors) == "table" then
for i, num in ipairs(colors) do
if i > width * height then break end
local color = number_to_color(num)
local x = (i - 1) % width
local y = math.floor((i - 1) / width)
texture = texture .. "^[fill:1x1:"..x + w_padding ..","..y + h_padding..":"..color
end
end

return texture
end

local lcds = {
-- on ceiling
--* [0] = {delta = {x = 0, y = 0.4, z = 0}, pitch = math.pi / -2},
Expand Down Expand Up @@ -202,10 +252,18 @@ end

local set_texture = function(ent)
local meta = minetest.get_meta(ent.object:get_pos())
local text = meta:get_string("text")
local message = meta:get_string("message")
local message_type = meta:get_string("message_type")

local texture
if message_type == "string" then
texture = generate_text_texture(create_lines(message))
else
texture = generate_raster_texture(minetest.deserialize(message))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be documented somewhere. Otherwise no-one else would know about this raster feature.

end
ent.object:set_properties({
textures = {
generate_texture(create_lines(text))
texture
}
})
end
Expand Down Expand Up @@ -260,12 +318,19 @@ local on_digiline_receive = function(pos, _, channel, msg)
local setchan = meta:get_string("channel")
if setchan ~= channel then return end

if type(msg) ~= "string" and type(msg) ~= "number" then return end
if type(msg) == "string" or type(msg) == "number" then
meta:set_string("message", msg)
meta:set_string("infotext", msg)
elseif type(msg) == "table" then
meta:set_string("message", minetest.serialize(msg))
meta:set_string("infotext", "")
else
return
end

meta:set_string("text", msg)
meta:set_string("infotext", msg)
meta:set_string("message_type", type(msg))

if msg ~= "" then
if type(msg) ~= string or msg ~= "" then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type(msg) ~= string is always true because type returns a string, and not a table (reference).

prepare_writing(pos)
end
end
Expand Down