led-hands-rpi/main.rb

84 lines
1.0 KiB
Ruby
Raw Normal View History

2019-01-27 13:59:09 -06:00
# frozen_string_literal: true
#!/usr/bin/env ruby
require 'bundler'
2019-02-23 21:12:06 -06:00
require 'sinatra'
2019-01-27 13:59:09 -06:00
require 'rpi_gpio'
Bundler.setup
Bundler.require
BLUE = 37
WHITE = 35
RED = 33
GREEN = 31
YELLOW = 29
2019-02-23 21:12:06 -06:00
LEDS = [BLUE, WHITE, RED, GREEN, YELLOW]
IO = RPi::GPIO
IO.set_numbering :board
2019-01-27 13:59:09 -06:00
2019-02-23 21:12:06 -06:00
LEDS.each do |led|
IO.setup led, as: :output, initialize: :low
IO.set_low led
end
2019-01-27 13:59:09 -06:00
2019-02-23 21:12:06 -06:00
# IO.reset
2019-01-27 13:59:09 -06:00
2019-02-23 21:12:06 -06:00
get '/' do
@led_status = led_status
erb :index
2019-01-27 13:59:09 -06:00
end
2019-02-23 21:12:06 -06:00
get '/cycle' do
clear
2019-01-27 13:59:09 -06:00
5.times do |i|
sleep 0.5
2019-02-23 21:12:06 -06:00
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
2019-01-27 13:59:09 -06:00
end
end
2019-02-23 21:12:06 -06:00
def led_status
{
blue: IO.high?(BLUE),
white: IO.high?(WHITE),
red: IO.high?(RED),
green: IO.high?(GREEN),
yellow: IO.high?(YELLOW)
}
end