techage/icta_controller/display.lua

305 lines
7.9 KiB
Lua
Raw Normal View History

2020-02-29 21:20:54 +03:00
--[[
TechAge
=======
2022-06-05 22:20:43 +03:00
Copyright (C) 2019-2022 Joachim Stolberg
2020-02-29 21:20:54 +03:00
2020-10-19 20:09:17 +03:00
AGPL v3
2020-02-29 21:20:54 +03:00
See LICENSE.txt for more information
2022-01-03 23:40:31 +03:00
2020-02-29 21:20:54 +03:00
ICTA Controller - Display
]]--
2022-01-03 23:40:31 +03:00
local S = techage.S
techage.display = {}
2022-01-03 23:40:31 +03:00
local NUM_ROWS = 5
2020-03-01 14:32:11 +03:00
local RADIUS = 6
local Param2ToFacedir = {[0] = 0, 0, 3, 1, 2, 0}
2022-01-03 23:40:31 +03:00
local function lcdlib_bugfix(text_tbl)
2020-04-14 19:46:50 +03:00
if text_tbl and next(text_tbl) then
local t = {}
for _,txt in ipairs(text_tbl) do
if txt == "" then
t[#t+1] = " "
else
t[#t+1] = txt
end
end
return table.concat(t, "\n")
end
return ""
end
2022-01-03 23:40:31 +03:00
function techage.display.display_update(pos, objref)
pos = vector.round(pos)
local nvm = techage.get_nvm(pos)
2022-01-03 23:40:31 +03:00
local text = lcdlib_bugfix(nvm.text)
2020-02-29 21:20:54 +03:00
local texture = lcdlib.make_multiline_texture(
"default", text,
2020-03-01 14:32:11 +03:00
70, 70, NUM_ROWS, "top", "#000")
2020-02-29 21:20:54 +03:00
objref:set_properties({ textures = {texture},
2021-09-24 23:19:36 +03:00
visual_size = {x=0.94, y=0.94} })
2020-02-29 21:20:54 +03:00
end
function techage.display.display_updateXL(pos, objref)
pos = vector.round(pos)
local nvm = techage.get_nvm(pos)
2022-01-03 23:40:31 +03:00
local text = lcdlib_bugfix(nvm.text)
local texture = lcdlib.make_multiline_texture(
"default", text,
126, 70, NUM_ROWS, "top", "#000")
objref:set_properties({ textures = {texture},
2021-09-24 23:19:36 +03:00
visual_size = {x=0.94*1.9, y=0.94} })
end
function techage.display.on_timer(pos)
local mem = techage.get_mem(pos)
mem.ticks = mem.ticks or 0
2022-01-03 23:40:31 +03:00
if mem.ticks > 0 then
2022-01-03 23:40:31 +03:00
local node = minetest.get_node(pos)
-- check if display is loaded and a player in front of the display
if node.name ~= "ignore" then
local dir = minetest.facedir_to_dir(Param2ToFacedir[node.param2 % 6])
2021-04-22 18:32:02 +03:00
dir.y = 0
local pos2 = vector.add(pos, vector.multiply(dir, RADIUS))
for _, obj in pairs(minetest.get_objects_inside_radius(pos2, RADIUS)) do
if obj:is_player() then
lcdlib.update_entities(pos)
mem.ticks = mem.ticks - 1
break
end
2020-02-29 21:20:54 +03:00
end
end
end
return true
2020-02-29 21:20:54 +03:00
end
techage.display.lcd_box = {
2020-02-29 21:20:54 +03:00
type = "wallmounted",
wall_top = {-8/16, 15/32, -8/16, 8/16, 8/16, 8/16}
}
2020-03-01 14:32:11 +03:00
minetest.register_node("techage:ta4_display", {
description = S("TA4 Display"),
2020-02-29 21:20:54 +03:00
inventory_image = 'techage_display_inventory.png',
tiles = {"techage_display.png"},
drawtype = "nodebox",
paramtype = "light",
use_texture_alpha = techage.CLIP,
2020-02-29 21:20:54 +03:00
sunlight_propagates = true,
paramtype2 = "wallmounted",
node_box = techage.display.lcd_box,
selection_box = techage.display.lcd_box,
2020-02-29 21:20:54 +03:00
light_source = 6,
2022-01-03 23:40:31 +03:00
2020-02-29 21:20:54 +03:00
display_entities = {
["techage:display_entity"] = { depth = 0.42,
on_display_update = techage.display.display_update},
2020-02-29 21:20:54 +03:00
},
after_place_node = function(pos, placer)
2020-03-01 14:32:11 +03:00
local number = techage.add_node(pos, "techage:ta4_display")
2020-02-29 21:20:54 +03:00
local meta = minetest.get_meta(pos)
meta:set_string("number", number)
meta:set_string("infotext", S("Display no: ")..number)
local nvm = techage.get_nvm(pos)
nvm.text = {"My", "Techage","TA4", "Display", "No: "..number}
2020-02-29 21:20:54 +03:00
lcdlib.update_entities(pos)
minetest.get_node_timer(pos):start(1)
2020-02-29 21:20:54 +03:00
end,
2020-06-14 00:32:00 +03:00
after_dig_node = function(pos, oldnode, oldmetadata)
techage.remove_node(pos, oldnode, oldmetadata)
2020-02-29 21:20:54 +03:00
end,
on_timer = techage.display.on_timer,
2020-02-29 21:20:54 +03:00
on_place = lcdlib.on_place,
on_construct = lcdlib.on_construct,
on_destruct = lcdlib.on_destruct,
on_rotate = lcdlib.on_rotate,
groups = {cracky=2, crumbly=2},
is_ground_content = false,
sounds = default.node_sound_glass_defaults(),
})
techage.display.lcd_boxXL = {
type = "fixed",
fixed = {-0.9, -8/16, -8/16, 0.9, -15/32, 8/16}
}
minetest.register_node("techage:ta4_displayXL", {
description = S("TA4 Display XL"),
inventory_image = 'techage_display_inventoryXL.png',
tiles = {"techage_displayXL.png"},
drawtype = "nodebox",
paramtype = "light",
use_texture_alpha = techage.CLIP,
sunlight_propagates = true,
paramtype2 = "wallmounted",
node_box = techage.display.lcd_boxXL,
selection_box = techage.display.lcd_boxXL,
light_source = 6,
2022-01-03 23:40:31 +03:00
display_entities = {
["techage:display_entityXL"] = { depth = 0.42,
on_display_update = techage.display.display_updateXL},
},
after_place_node = function(pos, placer)
local number = techage.add_node(pos, "techage:ta4_displayXL")
local meta = minetest.get_meta(pos)
meta:set_string("number", number)
meta:set_string("infotext", S("Display no: ")..number)
local nvm = techage.get_nvm(pos)
nvm.text = {"My", "Techage","TA4", "Display", "No: "..number}
lcdlib.update_entities(pos)
minetest.get_node_timer(pos):start(2)
end,
2020-06-14 00:32:00 +03:00
after_dig_node = function(pos, oldnode, oldmetadata)
techage.remove_node(pos, oldnode, oldmetadata)
end,
on_timer = techage.display.on_timer,
on_place = lcdlib.on_place,
on_construct = lcdlib.on_construct,
on_destruct = lcdlib.on_destruct,
on_rotate = lcdlib.on_rotate,
groups = {cracky=2, crumbly=2},
is_ground_content = false,
sounds = default.node_sound_glass_defaults(),
})
2020-02-29 21:20:54 +03:00
minetest.register_craft({
2020-03-01 14:32:11 +03:00
output = "techage:ta4_display",
2020-02-29 21:20:54 +03:00
recipe = {
{"", "", ""},
2020-03-01 14:32:11 +03:00
{"techage:basalt_glass_thin", "dye:green", "techage:ta4_wlanchip"},
2020-02-29 21:20:54 +03:00
{"", "default:copper_ingot", ""},
},
})
minetest.register_craft({
output = "techage:ta4_displayXL",
recipe = {
{"techage:ta4_display", "techage:ta4_display"},
{"", ""},
},
})
function techage.display.add_line(pos, payload, cycle_time)
local nvm = techage.get_nvm(pos)
local mem = techage.get_mem(pos)
2020-04-14 19:46:50 +03:00
nvm.text = nvm.text or {}
mem.ticks = mem.ticks or 0
local str = tostring(payload) or "oops"
2022-01-03 23:40:31 +03:00
if mem.ticks == 0 then
mem.ticks = cycle_time
2020-02-29 21:20:54 +03:00
end
2022-01-03 23:40:31 +03:00
while #nvm.text >= NUM_ROWS do
table.remove(nvm.text, 1)
2020-02-29 21:20:54 +03:00
end
table.insert(nvm.text, str)
2020-02-29 21:20:54 +03:00
end
2022-06-05 22:20:43 +03:00
function techage.display.write_row(pos, payload, cycle_time, beduino)
local nvm = techage.get_nvm(pos)
local mem = techage.get_mem(pos)
2022-06-05 22:20:43 +03:00
local str, row
2022-06-05 22:23:17 +03:00
2020-04-14 19:46:50 +03:00
nvm.text = nvm.text or {}
mem.ticks = mem.ticks or 0
2022-06-05 22:23:17 +03:00
if beduino or type(payload) == "string" then
2022-06-05 22:20:43 +03:00
row = tonumber(payload:sub(1,1) or "1") or 1
str = payload:sub(2) or "oops"
else
str = tostring(payload.get("str")) or "oops"
row = tonumber(payload.get("row")) or 1
end
2022-06-05 22:23:17 +03:00
if mem.ticks == 0 then
mem.ticks = cycle_time
end
2022-01-03 23:40:31 +03:00
if row < 1 then row = 1 end
if row > NUM_ROWS then row = NUM_ROWS end
2022-01-03 23:40:31 +03:00
2020-04-14 19:46:50 +03:00
while #nvm.text < row do
table.insert(nvm.text, "")
2020-02-29 21:20:54 +03:00
end
nvm.text[row] = str
end
function techage.display.clear_screen(pos, cycle_time)
local nvm = techage.get_nvm(pos)
local mem = techage.get_mem(pos)
mem.ticks = mem.ticks or 0
2022-01-03 23:40:31 +03:00
if mem.ticks == 0 then
mem.ticks = cycle_time
end
2020-04-14 19:46:50 +03:00
nvm.text = {}
2020-02-29 21:20:54 +03:00
end
2020-03-01 14:32:11 +03:00
techage.register_node({"techage:ta4_display"}, {
2020-02-29 21:20:54 +03:00
on_recv_message = function(pos, src, topic, payload)
if topic == "add" then -- add one line and scroll if necessary
techage.display.add_line(pos, payload, 1)
2020-02-29 21:20:54 +03:00
elseif topic == "set" then -- overwrite the given row
techage.display.write_row(pos, payload, 1)
2020-02-29 21:20:54 +03:00
elseif topic == "clear" then -- clear the screen
techage.display.clear_screen(pos, 1)
2020-02-29 21:20:54 +03:00
end
end,
2022-06-05 22:20:43 +03:00
on_beduino_receive_cmnd = function(pos, src, topic, payload)
if topic == 67 then -- add one line and scroll if necessary
techage.display.add_line(pos, payload, 1)
elseif topic == 68 then -- overwrite the given row
techage.display.write_row(pos, payload, 1, true)
elseif topic == 17 then -- clear the screen
techage.display.clear_screen(pos, 1)
else
return 2
end
return 0
end,
2022-01-03 23:40:31 +03:00
})
2020-02-29 21:20:54 +03:00
techage.register_node({"techage:ta4_displayXL"}, {
on_recv_message = function(pos, src, topic, payload)
if topic == "add" then -- add one line and scroll if necessary
techage.display.add_line(pos, payload, 2)
elseif topic == "set" then -- overwrite the given row
techage.display.write_row(pos, payload, 2)
elseif topic == "clear" then -- clear the screen
techage.display.clear_screen(pos, 2)
end
end,
2022-06-05 22:20:43 +03:00
on_beduino_receive_cmnd = function(pos, src, topic, payload)
if topic == 67 then -- add one line and scroll if necessary
techage.display.add_line(pos, payload, 2)
elseif topic == 68 then -- overwrite the given row
techage.display.write_row(pos, payload, 2, true)
elseif topic == 17 then -- clear the screen
techage.display.clear_screen(pos, 2)
else
return 2
end
return 0
end,
})
lcdlib.register_display_entity("techage:display_entity")
lcdlib.register_display_entity("techage:display_entityXL")