whipped-cream 0.0.1pre3 → 0.0.1pre4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -1,7 +1,9 @@
1
- # whipped-cream
2
- [![Gem Version](https://badge.fury.io/rb/whipped-cream.png)](http://badge.fury.io/rb/whipped-cream)
3
- [![Build Status](https://travis-ci.org/justincampbell/whipped-cream.png?branch=master)](https://travis-ci.org/justincampbell/whipped-cream)
4
- [![Code Climate](https://codeclimate.com/github/justincampbell/whipped-cream.png)](https://codeclimate.com/github/justincampbell/whipped-cream)
1
+ # ![Whipped Cream logo](header.png)
2
+
3
+ > HTTP topping for [Raspberry Pi](http://www.raspberrypi.org)
4
+ > [![Gem Version](https://badge.fury.io/rb/whipped-cream.png)](http://badge.fury.io/rb/whipped-cream)
5
+ > [![Build Status](https://travis-ci.org/justincampbell/whipped-cream.png?branch=master)](https://travis-ci.org/justincampbell/whipped-cream)
6
+ > [![Code Climate](https://codeclimate.com/github/justincampbell/whipped-cream.png)](https://codeclimate.com/github/justincampbell/whipped-cream)
5
7
 
6
8
  ## DSL
7
9
 
data/demo.rb CHANGED
@@ -4,6 +4,8 @@ button "Open/Close", pin: 1
4
4
 
5
5
  sensor "Door", pin: 2, high: "Open", low: "Closed"
6
6
 
7
+ switch "Light", pin: 3
8
+
7
9
  sensor "Ruby Version" do
8
10
  RUBY_VERSION
9
11
  end
Binary file
@@ -6,4 +6,5 @@ require 'whipped-cream/plugin'
6
6
  require 'whipped-cream/runner'
7
7
  require 'whipped-cream/sensor'
8
8
  require 'whipped-cream/server'
9
+ require 'whipped-cream/switch'
9
10
  require 'whipped-cream/version'
@@ -56,5 +56,9 @@ module WhippedCream
56
56
  def sensor(name, options = {}, &block)
57
57
  plugin.controls << Sensor.new(name, options, &block)
58
58
  end
59
+
60
+ def switch(name, options = {})
61
+ plugin.controls << Switch.new(name, options)
62
+ end
59
63
  end
60
64
  end
@@ -7,16 +7,18 @@ module PiPiper
7
7
  def initialize(options)
8
8
  @pin = options[:pin]
9
9
  @direction = options[:direction]
10
+
11
+ @value = 0
10
12
  end
11
13
 
12
14
  def on
13
- @value = :on
15
+ @value = 1
14
16
 
15
17
  log "Pin #{pin} on"
16
18
  end
17
19
 
18
20
  def off
19
- @value = :off
21
+ @value = 0
20
22
 
21
23
  log "Pin #{pin} off"
22
24
  end
@@ -36,6 +36,7 @@ module WhippedCream
36
36
  def configure
37
37
  configure_buttons
38
38
  configure_sensors
39
+ configure_switches
39
40
  end
40
41
 
41
42
  def configure_buttons
@@ -78,6 +79,18 @@ module WhippedCream
78
79
  end
79
80
  end
80
81
 
82
+ def configure_switches
83
+ plugin.switches.each do |switch|
84
+ create_pin switch, direction: :out
85
+
86
+ define_singleton_method switch.id do
87
+ pin = pins[switch.id]
88
+
89
+ pin.value == 1 ? pin.off : pin.on
90
+ end
91
+ end
92
+ end
93
+
81
94
  def create_pin(control, options = {})
82
95
  return unless control.pin
83
96
 
@@ -46,6 +46,7 @@ module WhippedCream
46
46
 
47
47
  def build_routes
48
48
  build_button_routes
49
+ build_switch_routes
49
50
  end
50
51
 
51
52
  def build_button_routes
@@ -57,6 +58,15 @@ module WhippedCream
57
58
  end
58
59
  end
59
60
 
61
+ def build_switch_routes
62
+ plugin.switches.each do |switch|
63
+ web.get "/#{switch.id}" do
64
+ runner.send(switch.id)
65
+ redirect to('/')
66
+ end
67
+ end
68
+ end
69
+
60
70
  # A Sinatra application skeleton that is used to build up the web server
61
71
  # for this plugin.
62
72
  class Web < Sinatra::Application
@@ -0,0 +1,11 @@
1
+ module WhippedCream
2
+ # A switch toggles a pin between on and off states
3
+ class Switch < Control
4
+ attr_reader :name, :pin
5
+
6
+ def initialize(name, options = {})
7
+ @name = name
8
+ @pin = options[:pin]
9
+ end
10
+ end
11
+ end
@@ -1,3 +1,3 @@
1
1
  module WhippedCream
2
- VERSION = "0.0.1pre3"
2
+ VERSION = "0.0.1pre4"
3
3
  end
@@ -0,0 +1,7 @@
1
+ <div class="item">
2
+ <h2><%= control.name %></h2>
3
+ <p><%= runner.pins[control.id].value.nonzero? ? "On" : "Off" %></p>
4
+ <form action="/<%= control.id %>" method="get">
5
+ <input type="submit" value="<%= runner.pins[control.id].value.nonzero? ? "Off" : "On" %>">
6
+ </form>
7
+ </div>
Binary file
@@ -136,4 +136,27 @@ describe WhippedCream::Builder do
136
136
  end
137
137
  end
138
138
  end
139
+
140
+ describe "#switch" do
141
+ subject { plugin.switches }
142
+
143
+ it { should be_empty }
144
+
145
+ context "with a switch" do
146
+ let(:plugin) {
147
+ described_class.build do
148
+ switch "Light", pin: 3
149
+ end
150
+ }
151
+
152
+ it "adds a switch" do
153
+ expect(plugin.switches).to have(1).item
154
+
155
+ switch = plugin.switches.first
156
+
157
+ expect(switch.name).to eq("Light")
158
+ expect(switch.pin).to eq(3)
159
+ end
160
+ end
161
+ end
139
162
  end
@@ -77,4 +77,32 @@ describe WhippedCream::Runner do
77
77
  end
78
78
  end
79
79
  end
80
+
81
+ context "with a switch" do
82
+ let(:plugin) {
83
+ WhippedCream::Plugin.build do
84
+ switch "Light", pin: 3
85
+ end
86
+ }
87
+
88
+ it "sets up that pin with direction: :out" do
89
+ pin = runner.pins[:light]
90
+
91
+ expect(pin).to be_a(PiPiper::Pin)
92
+ expect(pin.pin).to eq(3)
93
+ expect(pin.direction).to eq(:out)
94
+ end
95
+
96
+ it "defines a light method that switches the pin on and off" do
97
+ pin = runner.pins[:light]
98
+
99
+ expect(pin.value).to eq(0)
100
+ runner.light
101
+ expect(pin.value).to eq(1)
102
+ runner.light
103
+ expect(pin.value).to eq(0)
104
+ runner.light
105
+ expect(pin.value).to eq(1)
106
+ end
107
+ end
80
108
  end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe WhippedCream::Switch do
4
+ subject(:switch) { described_class.new(name, pin: pin) }
5
+
6
+ let(:name) { "Light" }
7
+ let(:pin) { 3 }
8
+
9
+ its(:name) { should eq(name) }
10
+ its(:id) { should eq(:light) }
11
+ its(:type) { should eq(:switch) }
12
+ its(:pin) { should eq(3) }
13
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: whipped-cream
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1pre3
4
+ version: 0.0.1pre4
5
5
  prerelease: 5
6
6
  platform: ruby
7
7
  authors:
@@ -192,6 +192,7 @@ files:
192
192
  - WELCOME
193
193
  - bin/whipped-cream
194
194
  - demo.rb
195
+ - header.png
195
196
  - lib/whipped-cream.rb
196
197
  - lib/whipped-cream/builder.rb
197
198
  - lib/whipped-cream/button.rb
@@ -209,11 +210,14 @@ files:
209
210
  - lib/whipped-cream/runner.rb
210
211
  - lib/whipped-cream/sensor.rb
211
212
  - lib/whipped-cream/server.rb
213
+ - lib/whipped-cream/switch.rb
212
214
  - lib/whipped-cream/version.rb
213
215
  - lib/whipped-cream/views/button.erb
214
216
  - lib/whipped-cream/views/index.erb
215
217
  - lib/whipped-cream/views/layout.erb
216
218
  - lib/whipped-cream/views/sensor.erb
219
+ - lib/whipped-cream/views/switch.erb
220
+ - logo.png
217
221
  - spec/lib/whipped-cream/builder_spec.rb
218
222
  - spec/lib/whipped-cream/button_spec.rb
219
223
  - spec/lib/whipped-cream/cli_spec.rb
@@ -222,6 +226,7 @@ files:
222
226
  - spec/lib/whipped-cream/runner_spec.rb
223
227
  - spec/lib/whipped-cream/sensor_spec.rb
224
228
  - spec/lib/whipped-cream/server_spec.rb
229
+ - spec/lib/whipped-cream/switch_spec.rb
225
230
  - spec/lib/whipped-cream_spec.rb
226
231
  - spec/spec_helper.rb
227
232
  - whipped-cream.gemspec
@@ -262,5 +267,6 @@ test_files:
262
267
  - spec/lib/whipped-cream/runner_spec.rb
263
268
  - spec/lib/whipped-cream/sensor_spec.rb
264
269
  - spec/lib/whipped-cream/server_spec.rb
270
+ - spec/lib/whipped-cream/switch_spec.rb
265
271
  - spec/lib/whipped-cream_spec.rb
266
272
  - spec/spec_helper.rb