dbrady-switchy 0.0.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/MIT-LICENSE +22 -0
- data/README.rdoc +92 -0
- data/bin/sparky +44 -0
- data/bin/switchy +56 -0
- data/lib/switchy.rb +134 -0
- metadata +73 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2008-2009 David Brady github@shinybit.com
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person
|
4
|
+
obtaining a copy of this software and associated documentation
|
5
|
+
files (the "Software"), to deal in the Software without
|
6
|
+
restriction, including without limitation the rights to use,
|
7
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
copies of the Software, and to permit persons to whom the
|
9
|
+
Software is furnished to do so, subject to the following
|
10
|
+
conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
17
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
19
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
20
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
21
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
= Switchy
|
2
|
+
|
3
|
+
Ruby Application to switch AC loads (e.g. turn a lamp on and off).
|
4
|
+
|
5
|
+
= Example
|
6
|
+
|
7
|
+
== Command-line App
|
8
|
+
|
9
|
+
TODO: This documentation is prescriptive, not descriptive. Update this
|
10
|
+
when it's finally implemented.
|
11
|
+
|
12
|
+
$ switchy 1 status
|
13
|
+
Light 1 is: OFF
|
14
|
+
$ switchy 1 on
|
15
|
+
Light 1 switched on.
|
16
|
+
$ switchy 1 status
|
17
|
+
Light 1 is: ON
|
18
|
+
$ switchy 1 off
|
19
|
+
Light 1 switched off.
|
20
|
+
$ switchy 1 on
|
21
|
+
Light 1 switched on.
|
22
|
+
|
23
|
+
== Ruby Library
|
24
|
+
|
25
|
+
TODO: This documentation is prescriptive, not descriptive. Update this
|
26
|
+
when it's finally implemented.
|
27
|
+
|
28
|
+
# Direct Access to individual lights
|
29
|
+
# ----------------------------------------
|
30
|
+
# Light 1 starts out off.
|
31
|
+
s = Switchy.new
|
32
|
+
s.light1
|
33
|
+
# => false # light 1 is off
|
34
|
+
s.light1 = true
|
35
|
+
# => false # previous state returned; light1 was off, now on
|
36
|
+
s.light1
|
37
|
+
# => true
|
38
|
+
s.light1 = false
|
39
|
+
# => true # previous state again; light1 was on, now off
|
40
|
+
|
41
|
+
# Access to lights array
|
42
|
+
# ----------------------------------------
|
43
|
+
s.lights
|
44
|
+
# => [false, false, false, false, false, false]
|
45
|
+
s.lights[2]
|
46
|
+
# => false
|
47
|
+
s.lights[2] = true
|
48
|
+
# => false
|
49
|
+
s.lights[2]
|
50
|
+
# => true
|
51
|
+
s.lights[2] = false
|
52
|
+
# => true
|
53
|
+
s.lights[2]
|
54
|
+
# => false
|
55
|
+
|
56
|
+
|
57
|
+
= The Hardware
|
58
|
+
|
59
|
+
TODO: Write me!
|
60
|
+
|
61
|
+
- This application
|
62
|
+
- Ruby serialport gem
|
63
|
+
- Teensy development board / Atmel AVR at90usb162
|
64
|
+
- usb_serial driver for Teensy
|
65
|
+
- switching circuit
|
66
|
+
- modified power strip
|
67
|
+
|
68
|
+
= USB Serial Driver
|
69
|
+
|
70
|
+
Teensy USB Serial Example, Simple Pin Control Shell
|
71
|
+
|
72
|
+
Example Commands
|
73
|
+
B0? Read Port B, pin 0
|
74
|
+
C2=0 Write Port C, pin 1 LOW
|
75
|
+
D6=1 Write Port D, pin 6 HIGH (D6 is LED pin)
|
76
|
+
|
77
|
+
>
|
78
|
+
|
79
|
+
= TODO
|
80
|
+
|
81
|
+
* Circuit defaults: should light onboard LED and no other outputs.
|
82
|
+
|
83
|
+
* There's an enable line on the 74ls244 driver chip. Use it. Device
|
84
|
+
should boot disabled. Once everything's had a chance to settle
|
85
|
+
(10-100ms or so), enable the driver chip.
|
86
|
+
|
87
|
+
* Add circuit schematics to the project.
|
88
|
+
|
89
|
+
* Fork the circuit, make a version with just a little LED light bar
|
90
|
+
and no 120VAC switching. (Trust me. The little LEDs are VERY
|
91
|
+
compelling!)
|
92
|
+
|
data/bin/sparky
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# TODO! This, along with switchy, needs to be made into a rubygem!
|
4
|
+
# Others may have difficulty using this in a portable fashion without
|
5
|
+
# a /Users/dbrady folder, for example...
|
6
|
+
|
7
|
+
def show_run
|
8
|
+
# system "switchy 1 0 2 1 3 1 4 1"
|
9
|
+
system "switchy 1 1 2 0 3 0 4 0"
|
10
|
+
end
|
11
|
+
|
12
|
+
def show_pending
|
13
|
+
system "switchy 1 0 2 1 3 0 4 0"
|
14
|
+
end
|
15
|
+
|
16
|
+
def show_fail
|
17
|
+
system "switchy 1 0 2 0 3 1 4 0"
|
18
|
+
end
|
19
|
+
|
20
|
+
def show_pass
|
21
|
+
system "switchy 1 0 2 0 3 0 4 1"
|
22
|
+
end
|
23
|
+
|
24
|
+
def show_reset
|
25
|
+
system "switchy 1 0 2 0 3 0 4 0"
|
26
|
+
end
|
27
|
+
|
28
|
+
case ARGV[0].downcase
|
29
|
+
when "reset" then show_reset
|
30
|
+
when "pending" then show_pending
|
31
|
+
when "fail" then show_fail
|
32
|
+
when "pass" then show_pass
|
33
|
+
when "run" then show_run
|
34
|
+
else
|
35
|
+
show_run
|
36
|
+
|
37
|
+
cmd = ARGV.map {|a| '"' + a.gsub(/"/, '\"') + '"' } * ' '
|
38
|
+
|
39
|
+
if system(cmd)
|
40
|
+
show_pass
|
41
|
+
else
|
42
|
+
show_fail
|
43
|
+
end
|
44
|
+
end
|
data/bin/switchy
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
$: << File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
4
|
+
|
5
|
+
def usage
|
6
|
+
u = <<USAGE
|
7
|
+
switchy - turn external lights on/off
|
8
|
+
switchy <light> <0|1> [<light2> <0|1> [...]]
|
9
|
+
|
10
|
+
light:
|
11
|
+
0 - Onboard LED (reversed: 0 = on, 1 = off)
|
12
|
+
1-4 External Switches (1 = on)
|
13
|
+
|
14
|
+
example: Turn off onboard LED, turn on lights 2 and 4, turn off light 3, leave light 1 alone:
|
15
|
+
switchy 0 1 2 1 4 1 3 0
|
16
|
+
USAGE
|
17
|
+
end
|
18
|
+
|
19
|
+
require 'switchy'
|
20
|
+
|
21
|
+
args = ARGV.dup
|
22
|
+
|
23
|
+
s = Switchy.new
|
24
|
+
# The serial port is a bit tetchy. Give it a few cycles to settle.
|
25
|
+
sleep 0.05
|
26
|
+
|
27
|
+
|
28
|
+
until args.empty?
|
29
|
+
light = args.shift.to_i
|
30
|
+
command = args.shift.to_i
|
31
|
+
|
32
|
+
unless light >= 0 && light < 5
|
33
|
+
puts usage
|
34
|
+
raise "ERROR: ARGV[1] must be between 0 and 4"
|
35
|
+
end
|
36
|
+
|
37
|
+
unless command >= 0 && command <= 1
|
38
|
+
puts usage
|
39
|
+
raise "ERROR: ARGV[2] must be one of on/off/status"
|
40
|
+
end
|
41
|
+
|
42
|
+
raise "ERROR: status not yet implemented" if command == 'status'
|
43
|
+
|
44
|
+
lights = [
|
45
|
+
Switchy::PINS::D6,
|
46
|
+
Switchy::PINS::C4,
|
47
|
+
Switchy::PINS::C5,
|
48
|
+
Switchy::PINS::C6,
|
49
|
+
Switchy::PINS::C7
|
50
|
+
]
|
51
|
+
|
52
|
+
s.set_pin lights[light], command
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
|
data/lib/switchy.rb
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "serialport"
|
4
|
+
|
5
|
+
class Switchy
|
6
|
+
module PINS
|
7
|
+
B0 = "B0"
|
8
|
+
B1 = "B1"
|
9
|
+
B2 = "B2"
|
10
|
+
B3 = "B3"
|
11
|
+
B4 = "B4"
|
12
|
+
B5 = "B5"
|
13
|
+
B6 = "B6"
|
14
|
+
B7 = "B7"
|
15
|
+
|
16
|
+
C2 = "C2"
|
17
|
+
C4 = "C4"
|
18
|
+
C5 = "C5"
|
19
|
+
C6 = "C6"
|
20
|
+
C7 = "C7"
|
21
|
+
|
22
|
+
D0 = "D0"
|
23
|
+
D1 = "D1"
|
24
|
+
D2 = "D2"
|
25
|
+
D3 = "D3"
|
26
|
+
D4 = "D4"
|
27
|
+
D5 = "D5"
|
28
|
+
D6 = "D6"
|
29
|
+
D7 = "D7"
|
30
|
+
|
31
|
+
OC1B = "C5"
|
32
|
+
|
33
|
+
OC1A = "C6"
|
34
|
+
|
35
|
+
ICP1 = "C7"
|
36
|
+
INT4 = "C7"
|
37
|
+
|
38
|
+
OC1C = "B7"
|
39
|
+
|
40
|
+
T1 = "B4"
|
41
|
+
|
42
|
+
M1S0 = "B3"
|
43
|
+
|
44
|
+
M0S1 = "B2"
|
45
|
+
|
46
|
+
SCLK = "B1"
|
47
|
+
|
48
|
+
SS = "B0"
|
49
|
+
|
50
|
+
INT7 = "D7"
|
51
|
+
HWB = "D7"
|
52
|
+
CTS = "D7"
|
53
|
+
T0 = "D7"
|
54
|
+
|
55
|
+
INT6 = "D6"
|
56
|
+
RTS = "D6"
|
57
|
+
LED = "D6"
|
58
|
+
|
59
|
+
XCK = "D5"
|
60
|
+
|
61
|
+
INT5 = "D4"
|
62
|
+
|
63
|
+
INT3 = "D3"
|
64
|
+
TXD1 = "D3"
|
65
|
+
TXD = "D3"
|
66
|
+
|
67
|
+
INT2 = "D2"
|
68
|
+
AIN1 = "D2"
|
69
|
+
RXD1 = "D2"
|
70
|
+
RXD = "D2"
|
71
|
+
|
72
|
+
INT1 = "D1"
|
73
|
+
AIN0 = "D1"
|
74
|
+
|
75
|
+
INT0 = "D0"
|
76
|
+
OC0B = "D0"
|
77
|
+
|
78
|
+
PD0 = "D0"
|
79
|
+
PD1 = "D1"
|
80
|
+
PD2 = "D2"
|
81
|
+
PD3 = "D3"
|
82
|
+
PD4 = "D4"
|
83
|
+
PD5 = "D5"
|
84
|
+
PD6 = "D6"
|
85
|
+
PD7 = "D7"
|
86
|
+
end
|
87
|
+
|
88
|
+
attr_accessor :modem, :baud
|
89
|
+
|
90
|
+
def initialize(modem = '/dev/tty.usbmodem12341', baud=38400)
|
91
|
+
@modem, @baud = modem, baud
|
92
|
+
connect
|
93
|
+
end
|
94
|
+
|
95
|
+
def connect
|
96
|
+
@sp = SerialPort.new @modem, @baud
|
97
|
+
puts "Connected"
|
98
|
+
end
|
99
|
+
|
100
|
+
def disconnect
|
101
|
+
# ???
|
102
|
+
end
|
103
|
+
|
104
|
+
# Set pin "b4", 1 # turn on pin b4
|
105
|
+
def set_pin(p, v)
|
106
|
+
cmd = "#{p.upcase}=#{v}\r\n"
|
107
|
+
puts cmd
|
108
|
+
@sp.write cmd
|
109
|
+
end
|
110
|
+
|
111
|
+
def set_light(l, v)
|
112
|
+
cmd = "C#{l+3}=#{v}\r\n"
|
113
|
+
puts cmd
|
114
|
+
@sp.write cmd
|
115
|
+
end
|
116
|
+
|
117
|
+
def light1=(v)
|
118
|
+
set_light 1, v
|
119
|
+
end
|
120
|
+
|
121
|
+
def light2=(v)
|
122
|
+
set_light 2, v
|
123
|
+
end
|
124
|
+
|
125
|
+
def light3=(v)
|
126
|
+
set_light 3, v
|
127
|
+
end
|
128
|
+
|
129
|
+
def light4=(v)
|
130
|
+
set_light 4, v
|
131
|
+
end
|
132
|
+
|
133
|
+
end
|
134
|
+
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dbrady-switchy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- David Brady
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-04-14 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: ruby-serialport
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description: Switchy USB serial load switcher
|
26
|
+
email: github@shinybit.com
|
27
|
+
executables:
|
28
|
+
- switchy
|
29
|
+
- sparky
|
30
|
+
extensions: []
|
31
|
+
|
32
|
+
extra_rdoc_files:
|
33
|
+
- README.rdoc
|
34
|
+
- MIT-LICENSE
|
35
|
+
files:
|
36
|
+
- bin/switchy
|
37
|
+
- bin/sparky
|
38
|
+
- lib/switchy.rb
|
39
|
+
- README.rdoc
|
40
|
+
- MIT-LICENSE
|
41
|
+
has_rdoc: true
|
42
|
+
homepage: http://github.com/dbrady/switchy
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options:
|
45
|
+
- --line-numbers
|
46
|
+
- --inline-source
|
47
|
+
- --main
|
48
|
+
- README.rdoc
|
49
|
+
- --title
|
50
|
+
- Switchy - USB serial load switcher
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
version:
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: "0"
|
64
|
+
version:
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.2.0
|
69
|
+
signing_key:
|
70
|
+
specification_version: 2
|
71
|
+
summary: Switchy USB serial load switcher
|
72
|
+
test_files: []
|
73
|
+
|