Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Added value attribute to Sensor to allow for analog read #67

Merged
merged 1 commit into from

3 participants

@efatsi

This would let you call .value on a Dino::Components::Sensor object and get the analog value at any time.

@justinledwards

This is a nice feature. It'd be cool if it worked on some of the other sensor libs too

@efatsi

Sure thing, it's a simple enough feature. Do you want me to throw it on all things which use the analog pins?

@austinbv
Owner

@efatsi sure that would be great!

@austinbv austinbv merged commit 15cd48e into austinbv:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
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
Something went wrong with that request. Please try again.