-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDEV_Zephyr.h
243 lines (180 loc) · 6.52 KB
/
DEV_Zephyr.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
////////////////////////////////////
// DEVICE-SPECIFIC SERVICE //
////////////////////////////////////
#include "extras/RFControl.h"
#define RF433_PIN 22 // pin used for 433MHz transmitter
// Zephyr Vent Hood
// Frequency: 433 MHz
// Encoding: Fixed pulse duration of 850 usec
// 0-Bit: 230 HIGH / 620 LOW
// 1-Bit: 620 HIGH / 230 LOW
// N-Bits: 20
// N-Cycles: 8
// Cycle Gap: 4000 usec
void transmitZephyr(uint32_t code);
boolean resetLight=false;
boolean resetFan=false;
RFControl RF433(RF433_PIN);
//////////////////////////////////
struct DEV_ZephyrLight : Service::LightBulb {
uint32_t lightCode;
uint32_t powerCode;
SpanCharacteristic *power;
int lightPin;
int state=0; // 0=off, 1=dim, 2=medium, 3=bright
DEV_ZephyrLight(uint32_t lightCode, uint32_t powerCode, int lightPin) : Service::LightBulb(){
power=new Characteristic::On();
new Characteristic::Name("Vent Hood Light");
this->lightCode=lightCode;
this->powerCode=powerCode;
this->lightPin=lightPin;
Serial.print("Configuring Zephyr Vent Hood Light 433MHz Transmitter with light code: ");
Serial.print(lightCode,HEX);
Serial.print(" power code: ");
Serial.print(powerCode,HEX);
Serial.print("\n");
WEBLOG("Configuring Zephyr Vent Hood Light 433MHz Transmitter with light code = 0x%05X and power code = 0x%05X",lightCode,powerCode);
new SpanButton(lightPin);
}
boolean update(){
if(!power->getVal() && power->getNewVal()){ // only transmit code to turn on light if power is off
LOG1("Zephyr Vent Hood Light: Power On\n");
WEBLOG("HomeKit Command: Turn on Light");
transmitZephyr(lightCode);
state=3; // light always turns on to brightest setting
} else
if(power->getVal() && !power->getNewVal()){ // only transmit code to turn off light if power is on
LOG1("Zephyr Vent Hood Light: Power Off\n");
WEBLOG("HomeKit Command: Turn off Light");
transmitZephyr(powerCode);
state=0;
resetFan=true; // if fan is on, we need to tell HomeKit it is now off
}
return(true);
} // update
void button(int pin, int pressType) override {
if(pressType==SpanButton::SINGLE){
LOG1("Zephyr Vent Hood Light SINGLE Button Press: Brightness Change\n");
WEBLOG("Light Button: Single Press");
transmitZephyr(lightCode);
state--; // decrement state
if(state==0){ // if reached zero, set power to OFF
power->setVal(false);
} else
if(state==-1){ // if state was already zero, reset to 3 and set power to ON
state=3;
power->setVal(true);
}
} else
if(pressType==SpanButton::DOUBLE){
LOG1("Zephyr Vent Hood Light DOUBLE Button Press: IGNORED\n");
WEBLOG("Light Button: Double Press");
} else
if(power->getVal()){
LOG1("Zephyr Vent Hood Light LONG Button Press: Powering off Vent Hood\n");
WEBLOG("Light Button: Long Press");
transmitZephyr(powerCode);
state=0;
power->setVal(false);
resetFan=true;
} else {
LOG1("Zephyr Vent Hood Light LONG Button Press: Power is already off!\n");
}
} // button
void loop(){
if(resetLight){
resetLight=false;
if(power->getVal()){
LOG1("Zephyr Vent Hood Light: Resetting Power Off\n");
power->setVal(false);
state=0;
}
}
} // loop
};
//////////////////////////////////
struct DEV_ZephyrFan : Service::Fan {
uint32_t fanCode;
uint32_t powerCode;
SpanCharacteristic *power;
int fanPin;
DEV_ZephyrFan(uint32_t fanCode, uint32_t powerCode, int fanPin) : Service::Fan(){
power=new Characteristic::Active();
new Characteristic::Name("Vent Hood Fan");
this->fanCode=fanCode;
this->powerCode=powerCode;
this->fanPin=fanPin;
Serial.print("Configuring Zephyr Vent Hood Fan 433MHz Transmitter with fan code: ");
Serial.print(fanCode,HEX);
Serial.print(" power code: ");
Serial.print(powerCode,HEX);
Serial.print("\n");
WEBLOG("Configuring Zephyr Vent Hood Fan 433MHz Transmitter with light code = 0x%05X and power code = 0x%05X",fanCode,powerCode);
new SpanButton(fanPin);
}
boolean update(){
if(!power->getVal() && power->getNewVal()){ // only transmit code to turn on fan if power is off
LOG1("Zephyr Vent Hood Fan: Power On\n");
WEBLOG("HomeKit Command: Turn on Fan");
transmitZephyr(fanCode);
} else
if(power->getVal() && !power->getNewVal()){ // only transmit code to turn off fan if power is on
LOG1("Zephyr Vent Hood Fan: Power Off\n");
WEBLOG("HomeKit Command: Turn off Fan");
transmitZephyr(powerCode);
resetLight=true;
}
return(true);
} // update
void button(int pin, int pressType) override {
if(pressType==SpanButton::SINGLE){
LOG1("Zephyr Vent Hood Fan SINGLE Button Press: Speed Change\n");
WEBLOG("Fan Button: Single Press");
transmitZephyr(fanCode);
if(!power->getVal())
power->setVal(true);
} else
if(pressType==SpanButton::DOUBLE){
LOG1("Zephyr Vent Hood Fan DOUBLE Button Press: IGNORED\n");
WEBLOG("Fan Button: Double Press");
} else
if(power->getVal()){
LOG1("Zephyr Vent Hood Fan LONG Button Press: Powering off Vent Hood\n");
WEBLOG("Fan Button: Long Press");
transmitZephyr(powerCode);
power->setVal(false);
resetLight=true;
} else {
LOG1("Zephyr Vent Hood Fan LONG Button Press: Power is already off!\n");
}
} // button
void loop(){
if(resetFan){
resetFan=false;
if(power->getVal()){
LOG1("Zephyr Vent Hood Fan: Resetting Power Off\n");
power->setVal(false);
}
}
} // loop
};
//////////////////////////////////
void transmitZephyr(uint32_t code){
char c[32];
sprintf(c,"Transmitting code: %lx\n",code);
LOG1(c);
WEBLOG("Transmitting code: 0x%05X",code);
RF433.clear();
for(int b=19;b>0;b--){
if(code&(1<<b))
RF433.add(620,230);
else
RF433.add(230,620);
}
if(code&1)
RF433.add(620,4230);
else
RF433.add(230,4620);
RF433.start(8,1);
delay(200); // wait 200 ms before returning to ensure vent has sufficient time to process request
}