dino 0.10.0 → 0.11.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. data/CHANGELOG.md +67 -0
  2. data/README.md +24 -8
  3. data/bin/dino +108 -0
  4. data/dino.gemspec +2 -1
  5. data/examples/button/button.rb +3 -2
  6. data/examples/ethernet.rb +15 -0
  7. data/examples/{ir_receiver.rb → ir_receiver/ir_receiver.rb} +3 -2
  8. data/examples/led/led.rb +3 -3
  9. data/examples/{potentiometer.rb → potentiometer/potentiometer.rb} +4 -6
  10. data/examples/{rgb_led.rb → rgb_led/rgb_led.rb} +4 -6
  11. data/examples/{sensor.rb → sensor/sensor.rb} +5 -6
  12. data/examples/ser2net.rb +31 -0
  13. data/examples/servo/servo.rb +16 -0
  14. data/examples/stepper/stepper.rb +3 -3
  15. data/lib/dino/board.rb +69 -59
  16. data/lib/dino/components/base_component.rb +8 -7
  17. data/lib/dino/components/rgb_led.rb +19 -17
  18. data/lib/dino/components/sensor.rb +2 -2
  19. data/lib/dino/components/servo.rb +8 -5
  20. data/lib/dino/components/stepper.rb +9 -9
  21. data/lib/dino/tx_rx.rb +5 -4
  22. data/lib/dino/tx_rx/base.rb +64 -0
  23. data/lib/dino/tx_rx/serial.rb +44 -0
  24. data/lib/dino/tx_rx/tcp.rb +23 -0
  25. data/lib/dino/version.rb +1 -1
  26. data/spec/lib/board_spec.rb +53 -39
  27. data/spec/lib/components/led_spec.rb +1 -1
  28. data/spec/lib/components/rgb_led_spec.rb +49 -4
  29. data/spec/lib/components/sensor_spec.rb +14 -10
  30. data/spec/lib/components/servo_spec.rb +9 -4
  31. data/spec/lib/components/stepper_spec.rb +2 -2
  32. data/spec/lib/tx_rx/serial_spec.rb +111 -0
  33. data/spec/lib/tx_rx/tcp_spec.rb +37 -0
  34. data/spec/spec_helper.rb +10 -1
  35. data/src/du/du.ino +19 -0
  36. data/src/du_ethernet/du_ethernet.ino +71 -0
  37. data/src/lib/Dino.cpp +257 -0
  38. data/src/lib/Dino.h +85 -0
  39. metadata +25 -17
  40. data/examples/servo.rb +0 -13
  41. data/examples/telnet.rb +0 -28
  42. data/lib/dino/tx_rx/telnet.rb +0 -53
  43. data/lib/dino/tx_rx/usb_serial.rb +0 -69
  44. data/spec/lib/tx_rx/telnet_spec.rb +0 -66
  45. data/spec/lib/tx_rx/usb_serial_spec.rb +0 -101
  46. data/src/du.ino +0 -251
@@ -0,0 +1,85 @@
1
+ /*
2
+ Library for dino ruby gem.
3
+ */
4
+
5
+ #ifndef Dino_h
6
+ #define Dino_h
7
+
8
+ #include "Arduino.h"
9
+ #include <Servo.h>
10
+
11
+ // Allocate listener storage based on what board we're running.
12
+ #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
13
+ # define PIN_COUNT 70
14
+ # define SERVO_OFFSET 22
15
+ #else
16
+ # define PIN_COUNT 22
17
+ # define SERVO_OFFSET 2
18
+ #endif
19
+
20
+ // Uncomment this line to enable debugging mode.
21
+ // #define debug true
22
+
23
+ class Dino {
24
+ public:
25
+ Dino();
26
+ void setupWrite(void (*writeCallback)(char *str));
27
+ void parse(char c);
28
+ void process();
29
+ void updateListeners();
30
+
31
+ private:
32
+ // Manage heartbeat and listeners.
33
+ long heartRate;
34
+ long lastUpdate;
35
+ unsigned int loopCount;
36
+ unsigned int analogDivider;
37
+
38
+ // Storage for enough analog and digital listeners for UNO or Nano board.
39
+ // Correspond to raw pin number by array index, and store boolean. false == disabled.
40
+ boolean analogListeners[PIN_COUNT];
41
+ boolean digitalListeners[PIN_COUNT];
42
+
43
+ // Keep track of the last read values for digital listeners. Only write responses when changed.
44
+ byte digitalListenerValues[PIN_COUNT];
45
+
46
+ // Request storage.
47
+ char request[8];
48
+ int index;
49
+ char cmdStr[3];
50
+ byte cmd;
51
+ char pinStr[3];
52
+ byte pin;
53
+ char valStr[4];
54
+ int val;
55
+
56
+ // Value and response storage.
57
+ int rval;
58
+ char response[8];
59
+ void (*_writeCallback)(char *str);
60
+ void writeResponse();
61
+
62
+ Servo servos[12];
63
+
64
+ // API-accessible functions.
65
+ void setMode ();
66
+ void dWrite ();
67
+ void dRead ();
68
+ void aWrite ();
69
+ void aRead ();
70
+ void addDigitalListener ();
71
+ void addAnalogListener ();
72
+ void removeListener ();
73
+ void servoToggle ();
74
+ void servoWrite ();
75
+ void reset ();
76
+ void setAnalogDivider ();
77
+ void setHeartRate ();
78
+
79
+ // Internal functions.
80
+ long timeSince (long event);
81
+ void updateDigitalListeners ();
82
+ void updateAnalogListeners ();
83
+ };
84
+
85
+ #endif
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dino
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.11.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-16 00:00:00.000000000 Z
12
+ date: 2013-06-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: serialport
@@ -31,30 +31,34 @@ description: ! "A utility library for interfacting with an Arduino.\n Designed
31
31
  control, expansion, and with love."
32
32
  email:
33
33
  - austinbv@gmail.com
34
- executables: []
34
+ executables:
35
+ - dino
35
36
  extensions: []
36
37
  extra_rdoc_files: []
37
38
  files:
38
39
  - .gitignore
39
40
  - .rvmrc
40
41
  - .travis.yml
42
+ - CHANGELOG.md
41
43
  - Gemfile
42
44
  - LICENSE
43
45
  - README.md
44
46
  - Rakefile
47
+ - bin/dino
45
48
  - dino.gemspec
46
49
  - examples/button/button.png
47
50
  - examples/button/button.rb
48
- - examples/ir_receiver.rb
51
+ - examples/ethernet.rb
52
+ - examples/ir_receiver/ir_receiver.rb
49
53
  - examples/led/led.png
50
54
  - examples/led/led.rb
51
- - examples/potentiometer.rb
52
- - examples/rgb_led.rb
53
- - examples/sensor.rb
54
- - examples/servo.rb
55
+ - examples/potentiometer/potentiometer.rb
56
+ - examples/rgb_led/rgb_led.rb
57
+ - examples/sensor/sensor.rb
58
+ - examples/ser2net.rb
59
+ - examples/servo/servo.rb
55
60
  - examples/stepper/stepper.png
56
61
  - examples/stepper/stepper.rb
57
- - examples/telnet.rb
58
62
  - lib/dino.rb
59
63
  - lib/dino/board.rb
60
64
  - lib/dino/board_not_found.rb
@@ -68,8 +72,9 @@ files:
68
72
  - lib/dino/components/servo.rb
69
73
  - lib/dino/components/stepper.rb
70
74
  - lib/dino/tx_rx.rb
71
- - lib/dino/tx_rx/telnet.rb
72
- - lib/dino/tx_rx/usb_serial.rb
75
+ - lib/dino/tx_rx/base.rb
76
+ - lib/dino/tx_rx/serial.rb
77
+ - lib/dino/tx_rx/tcp.rb
73
78
  - lib/dino/version.rb
74
79
  - spec/lib/board_not_found_spec.rb
75
80
  - spec/lib/board_spec.rb
@@ -80,10 +85,13 @@ files:
80
85
  - spec/lib/components/sensor_spec.rb
81
86
  - spec/lib/components/servo_spec.rb
82
87
  - spec/lib/components/stepper_spec.rb
83
- - spec/lib/tx_rx/telnet_spec.rb
84
- - spec/lib/tx_rx/usb_serial_spec.rb
88
+ - spec/lib/tx_rx/serial_spec.rb
89
+ - spec/lib/tx_rx/tcp_spec.rb
85
90
  - spec/spec_helper.rb
86
- - src/du.ino
91
+ - src/du/du.ino
92
+ - src/du_ethernet/du_ethernet.ino
93
+ - src/lib/Dino.cpp
94
+ - src/lib/Dino.h
87
95
  homepage: https://github.com/austinbv/dino
88
96
  licenses: []
89
97
  post_install_message:
@@ -107,7 +115,7 @@ rubyforge_project:
107
115
  rubygems_version: 1.8.24
108
116
  signing_key:
109
117
  specification_version: 3
110
- summary: Control your arduino through a serial port
118
+ summary: Control your Arduino with Ruby.
111
119
  test_files:
112
120
  - spec/lib/board_not_found_spec.rb
113
121
  - spec/lib/board_spec.rb
@@ -118,6 +126,6 @@ test_files:
118
126
  - spec/lib/components/sensor_spec.rb
119
127
  - spec/lib/components/servo_spec.rb
120
128
  - spec/lib/components/stepper_spec.rb
121
- - spec/lib/tx_rx/telnet_spec.rb
122
- - spec/lib/tx_rx/usb_serial_spec.rb
129
+ - spec/lib/tx_rx/serial_spec.rb
130
+ - spec/lib/tx_rx/tcp_spec.rb
123
131
  - spec/spec_helper.rb
@@ -1,13 +0,0 @@
1
- #
2
- # This is an example of how to use the servo class
3
- #
4
-
5
- require File.expand_path('../../lib/dino', __FILE__)
6
-
7
- board = Dino::Board.new(Dino::TxRx.new)
8
- servo = Dino::Components::Servo.new(pin: 9, board: board)
9
-
10
- loop do
11
- servo.position += 9
12
- sleep 0.5
13
- end
@@ -1,28 +0,0 @@
1
- #
2
- # Example showing how to access an Arduino over a telnet connection.
3
- # Running ser2net on the machine that the Arduino is connected to will serve up the serial interface over telnet.
4
- # You can then communicate with that Arduino over your local network or the Internet.
5
- #
6
- # Example ser2net command for an Arduino UNO on Mac:
7
- # ser2net -u -C "9000:raw:0:/dev/cu.usbmodem621:115200"
8
- #
9
- # Replace 9000 with the port number you want to use and /dev/cu.usbmodem621 with your Arduino device.
10
- # Arduino UNOs are usually /dev/ACM0 under Linux.
11
- #
12
- # ser2net is preinstalled on many Linuxes. Install ser2net on Mac with:
13
- # brew install ser2net
14
- #
15
- # http://sourceforge.net/projects/ser2net/ for more info on configuring ser2net.
16
- #
17
-
18
- require File.expand_path('../../lib/dino', __FILE__)
19
-
20
- # The host and port for the telnet connection must be passed in as arguments.
21
- connection = Dino::TxRx::Telnet.new("localhost", 9000)
22
- board = Dino::Board.new(connection)
23
- led = Dino::Components::Led.new(pin: 13, board: board)
24
-
25
- [:on, :off].cycle do |switch|
26
- led.send(switch)
27
- sleep 0.5
28
- end
@@ -1,53 +0,0 @@
1
- require 'net/telnet'
2
- require 'observer'
3
-
4
- module Dino
5
- module TxRx
6
- class Telnet
7
- include Observable
8
-
9
- def initialize(host, port)
10
- @host, @port = host, port
11
- @read_buffer = ""
12
- end
13
-
14
- def io
15
- @io ||= connect
16
- end
17
-
18
- def read
19
- @thread ||= Thread.new do
20
- loop do
21
- io.waitfor("\n") do |text|
22
- @read_buffer += text
23
- while line = @read_buffer.slice!(/^.*\n/) do
24
- pin, message = line.chop.split(/::/)
25
- pin && message && changed && notify_observers(pin, message)
26
- end
27
- end
28
- sleep 0.004
29
- end
30
- end
31
- end
32
-
33
- def close_read
34
- return nil if @thread.nil?
35
- Thread.kill(@thread)
36
- @read_buffer = ""
37
- @thread = nil
38
- end
39
-
40
- def write(message)
41
- io.puts(message)
42
- end
43
-
44
- private
45
-
46
- def connect
47
- Net::Telnet.new("Host" => @host, "Port" => @port)
48
- rescue
49
- raise BoardNotFound
50
- end
51
- end
52
- end
53
- end
@@ -1,69 +0,0 @@
1
- require 'serialport'
2
- require 'observer'
3
-
4
- module Dino
5
- module TxRx
6
- class USBSerial
7
- include Observable
8
-
9
- BAUD = 115200
10
-
11
- def initialize
12
- @first_write = true
13
- end
14
-
15
- def io
16
- raise BoardNotFound unless @io ||= find_arduino
17
- @io
18
- end
19
-
20
- def io=(device)
21
- @io = SerialPort.new(device, BAUD)
22
- end
23
-
24
- def read
25
- @thread ||= Thread.new do
26
- loop do
27
- if IO.select([io], nil, nil, 0.005)
28
- pin, message = *io.gets.chop.split(/::/)
29
- pin && message && changed && notify_observers(pin, message)
30
- end
31
- sleep 0.004
32
- end
33
- end
34
- end
35
-
36
- def close_read
37
- return nil if @thread.nil?
38
- Thread.kill(@thread)
39
- @thread = nil
40
- end
41
-
42
- def write(message)
43
- IO.select(nil, [io], nil)
44
- io.puts(message)
45
- end
46
-
47
- private
48
-
49
- def tty_devices
50
- if RUBY_PLATFORM.include?("mswin") || RUBY_PLATFORM.include?("mingw")
51
- ["COM1", "COM2", "COM3", "COM4"]
52
- else
53
- `ls /dev`.split("\n").grep(/usb|ACM/).map{|d| "/dev/#{d}"}
54
- end
55
- end
56
-
57
- def find_arduino
58
- tty_devices.map do |device|
59
- next if device.match /^cu/
60
- begin
61
- SerialPort.new(device, BAUD)
62
- rescue
63
- nil
64
- end
65
- end.compact.first
66
- end
67
- end
68
- end
69
- end
@@ -1,66 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module Dino
4
- describe TxRx::Telnet do
5
-
6
- before :each do
7
- @mock_telnet = mock
8
- Net::Telnet.stub(:new) { @mock_telnet }
9
- @instance = TxRx::Telnet.new("127.0.0.1", 3001)
10
- end
11
-
12
- describe '#connect' do
13
- it 'should raise a BoardNotFound exception if it cannot connect to the server' do
14
- Net::Telnet.stub(:new).and_raise
15
- @instance = TxRx::Telnet.new("0.0.0.0", 999999)
16
- expect { @instance.io }.to raise_exception BoardNotFound
17
- end
18
- end
19
-
20
- describe '#io' do
21
- it 'should set io to a new telnet connection with the specified host and port' do
22
- Net::Telnet.should_receive(:new).with("Host" => "127.0.0.1", "Port" => 3001)
23
- @instance.io
24
- end
25
-
26
- it 'should use the existing io instance if set' do
27
- Net::Telnet.should_receive(:new).exactly(1).times.with("Host" => "127.0.0.1", "Port" => 3001)
28
- 2.times { @instance.io }
29
- end
30
- end
31
-
32
- describe '#read' do
33
- it 'should create a new thread' do
34
- Thread.should_receive :new
35
- @instance.read
36
- end
37
-
38
- it 'should get messages from the device' do
39
- Thread.should_receive(:new).and_yield
40
- @instance.should_receive(:loop).and_yield
41
- @instance.io.should_receive(:waitfor).with("\n").and_yield("foo::bar\n")
42
- @instance.should_receive(:changed).and_return(true)
43
- @instance.should_receive(:notify_observers).with('foo','bar')
44
-
45
- @instance.read
46
- end
47
- end
48
-
49
- describe '#close_read' do
50
- it 'should kill the reading thread' do
51
- @instance.instance_variable_set(:@thread, mock_thread = mock)
52
- Thread.should_receive(:kill).with(mock_thread)
53
- @instance.read
54
- @instance.close_read
55
- end
56
- end
57
-
58
- describe '#write' do
59
- it 'should write to the device' do
60
- @mock_telnet.should_receive(:puts).with("foo::bar")
61
- @instance.write("foo::bar")
62
- end
63
- end
64
-
65
- end
66
- end
@@ -1,101 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module Dino
4
- describe TxRx::USBSerial do
5
- it { should be }
6
-
7
- describe '#initialize' do
8
- it 'should set first_write to false' do
9
- TxRx::USBSerial.new.instance_variable_get(:@first_write).should == true
10
- end
11
- end
12
-
13
- describe '#io' do
14
- context "on windows" do
15
- it 'should instantiate a new SerialPort for each usb tty device found' do
16
- original_platform = RUBY_PLATFORM
17
- RUBY_PLATFORM = "mswin"
18
- subject.should_receive(:tty_devices).and_return(["COM1", "COM2", "COM3", "COM4"])
19
- SerialPort.should_receive(:new).with('COM1', TxRx::USBSerial::BAUD).and_return(mock_serial = mock)
20
- SerialPort.should_receive(:new).with('COM2', TxRx::USBSerial::BAUD).and_return(mock)
21
- SerialPort.should_receive(:new).with('COM3', TxRx::USBSerial::BAUD).and_return(mock)
22
- SerialPort.should_receive(:new).with('COM4', TxRx::USBSerial::BAUD).and_return(mock)
23
-
24
- subject.io.should == mock_serial
25
- RUBY_PLATFORM = original_platform
26
- end
27
- end
28
-
29
- context "on unix" do
30
- it 'should instantiate a new SerialPort for each usb tty device found' do
31
- subject.should_receive(:tty_devices).and_return(['/dev/tty1.usb', '/dev/tty1.usb', '/dev/tty.ACM0'])
32
- SerialPort.should_receive(:new).with('/dev/tty1.usb', TxRx::USBSerial::BAUD).and_return(mock_serial = mock)
33
- SerialPort.should_receive(:new).with('/dev/tty1.usb', TxRx::USBSerial::BAUD).and_return(mock)
34
- SerialPort.should_receive(:new).with('/dev/tty.ACM0', TxRx::USBSerial::BAUD).and_return(mock)
35
-
36
- subject.io.should == mock_serial
37
- end
38
- end
39
-
40
- it 'should use the existing io instance if set' do
41
- subject.should_not_receive(:tty_devices)
42
- SerialPort.stub(:new).and_return(mock_serial = mock)
43
-
44
- subject.io = '/dev/tty1.usb'
45
- subject.io.should == mock_serial
46
- end
47
-
48
- it 'should raise a BoardNotFound exception if there is no board connected' do
49
- SerialPort.stub(:new).and_raise
50
- expect { subject.io }.to raise_exception BoardNotFound
51
- end
52
- end
53
-
54
- describe '#io=' do
55
- it 'should set io to a new serial port with the specified device' do
56
- SerialPort.should_receive(:new).with('/dev/tty1.usb', TxRx::USBSerial::BAUD).and_return(mock_serial = mock)
57
- subject.io = '/dev/tty1.usb'
58
- subject.instance_variable_get(:@io).should == mock_serial
59
- end
60
- end
61
-
62
- describe '#read' do
63
- it 'should create a new thread' do
64
- Thread.should_receive :new
65
- subject.read
66
- end
67
-
68
- it 'should get messages from the device' do
69
- subject.stub(:io).and_return(mock_serial = mock)
70
-
71
- IO.should_receive(:select).and_return(true)
72
- Thread.should_receive(:new).and_yield
73
- subject.should_receive(:loop).and_yield
74
- mock_serial.should_receive(:gets).and_return("foo::bar\n")
75
- subject.should_receive(:changed).and_return(true)
76
- subject.should_receive(:notify_observers).with('foo','bar')
77
-
78
- subject.read
79
- end
80
- end
81
-
82
- describe '#close_read' do
83
- it 'should kill the reading thread' do
84
- subject.instance_variable_set(:@thread, mock_thread = mock)
85
- Thread.should_receive(:kill).with(mock_thread)
86
- subject.read
87
- subject.close_read
88
- end
89
- end
90
-
91
- describe '#write' do
92
- it 'should write to the device' do
93
- IO.should_receive(:select).and_return(true)
94
-
95
- subject.stub(:io).and_return(mock_serial = mock)
96
- mock_serial.should_receive(:puts).with('a message')
97
- subject.write('a message')
98
- end
99
- end
100
- end
101
- end