Advertisement
XileHorizon

Lumia Test 7

Feb 21st, 2025
477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.71 KB | None | 0 0
  1. -- color_control.lua
  2.  
  3. -- 1. Define color-to-relay mapping
  4. local colorToRelay = {
  5.   red =    { relayName = "redstone_relay_0", side = "front" },
  6.   blue =   { relayName = "redstone_relay_1", side = "front" },
  7.   green =  { relayName = "redstone_relay_2", side = "front" },
  8.   yellow = { relayName = "redstone_relay_3", side = "front" },
  9.   purple = { relayName = "redstone_relay_4", side = "front" },
  10.   orange = { relayName = "redstone_relay_5", side = "front" },
  11. }
  12.  
  13. -- 2. Helper: turn off all relays
  14. local function turnOffAll()
  15.   for _, info in pairs(colorToRelay) do
  16.     local relay = peripheral.wrap(info.relayName)
  17.     if relay then
  18.       relay.setOutput(info.side, false)
  19.     end
  20.   end
  21. end
  22.  
  23. turnOffAll()  -- ensure everything is off at start
  24.  
  25. -- Keep track of what's currently active
  26. local currentColor = nil
  27.  
  28. local function activateColor(newColor)
  29.   -- If our newColor is a boolean, convert it to a string just so we don't get a concat error
  30.   if type(newColor) == "boolean" then
  31.     newColor = tostring(newColor)  -- "true" or "false"
  32.   end
  33.  
  34.   -- 3A. Turn off the old color's relay
  35.   if currentColor and colorToRelay[currentColor] then
  36.     local oldInfo = colorToRelay[currentColor]
  37.     local oldRelay = peripheral.wrap(oldInfo.relayName)
  38.     if oldRelay then
  39.       oldRelay.setOutput(oldInfo.side, false)
  40.     end
  41.   end
  42.  
  43.   -- 3B. Turn on the new color's relay
  44.   if colorToRelay[newColor] then
  45.     local info = colorToRelay[newColor]
  46.     local relay = peripheral.wrap(info.relayName)
  47.     if relay then
  48.       relay.setOutput(info.side, true)
  49.       currentColor = newColor
  50.       print("Activated color: "..newColor)
  51.     else
  52.       print("Relay peripheral not found for color: "..newColor)
  53.     end
  54.   else
  55.     print("Unknown color: "..tostring(newColor))
  56.   end
  57. end
  58.  
  59. -- 4. Connect to your Node.js WebSocket server
  60. local url = "ws://kev.unoriginal.studio:8080"
  61. print("Connecting to "..url)
  62. http.websocketAsync(url)
  63.  
  64. -- 5. Event loop to handle WebSocket messages
  65. while true do
  66.   local event, p1, p2, p3 = os.pullEvent()
  67.   if event == "websocket_success" then
  68.     print("WebSocket connected!")
  69.     -- p2 is the websocket handle
  70.     -- Optionally send a greeting
  71.     p2.send("Hello from CC: Tweaked")
  72.  
  73.   elseif event == "websocket_failure" then
  74.     print("Failed to connect:", p2)
  75.     break  -- exit the program, or handle reconnection logic
  76.  
  77.   elseif event == "websocket_message" then
  78.     -- p2 = the websocket handle, p3 = the message content (could be boolean or string)
  79.     local colorMsg = p3
  80.     print("Received color:", tostring(colorMsg))
  81.     activateColor(colorMsg)
  82.  
  83.   elseif event == "websocket_closed" then
  84.     print("WebSocket closed.")
  85.     break
  86.   end
  87. end
  88.  
  89. print("Exiting program.")
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement