smalrubot 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bce2e97fd3fff2dcf1019a4d60544dc52511e20d
4
+ data.tar.gz: df2409e867c64bd896c51ba7b0c294e55b43044d
5
+ SHA512:
6
+ metadata.gz: eeb5279b6bca843cd950c8eff66b10038b6dc1c0116e1d3ec83a8aba4317343bc7698530ea8c17668b320c3a529b1b622ace26446c9a4cbfe09e7544cab0e00d
7
+ data.tar.gz: 02f62597953d17185210b721f656c8acf2b637baa5b822f37ddf55bebc9f386fa630c662cfe95f8ae4ac3e892f43186cffeac65b506518030bc25e575f0442bd
@@ -0,0 +1,22 @@
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
22
+ /sr
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
4
+ script: bundle exec rspec spec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in smalrubot.gemspec
4
+ gemspec
data/LEGAL ADDED
@@ -0,0 +1,82 @@
1
+ LEGAL NOTICE INFORMATION
2
+ ------------------------
3
+
4
+ All the files in this distribution are covered under either the
5
+ smalrubot license (see the file LICENSE) except some files
6
+ mentioned below.
7
+
8
+ .gitignore
9
+ .rvmrc
10
+ .travis.yml
11
+ bin/dino
12
+ CHANGELOG.md
13
+ dino.gemspec
14
+ examples/button
15
+ examples/button/button.png
16
+ examples/button/button.rb
17
+ examples/ethernet.rb
18
+ examples/ir_receiver
19
+ examples/ir_receiver/ir_receiver.rb
20
+ examples/led
21
+ examples/led/led.png
22
+ examples/led/led.rb
23
+ examples/potentiometer
24
+ examples/potentiometer/potentiometer.rb
25
+ examples/rgb_led
26
+ examples/rgb_led/rgb_led.rb
27
+ examples/sensor
28
+ examples/sensor/sensor.rb
29
+ examples/ser2net.rb
30
+ examples/servo
31
+ examples/servo/servo.rb
32
+ examples/stepper
33
+ examples/stepper/stepper.png
34
+ examples/stepper/stepper.rb
35
+ Gemfile
36
+ lib/dino/board.rb
37
+ lib/dino/board_not_found.rb
38
+ lib/dino/components
39
+ lib/dino/components/base_component.rb
40
+ lib/dino/components/button.rb
41
+ lib/dino/components/ir_receiver.rb
42
+ lib/dino/components/led.rb
43
+ lib/dino/components/rgb_led.rb
44
+ lib/dino/components/sensor.rb
45
+ lib/dino/components/servo.rb
46
+ lib/dino/components/stepper.rb
47
+ lib/dino/components.rb
48
+ lib/dino/tx_rx
49
+ lib/dino/tx_rx/base.rb
50
+ lib/dino/tx_rx/serial.rb
51
+ lib/dino/tx_rx/tcp.rb
52
+ lib/dino/tx_rx.rb
53
+ lib/dino/version.rb
54
+ lib/dino.rb
55
+ LICENSE
56
+ Rakefile
57
+ README.md
58
+ spec/lib/board_not_found_spec.rb
59
+ spec/lib/board_spec.rb
60
+ spec/lib/components
61
+ spec/lib/components/base_component_spec.rb
62
+ spec/lib/components/button_spec.rb
63
+ spec/lib/components/led_spec.rb
64
+ spec/lib/components/rgb_led_spec.rb
65
+ spec/lib/components/sensor_spec.rb
66
+ spec/lib/components/servo_spec.rb
67
+ spec/lib/components/stepper_spec.rb
68
+ spec/lib/tx_rx
69
+ spec/lib/tx_rx/serial_spec.rb
70
+ spec/lib/tx_rx/tcp_spec.rb
71
+ spec/spec_helper.rb
72
+ src/du/du.ino
73
+ src/du_ethernet
74
+ src/du_ethernet/du_ethernet.ino
75
+ src/lib/Dino.cpp
76
+ src/lib/Dino.h
77
+
78
+ These were forked from Dino ( https://github.com/austinbv/dino/tree/15cd48e849faeb08c7dc017ff39ff16ff2e165a3 ).
79
+ After that we modified.
80
+ Copyright (c) 2012 Austin
81
+ These files were released under the MIT license:
82
+ https://github.com/austinbv/dino/blob/master/LICENSE .
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Kouji Takao <kouji.takao@gmail.com>
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,48 @@
1
+ # smalrubot
2
+
3
+ The smalrubot is a library and an Arduino sketch for Smalruby.
4
+
5
+ This is a part of the Smalruby ([http://smalruby.jp](http://smalruby.jp)) Project.
6
+
7
+ This was forked from Dino ([austinbv/dino:15cd48e8](https://github.com/austinbv/dino/tree/15cd48e849faeb08c7dc017ff39ff16ff2e165a3)).
8
+
9
+ ## Installation
10
+
11
+ requirements:
12
+
13
+ * Windows or UNIX like OS (Mac OS X, Linux, etc...)
14
+ * Ruby 2.0.0-p481 or higher.
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ ```ruby
19
+ gem 'smalrubot'
20
+ ```
21
+
22
+ And then execute:
23
+
24
+ ```shell
25
+ $ bundle
26
+ ```
27
+
28
+ Or install it yourself as:
29
+
30
+ ```shell
31
+ $ gem install smalrubot
32
+ ```
33
+
34
+ ## Usage
35
+
36
+ TODO: Write usage instructions here
37
+
38
+ ## Contributing
39
+
40
+ 1. Fork it ( https://github.com/smalruby/smalrubot/fork )
41
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
42
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
43
+ 4. Push to the branch (`git push origin my-new-feature`)
44
+ 5. Create a new Pull Request
45
+
46
+ ## License
47
+
48
+ MIT (see [LICENSE](link:LICENSE))
@@ -0,0 +1,35 @@
1
+ require "bundler/gem_helper"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
8
+ namespace :gem do
9
+ Bundler::GemHelper.install_tasks
10
+ end
11
+
12
+ desc 'release and bump up version'
13
+ task :release do
14
+ ENV['GEM_PLATFORM'] = 'linux'
15
+ Rake::Task['gem:release'].invoke
16
+
17
+ require 'smalrubot/version'
18
+ next_version = Smalrubot::VERSION.split('.').tap { |versions|
19
+ versions[-1] = (versions[-1].to_i + 1).to_s
20
+ }.join('.')
21
+ File.open('lib/smalrubot/version.rb', 'r+') do |f|
22
+ lines = []
23
+ while line = f.gets
24
+ line = "#{$1} '#{next_version}'\n" if /(\s*VERSION = )/.match(line)
25
+ lines << line
26
+ end
27
+ f.rewind
28
+ f.write(lines.join)
29
+ end
30
+ sh 'git add lib/smalrubot/version.rb'
31
+ sh "git commit -m #{next_version}"
32
+ sh 'git push'
33
+ end
34
+
35
+ task :default => [:spec]
@@ -0,0 +1,82 @@
1
+ #!/usr/bin/env ruby
2
+ require "pathname"
3
+ require "fileutils"
4
+
5
+ $options = {}
6
+ $options[:sketch_names] = []
7
+
8
+ def error(message)
9
+ $stderr.puts "Error: " + message
10
+ usage
11
+ end
12
+
13
+ def usage
14
+ $stderr.puts "Usage: #{File.basename($0)} COMMAND [command-specific-options]"
15
+ $stderr.puts
16
+ $stderr.puts "Commands:"
17
+ $stderr.puts " generate-sketch [options]"
18
+ $stderr.puts
19
+ $stderr.puts " Options:"
20
+ $stderr.puts " --baud BAUD"
21
+ $stderr.puts " --debug"
22
+ $stderr.puts
23
+ exit(2)
24
+ end
25
+
26
+ # Command must be the first argument.
27
+ $options[:command] = ARGV.shift
28
+ usage if $options[:command].match /help/
29
+
30
+ # Parse the rest loosely.
31
+ loop do
32
+ case ARGV[0]
33
+ when '--baud'
34
+ ARGV.shift; $options[:baud] = ARGV.shift
35
+ when '--debug'
36
+ ARGV.shift; $options[:debug] = true
37
+ when /^-/
38
+ error "Invalid argument '#{ARGV[0]}'"
39
+ else break
40
+ end
41
+ end
42
+
43
+ $options[:sketch_names] = ['sr']
44
+
45
+ error "Invalid command '#{$options[:command]}'" unless $options[:command] == "generate-sketch"
46
+ error "No sketches or invalid sketches specified" if $options[:sketch_names].empty?
47
+
48
+ $options[:sketch_names].each do |sketch_name|
49
+ # Define the sources.
50
+ sketch_dir = sketch_name
51
+ sketch_file = sketch_name + ".ino"
52
+ src_dir = Pathname.new(__FILE__).realpath.to_s.chomp("/bin/smalrubot") + "/sketch"
53
+ src_header = File.join src_dir, "lib", "Smalrubot.h"
54
+ src_implementation = File.join src_dir, "lib", "Smalrubot.cpp"
55
+ src_sketch = File.join src_dir, sketch_dir, sketch_file
56
+
57
+ # Read the files.
58
+ header = File.read(src_header)
59
+ implementation = File.read(src_implementation)
60
+ sketch = File.read(src_sketch)
61
+
62
+ # Modify them based on the arguments.
63
+ if $options[:baud]
64
+ sketch.gsub! "115200", $options[:baud]
65
+ end
66
+ if $options[:debug]
67
+ header.gsub! "// #define debug true", "#define debug true"
68
+ end
69
+
70
+ # Define the destinations.
71
+ working_dir = Dir.pwd
72
+ dest_dir = File.join working_dir, sketch_dir
73
+ Dir::mkdir dest_dir
74
+ dest_header = File.join dest_dir, "Smalrubot.h"
75
+ dest_implementation = File.join dest_dir, "Smalrubot.cpp"
76
+ dest_sketch = File.join dest_dir, sketch_file
77
+
78
+ # Write the files.
79
+ File.open(dest_header, 'w') { |f| f.write header }
80
+ File.open(dest_implementation, 'w') { |f| f.write implementation }
81
+ File.open(dest_sketch, 'w') { |f| f.write sketch }
82
+ end
@@ -0,0 +1,13 @@
1
+ require 'smalrubot'
2
+
3
+ Smalrubot.debug_mode = true
4
+ board = Smalrubot::Board.new(Smalrubot::TxRx::Serial.new)
5
+
6
+ board.digital_write(13, 255)
7
+ sleep(1)
8
+ board.digital_write(13, 0)
9
+
10
+ 10.times do
11
+ p board.analog_read(0)
12
+ sleep(0.5)
13
+ end
@@ -0,0 +1,32 @@
1
+ require 'smalrubot/board_not_found'
2
+ require 'smalrubot/version'
3
+ require 'smalrubot/tx_rx'
4
+ require 'smalrubot/board'
5
+ require 'smalrubot/components'
6
+
7
+ module Smalrubot
8
+ @@debug = false
9
+
10
+ module_function
11
+
12
+ def debug_log(*message)
13
+ if debug?
14
+ puts(sprintf(*message).chomp)
15
+ end
16
+ end
17
+
18
+ def show_backtrace(exception)
19
+ if debug?
20
+ puts(exception)
21
+ puts(' ' + exception.backtrace.join("\n "))
22
+ end
23
+ end
24
+
25
+ def debug?
26
+ @@debug
27
+ end
28
+
29
+ def debug_mode=(val)
30
+ @@debug = val
31
+ end
32
+ end
@@ -0,0 +1,104 @@
1
+ module Smalrubot
2
+ class Board
3
+ attr_reader :analog_zero
4
+ LOW, HIGH = 000, 255
5
+
6
+ def initialize(io)
7
+ @io = io
8
+ @mutex = Mutex.new
9
+ handshake
10
+ end
11
+
12
+ def handshake
13
+ @mutex.synchronize do
14
+ @analog_zero = @io.handshake
15
+ end
16
+ end
17
+
18
+ def write(msg, opts = {})
19
+ formatted_msg = opts.delete(:no_wrap) ? msg : "!#{msg}."
20
+ @io.write(formatted_msg)
21
+ end
22
+
23
+ def read
24
+ @io.read(1)
25
+ end
26
+
27
+ def set_pin_mode(pin, mode, pullup=nil)
28
+ pin, value = normalize_pin(pin), normalize_value(mode == :out ? 0 : 1)
29
+ write("00#{pin}#{value}")
30
+ set_pullup(pin, pullup) if mode == :in
31
+ end
32
+
33
+ def set_pullup(pin, pullup)
34
+ pullup ? digital_write(pin, HIGH) : digital_write(pin, LOW)
35
+ end
36
+
37
+ WRITE_COMMANDS = {
38
+ digital_write: '01',
39
+ analog_write: '03',
40
+ servo_toggle: '08',
41
+ servo_write: '09'
42
+ }
43
+
44
+ WRITE_COMMANDS.each_key do |command|
45
+ define_method(command) do |pin, value=nil|
46
+ cmd = normalize_cmd(WRITE_COMMANDS[command])
47
+ write("#{cmd}#{normalize_pin(pin)}#{normalize_value(value)}")
48
+ end
49
+ end
50
+
51
+ READ_COMMANDS = {
52
+ digital_read: '02',
53
+ analog_read: '04',
54
+ }
55
+
56
+ READ_COMMANDS.each_key do |command|
57
+ define_method(command) do |pin|
58
+ cmd = normalize_cmd(READ_COMMANDS[command])
59
+ req_pin = nil
60
+ res_pin = nil
61
+ message = nil
62
+ @mutex.synchronize do
63
+ req_pin = normalize_pin(pin)
64
+ write("#{cmd}#{req_pin}#{normalize_value(0)}")
65
+ res_pin, message = *read
66
+ end
67
+ if res_pin && message
68
+ if res_pin != req_pin
69
+ raise "FATAL: request and response pins are differece: request #{req_pin}, response: #{res_pin}"
70
+ end
71
+ message.to_i
72
+ else
73
+ nil
74
+ end
75
+ end
76
+ end
77
+
78
+ def normalize_pin(pin)
79
+ if pin.to_s.match /\Aa/i
80
+ int_pin = @analog_zero + pin.to_s.gsub(/\Aa/i, '').to_i
81
+ else
82
+ int_pin = pin
83
+ end
84
+ raise Exception.new('pin number must be in 0-99') if int_pin.to_i > 99
85
+ return normalize(int_pin, 2)
86
+ end
87
+
88
+ def normalize_cmd(cmd)
89
+ raise Exception.new('commands can only be two digits') if cmd.to_s.length > 2
90
+ normalize(cmd, 2)
91
+ end
92
+
93
+ def normalize_value(value)
94
+ raise Exception.new('values are limited to three digits') if value.to_s.length > 3
95
+ normalize(value, 3)
96
+ end
97
+
98
+ private
99
+
100
+ def normalize(pin, spaces)
101
+ pin.to_s.rjust(spaces, '0')
102
+ end
103
+ end
104
+ end