-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSonoffDual.cpp
54 lines (42 loc) · 1.17 KB
/
SonoffDual.cpp
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
#include "SonoffDual.h"
using namespace SonoffDualInternal;
SonoffDualClass::SonoffDualClass() {
}
void SonoffDualClass::setup() {
Serial.begin(19200);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH);
setRelays(false, false);
setLed(false);
}
SonoffDualButton SonoffDualClass::handleButton() {
if (Serial.available() >= 4) {
uint8_t values[4] = {0, 0, 0, 0};
values[0] = Serial.read();
values[1] = Serial.read();
values[2] = Serial.read();
values[3] = Serial.read();
if (values[0] != 0xA0 || values[3] != 0xA1) return SonoffDualButton::NONE;
uint16_t buttonCode = (values[1] << 8) | values[2];
if (buttonCode == 0xF500) {
return SonoffDualButton::LONG;
} else {
return SonoffDualButton::SHORT;
}
}
return SonoffDualButton::NONE;
}
void SonoffDualClass::setLed(bool on) {
digitalWrite(LED_PIN, on ? LED_ON : LED_OFF);
}
void SonoffDualClass::setRelays(bool first, bool second) {
uint8_t state = 0;
state ^= ((first ? 1 : 0) << 0);
state ^= ((second ? 1 : 0) << 1);
Serial.write(0xA0);
Serial.write(0x04);
Serial.write(state);
Serial.write(0xA1);
Serial.flush();
}
SonoffDualClass SonoffDual;