electron 0.1
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/lib/electron.rb +89 -0
- data/lib/electron/base.rb +17 -0
- data/lib/electron/button.rb +21 -0
- data/lib/electron/led.rb +24 -0
- metadata +49 -0
data/lib/electron.rb
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'serialport'
|
2
|
+
|
3
|
+
class Electron#ics
|
4
|
+
|
5
|
+
HIGH = 1
|
6
|
+
LOW = 0
|
7
|
+
OUTPUT = 0
|
8
|
+
INPUT = 1
|
9
|
+
|
10
|
+
attr_reader :port
|
11
|
+
attr_accessor :debug
|
12
|
+
def initialize(options = {})
|
13
|
+
@debug = options[:debug] || false
|
14
|
+
@port = options[:port] || _scan_for_port
|
15
|
+
@baud_rate = options[:baud_rate] || 115200
|
16
|
+
@data_bits = options[:data_bits] || 8
|
17
|
+
@stop_bits = options[:stop_bits] || 1
|
18
|
+
@parity = options[:party] || SerialPort::NONE
|
19
|
+
connect
|
20
|
+
end
|
21
|
+
|
22
|
+
def connect
|
23
|
+
puts "connecting to #{@port}" if debug
|
24
|
+
@board = SerialPort.new @port, @baud_rate, @data_bits, @stop_bits, @parity
|
25
|
+
end
|
26
|
+
|
27
|
+
def helpers(&block)
|
28
|
+
self.instance_eval(&block)
|
29
|
+
end
|
30
|
+
|
31
|
+
def loop(&block)
|
32
|
+
while(true) do
|
33
|
+
block.call
|
34
|
+
sleep 0.1
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def read
|
39
|
+
@board.gets
|
40
|
+
end
|
41
|
+
|
42
|
+
def close
|
43
|
+
@board.close
|
44
|
+
end
|
45
|
+
|
46
|
+
def pin_mode(pin, value)
|
47
|
+
_write("pm", pin, value)
|
48
|
+
end
|
49
|
+
|
50
|
+
def analog_write(pin, value)
|
51
|
+
_write("aw", pin, value)
|
52
|
+
end
|
53
|
+
|
54
|
+
def digital_write(pin, value)
|
55
|
+
_write("dw", pin, value)
|
56
|
+
end
|
57
|
+
|
58
|
+
def digital_read(pin)
|
59
|
+
_write("dr", pin)
|
60
|
+
read.to_i
|
61
|
+
end
|
62
|
+
|
63
|
+
def analog_read(pin)
|
64
|
+
_write("ar", pin)
|
65
|
+
read.to_i
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def _write(*args)
|
71
|
+
puts "#{@port}: !#{args.join(" ")}." if debug
|
72
|
+
begin
|
73
|
+
@board.write "!#{args.join(" ")}."
|
74
|
+
rescue Errno::ENXIO => e
|
75
|
+
sleep 1
|
76
|
+
connect
|
77
|
+
@board.write "!#{args.join(" ")}."
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def _scan_for_port
|
82
|
+
serial_port = `ls /dev | grep usb`.chomp.split(/\n/).select {|port| port =~ /^cu\./}.first
|
83
|
+
"/dev/#{serial_port}"
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
require 'electron/base'
|
88
|
+
require 'electron/led'
|
89
|
+
require 'electron/button'
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class Electron
|
2
|
+
class Button < Base
|
3
|
+
|
4
|
+
def setup
|
5
|
+
@board.pin_mode(@pin, Electron::INPUT)
|
6
|
+
end
|
7
|
+
|
8
|
+
def current_state
|
9
|
+
@board.digital_read(@pin)
|
10
|
+
end
|
11
|
+
|
12
|
+
def down?
|
13
|
+
current_state == 1
|
14
|
+
end
|
15
|
+
|
16
|
+
def up?
|
17
|
+
current_state == 0
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
data/lib/electron/led.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
class Electron
|
2
|
+
class LED < Base
|
3
|
+
|
4
|
+
def setup
|
5
|
+
@board.pin_mode(@pin, Electron::OUTPUT)
|
6
|
+
end
|
7
|
+
|
8
|
+
def on
|
9
|
+
@board.digital_write(@pin, Electron::HIGH)
|
10
|
+
end
|
11
|
+
def off
|
12
|
+
@board.digital_write(@pin, Electron::LOW)
|
13
|
+
end
|
14
|
+
|
15
|
+
def blink(delay = 0.2)
|
16
|
+
loop do
|
17
|
+
on
|
18
|
+
sleep delay
|
19
|
+
off
|
20
|
+
sleep delay
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: electron
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Bryan Goines
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-24 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description:
|
15
|
+
email: bryann83@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/electron/base.rb
|
21
|
+
- lib/electron/button.rb
|
22
|
+
- lib/electron/led.rb
|
23
|
+
- lib/electron.rb
|
24
|
+
homepage: http://github.com/bry4n/electron
|
25
|
+
licenses: []
|
26
|
+
post_install_message:
|
27
|
+
rdoc_options: []
|
28
|
+
require_paths:
|
29
|
+
- lib
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ! '>='
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
requirements: []
|
43
|
+
rubyforge_project:
|
44
|
+
rubygems_version: 1.8.23
|
45
|
+
signing_key:
|
46
|
+
specification_version: 3
|
47
|
+
summary: ! '...'
|
48
|
+
test_files: []
|
49
|
+
has_rdoc:
|