From 167896ade8997b54797627d3a2631eacbb1bc396 Mon Sep 17 00:00:00 2001 From: Matthieu Jolimaitre Date: Wed, 15 May 2024 04:22:05 +0200 Subject: [PATCH 1/2] add utils function repository --- cc-tweaked/utils.lua | 64 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 cc-tweaked/utils.lua diff --git a/cc-tweaked/utils.lua b/cc-tweaked/utils.lua new file mode 100644 index 0000000..54234ad --- /dev/null +++ b/cc-tweaked/utils.lua @@ -0,0 +1,64 @@ +local function dbg(item) + local function indent(indentation) + return string.rep(" ", indentation) + end + + local function dbg_indent(item_, indentation) + local result = "" + if type(item_) == "table" then + result = "{" + for key, value in pairs(item_) do + result = result + .. "\n" .. indent(indentation + 1) + .. dbg_indent(key, indentation + 1) .. ": " + .. dbg_indent(value, indentation + 1) + end + result = result + .. "\n" .. indent(indentation) .. "}" + elseif type(item_) == "string" then + result = "\"" .. item_ .. "\"" + else + result = tostring(item_) + end + return result + end + + print(dbg_indent(item, 0)) + return item +end + +local function new_config(name) + local path = "/etc/" .. name + + local function read_key_in_line(line, key) + local prefix = key .. ":" + local prefix_len = string.len(prefix) + local actual = string.sub(line, 0, prefix_len) + if not (prefix == actual) then return nil end + local value = string.sub(line, prefix_len + 1) + return value + end + + local function get(key, default) + local file = fs.open(path, "r") + if file == nil then return default end + while true do + local line = file.readLine() + if line == nil then return default end + local value = read_key_in_line(line, key) + if not (value == nil) then return value end + end + end + + local function get_num(key, default) + local str = get(key, nil) + if str == nil then return default end + local result = tonumber(str) + if result == nil then + error("Tried to parse conf '" .. path .. "'\nkey \"" .. key .. "\": \"" .. str .. "\" as number.") + end + return result + end + + return { get = get, get_num = get_num } +end From 9789676cad318678da87ca9991cf86ecb21791a0 Mon Sep 17 00:00:00 2001 From: Matthieu Jolimaitre Date: Wed, 15 May 2024 04:22:27 +0200 Subject: [PATCH 2/2] add me-matrix display script --- cc-tweaked/scripts/me-matrix.lua | 131 +++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 cc-tweaked/scripts/me-matrix.lua diff --git a/cc-tweaked/scripts/me-matrix.lua b/cc-tweaked/scripts/me-matrix.lua new file mode 100644 index 0000000..347d77c --- /dev/null +++ b/cc-tweaked/scripts/me-matrix.lua @@ -0,0 +1,131 @@ +local function new_config(name) + local path = "/etc/" .. name + + local function read_key_in_line(line, key) + local prefix = key .. ":" + local prefix_len = string.len(prefix) + local actual = string.sub(line, 0, prefix_len) + if not (prefix == actual) then return nil end + local value = string.sub(line, prefix_len + 1) + return value + end + + local function get(key, default) + local file = fs.open(path, "r") + if file == nil then return default end + while true do + local line = file.readLine() + if line == nil then return default end + local value = read_key_in_line(line, key) + if not (value == nil) then return value end + end + end + + local function get_num(key, default) + local str = get(key, nil) + if str == nil then return default end + local result = tonumber(str) + if result == nil then + error("Tried to parse conf '" .. path .. "'\nkey \"" .. key .. "\": \"" .. str .. "\" as number.") + end + return result + end + + return { get = get, get_num = get_num } +end + +local function item_list_to_map(item_list) + local result_ = {} + for _, item in pairs(item_list) do + local name = item.name + result_[name] = item.amount + end + return result_ +end + +local function diff_items(prev_item_map, item_map) + local diff = {} + for name, amount in pairs(item_map) do + local prev_amount = prev_item_map[name] + if prev_amount == nil then + diff[#diff + 1] = { name = name, amount = amount } + else + local item_diff = amount - prev_amount + if not (item_diff == 0) then + diff[#diff + 1] = { name = name, amount = item_diff } + end + end + end + return diff +end + +local function add_frame_item_pos(frame, max) + for _, item in pairs(frame) do + item.pos = math.random(max) + end +end + +local function display_name(complete) + local index = string.find(complete, ":") + local name = string.sub(complete, index + 1) + local initials = string.sub(name, 1, 2) + local last_found = 1 + while true do + local index_ = string.find(name, "_", last_found) + if index_ == nil then break end + last_found = index_ + 1 + initials = initials .. "_" .. string.sub(name, last_found, last_found + 1) + end + return initials +end + +local function main() + local ae = peripheral.find("meBridge") + if ae == nil then + print("ME bridge not found") + return + end + + local monitor = peripheral.find("monitor") + if monitor == nil then + print("Monitor not found") + return + end + monitor.setTextScale(0.5) + local width, height = monitor.getSize() + local delay = new_config("me-matrix").get_num("delay", 0.1) + print("Running with: res (", width, ",", height, "), delay ", delay, "s") + + local prev_item_map = item_list_to_map(ae.listItems()) + local frames = {} + local iteration = 0 + while true + do + local item_map = item_list_to_map(ae.listItems()) + local frame = diff_items(prev_item_map, item_map) + prev_item_map = item_map + + add_frame_item_pos(frame, width) + table.insert(frames, 1, frame) + table.remove(frames, height + 1) + + monitor.clear() + for frame_index, frame_ in ipairs(frames) do + for _, item in ipairs(frame_) do + monitor.setCursorPos(item.pos, frame_index) + if item.amount > 0 then + monitor.setTextColour(colors.lightBlue) + else + monitor.setTextColour(colors.purple) + end + monitor.write(display_name(item.name)) + end + end + + iteration = iteration + 1 + if iteration % 10 == 0 then print("Iteration", iteration) end + os.sleep(delay) + end +end + +main()