domotics-arduino 0.3.0 → 0.3.2
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/Rakefile +8 -4
- data/domotics-arduino.gemspec +2 -2
- data/lib/domotics/arduino/arduino_base.rb +8 -6
- data/lib/domotics/arduino/board_emulator.rb +50 -0
- data/lib/domotics/arduino/version.rb +1 -1
- data/test/test_arduino.rb +27 -16
- metadata +4 -6
- data/.Rakefile.kate-swp +0 -0
- data/test/board_emulator.rb +0 -39
data/Rakefile
CHANGED
@@ -33,19 +33,23 @@ task :major, :commit_message do |t, args|
|
|
33
33
|
update(args[:commit_message]){ |sv,i| i == MAJOR ? sv.succ : "0" }
|
34
34
|
end
|
35
35
|
|
36
|
+
|
36
37
|
def update(msg)
|
37
38
|
# Update version
|
38
|
-
File.open "lib/domotics/arduino/version.rb", "r+" do |f|
|
39
|
+
File.open "./lib/domotics/arduino/version.rb", "r+" do |f|
|
39
40
|
up = f.read.sub(/\d+.\d+.\d+/) { |ver| ver.split('.').map.with_index{ |sv, i| yield sv,i }.join('.') }
|
40
41
|
f.seek 0
|
41
42
|
f.write up
|
42
43
|
end
|
44
|
+
puts "Changed version."
|
45
|
+
puts "Removed #{File.delete(*Dir['./*.gem'])} old gems"
|
46
|
+
Dir["./*.gemspec"].each { |spec| puts %x(gem build #{spec}) }
|
47
|
+
Dir["./*.gem"].each { |gem| puts %x(gem push #{gem}) }
|
43
48
|
# add new files to repo
|
44
49
|
%x(git add --all .)
|
45
50
|
# commit
|
46
51
|
if msg then %x(git commit -a -m "#{msg}")
|
47
52
|
else %x(git commit -a --reuse-message=HEAD); end
|
48
|
-
|
49
|
-
|
50
|
-
Rake::Task[:release].invoke
|
53
|
+
%x(git push)
|
54
|
+
puts "Pushed to github."
|
51
55
|
end
|
data/domotics-arduino.gemspec
CHANGED
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["Goredar"]
|
10
10
|
spec.email = ["goredar@gmail.com"]
|
11
11
|
spec.description = %q{Arduino part of Domotics}
|
12
|
-
spec.summary = %q{
|
13
|
-
spec.homepage = ""
|
12
|
+
spec.summary = %q{Hardware drivers for arduino}
|
13
|
+
spec.homepage = "https://dom.goredar.it"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files`.split($/)
|
@@ -21,7 +21,7 @@ module Domotics
|
|
21
21
|
SUCCESSREPRLY = 2
|
22
22
|
FAILREPRLY = 3
|
23
23
|
# Board events
|
24
|
-
EVENTS = {'0' => :
|
24
|
+
EVENTS = {'0' => :pin_state_changed}
|
25
25
|
# Commands for board
|
26
26
|
SETPINMODE = 0
|
27
27
|
GETDIGITAL = 1
|
@@ -65,6 +65,8 @@ module Domotics
|
|
65
65
|
@pwm_pins = Array.new(12) { |index| 2+index } + [44,45,46]
|
66
66
|
end
|
67
67
|
@logger = args[:logger] || Logger.new(STDERR)
|
68
|
+
@timeout = args[:timeout] || 1
|
69
|
+
@retry_wait = args[:retry_wait] || 5
|
68
70
|
# Not allow multiple command sends
|
69
71
|
@command_lock = Mutex.new
|
70
72
|
@reply = Queue.new
|
@@ -184,13 +186,13 @@ module Domotics
|
|
184
186
|
private
|
185
187
|
|
186
188
|
# Default event handler simple prints event.
|
187
|
-
def event_handler(
|
188
|
-
raise
|
189
|
+
def event_handler(event)
|
190
|
+
raise event[:e] if event[:e].is_a? ArduinoError
|
189
191
|
end
|
190
192
|
|
191
193
|
# Send command directly to board
|
192
194
|
def send_command(command, pin = 0, value = 0)
|
193
|
-
Timeout.timeout(
|
195
|
+
Timeout.timeout(@timeout) do
|
194
196
|
@command_lock.synchronize do
|
195
197
|
@board.puts("#{command} #{pin} #{value}")
|
196
198
|
# Get reply
|
@@ -285,8 +287,8 @@ module Domotics
|
|
285
287
|
tries = tries || 0
|
286
288
|
tries += 1
|
287
289
|
if tries <= 3
|
288
|
-
@logger.info { "Attempt #{tries}: try to reconnect in #{
|
289
|
-
sleep
|
290
|
+
@logger.info { "Attempt #{tries}: try to reconnect in #{@retry_wait**tries} seconds." }
|
291
|
+
sleep @retry_wait**tries
|
290
292
|
retry
|
291
293
|
end
|
292
294
|
@logger.error { "Board [#{@port_str}] malfunction. Automatic restart failed." }
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'pty'
|
2
|
+
|
3
|
+
module Domotics
|
4
|
+
module Arduino
|
5
|
+
|
6
|
+
class BoardEmulator
|
7
|
+
attr_accessor :type, :state_list
|
8
|
+
def initialize(args = {})
|
9
|
+
success = ArduinoBase::SUCCESSREPRLY
|
10
|
+
@master, @slave = PTY.open
|
11
|
+
@type = args[:type] || :normal
|
12
|
+
@state_list = Hash.new(ArduinoBase::LOW)
|
13
|
+
Thread.new do
|
14
|
+
loop do
|
15
|
+
case @type
|
16
|
+
when :dead
|
17
|
+
terminate
|
18
|
+
when :crasy
|
19
|
+
@master.gets
|
20
|
+
@master.puts rand(4).times.map{ rand(10) }.join(' ')
|
21
|
+
when :disconnect
|
22
|
+
@master.gets
|
23
|
+
@slave.close
|
24
|
+
@master.close
|
25
|
+
else
|
26
|
+
message = @master.gets
|
27
|
+
command, pin, value = message.chomp.split(" ").map{ |m| m.to_i }
|
28
|
+
case command
|
29
|
+
when ArduinoBase::ECHOREPLY
|
30
|
+
@master.puts "#{value} #{pin}"
|
31
|
+
when ArduinoBase::SETDIGITAL
|
32
|
+
@state_list[pin] = value
|
33
|
+
@master.puts success
|
34
|
+
else
|
35
|
+
@master.puts success
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
def port
|
42
|
+
@slave.path
|
43
|
+
end
|
44
|
+
def toggle_pin(pin)
|
45
|
+
@state_list[pin] = @state_list[pin] == ArduinoBase::LOW ? ArduinoBase::HIGH : ArduinoBase::LOW
|
46
|
+
@master.puts "0 #{pin} #{@state_list[pin]}"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/test/test_arduino.rb
CHANGED
@@ -1,34 +1,45 @@
|
|
1
1
|
require 'test/unit'
|
2
2
|
require 'domotics/arduino'
|
3
|
-
require 'board_emulator'
|
4
3
|
|
5
4
|
class ArduinoTestBoard
|
5
|
+
attr_reader :last_event
|
6
6
|
include Domotics::Arduino::ArduinoBase
|
7
|
+
def event_handler(event)
|
8
|
+
super
|
9
|
+
@last_event = event
|
10
|
+
end
|
7
11
|
end
|
8
12
|
|
9
13
|
class ArduinoTest < Test::Unit::TestCase
|
10
|
-
def
|
11
|
-
|
14
|
+
def setup
|
15
|
+
#@brd = ArduinoTestBoard.new(port: Domotics::Arduino::BoardEmulator.new.port)
|
12
16
|
end
|
13
17
|
def test_dead_board
|
14
|
-
assert_raise
|
15
|
-
ArduinoTestBoard.new(port: BoardEmulator.new(type: :dead).port)
|
16
|
-
end
|
18
|
+
assert_raise(Domotics::Arduino::ArduinoError) { emulate :dead }
|
17
19
|
end
|
18
20
|
def test_crasy_board
|
19
|
-
assert_raise
|
20
|
-
ArduinoTestBoard.new(port: BoardEmulator.new(type: :crasy).port)
|
21
|
-
end
|
21
|
+
assert_raise(Domotics::Arduino::ArduinoError) { emulate :crasy }
|
22
22
|
end
|
23
23
|
def test_disconnected_board
|
24
|
-
assert_raise
|
25
|
-
|
26
|
-
|
24
|
+
assert_raise(Domotics::Arduino::ArduinoError) { emulate :disconnect }
|
25
|
+
end
|
26
|
+
def test_set_digital
|
27
|
+
brd, emul = emulate
|
28
|
+
brd.set_digital(13, Domotics::Arduino::ArduinoBase::HIGH)
|
29
|
+
assert_equal Domotics::Arduino::ArduinoBase::HIGH, emul.state_list[13]
|
30
|
+
end
|
31
|
+
def test_watch
|
32
|
+
brd, emul = emulate
|
33
|
+
emul.toggle_pin (13)
|
34
|
+
sleep 0.1
|
35
|
+
assert_equal Hash[:event, :pin_state_changed, :pin, 13, :state, Domotics::Arduino::ArduinoBase::HIGH], brd.last_event
|
27
36
|
end
|
28
|
-
def
|
29
|
-
|
37
|
+
def emulate(type = :normal)
|
38
|
+
emul = Domotics::Arduino::BoardEmulator.new :type => type
|
39
|
+
brd = ArduinoTestBoard.new port: emul.port, retry_wait: 1
|
40
|
+
[brd, emul]
|
30
41
|
end
|
31
|
-
def
|
32
|
-
|
42
|
+
def teardown
|
43
|
+
#@brd.destroy
|
33
44
|
end
|
34
45
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: domotics-arduino
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -66,7 +66,6 @@ executables: []
|
|
66
66
|
extensions: []
|
67
67
|
extra_rdoc_files: []
|
68
68
|
files:
|
69
|
-
- .Rakefile.kate-swp
|
70
69
|
- .gitignore
|
71
70
|
- Gemfile
|
72
71
|
- LICENSE.txt
|
@@ -75,6 +74,7 @@ files:
|
|
75
74
|
- domotics-arduino.gemspec
|
76
75
|
- lib/domotics/arduino.rb
|
77
76
|
- lib/domotics/arduino/arduino_base.rb
|
77
|
+
- lib/domotics/arduino/board_emulator.rb
|
78
78
|
- lib/domotics/arduino/digital_pin.rb
|
79
79
|
- lib/domotics/arduino/digital_pin/nc_sensor.rb
|
80
80
|
- lib/domotics/arduino/digital_pin/no_sensor.rb
|
@@ -82,9 +82,8 @@ files:
|
|
82
82
|
- lib/domotics/arduino/firmware/stepper.c
|
83
83
|
- lib/domotics/arduino/pwm_pin.rb
|
84
84
|
- lib/domotics/arduino/version.rb
|
85
|
-
- test/board_emulator.rb
|
86
85
|
- test/test_arduino.rb
|
87
|
-
homepage:
|
86
|
+
homepage: https://dom.goredar.it
|
88
87
|
licenses:
|
89
88
|
- MIT
|
90
89
|
post_install_message:
|
@@ -108,8 +107,7 @@ rubyforge_project:
|
|
108
107
|
rubygems_version: 1.8.25
|
109
108
|
signing_key:
|
110
109
|
specification_version: 3
|
111
|
-
summary:
|
110
|
+
summary: Hardware drivers for arduino
|
112
111
|
test_files:
|
113
|
-
- test/board_emulator.rb
|
114
112
|
- test/test_arduino.rb
|
115
113
|
has_rdoc:
|
data/.Rakefile.kate-swp
DELETED
Binary file
|
data/test/board_emulator.rb
DELETED
@@ -1,39 +0,0 @@
|
|
1
|
-
require 'pty'
|
2
|
-
|
3
|
-
class BoardEmulator
|
4
|
-
def initialize(args = {})
|
5
|
-
success = Domotics::Arduino::ArduinoBase::SUCCESSREPRLY
|
6
|
-
@master, @slave = PTY.open
|
7
|
-
type = args[:type] || :normal
|
8
|
-
Thread.new do
|
9
|
-
case type
|
10
|
-
when :dead
|
11
|
-
terminate
|
12
|
-
when :crasy
|
13
|
-
loop do
|
14
|
-
@master.gets
|
15
|
-
ans = rand(4).times.map{ rand(10) }.join(' ')
|
16
|
-
@master.puts ans
|
17
|
-
end
|
18
|
-
when :disconnect
|
19
|
-
@master.gets
|
20
|
-
@slave.close
|
21
|
-
@master.close
|
22
|
-
else
|
23
|
-
loop do
|
24
|
-
raise unless message = @master.gets
|
25
|
-
command, pin, value = message.chomp.split(" ").map{ |m| m.to_i }
|
26
|
-
case command
|
27
|
-
when Domotics::Arduino::ArduinoBase::ECHOREPLY
|
28
|
-
@master.puts "#{value} #{pin}"
|
29
|
-
else
|
30
|
-
@master.puts success
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
def port
|
37
|
-
@slave.path
|
38
|
-
end
|
39
|
-
end
|