led-hands-rpi/main.rb
2019-02-24 03:12:06 +00:00

84 lines
1.0 KiB
Ruby
Executable File

# frozen_string_literal: true
#!/usr/bin/env ruby
require 'bundler'
require 'sinatra'
require 'rpi_gpio'
Bundler.setup
Bundler.require
BLUE = 37
WHITE = 35
RED = 33
GREEN = 31
YELLOW = 29
LEDS = [BLUE, WHITE, RED, GREEN, YELLOW]
IO = RPi::GPIO
IO.set_numbering :board
LEDS.each do |led|
IO.setup led, as: :output, initialize: :low
IO.set_low led
end
# IO.reset
get '/' do
@led_status = led_status
erb :index
end
get '/cycle' do
clear
5.times do |i|
sleep 0.5
IO.set_high LEDS[i]
IO.set_low LEDS[i - 1]
end
clear
redirect to('/')
end
get '/led/:color' do |led|
clear
IO.set_high pick(led)
redirect to('/')
end
def pick led
case led
when 'blue'
BLUE
when 'white'
WHITE
when 'red'
RED
when 'green'
GREEN
when 'yellow'
YELLOW
end
end
def clear
LEDS.each do |led|
IO.set_low led
end
end
def led_status
{
blue: IO.high?(BLUE),
white: IO.high?(WHITE),
red: IO.high?(RED),
green: IO.high?(GREEN),
yellow: IO.high?(YELLOW)
}
end