Skip to content
Browse files

Merge pull request #67 from efatsi/master

Added value attribute to Sensor to allow for analog read
  • Loading branch information...
2 parents f270d48 + 9202d87 commit 15cd48e849faeb08c7dc017ff39ff16ff2e165a3 @austinbv committed Dec 4, 2013
Showing with 18 additions and 2 deletions.
  1. +4 −0 lib/dino/components/sensor.rb
  2. +14 −2 spec/lib/components/sensor_spec.rb
View
4 lib/dino/components/sensor.rb
@@ -1,8 +1,11 @@
module Dino
module Components
class Sensor < BaseComponent
+ attr_reader :value
+
def after_initialize(options={})
@data_callbacks = []
+ @value = 0
@board.add_analog_hardware(self)
@board.start_read
end
@@ -12,6 +15,7 @@ def when_data_received(&block)
end
def update(data)
+ @value = data
@data_callbacks.each do |callback|
callback.call(data)
end
View
16 spec/lib/components/sensor_spec.rb
@@ -29,10 +29,15 @@ module Components
sensor = Sensor.new(board: board, pin: 'a pin')
sensor.instance_variable_get(:@data_callbacks).should == []
end
+
+ it 'should initialize value' do
+ sensor = Sensor.new(board: board, pin: 'a pin')
+ sensor.value.should == 0
+ end
end
describe '#when_data_received' do
-
+
it "should add a callback to the list of callbacks" do
sensor = Sensor.new(board: board, pin: 'a pin')
sensor.when_data_received { "this is a block" }
@@ -43,7 +48,7 @@ module Components
describe '#update' do
it 'should call all callbacks passing in the given data' do
sensor = Sensor.new(board: board, pin: 'a pin')
-
+
first_block_data = nil
second_block_data = nil
sensor.when_data_received do |data|
@@ -56,6 +61,13 @@ module Components
sensor.update('Some data')
[first_block_data, second_block_data].each { |block_data| block_data.should == "Some data" }
end
+
+ it 'should update the value' do
+ sensor = Sensor.new(board: board, pin: 'a pin')
+
+ sensor.update('Some data')
+ sensor.value.should == 'Some data'
+ end
end
end
end

0 comments on commit 15cd48e

Please sign in to comment.
Something went wrong with that request. Please try again.