Mercurial > code > home > repos > homeauto
annotate service/garageArduino/garage/garage.pde @ 845:edd2486d6e2c
rename
Ignore-this: 1e379baf7a8a5ae7cf361bd7c6228d7d
darcs-hash:20120825212332-312f9-08a24630ac0ab13635b81c3ab7e843e5634a2fb4
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Sat, 25 Aug 2012 14:23:32 -0700 |
parents | 9cf01cee74f6 |
children |
rev | line source |
---|---|
839
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
1 /* |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
2 board is Arduino UNO with '328 |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
3 */ |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
4 |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
5 int datapin = 7; // DI |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
6 int latchpin = 9; // LI |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
7 int clockpin = 8; // CI |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
8 |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
9 unsigned long SB_CommandPacket; |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
10 int SB_CommandMode; |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
11 int SB_BlueCommand; |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
12 int SB_RedCommand; |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
13 int SB_GreenCommand; |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
14 |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
15 #define SHIFT(val) shiftOut(datapin, clockpin, MSBFIRST, val) |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
16 |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
17 void SB_SendPacket() { |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
18 /* high bits are 00 for color, 01 for current */ |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
19 SB_CommandPacket = SB_CommandMode & B11; |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
20 SB_CommandPacket = (SB_CommandPacket << 10) | (SB_BlueCommand & 1023); |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
21 SB_CommandPacket = (SB_CommandPacket << 10) | (SB_RedCommand & 1023); |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
22 SB_CommandPacket = (SB_CommandPacket << 10) | (SB_GreenCommand & 1023); |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
23 |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
24 SHIFT(SB_CommandPacket >> 24); |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
25 SHIFT(SB_CommandPacket >> 16); |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
26 SHIFT(SB_CommandPacket >> 8); |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
27 SHIFT(SB_CommandPacket); |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
28 } |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
29 |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
30 void latch() { |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
31 delayMicroseconds(100); |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
32 digitalWrite(latchpin,HIGH); // latch data into registers |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
33 delayMicroseconds(100); |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
34 digitalWrite(latchpin,LOW); |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
35 } |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
36 |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
37 void setCurrent(byte r, byte g, byte b) { |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
38 /* 127 = max */ |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
39 SB_CommandMode = B01; // Write to current control registers |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
40 SB_RedCommand = r; |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
41 SB_GreenCommand = g; |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
42 SB_BlueCommand = b; |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
43 SB_SendPacket(); |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
44 latch(); |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
45 } |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
46 |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
47 |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
48 void setup() { |
805 | 49 |
50 pinMode(2, INPUT); | |
51 digitalWrite(2, LOW); | |
52 // PIR sensor on here is a | |
53 // http://octopart.com/555-28027-parallax-708653 in a | |
54 // http://octopart.com/1551ggy-hammond-15686 box | |
55 | |
56 // the phototransistor on analog2 is jameco 2006414 | |
57 | |
810
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
58 pinMode(3, OUTPUT); |
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
59 digitalWrite(3, LOW); |
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
60 // this drives a relay for the garage door. There is a |
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
61 // LP filter on it so the garage doesn't open if there's |
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
62 // an arduino power-on glitch. It may be that atmel has |
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
63 // circuitry to prevent such glitches, so a pull-down |
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
64 // resistor may be enough. I haven't checked carefully. |
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
65 |
839
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
66 pinMode(4, OUTPUT); |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
67 pinMode(5, OUTPUT); |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
68 // video input selector. picks which video input will be multiplexed |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
69 // into the bt848 capture card |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
70 |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
71 pinMode(6, OUTPUT); // front yard light |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
72 |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
73 pinMode(7, OUTPUT); // bathroom shiftbrite data |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
74 pinMode(8, OUTPUT); // bathroom shiftbrite clk |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
75 pinMode(9, OUTPUT); // bathroom shiftbrite latch |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
76 |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
77 for (int i=0; i < 1; i++) { |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
78 setCurrent(127, 127, 127); |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
79 } |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
80 |
805 | 81 Serial.begin(115200); |
82 } | |
83 | |
84 int newBlinks = 0; | |
85 int lastLevel = 0; | |
86 int threshold = 750; | |
87 int hold = 3; // pulse must last this many loops. Guessing-- I don't know the loop rate or the pulse width | |
88 int seenFor = 0; | |
89 | |
90 void loop() | |
91 { | |
92 unsigned char head, cmd, arg; | |
93 int level = analogRead(3) < threshold; | |
94 | |
95 if (level) { | |
96 seenFor++; | |
97 if (seenFor == hold) { | |
98 newBlinks++; | |
99 } | |
100 } else { | |
101 seenFor = 0; | |
102 } | |
103 | |
104 if (Serial.available() >= 3) { | |
105 head = Serial.read(); | |
106 if (head != 0x60) { | |
107 Serial.flush(); | |
108 return; | |
109 } | |
110 cmd = Serial.read(); | |
111 arg = Serial.read(); | |
112 Serial.flush(); | |
113 if (cmd == 0x00) { | |
114 Serial.print("{\"ok\":true}\n"); | |
115 } else if (cmd == 0x01) { // poll | |
116 Serial.print("{\"newBlinks\":"); | |
117 Serial.print(newBlinks); | |
118 Serial.print(", \"motion\":"); | |
119 Serial.print(digitalRead(2) ? "true" : "false"); | |
120 Serial.print("}\n"); | |
121 newBlinks = 0; | |
122 } else if (cmd == 0x02) { | |
123 // current level | |
124 Serial.print("{\"z\":"); | |
125 Serial.print(analogRead(3)); | |
126 Serial.print("}\n"); | |
127 } else if (cmd == 0x03) { | |
128 if (arg != 0) { | |
129 threshold = arg << 2; | |
130 } | |
131 Serial.print("{\"threshold\":"); | |
132 Serial.print(threshold); | |
133 Serial.print("}\n"); | |
810
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
134 } else if (cmd == 0x04) { |
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
135 digitalWrite(3, arg); |
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
136 Serial.print("{\"garage\":"); |
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
137 Serial.print(arg ? "true" : "false"); |
44e1ca03ddf1
move garage door opener from parport to arduino
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
138 Serial.print("}\n"); |
839
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
139 } else if (cmd == 0x05) { // set video |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
140 digitalWrite(4, arg & 1); |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
141 digitalWrite(5, arg & 2); |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
142 Serial.print("{\"videoSelect\":"); |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
143 Serial.print(arg); |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
144 Serial.print("}\n"); |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
145 } else if (cmd == 0x06) { // talk to shiftbrite |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
146 /* |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
147 one byte for the string length, then a buffer to be shifted |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
148 out to all the shiftbrites |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
149 */ |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
150 for (int i=0; i<arg; i++) { |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
151 while (Serial.available() == 0) NULL; |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
152 SHIFT(Serial.read()); |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
153 } |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
154 latch(); |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
155 Serial.print("{\"ok\":1}\n"); |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
156 |
9cf01cee74f6
garage: videoselect and shiftbrite move from parallel to arduino
drewp <drewp@bigasterisk.com>
parents:
810
diff
changeset
|
157 } |
805 | 158 } |
159 } | |
160 | |
161 |