From 67f267d7ab2d29a6f388f27a4c930bb798dfbbbb Mon Sep 17 00:00:00 2001 From: Mark Moser Date: Sun, 24 Feb 2019 03:12:06 +0000 Subject: [PATCH] web interactions --- Gemfile | 3 ++- Gemfile.lock | 18 +++++++++++++ main.rb | 72 ++++++++++++++++++++++++++++++++++++++++--------- views/index.erb | 24 +++++++++++++++++ 4 files changed, 104 insertions(+), 13 deletions(-) create mode 100644 views/index.erb diff --git a/Gemfile b/Gemfile index 91b5ecb..2ecf02b 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/Gemfile.lock b/Gemfile.lock index bd4d528..12152a9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 diff --git a/main.rb b/main.rb index 512fc1b..bf65dc2 100755 --- a/main.rb +++ b/main.rb @@ -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 diff --git a/views/index.erb b/views/index.erb new file mode 100644 index 0000000..1fe079f --- /dev/null +++ b/views/index.erb @@ -0,0 +1,24 @@ + + + + + LED Manager + + +

LED Manager

+ + +
+    <%= led_status %>
+  
+ + + +