ban 0.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/spec/ban_spec.rb ADDED
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ban do
4
+ it 'should have a version number' do
5
+ Ban::VERSION.should_not be_nil
6
+ end
7
+
8
+ let(:low_str) { '21588|24|322|1' }
9
+ let(:low_str7bit) do
10
+ [
11
+ 50, 0, 49, 0, 53, 0, 56, 0, 56, 0, 124, 0, 50, 0,
12
+ 52, 0, 124, 0, 51, 0, 50, 0, 50, 0, 124, 0, 49, 0
13
+ ]
14
+ end
15
+ let(:high_str) { "\xFD" }
16
+ let(:high_str7bit) do
17
+ [
18
+ 125, 1
19
+ ]
20
+ end
21
+
22
+ context '.decode' do
23
+ it 'should decode low' do
24
+ described_class.decode7bit(low_str7bit).should == low_str
25
+ end
26
+
27
+ it 'should decode high' do
28
+ described_class.decode7bit(high_str7bit).should == high_str
29
+ end
30
+ end
31
+
32
+ context '.encode' do
33
+ it 'should encode low' do
34
+ described_class.encode7bit(low_str).should == low_str7bit
35
+ end
36
+
37
+ it 'should encode high' do
38
+ described_class.encode7bit(high_str).should == high_str7bit
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,5 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'simplecov'
3
+ SimpleCov.start
4
+
5
+ require 'ban'
@@ -0,0 +1,144 @@
1
+ #include <Firmata.h>
2
+ #include <RCSwitch.h>
3
+ #include <IRremote.h>
4
+
5
+ // Firmata Commands
6
+ enum Messages {
7
+ RECV_RC_ON = 0x20,
8
+ RECV_RC_OFF = 0x21,
9
+ SEND_RC = 0x35,
10
+ SEND_IR = 0x36,
11
+ SEND_DOOR = 0x37,
12
+ };
13
+
14
+ // Used Pins
15
+ enum Pins {
16
+ PIN_IR = 11,
17
+ PIN_RC_SEND = 8,
18
+ PIN_RC_RECV = 0, // Receiver on interrupt 0 => that is pin #2
19
+ PIN_DOOR = 9
20
+ };
21
+
22
+ // Door States
23
+ enum DoorStates {
24
+ DOOR_OPEN = HIGH,
25
+ DOOR_CLOSED = LOW
26
+ };
27
+
28
+ // Global constants
29
+ IRrecv irrecv(PIN_IR);
30
+ RCSwitch rcSwitchControl = RCSwitch();
31
+ int lastDoorState = DOOR_CLOSED;
32
+
33
+ /* Extracts the switch portion of the rcAddres.
34
+ */
35
+ static byte getSwitch(char* rcAddress) {
36
+ // Parse the switch number and if it should be turned on
37
+ return (rcAddress[5] - 'A') + 1;
38
+ }
39
+
40
+ /* Turns the switch on.
41
+ * @param rcAddress The data used to switch are in this format
42
+ * "11110A". The first five chars are the group
43
+ * of the switch the next is the switch char.
44
+ */
45
+ void rcSwitchOn(char* rcAddress) {
46
+ byte switchNr = getSwitch(rcAddress);
47
+ rcAddress[5] = '\0'; // remove everything but the bit pattern
48
+ rcSwitchControl.switchOn(rcAddress, switchNr);
49
+ }
50
+
51
+ /* Turns the switch off.
52
+ * @param rcAddress The data used to switch are in this format
53
+ * "11110A". The first five chars are the group
54
+ * of the switch the next is the switch char.
55
+ */
56
+ void rcSwitchOff(char* rcAddress) {
57
+ byte switchNr = getSwitch(rcAddress);
58
+ rcAddress[5] = '\0'; // remove everything but the bit pattern
59
+ rcSwitchControl.switchOff(rcAddress, switchNr);
60
+ }
61
+
62
+ void stringDataProxy(char* data) {
63
+ switch((byte)data[0]) {
64
+ case RECV_RC_ON:
65
+ rcSwitchOn(&data[1]);
66
+ break;
67
+ case RECV_RC_OFF:
68
+ rcSwitchOff(&data[1]);
69
+ break;
70
+ default:
71
+ // TODO error handling
72
+ break;
73
+ }
74
+ }
75
+
76
+ void setup()
77
+ {
78
+ rcSwitchControl.enableTransmit(PIN_RC_SEND);
79
+ rcSwitchControl.enableReceive(PIN_RC_RECV);
80
+ irrecv.enableIRIn();
81
+
82
+ pinMode(PIN_DOOR, INPUT);
83
+
84
+ Firmata.setFirmwareNameAndVersion("Home Control", 1, 1);
85
+ Firmata.attach(STRING_DATA, stringDataProxy);
86
+ Firmata.begin();
87
+ }
88
+
89
+ void rcReceiveAndTransmit() {
90
+ if (rcSwitchControl.available()) {
91
+ long decimal = rcSwitchControl.getReceivedValue();
92
+
93
+ if (decimal == 0) {
94
+ Firmata.sendString(SEND_RC, "Error: Unknown encoding");
95
+ } else {
96
+ char instructionBuffer[70];
97
+ String instruction = String(decimal); // 21780
98
+ String sep = String('|');
99
+ instruction += sep;
100
+ instruction += String(rcSwitchControl.getReceivedBitlength()); // 24 bit
101
+ instruction += sep;
102
+ instruction += String(rcSwitchControl.getReceivedDelay()); // 322 (ms)
103
+ instruction += sep;
104
+ instruction += String(rcSwitchControl.getReceivedProtocol()); // 1
105
+ instruction.toCharArray(instructionBuffer, sizeof(instructionBuffer));
106
+ Firmata.sendSysex(SEND_RC, (byte)instruction.length(),
107
+ (byte *)instructionBuffer);
108
+ }
109
+
110
+ rcSwitchControl.resetAvailable();
111
+ }
112
+ }
113
+
114
+ void irReceiveAndTransmit() {
115
+ decode_results results;
116
+
117
+ if (irrecv.decode(&results)) {
118
+ String ircode = String(results.value);
119
+ char ircodeBuffer[20];
120
+ ircode.toCharArray(ircodeBuffer, sizeof(ircodeBuffer));
121
+ Firmata.sendSysex(SEND_IR, (byte)ircode.length(),
122
+ (byte *)ircodeBuffer);
123
+ irrecv.resume();
124
+ }
125
+ }
126
+
127
+ void processDoorAndTransmit() {
128
+ int newDoorState = digitalRead(PIN_DOOR);
129
+
130
+ if (newDoorState != lastDoorState) {
131
+ // state of the door changed
132
+ Firmata.sendString(SEND_DOOR,
133
+ (newDoorState == DOOR_OPEN ? "OPEN" : "CLOSE"));
134
+ lastDoorState = newDoorState;
135
+ }
136
+ }
137
+
138
+ void loop()
139
+ {
140
+ rcReceiveAndTransmit();
141
+ irReceiveAndTransmit();
142
+ processDoorAndTransmit();
143
+ while(Firmata.available()) Firmata.processInput();
144
+ }
metadata ADDED
@@ -0,0 +1,194 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ban
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Vincent Landgraf
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: eventmachine
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: arduino_firmata
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: thor
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: em-websocket
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ! '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: A home control system based on firmata
126
+ email:
127
+ - setcool@gmx.de
128
+ executables:
129
+ - ban
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - .gitignore
134
+ - .rspec
135
+ - .ruby-version
136
+ - .travis.yml
137
+ - Gemfile
138
+ - LICENSE.txt
139
+ - README.md
140
+ - Rakefile
141
+ - ban.gemspec
142
+ - bin/ban
143
+ - doc/ban-layout.png
144
+ - doc/ban-layout.svg
145
+ - doc/ban.fzz
146
+ - examples/client.rb
147
+ - ino.ini
148
+ - lib/ban.rb
149
+ - lib/ban/board.rb
150
+ - lib/ban/cli.rb
151
+ - lib/ban/event.rb
152
+ - lib/ban/events/door_event.rb
153
+ - lib/ban/events/ir_event.rb
154
+ - lib/ban/events/rc_event.rb
155
+ - lib/ban/server.rb
156
+ - lib/ban/version.rb
157
+ - spec/ban/events/door_event_spec.rb
158
+ - spec/ban/events/ir_event_spec.rb
159
+ - spec/ban/events/rc_event_spec.rb
160
+ - spec/ban_spec.rb
161
+ - spec/spec_helper.rb
162
+ - src/HomeControl.ino
163
+ homepage: https://github.com/threez/ban
164
+ licenses:
165
+ - MIT
166
+ metadata: {}
167
+ post_install_message:
168
+ rdoc_options: []
169
+ require_paths:
170
+ - lib
171
+ required_ruby_version: !ruby/object:Gem::Requirement
172
+ requirements:
173
+ - - ! '>='
174
+ - !ruby/object:Gem::Version
175
+ version: '0'
176
+ required_rubygems_version: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ! '>='
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ requirements: []
182
+ rubyforge_project:
183
+ rubygems_version: 2.0.6
184
+ signing_key:
185
+ specification_version: 4
186
+ summary: Ban is a arduino firmware (based on firmata) that receives incoming remote
187
+ control switch commands, infrared commands, door open/close signaling and can send
188
+ out remote control commands also. Ban distributes all this information over a websocket.
189
+ test_files:
190
+ - spec/ban/events/door_event_spec.rb
191
+ - spec/ban/events/ir_event_spec.rb
192
+ - spec/ban/events/rc_event_spec.rb
193
+ - spec/ban_spec.rb
194
+ - spec/spec_helper.rb