-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathWiFiLamp.cpp
81 lines (65 loc) · 2.06 KB
/
WiFiLamp.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
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
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <IPAddress.h>
#include "Settings.h"
#include "WiFiLamp.h"
#include "Screen.h" // for lcdDebugPrintf
WiFiLampClass WiFiLamp;
void WiFiLampClass::begin() {
for (int i = 0; i < SettingsClass::N_DEVICES; i++) {
if (Settings.dev_hostname[i][0] != '\0') {
WiFi.hostByName(Settings.dev_hostname[i], _dev_ip[i]);
}
}
}
bool WiFiLampClass::lampOn(uint8_t i) {
uint8_t _cmd[4] = { 0x71, 0x23, 0x0f, 0xa3 };
return _sendCommand(i, _cmd, sizeof(_cmd));
}
bool WiFiLampClass::lampOff(uint8_t i) {
uint8_t _cmd[4] = { 0x71, 0x24, 0x0f, 0xa4 };
return _sendCommand(i, _cmd, sizeof(_cmd));
}
bool WiFiLampClass::lampRGBW(uint8_t i, uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
uint8_t _cmd[8] = { 0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00 };
// RGB and W seem to be mutually exclusive (at least from iPad app)
if (w > 0) {
_cmd[4] = w;
_cmd[5] = 0x0f; // RGB/W mask?
} else {
_cmd[1] = r;
_cmd[2] = g;
_cmd[3] = b;
_cmd[5] = 0xf0; // RGB/W mask?
}
// Compute checksum
for (int i = 0; i < sizeof(_cmd) - 1; i++)
_cmd[sizeof(_cmd)-1] += _cmd[i];
return _sendCommand(i, _cmd, sizeof(_cmd));
}
bool WiFiLampClass::lampPreset(uint8_t i, uint8_t ip) {
if (ip < 0 || ip >= SettingsClass::N_PRESETS) {
return false;
}
return lampRGBW(i, Settings.preset_r[ip], Settings.preset_g[ip], Settings.preset_b[ip], Settings.preset_w[ip]);
}
bool WiFiLampClass::_sendCommand(uint8_t i, const uint8_t *data, size_t len) {
if (i < 0 || i >= SettingsClass::N_DEVICES) {
//lcdDebugPrintf(2, "ERR: BOUND");
return false; // Invalid device
}
if (Settings.dev_hostname[i][0] == '\0') {
//lcdDebugPrintf(2, "ERR: NOIP");
return false; // Not configured
}
WiFiClient tcp;
if (!tcp.connect(_dev_ip[i], TCP_PORT)) {
//lcdDebugPrintf(2, "ERR: CONN");
return false; // Failed to connect
}
tcp.write(data, len);
tcp.flush();
delay(50); // TODO: Necessary?? ; Make sure data gets chance to be sent out
return true;
}