Advertisement
bryangrossman

Rednet Communication Test

Nov 10th, 2024 (edited)
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.82 KB | None | 0 0
  1. -- Rednet Communication Test Program for Computer A and B
  2. -- This program runs in two modes based on the configuration in an INI file.
  3.  
  4. -- Save INI Configuration File
  5. local function saveConfig(config)
  6.     local file = fs.open("config.ini", "w")
  7.     for key, value in pairs(config) do
  8.         file.writeLine(key .. "=" .. value)
  9.     end
  10.     file.close()
  11. end
  12.  
  13. -- Load INI Configuration File
  14. local function loadConfig()
  15.     if not fs.exists("config.ini") then
  16.         -- Create a default config.ini file with mode A
  17.         local defaultConfig = { mode = "A" }
  18.         saveConfig(defaultConfig)
  19.     end
  20.  
  21.     local file = fs.open("config.ini", "r")
  22.     local config = {}
  23.     for line in file.readLine do
  24.         local key, value = string.match(line, "(%w+)%s*=%s*(%w+)")
  25.         if key and value then
  26.             config[key] = value
  27.         end
  28.     end
  29.     file.close()
  30.     return config
  31. end
  32.  
  33. -- Message Queue to hold incoming messages
  34. local messageQueue = {}
  35.  
  36. -- Function to receive messages and append them to the queue
  37. local function messageListener()
  38.     while true do
  39.         local senderId, message = rednet.receive()
  40.         table.insert(messageQueue, {senderId = senderId, message = message})
  41.     end
  42. end
  43.  
  44. -- Function to control the lamp based on received messages
  45. local function lampController()
  46.     while true do
  47.         if #messageQueue > 0 then
  48.             local receivedMessage = table.remove(messageQueue, 1)
  49.             local message = receivedMessage.message
  50.             if message == "on" then
  51.                 redstone.setOutput("right", true)
  52.                 print("Lamp turned ON")
  53.             elseif message == "off" then
  54.                 redstone.setOutput("right", false)
  55.                 print("Lamp turned OFF")
  56.             end
  57.         end
  58.         sleep(0.1)  -- Small delay to avoid busy waiting
  59.     end
  60. end
  61.  
  62. -- Main Program
  63. local config = loadConfig()
  64.  
  65. if config["mode"] == "A" then
  66.     -- Computer A: Operating the Switch
  67.     rednet.open("back")
  68.     print("Running as Computer A - Switch Controller")
  69.     local previousState = nil
  70.     while true do
  71.         local switchState = redstone.getInput("left")
  72.         if switchState ~= previousState then
  73.             local message = switchState and "on" or "off"
  74.             rednet.broadcast(message)
  75.             print("Broadcasting switch state: " .. message)
  76.             previousState = switchState
  77.         end
  78.         sleep(0.1)  -- Small delay to reduce CPU usage
  79.     end
  80.  
  81. elseif config["mode"] == "B" then
  82.     -- Computer B: Operating the Lamp
  83.     rednet.open("back")
  84.     print("Running as Computer B - Lamp Controller")
  85.     -- Run both the message listener and the lamp controller in parallel
  86.     parallel.waitForAll(messageListener, lampController)
  87.  
  88. else
  89.     error("Invalid mode in configuration file! Must be 'A' or 'B'")
  90. end
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement