Skip to content

Commit

Permalink
Adds error checking to all controls to verify that specified pins are…
Browse files Browse the repository at this point in the history
… valid GPIO pins.
  • Loading branch information
rringler committed Jan 7, 2014
1 parent af58e82 commit c4ed862
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 53 deletions.
6 changes: 3 additions & 3 deletions demo.rb
@@ -1,10 +1,10 @@
name "Demo"

button "Open/Close", pin: 1
button "Open/Close", pin: 4

sensor "Door", pin: 2, high: "Open", low: "Closed"
sensor "Door", pin: 17, high: "Open", low: "Closed"

switch "Light", pin: 3
switch "Light", pin: 18

sensor "Ruby Version" do
RUBY_VERSION
Expand Down
4 changes: 4 additions & 0 deletions lib/whipped-cream/button.rb
Expand Up @@ -7,6 +7,10 @@ class Button < Control
attr_reader :name, :pin, :block

def initialize(name, options = {})
raise "Invalid pin. The pin must be one of "\
"the Raspberry Pi's valid GPIO pins: "\
"#{VALID_GPIO_PINS}" unless VALID_GPIO_PINS.include?(options[:pin])

@name = name
@pin = options[:pin]
@block = options[:block]
Expand Down
2 changes: 2 additions & 0 deletions lib/whipped-cream/control.rb
@@ -1,6 +1,8 @@
module WhippedCream
# An abstract class for controls to inherit from
class Control
VALID_GPIO_PINS = [4, 17, 18, 22, 23, 24, 25, 27]

def id
name.downcase.gsub(/[^\w]+/, '_').gsub(/^_|_$/, '').to_sym
end
Expand Down
7 changes: 7 additions & 0 deletions lib/whipped-cream/sensor.rb
Expand Up @@ -8,6 +8,13 @@ class Sensor < Control
attr_reader :name, :pin, :low, :high, :on_low, :on_high, :block

def initialize(name, options = {}, &block)
# Pushing nil onto VALID_GPIO_PINS to allow a pinless sensor
raise "Invalid pin. The pin must be one of "\
"the Raspberry Pi's valid GPIO pins: "\
"#{VALID_GPIO_PINS}" unless VALID_GPIO_PINS.dup
.push(nil)
.include?(options[:pin])

@name = name
@pin = options[:pin]
@low = options[:low]
Expand Down
4 changes: 4 additions & 0 deletions lib/whipped-cream/switch.rb
Expand Up @@ -4,6 +4,10 @@ class Switch < Control
attr_reader :name, :pin

def initialize(name, options = {})
raise "Invalid pin. The pin must be one of "\
"the Raspberry Pi's valid GPIO pins: "\
"#{VALID_GPIO_PINS}" unless VALID_GPIO_PINS.include?(options[:pin])

@name = name
@pin = options[:pin]
end
Expand Down
14 changes: 7 additions & 7 deletions spec/lib/whipped-cream/builder_spec.rb
Expand Up @@ -13,7 +13,7 @@
<<-PLUGIN
name "Garage"
button "Open/Close", pin: 1
button "Open/Close", pin: 4
PLUGIN
}

Expand Down Expand Up @@ -89,7 +89,7 @@ def foo
context "with a button" do
let(:plugin) {
described_class.build do
button "Open/Close", pin: 1 do
button "Open/Close", pin: 4 do
:tap
end
end
Expand All @@ -101,7 +101,7 @@ def foo
button = plugin.buttons.first

expect(button.name).to eq("Open/Close")
expect(button.pin).to eq(1)
expect(button.pin).to eq(4)
end
end
end
Expand All @@ -115,7 +115,7 @@ def foo
let(:plugin) {
described_class.build do
sensor "Door",
pin: 2,
pin: 17,
low: "Closed",
high: "Open",
on_high: :door_opened
Expand All @@ -128,7 +128,7 @@ def foo
sensor = plugin.sensors.first

expect(sensor.name).to eq("Door")
expect(sensor.pin).to eq(2)
expect(sensor.pin).to eq(17)
expect(sensor.low).to eq("Closed")
expect(sensor.high).to eq("Open")
expect(sensor.on_high).to eq(:door_opened)
Expand All @@ -145,7 +145,7 @@ def foo
context "with a switch" do
let(:plugin) {
described_class.build do
switch "Light", pin: 3
switch "Light", pin: 18
end
}

Expand All @@ -155,7 +155,7 @@ def foo
switch = plugin.switches.first

expect(switch.name).to eq("Light")
expect(switch.pin).to eq(3)
expect(switch.pin).to eq(18)
end
end
end
Expand Down
24 changes: 17 additions & 7 deletions spec/lib/whipped-cream/button_spec.rb
@@ -1,13 +1,23 @@
require 'spec_helper'

describe WhippedCream::Button do
subject(:button) { described_class.new(name, pin: pin, block: block) }
context 'valid button' do
subject(:button) { described_class.new(name, pin: valid_pin, block: block) }

let(:name) { "Open/Close" }
let(:pin) { nil }
let(:block) { nil }
let(:name) { "Open/Close" }
let(:valid_pin) { 4 }
let(:block) { nil }

its(:name) { should eq(name) }
its(:id) { should eq(:open_close) }
its(:type) { should eq(:button) }
its(:name) { should eq(name) }
its(:id) { should eq(:open_close) }
its(:type) { should eq(:button) }
end

context 'invalid button' do
let(:invalid_pin) { 3 }

it 'should raise an error on initialization' do
expect { described_class.new(name, pin: invalid_pin) }.to raise_error
end
end
end
2 changes: 1 addition & 1 deletion spec/lib/whipped-cream/cli_spec.rb
Expand Up @@ -12,7 +12,7 @@
<<-PLUGIN
name "Garage"
button "Open/Close", pin: 1
button "Open/Close", pin: 4
PLUGIN
}
let(:pi_address) { "192.168.0.123" }
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/whipped-cream/plugin_spec.rb
Expand Up @@ -39,7 +39,7 @@

context "with a button" do
before do
plugin.controls << WhippedCream::Button.new("Open/Close")
plugin.controls << WhippedCream::Button.new("Open/Close", pin: 4)
end

its(:controls) { should_not be_empty }
Expand Down
12 changes: 6 additions & 6 deletions spec/lib/whipped-cream/runner_spec.rb
Expand Up @@ -14,15 +14,15 @@
context "with a button" do
let(:plugin) {
WhippedCream::Plugin.build do
button "Open/Close", pin: 1
button "Open/Close", pin: 4
end
}

it "sets up that pin with direction: :out" do
pin = runner.pins[:open_close]

expect(pin).to be_a(PiPiper::Pin)
expect(pin.pin).to eq(1)
expect(pin.pin).to eq(4)
expect(pin.direction).to eq(:out)
end

Expand All @@ -40,15 +40,15 @@
context "with a sensor" do
let(:plugin) {
WhippedCream::Plugin.build do
sensor "Door", pin: 2, low: "Open", high: "Closed"
sensor "Door", pin: 17, low: "Open", high: "Closed"
end
}

it "sets up that pin with direction: :in" do
pin = runner.pins[:door]

expect(pin).to be_a(PiPiper::Pin)
expect(pin.pin).to eq(2)
expect(pin.pin).to eq(17)
expect(pin.direction).to eq(:in)
end

Expand Down Expand Up @@ -81,15 +81,15 @@
context "with a switch" do
let(:plugin) {
WhippedCream::Plugin.build do
switch "Light", pin: 3
switch "Light", pin: 18
end
}

it "sets up that pin with direction: :out" do
pin = runner.pins[:light]

expect(pin).to be_a(PiPiper::Pin)
expect(pin.pin).to eq(3)
expect(pin.pin).to eq(18)
expect(pin.direction).to eq(:out)
end

Expand Down
57 changes: 39 additions & 18 deletions spec/lib/whipped-cream/sensor_spec.rb
@@ -1,27 +1,48 @@
require 'spec_helper'

describe WhippedCream::Sensor do
subject(:sensor) { described_class.new(name, options, &block) }

let(:name) { "Door" }
let(:options) {
{
pin: 2,
high: "Open",
low: "Closed",
on_high: :door_opened
context 'valid sensor' do
subject(:sensor) { described_class.new(name, valid_options, &block) }

let(:name) { "Door" }
let(:valid_options) {
{
pin: 17,
high: "Open",
low: "Closed",
on_high: :door_opened
}
}
}
let(:block) { nil }
let(:block) { nil }

its(:name) { should eq(name) }
its(:id) { should eq(:door) }

its(:name) { should eq(name) }
its(:id) { should eq(:door) }
its(:pin) { should eq(17) }

its(:pin) { should eq(2) }
its(:high) { should eq("Open") }
its(:low) { should eq("Closed") }

its(:high) { should eq("Open") }
its(:low) { should eq("Closed") }
its(:on_high) { should eq(:door_opened) }
its(:on_low) { should be_nil }
end

context 'invalid sensor' do
let(:name) { "Door" }
let(:invalid_options) {
{
pin: 3,
high: "Open",
low: "Closed",
on_high: :door_opened
}
}
let(:block) { nil }

its(:on_high) { should eq(:door_opened) }
its(:on_low) { should be_nil }
it 'should raise an error on initialization' do
expect {
described_class.new(name, invalid_options, &block)
}.to raise_error
end
end
end
6 changes: 3 additions & 3 deletions spec/lib/whipped-cream/server_spec.rb
Expand Up @@ -5,7 +5,7 @@

let(:plugin) {
WhippedCream::Plugin.build do
button "Open/Close", pin: 1
button "Open/Close", pin: 4
end
}

Expand All @@ -28,7 +28,7 @@
context "with a button" do
let(:plugin) {
WhippedCream::Plugin.build do
button "Open/Close", pin: 1
button "Open/Close", pin: 4
end
}

Expand All @@ -44,7 +44,7 @@
context "with a switch" do
let(:plugin) {
WhippedCream::Plugin.build do
switch "Light", pin: 1
switch "Light", pin: 18
end
}

Expand Down
24 changes: 17 additions & 7 deletions spec/lib/whipped-cream/switch_spec.rb
@@ -1,13 +1,23 @@
require 'spec_helper'

describe WhippedCream::Switch do
subject(:switch) { described_class.new(name, pin: pin) }
context 'valid switch' do
subject(:switch) { described_class.new(name, pin: valid_pin) }

let(:name) { "Light" }
let(:pin) { 3 }
let(:name) { "Light" }
let(:valid_pin) { 18 }

its(:name) { should eq(name) }
its(:id) { should eq(:light) }
its(:type) { should eq(:switch) }
its(:pin) { should eq(3) }
its(:name) { should eq(name) }
its(:id) { should eq(:light) }
its(:type) { should eq(:switch) }
its(:pin) { should eq(18) }
end

context 'invalid switch' do
let(:invalid_pin) { 3 }

it 'should raise an error upon initialization' do
expect { described_class.new(name, pin: invalid_pin) }.to raise_error
end
end
end

0 comments on commit c4ed862

Please sign in to comment.