web interactions

This commit is contained in:
Mark Moser 2019-02-24 03:12:06 +00:00
parent cbcf60a3ed
commit 67f267d7ab
4 changed files with 104 additions and 13 deletions

View File

@ -4,5 +4,6 @@ source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
# gem "sinatra"
gem "thin"
gem "sinatra"
gem 'rpi_gpio'

View File

@ -1,13 +1,31 @@
GEM
remote: https://rubygems.org/
specs:
daemons (1.3.1)
eventmachine (1.2.7)
mustermann (1.0.3)
rack (2.0.6)
rack-protection (2.0.5)
rack
rpi_gpio (0.3.3)
sinatra (2.0.5)
mustermann (~> 1.0)
rack (~> 2.0)
rack-protection (= 2.0.5)
tilt (~> 2.0)
thin (1.7.2)
daemons (~> 1.0, >= 1.0.9)
eventmachine (~> 1.0, >= 1.0.4)
rack (>= 1, < 3)
tilt (2.0.9)
PLATFORMS
ruby
DEPENDENCIES
rpi_gpio
sinatra
thin
BUNDLED WITH
2.0.1

72
main.rb
View File

@ -3,33 +3,81 @@
#!/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]
LEDS = [BLUE, WHITE, RED, GREEN, YELLOW]
IO = RPi::GPIO
IO.set_numbering :board
io = RPi::GPIO
io.set_numbering :board
leds.each do |led|
io.setup led, as: :output, initialize: :low
io.set_low led
LEDS.each do |led|
IO.setup led, as: :output, initialize: :low
IO.set_low led
end
10.times do
# 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]
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
io.reset
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

24
views/index.erb Normal file
View File

@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>LED Manager</title>
</head>
<body>
<h1>LED Manager</h1>
<ul>
<li><a href="/cycle">Cycle 3 Times</a></li>
<li><a href="/led/blue">Blue</a></li>
<li><a href="/led/white">White</a></li>
<li><a href="/led/red">Red</a></li>
<li><a href="/led/green">Green</a></li>
<li><a href="/led/yellow">Yellow</a></li>
</ul>
<pre>
<%= led_status %>
</pre>
</body>
</html>