fix(comparator): 改进比较器检测逻辑并增强测试程序

优化比较器检测逻辑,通过多种方式尝试查找比较器设备
增强测试程序功能,提供更详细的检测结果和状态信息
This commit is contained in:
2025-09-05 22:06:41 +08:00
parent 4474238da1
commit 9ed253be4a
3 changed files with 75 additions and 20 deletions

View File

@@ -27,11 +27,30 @@ term.setBackgroundColor(old_bg)
term.at(1, 2)
-- 检查是否有比较器(comparator)连接
local comparator = peripheral.find("comparator")
local comparator = nil
local peripherals = peripheral.getNames()
-- 尝试通过不同方式查找比较器
for _, name in ipairs(peripherals) do
local p_type = peripheral.getType(name)
-- 检查类型是否包含'comparator'或'redstone'关键字
if string.find(p_type, "comparator") or string.find(p_type, "redstone") then
comparator = peripheral.wrap(name)
print(colors.green .. "Found potential comparator: " .. name .. " (Type: " .. p_type .. ")" .. colors.white)
break
end
end
-- 如果上述方法失败尝试直接使用peripheral.find
if not comparator then
-- 如果没有找到比较器,列出所有连接的外围设备
comparator = peripheral.find("comparator")
if comparator then
print(colors.green .. "Comparator detected using peripheral.find!" .. colors.white)
end
end
if not comparator then
-- 如果仍然没有找到比较器,列出所有连接的外围设备
print(colors.red .. "No comparator detected!" .. colors.white)
if #peripherals > 0 then
print(colors.yellow .. "Connected peripherals:" .. colors.white)
@@ -43,8 +62,6 @@ if not comparator then
print(colors.red .. "No peripherals detected at all." .. colors.white)
end
error("Please connect a comparator to the computer.", 0)
else
print(colors.green .. "Comparator detected!" .. colors.white)
end
-- 存储所有连接的外围设备名称
@@ -171,10 +188,9 @@ local function autoSort()
print(colors.green .. "Starting auto-sorting..." .. colors.white)
print("Press Ctrl+T to stop.")
-- 获取比较器对象
local comparator = peripheral.wrap(peripheral.find("comparator"))
-- 检查全局比较器对象是否有效
if not comparator then
print(colors.red .. "Failed to wrap comparator." .. colors.white)
print(colors.red .. "Comparator not available." .. colors.white)
return
end

View File

@@ -1,17 +1,60 @@
-- Simple comparator test program
-- Improved comparator test program
local peripheral = require("peripheral")
local term = require("term")
local colors = require("colors")
local string = require("string")
print(colors.green .. "=== Comparator Test ===" .. colors.white)
print(colors.green .. "=== Improved Comparator Test ===" .. colors.white)
-- 查找比较器
local comparator = peripheral.find("comparator")
if not comparator then
print(colors.red .. "No comparator detected!" .. colors.white)
-- 尝试通过不同方式查找比较器
local comparator = nil
local peripherals = peripheral.getNames()
local comparator_found = false
print(colors.blue .. "Searching for comparator..." .. colors.white)
-- 方法1: 检查所有外围设备的类型
for _, name in ipairs(peripherals) do
local p_type = peripheral.getType(name)
print("Checking peripheral: " .. name .. " (Type: " .. p_type .. ")")
-- 检查类型是否包含'comparator'或'redstone'关键字
if string.find(p_type, "comparator") or string.find(p_type, "redstone") then
comparator = peripheral.wrap(name)
print(colors.green .. "Found potential comparator: " .. name .. " (Type: " .. p_type .. ")" .. colors.white)
comparator_found = true
break
end
end
-- 方法2: 如果上述方法失败尝试直接使用peripheral.find
if not comparator_found then
print(colors.yellow .. "Method 1 failed. Trying peripheral.find..." .. colors.white)
comparator = peripheral.find("comparator")
if comparator then
print(colors.green .. "Comparator detected using peripheral.find!" .. colors.white)
comparator_found = true
end
end
-- 显示结果
if comparator_found then
print(colors.green .. "=== Comparator Test Results ===" .. colors.white)
print("Comparator found: YES")
-- 尝试获取比较器信号
local signal = comparator.getOutputSignal()
print("Output signal level: " .. signal)
if signal > 0 then
print(colors.blue .. "Comparator is detecting items in connected chests." .. colors.white)
else
print(colors.yellow .. "Comparator is not detecting any items. Try adding items to a chest." .. colors.white)
end
else
print(colors.red .. "=== Comparator Test Results ===" .. colors.white)
print("Comparator found: NO")
-- 列出所有连接的外围设备
local peripherals = peripheral.getNames()
if #peripherals > 0 then
print(colors.yellow .. "Connected peripherals:" .. colors.white)
for _, name in ipairs(peripherals) do
@@ -21,10 +64,6 @@ if not comparator then
else
print(colors.red .. "No peripherals detected at all." .. colors.white)
end
else
print(colors.green .. "Comparator detected!" .. colors.white)
print("Comparator type: " .. peripheral.getType(peripheral.find("comparator")))
print("Output signal level: " .. comparator.getOutputSignal())
end
print(colors.blue .. "Test completed." .. colors.white)

View File

@@ -1,5 +1,5 @@
-- LeonOS installer
local INSTALLER_VERSION = "0.3.8 Beta 20"
local INSTALLER_VERSION = "0.3.8 Beta 21"
local DEFAULT_ROM_DIR = "/leonos"
print("Start loading LeonOS installer ("..INSTALLER_VERSION..")...")