Posted 29 January 2017 - 07:43 PM
I've never worked with bundled cables before and really enjoyed developing buttonBuddy, so I decided to learn about bundled cables and write another API.
cableBuddy is designed to make working with bundled cables easier - everything is boiled down to eight simple methods. Just set (or query) each side and let cableBuddy sort it out for you. cableBuddy handles the bundled cable/redstone and color APIs so you don't have to.
local cable = dofile("cableBuddy.api")
After being loaded, all methods are called via cable.method(arguments)
side (string) = the side on which the color is to be set.
color (number) = the color value to be assigned to the selected side.
side (string) = the side on which the color(s) is/are to be set.
color (number) = the color value(s) to be assigned to the selected side.
side (string) = the side on which the color(s) is/are to be removed.
color (number) = the color value(s) to be removed from the selected side.
side (string) = the side on which all colors are to be removed.
side (string) = the side for which the retrieve the input color(s) value.
side (string) = the side for which the retrieve the output color(s) value.
side (string) = the side for which the retrieve the output color(s) value.
color (number) = the color to look for on the specified input side.
side (string) = the side for which the retrieve the output color(s) value.
color (number) = the color to look for on the specified output side.
local cable = dofile("cableBuddy.api")
After being loaded, all methods are available via cable.method(…)
local cable = dofile("cableBuddy.api")
After being loaded, all methods are available via cable.method(arguments)
Pastebin: Sy4zgkXS
ComputerCraft: pastebin get Sy4zgkXS cableBuddy.api
As always, thank you to everyone who made this possible!
cableBuddy is designed to make working with bundled cables easier - everything is boiled down to eight simple methods. Just set (or query) each side and let cableBuddy sort it out for you. cableBuddy handles the bundled cable/redstone and color APIs so you don't have to.
Spoiler
cableBuddy is designed to be loaded with dofile()local cable = dofile("cableBuddy.api")
After being loaded, all methods are called via cable.method(arguments)
Spoiler
Sets the specified side(s) to the single value specified for each side. The single value provided for each side may represent a single color or a combined color value.
cable.set(side, color, side, color, etc...)
side (string) = the side on which the color is to be set.
color (number) = the color value to be assigned to the selected side.
Spoiler
Adds the specified color(s) to the specified side(s), leaving all other colors intact. Each color value provided may represent a single color or a combined color value.
cable.add(side, color, color, color, side, color, etc...)
side (string) = the side on which the color(s) is/are to be set.
color (number) = the color value(s) to be assigned to the selected side.
Spoiler
Removes the specified color(s) from the specified side(s), leaving all other colors intact. Each color value provided may represent a single color or a combined color value.
cable.remove(side, color, color, color, side, color, etc...)
side (string) = the side on which the color(s) is/are to be removed.
color (number) = the color value(s) to be removed from the selected side.
Spoiler
Sets the specified side(s) to no output (all colors off).
cable.off(side, side, etc...)
side (string) = the side on which all colors are to be removed.
Spoiler
Returns the current input color value for the side specified.
local cableColors = cable.getInput(side)
side (string) = the side for which the retrieve the input color(s) value.
Spoiler
Returns the current output color value for the side specified.
local cableColors = cable.getOutput(side)
side (string) = the side for which the retrieve the output color(s) value.
Spoiler
Returns true or false depending on whether the provided color value is currently an active input. The color value provided may represent a single color or a combined color value.
local colorIsOnSide = cable.findInputColor(side, color)
- or -
if cable.findInputColor(side, color) then...
side (string) = the side for which the retrieve the output color(s) value.
color (number) = the color to look for on the specified input side.
Spoiler
Returns true or false depending on whether the provided color value is currently an active output. The color value provided may represent a single color or a combined color value.
local colorIsOnSide = cable.findOutputColor(side, color)
- or -
if cable.findOutputColor(side, color) then...
side (string) = the side for which the retrieve the output color(s) value.
color (number) = the color to look for on the specified output side.
Code Examples
cableBuddy is designed to be loaded with dofile()local cable = dofile("cableBuddy.api")
After being loaded, all methods are available via cable.method(…)
A simple repeater
This checks to see if the blue wire is active on the left, and if it is, sets the right output equal to the left input.
local cable = dofile("cableBuddy.api") --# load cableBuddy
if cable.findInputColor("left", colors.blue) then --# if the left side input has colors.blue turned on then...
cable.set("right", cable.getInput("left")) --# set the right side output to mirror the left side input
end
Bundled Cable Switch Panel
This makes use of both cableBuddy and buttonBuddy to create a basic bundled cable switcher. Each side is represented in a column, with each column populated with buttons for each color for the respective side. Advanced computer required.
if not term.isColor() or pocket or turtle then error("Advanced computer required!", 0) end --# Ensure the program is running on an advanced computer
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
term.clear()
local button = dofile("buttonBuddy.api") --# load buttonBuddy API
local cable = dofile("cableBuddy.api") --# load cableBuddy API
local validColors = { [1] = true; [2] = true; [4] = true; [8] = true; [16] = true; [32] = true; [64] = true; [128] = true; [256] = true; [512] = true; [1024] = true; [2048] = true; [4096] = true; [8192] = true; [16384] = true, [32768] = true; }
local colorNames = { [1] = "White"; [2] = "Orange"; [4] = "Magenta"; [8] = "L. Blue"; [16] = "Yellow"; [32] = "Lime"; [64] = "Pink"; [128] = "Gray"; [256] = "L. Gray"; [512] = "Cyan"; [1024] = "Purple"; [2048] = "Blue"; [4096] = "Brown"; [8192] = "Green"; [16384] = "Red", [32768] = "Black"; }
local xPos, yPos = -7, 0 --# Cursor position values
--# Function to be carried out on button press
local function buttonPress(id) --# This function takes advantage of the buttonBuddy button ID passed from buttonBuddy
if button.getText(id) == " QUIT " then --# If the button is the quit button then...
term.setBackgroundColor(colors.black)
term.setCursorPos(1, 1)
term.clear()
error() --# Exit the program
end
local colorAssignment --# Declare colorAssignment variable (for holding the color value of the cable output)
for num, name in pairs(colorNames) do --# Cycle through the color names table
if button.getText(id) == name then colorAssignment = num break end --# set colorAssignment based on button text
end
if cable.findOutputColor(button.getGroup(id), colorAssignment) then --# if the selected color is on then...
cable.remove(button.getGroup(id), colorAssignment) --# remove the appropriate color (side chosen by group)
else --# otherwise...
cable.add(button.getGroup(id), colorAssignment) --# add the appropriate color (side chosen by group)
end
button.setColors(id, nil, cable.findOutputColor(button.getGroup(id), colorAssignment) and colors.green or colors.red) --# Set the button's color (green = active, red = inactive)
end
--# Create buttons and write side labels to screen
for _, side in pairs(rs.getSides()) do --# Cycle through the computer's sides
xPos, yPos = xPos + 8, 1 --# Set xPos & yPos values for each column
term.setBackgroundColor(colors.black)
term.setCursorPos(math.floor(xPos + ((7 - #side) / 2)), yPos) --# Position the cursor to center the side (column) label
term.write(side) --# label for each side (column)
for value in pairs(validColors) do --# Cycle through the valid colors table
yPos = yPos + 1 --# Increment the yPos value
button.create(colorNames[value], side, xPos, yPos, 7, 1, 1, true, buttonPress, colors.white, cable.findOutputColor(side, value) and colors.green or colors.red) --# create button for each color on each side (green = active, red = inactive)
end
end
button.create(" QUIT ", nil, 1, 18, 1, 1, 1, true, buttonPress, colors.white, colors.orange) --# QUIT button near bottom left of screen
--# Render buttons
for _, side in pairs(rs.getSides()) do --# Cycle through the computer's sides
button.render(side, true) --# render all our groups (sides), skipping ungrouped buttons (in this case, the quit button)
end
button.render() --# render only ungrouped buttons (in this case, the quit button)
--# Main loop
while true do --# start an infinite loop
local event, data, x, y = os.pullEvent() --# wait for an event
if event == "mouse_click" then
for _, side in pairs(rs.getSides()) do --# Cycle through the computer's sides
local success, id = button.check(x, y, data, side) --# Parse buttonBuddy buttons to see if any were clicked and catch the return results in variable
if success then button.render(id) break end --# If a buttonBuddy button was pressed and successfully carried out its function, redraw the button
end
end
end
Spoiler
cableBuddy is designed to be loaded with dofile()local cable = dofile("cableBuddy.api")
After being loaded, all methods are available via cable.method(arguments)
Pastebin: Sy4zgkXS
ComputerCraft: pastebin get Sy4zgkXS cableBuddy.api
As always, thank you to everyone who made this possible!
Edited on 20 November 2017 - 08:47 PM