Skip to content

Instantly share code, notes, and snippets.

@b1nary
Last active September 25, 2018 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b1nary/f576d42067cc16d2684d to your computer and use it in GitHub Desktop.
Save b1nary/f576d42067cc16d2684d to your computer and use it in GitHub Desktop.
LedBorg examples ported to Ruby. All examples are 1/1 ported from the Python orginals @ http://piborg.org/ledborg/examples
#!/usr/bin/env ruby
# Load library functions we want
require 'open-uri'
# Setup paramters
cheerlightsUrl = "http://api.thingspeak.com/channels/1417/field/1/last.txt"
colourMap = { "red" => "200",
"green" => "020",
"blue" => "002",
"cyan" => "022",
"white" => "111",
"warmwhite" => "222",
"purple" => "102",
"magenta" => "202",
"yellow" => "220",
"orange" => "210"
}
# Loop indefinitely
while true
begin # Catch errors
colourName = open(cheerlightsUrl) { |io| io.read } # Read cheerlights file via URL
if colourMap.include? colourName # If we recognise this colour name then ...
ledBorgColour = colourMap[colourName] # Get the LedBorg colour to use from the name
else # Otherwise ...
puts "Unexpected colour #{colourName}" # Display the name we did not recognise
ledBorgColour = "000" # Use the colour of black / off
end
ledBorg = open('/dev/ledborg', 'w') # Open the LedBorg driver
ledBorg.write(ledBorgColour) # Set LedBorg to the new colour
ledBorg.close # Close the LedBorg driver
rescue
end
sleep(1) # Wait for 1 second
end
#!/usr/bin/env ruby
# Loop indefinitely
while true
red = rand(2) # Generate a value for red between 0 and 2 inclusive
green = rand(2) # Generate a value for green between 0 and 2 inclusive
blue = rand(2) # Generate a value for blue between 0 and 2 inclusive
colour = "#{red}#{green}#{blue}" # Create a text string of the form "RGB" from our random values
ledBorg = open('/dev/ledborg', 'w') # Open the LedBorg device for writing to
ledBorg.write(colour) # Write the colour string to the LedBorg device
ledBorg.close # Close the LedBorg device
sleep(1) # Wait for 1 second
end
#!/usr/bin/env ruby
# Setup our sequence list
colours = ["200", "210", "110", "120", "020", "021", "011", "012", "002", "102", "101", "201"]
# Loop indefinitely
while true
colours.each do |colour| # For each colour in the list in order...
ledBorg = open('/dev/ledborg', 'w') # Open the LedBorg device for writing to
ledBorg.write(colour) # Write the colour string to the LedBorg device
ledBorg.close # Close the LedBorg device
sleep(1) # Wait for 1 second
end
end
#!/usr/bin/env ruby
# Function to get a keypress without showing text on screen
# this function is a bit more complex then the rest of the
# example, understanding it is a more advanced topic
def getKey
begin
system("stty raw -echo")
str = STDIN.getc
ensure
system("stty -raw echo")
end
str.chr
end
# Display the options to the user as a menu
puts "+-------------------------------+"
puts "| LedBorg |"
puts "+-------------------------------+"
puts "| |"
puts "| R Cycle red |"
puts "| G Cycle green |"
puts "| B Cycle blue |"
puts "| 0 Set all off |"
puts "| 1 Set all 50% |"
puts "| 2 Set all 100% |"
puts "| I Invert all channels |"
puts "| |"
puts "| Q Quit |"
puts "| |"
puts "+-------------------------------+"
puts
# Loop indefinitely
while true
# Get the next user command
command = getKey # Read the next command from the user
# Read the current colour from the LedBorg device
oldColour = open('/dev/ledborg', 'r').read # Read the entire contents of the LedBorg file (should only be 3 characters long)
red = oldColour[0].to_i # Get the red setting from the colour string
green = oldColour[1].to_i # Get the green setting from the colour string
blue = oldColour[2].to_i # Get the blue setting from the colour string
# Process the users command
command.upcase! # Convert the command to upper case (we do not care what case it is)
if command == "R" # If the user pressed R
red += 1 # Increase red by 1
red = 0 if red > 2 # Set back to 0 if green is now above 100%
elsif command == "G" # If the user pressed G ...
green += 1 # Increase green by 1
green = 0 if green > 2 # Set back to 0 if green is now above 100%
elsif command == "B" # If the user pressed B...
blue += 1 # Increase blue by 1
blue = 0 if blue > 2 # Set back to 0 if blue is now above 100%
elsif command == "0" # If the user pressed B...
red = 0 # Set red to off
green = 0 # Set green to off
blue = 0 # Set blue to off
elsif command == "1" # If the user pressed 1...
red = 1 # Set red to 50%
green = 1 # Set green to 50%
blue = 1 # Set blue to 50%
elsif command == "2" # If the user pressed 2...
red = 2 # Set red to 100%
green = 2 # Set green to 100%
blue = 2 # Set blue to 100%
elsif command == "I" # If the user pressed I...
red = 2 - red # Set red to its inverse
green = 2 - green # Set green to its inverse
blue = 2 - blue # Set blue to its inverse
elsif command == "Q" # If the user pressed Q...
break # Jump out of the while loop
elsif command == "\3" # If the user pressed CTRL+C...
break # Jump out of the while loop
else # If the user pressed anything else
puts "Unknown option #{command}" # Print an error message
next # Jump back to the start of the while loop (skip setting the colour)
end
newColour = "#{red}#{green}#{blue}" # Create a text string of the form "RGB" from our new values
ledBorg = open('/dev/ledborg', 'w') # Open the LedBorg device for writing to
ledBorg.write(newColour) # Write the new colour string to the LedBorg device
ledBorg.close # Close the LedBorg device
end
@piborg
Copy link

piborg commented Apr 16, 2014

Your Ruby port has been selected as this weeks winner for the PiBorg free swag competition.
Let us know your details via email info@piborg.org to claim your prize.

Congratulations,
PiBorg.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment