dino 0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,21 @@
1
+ *.gem
2
+ *.rbc
3
+ .*.swp
4
+ .*.swo
5
+ junk.*
6
+ .idea/*
7
+ .bundle
8
+ .config
9
+ .yardoc
10
+ Gemfile.lock
11
+ InstalledFiles
12
+ _yardoc
13
+ coverage
14
+ doc/
15
+ lib/bundler/man
16
+ pkg
17
+ rdoc
18
+ spec/reports
19
+ test/tmp
20
+ test/version_tmp
21
+ tmp
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use ruby-1.9.3@dino --create
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dino.gemspec
4
+ gemspec
5
+
6
+ gem 'rspec'
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Austin
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,14 @@
1
+ # Welcome to Dino
2
+
3
+ ## Get started in no time
4
+ Dino was designed to help you start working with your Arduino in minutes.
5
+
6
+ > * Burn the file `src/du.ino` to your Arduino
7
+ > ** You can do this in the [Arduino IDE](http://www.arduino.cc/en/Main/software)
8
+ > * Plug in your Arduino and wire the led like in `examples/led/led.png`
9
+ > * Run `ruby example/led/led.rb`
10
+
11
+ By now you should be blinking away
12
+
13
+
14
+ Take a look in the example directory for more information
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/dino/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Austinbv"]
6
+ gem.email = ["austinbv@gmail.com"]
7
+ gem.description = %q{A utility library for interfacting with an Arduino.
8
+ Designed for control, expansion, and with love.}
9
+ gem.summary = %q{Control your arduino through a serial port}
10
+ gem.homepage = 'https://github.com/austinbv/dino'
11
+
12
+ gem.files = `git ls-files`.split($\)
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "dino"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Dino::VERSION
17
+
18
+ gem.add_dependency 'serialport'
19
+ end
@@ -0,0 +1,23 @@
1
+ #
2
+ # This is an example of how to use the button class
3
+ # You must register helpers and have the main thread
4
+ # sleep or in someway keep running or your program
5
+ # will exit before any callbacks can be called
6
+ #
7
+ require '../lib/dino'
8
+
9
+ board = Dino::Board.new(Dino::TxRx.new)
10
+ button = Dino::Components::Button.new(pin: 2, board: board)
11
+
12
+ button_down = Proc.new do
13
+ puts "button down"
14
+ end
15
+
16
+ button_up = Proc.new do
17
+ puts "button up"
18
+ end
19
+
20
+ button.down(button_down)
21
+ button.up(button_up)
22
+
23
+ sleep
@@ -0,0 +1,33 @@
1
+ #
2
+ # This is an example of how to use the button class
3
+ # You must register helpers and have the main thread
4
+ # sleep or in someway keep running or your program
5
+ # will exit before any callbacks can be called
6
+ #
7
+ require '../lib/dino'
8
+
9
+ board = Dino::Board.new(Dino::TxRx.new)
10
+ ir = Dino::Components::IrReceiver.new(pin: 2, board: board)
11
+ led = Dino::Components::Led.new(pin: 13, board: board)
12
+
13
+ n = 0
14
+
15
+ flash = Proc.new do
16
+ n += 1
17
+ puts "light flash #{n}"
18
+ end
19
+
20
+ sleep 2
21
+ Thread.new do
22
+ loop do
23
+ led.on
24
+ sleep 0.01
25
+ led.off
26
+ sleep 0.01
27
+ end
28
+ end
29
+
30
+ sleep 4
31
+ ir.flash(flash)
32
+
33
+ sleep
@@ -0,0 +1,14 @@
1
+ #
2
+ # This is a simple example to blink an led
3
+ # every half a second
4
+ #
5
+
6
+ require '../lib/dino'
7
+
8
+ board = Dino::Board.new(Dino::TxRx.new)
9
+ led = Dino::Components::Led.new(pin: 13, board: board)
10
+
11
+ [:on, :off].cycle do |switch|
12
+ led.send(switch)
13
+ sleep 0.5
14
+ end
@@ -0,0 +1,26 @@
1
+ #
2
+ # This is an example of how to use the sensor class
3
+ # with a potentiometer to control the flash speed of an
4
+ # LED. The set_delay callback reads from the potentiometer
5
+ # and changes the sleep delay for the LED on/off cycle.
6
+ #
7
+
8
+ require '../lib/dino'
9
+
10
+ board = Dino::Board.new(Dino::TxRx.new)
11
+ led = Dino::Components::Led.new(pin: 13, board: board)
12
+ potentiometer = Dino::Components::Sensor.new(pin: 'A0', board: board)
13
+
14
+ delay = 500.0
15
+
16
+ set_delay = Proc.new do |data|
17
+ puts "DATA: #{delay = data.to_i}"
18
+ end
19
+
20
+ potentiometer.when_data_received(set_delay)
21
+
22
+ [:on, :off].cycle do |switch|
23
+ puts "DELAY: #{seconds = (delay / 1000.0)}"
24
+ led.send(switch)
25
+ sleep seconds
26
+ end
@@ -0,0 +1,39 @@
1
+ #
2
+ # This is a simple example to blink an led
3
+ # every half a second
4
+ #
5
+
6
+ require '../lib/dino'
7
+
8
+ board = Dino::Board.new(Dino::TxRx.new)
9
+ led = Dino::Components::RgbLed.new(pins: {red: 11, green: 10, blue: 9}, board: board)
10
+ potentiometer = Dino::Components::Sensor.new(pin: 'A0', board: board)
11
+
12
+
13
+ delay = 500.0
14
+
15
+ set_delay = Proc.new do |data|
16
+ sleep 0.5
17
+ puts "DATA: #{delay = data.to_i}"
18
+ end
19
+
20
+ potentiometer.when_data_received(set_delay)
21
+
22
+ sleep(2)
23
+ loop do
24
+ puts "DELAY: #{seconds = (delay / 1000.0)}"
25
+ p 'red'
26
+ led.red
27
+ sleep(seconds)
28
+ led.blue
29
+ p 'blue'
30
+ sleep(seconds)
31
+ led.green
32
+ p 'green'
33
+ sleep(seconds)
34
+ end
35
+ #led.color(224, 27, 106)
36
+ #sleep(1)
37
+ #led.color(255, 149, 7)
38
+ #sleep(1)
39
+ #led.off
@@ -0,0 +1,18 @@
1
+ #
2
+ # This is an example of how to use the sensor class
3
+ # You must register data callbacks and have the main thread
4
+ # sleep or in someway keep running or your program
5
+ # will exit before any callbacks can be called
6
+ #
7
+ require '../lib/dino'
8
+
9
+ board = Dino::Board.new(Dino::TxRx.new)
10
+ sensor = Dino::Components::Sensor.new(pin: 'A0', board: board)
11
+
12
+ on_data = Proc.new do |data|
13
+ puts data
14
+ end
15
+
16
+ sensor.when_data_received(on_data)
17
+
18
+ sleep
@@ -0,0 +1,16 @@
1
+ #
2
+ # This is an example of how to use the button class
3
+ # You must register helpers and have the main thread
4
+ # sleep or in someway keep running or your program
5
+ # will exit before any callbacks can be called
6
+ #
7
+ $: << File.expand_path('../../lib', __FILE__)
8
+ require '../lib/dino'
9
+
10
+ board = Dino::Board.new(Dino::TxRx.new)
11
+ servo = Dino::Components::Servo.new(pin: 9, board: board)
12
+
13
+ loop do
14
+ servo.position += 9
15
+ sleep 0.5
16
+ end
@@ -0,0 +1,18 @@
1
+ #
2
+ # This is a simple example to move a stepper motor using the sparkfun easydriver shield: https://www.sparkfun.com/products/10267?
3
+ #
4
+
5
+ require '../lib/dino'
6
+
7
+ board = Dino::Board.new(Dino::TxRx.new)
8
+ stepper = Dino::Components::Stepper.new(board: board, pins: { step: 10, direction: 8 })
9
+
10
+ 1600.times do
11
+ stepper.step_cc
12
+ sleep 0.001
13
+ end
14
+
15
+ 1600.times do
16
+ stepper.step_cw
17
+ sleep 0.001
18
+ end
@@ -0,0 +1,15 @@
1
+ require 'dino/version'
2
+ require 'dino/tx_rx'
3
+ require 'dino/board'
4
+
5
+ require 'dino/components/base_component'
6
+ require 'dino/components/led'
7
+ require 'dino/components/button'
8
+ require 'dino/components/sensor'
9
+ require 'dino/components/rgb_led'
10
+ require 'dino/components/servo'
11
+ require 'dino/components/stepper'
12
+ require 'dino/components/ir_receiver'
13
+
14
+ module Dino
15
+ end
@@ -0,0 +1,114 @@
1
+ module Dino
2
+ class Board
3
+ attr_reader :digital_hardware, :analog_hardware
4
+ LOW, HIGH = 000, 255
5
+
6
+ def initialize(io)
7
+ @io, @digital_hardware, @analog_hardware = io, [], []
8
+ io.add_observer(self)
9
+ send_clearing_bytes
10
+ start_heart_beat
11
+ end
12
+
13
+ def update(pin, msg)
14
+ (@digital_hardware + @analog_hardware).each do |part|
15
+ part.update(msg) if normalize_pin(pin) == normalize_pin(part.pin)
16
+ end
17
+ end
18
+
19
+ def add_digital_hardware(part)
20
+ set_pin_mode(part.pin, :in)
21
+ @digital_hardware << part
22
+ end
23
+
24
+ def remove_digital_hardware(part)
25
+ @digital_hardware.delete(part)
26
+ end
27
+
28
+ def add_analog_hardware(part)
29
+ set_pin_mode(part.pin, :in)
30
+ @analog_hardware << part
31
+ end
32
+
33
+ def remove_analog_hardware(part)
34
+ @analog_hardware.delete(part)
35
+ end
36
+
37
+ def start_read
38
+ @io.read
39
+ end
40
+
41
+ def stop_read
42
+ @io.close_read
43
+ end
44
+
45
+ def write(msg, opts = {})
46
+ formatted_msg = opts.delete(:no_wrap) ? msg : "!#{msg}."
47
+ @io.write(formatted_msg)
48
+ end
49
+
50
+ def digital_write(pin, value)
51
+ pin, value = normalize_pin(pin), normalize_value(value)
52
+ write("01#{pin}#{value}")
53
+ end
54
+
55
+ def digital_read(pin)
56
+ pin, value = normalize_pin(pin), normalize_value(0)
57
+ write("02#{pin}#{value}")
58
+ end
59
+
60
+ def analog_write(pin, value)
61
+ pin, value = normalize_pin(pin), normalize_value(value)
62
+ write("03#{pin}#{value}")
63
+ end
64
+
65
+ def analog_read(pin)
66
+ pin, value = normalize_pin(pin), normalize_value(0)
67
+ write("04#{pin}#{value}")
68
+ end
69
+
70
+ def set_pin_mode(pin, mode)
71
+ pin, value = normalize_pin(pin), normalize_value(mode == :out ? 1 : 0)
72
+ write("00#{pin}#{value}")
73
+ end
74
+
75
+ def set_debug(on_off)
76
+ pin, value = normalize_pin(0), normalize_value(on_off == :on ? 1 : 0)
77
+ write("99#{pin}#{value}")
78
+ end
79
+
80
+ def normalize_pin(pin)
81
+ raise Exception.new('pins can only be two digits') if pin.to_s.length > 2
82
+ normalize(pin, 2)
83
+ end
84
+
85
+ def normalize_value(value)
86
+ raise Exception.new('values are limited to three digits') if value.to_s.length > 3
87
+ normalize(value, 3)
88
+ end
89
+
90
+ private
91
+
92
+ def start_heart_beat
93
+ @heart_beat ||= Thread.new do
94
+ loop do
95
+ sleep 0.005
96
+ @digital_hardware.each do |part|
97
+ digital_read(part.pin)
98
+ end
99
+ @analog_hardware.each do |part|
100
+ analog_read(part.pin)
101
+ end
102
+ end
103
+ end
104
+ end
105
+
106
+ def normalize(pin, spaces)
107
+ pin.to_s.rjust(spaces, '0')
108
+ end
109
+
110
+ def send_clearing_bytes
111
+ write('00000000', no_wrap: true)
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,33 @@
1
+ module Dino
2
+ module Components
3
+ class BaseComponent
4
+ attr_reader :pin, :board
5
+ alias :pins :pin
6
+
7
+ def initialize(options={})
8
+ self.pin = options[:pin] || options[:pins]
9
+ self.board = options[:board]
10
+
11
+ raise 'board and pin or pins are required for a component' if self.board.nil? || self.pin.nil?
12
+ end
13
+
14
+ protected
15
+
16
+ attr_writer :pin, :board
17
+ alias :pins= :pin=
18
+
19
+ def digital_write(value, pin = self.pin)
20
+ self.board.digital_write(pin, value)
21
+ end
22
+
23
+ def analog_write(value, pin = self.pin)
24
+ self.board.analog_write(pin, value)
25
+ end
26
+
27
+ def set_pin_mode(mode, pin = self.pin)
28
+ self.board.set_pin_mode(pin, mode)
29
+ end
30
+ end
31
+ end
32
+ end
33
+