Skip to content

Commit 1b993b0

Browse files
committed
(2.4.1) Re-added 2.2.0+ color callbacks
1 parent 748949c commit 1b993b0

File tree

6 files changed

+134
-14
lines changed

6 files changed

+134
-14
lines changed
+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* This is a basic example on how to use Espalexa with RGB color devices.
3+
*/
4+
#ifdef ARDUINO_ARCH_ESP32
5+
#include <WiFi.h>
6+
#else
7+
#include <ESP8266WiFi.h>
8+
#endif
9+
#define ESPALEXA_ASYNC
10+
#include <Espalexa.h>
11+
12+
// prototypes
13+
boolean connectWifi();
14+
15+
//callback function prototype
16+
void colorLightChanged(uint8_t brightness, uint32_t rgb);
17+
18+
// Change this!!
19+
const char* ssid = "...";
20+
const char* password = "wifipassword";
21+
22+
boolean wifiConnected = false;
23+
24+
Espalexa espalexa;
25+
26+
void setup()
27+
{
28+
Serial.begin(115200);
29+
// Initialise wifi connection
30+
wifiConnected = connectWifi();
31+
32+
if(wifiConnected){
33+
espalexa.addDevice("Color Light", colorLightChanged);
34+
35+
espalexa.begin();
36+
37+
} else
38+
{
39+
while (1) {
40+
Serial.println("Cannot connect to WiFi. Please check data and reset the ESP.");
41+
delay(2500);
42+
}
43+
}
44+
}
45+
46+
void loop()
47+
{
48+
espalexa.loop();
49+
delay(1);
50+
}
51+
52+
//the color device callback function has two parameters
53+
void colorLightChanged(uint8_t brightness, uint32_t rgb) {
54+
//do what you need to do here, for example control RGB LED strip
55+
Serial.print("Brightness: ");
56+
Serial.print(brightness);
57+
Serial.print(", Red: ");
58+
Serial.print((rgb >> 16) & 0xFF); //get red component
59+
Serial.print(", Green: ");
60+
Serial.print((rgb >> 8) & 0xFF); //get green
61+
Serial.print(", Blue: ");
62+
Serial.println(rgb & 0xFF); //get blue
63+
}
64+
65+
// connect to wifi – returns true if successful or false if not
66+
boolean connectWifi(){
67+
boolean state = true;
68+
int i = 0;
69+
70+
WiFi.mode(WIFI_STA);
71+
WiFi.begin(ssid, password);
72+
Serial.println("");
73+
Serial.println("Connecting to WiFi");
74+
75+
// Wait for connection
76+
Serial.print("Connecting...");
77+
while (WiFi.status() != WL_CONNECTED) {
78+
delay(500);
79+
Serial.print(".");
80+
if (i > 40){
81+
state = false; break;
82+
}
83+
i++;
84+
}
85+
Serial.println("");
86+
if (state){
87+
Serial.print("Connected to ");
88+
Serial.println(ssid);
89+
Serial.print("IP address: ");
90+
Serial.println(WiFi.localIP());
91+
}
92+
else {
93+
Serial.println("Connection failed.");
94+
}
95+
return state;
96+
}

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Espalexa
2-
version=2.4.0
2+
version=2.4.1
33
author=Christian Schwinne
44
maintainer=Christian Schwinne
55
sentence=Library to control an ESP module with the Alexa voice assistant

readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Now compatible with both ESP8266 and ESP32!
44

55
#### What does this do similar projects don't already?
66

7-
It allows you to set a ranged value (e.g. Brightness, Temperature) additionally to standard on/off control.
7+
It allows you to set a ranged value (e.g. Brightness, Temperature) and optionally a color, additionally to standard on/off control.
88
For example, you can say "Alexa, turn the light to 75% / 21 degrees".
99
Alexa now finally supports colors with the local API! You can see how to add color devices in the EspalexaColor example.
1010
Then, you can say "Alexa, turn the light to Blue". Color temperature (white shades) is also supported.

src/Espalexa.h

+15-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*/
1111
/*
1212
* @title Espalexa library
13-
* @version 2.4.0
13+
* @version 2.4.1
1414
* @author Christian Schwinne
1515
* @license MIT
1616
* @contributors d-999
@@ -49,7 +49,7 @@
4949
#include <WiFiUdp.h>
5050

5151
#ifdef ESPALEXA_DEBUG
52-
#pragma message "Espalexa 2.4.0 debug mode"
52+
#pragma message "Espalexa 2.4.1 debug mode"
5353
#define EA_DEBUG(x) Serial.print (x)
5454
#define EA_DEBUGLN(x) Serial.println (x)
5555
#else
@@ -174,7 +174,7 @@ class Espalexa {
174174
}
175175
res += "\r\nFree Heap: " + (String)ESP.getFreeHeap();
176176
res += "\r\nUptime: " + (String)millis();
177-
res += "\r\n\r\nEspalexa library v2.4.0 by Christian Schwinne 2019";
177+
res += "\r\n\r\nEspalexa library v2.4.1 by Christian Schwinne 2019";
178178
server->send(200, "text/plain", res);
179179
}
180180
#endif
@@ -386,7 +386,7 @@ class Espalexa {
386386
return true;
387387
}
388388

389-
//deprecated brightness-only callback
389+
//brightness-only callback
390390
bool addDevice(String deviceName, BrightnessCallbackFunction callback, uint8_t initialValue = 0)
391391
{
392392
EA_DEBUG("Constructing device ");
@@ -395,6 +395,17 @@ class Espalexa {
395395
EspalexaDevice* d = new EspalexaDevice(deviceName, callback, initialValue);
396396
return addDevice(d);
397397
}
398+
399+
//brightness-only callback
400+
bool addDevice(String deviceName, ColorCallbackFunction callback, uint8_t initialValue = 0)
401+
{
402+
EA_DEBUG("Constructing device ");
403+
EA_DEBUGLN((currentDeviceCount+1));
404+
if (currentDeviceCount >= ESPALEXA_MAXDEVICES) return false;
405+
EspalexaDevice* d = new EspalexaDevice(deviceName, callback, initialValue);
406+
return addDevice(d);
407+
}
408+
398409

399410
bool addDevice(String deviceName, DeviceCallbackFunction callback, EspalexaDeviceType t = EspalexaDeviceType::dimmable, uint8_t initialValue = 0)
400411
{

src/EspalexaDevice.cpp

+14-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
EspalexaDevice::EspalexaDevice(){}
66

7-
EspalexaDevice::EspalexaDevice(String deviceName, BrightnessCallbackFunction gnCallback, uint8_t initialValue) { //constructor
7+
EspalexaDevice::EspalexaDevice(String deviceName, BrightnessCallbackFunction gnCallback, uint8_t initialValue) { //constructor for dimmable device
88

99
_deviceName = deviceName;
1010
_callback = gnCallback;
@@ -13,11 +13,19 @@ EspalexaDevice::EspalexaDevice(String deviceName, BrightnessCallbackFunction gnC
1313
_type = EspalexaDeviceType::dimmable;
1414
}
1515

16-
EspalexaDevice::EspalexaDevice(String deviceName, DeviceCallbackFunction gnCallback, EspalexaDeviceType t, uint8_t initialValue) { //constructor for color device
16+
EspalexaDevice::EspalexaDevice(String deviceName, ColorCallbackFunction gnCallback, uint8_t initialValue) { //constructor for color device
17+
18+
_deviceName = deviceName;
19+
_callbackCol = gnCallback;
20+
_val = initialValue;
21+
_val_last = _val;
22+
_type = EspalexaDeviceType::extendedcolor;
23+
}
24+
25+
EspalexaDevice::EspalexaDevice(String deviceName, DeviceCallbackFunction gnCallback, EspalexaDeviceType t, uint8_t initialValue) { //constructor for general device
1726

1827
_deviceName = deviceName;
1928
_callbackDev = gnCallback;
20-
_callback = nullptr;
2129
_type = t;
2230
_val = initialValue;
2331
_val_last = _val;
@@ -305,5 +313,7 @@ void EspalexaDevice::setColor(uint8_t r, uint8_t g, uint8_t b)
305313

306314
void EspalexaDevice::doCallback()
307315
{
308-
(_callback != nullptr) ? _callback(_val) : _callbackDev(this);
316+
if (_callback != nullptr) {_callback(_val); return;}
317+
if (_callbackDev != nullptr) {_callbackDev(this); return;}
318+
if (_callbackCol != nullptr) _callbackCol(_val, getRGB());
309319
}

src/EspalexaDevice.h

+7-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ typedef class EspalexaDevice;
77

88
typedef void (*BrightnessCallbackFunction) (uint8_t b);
99
typedef void (*DeviceCallbackFunction) (EspalexaDevice* d);
10+
typedef void (*ColorCallbackFunction) (uint8_t br, uint32_t col);
1011

1112
enum class EspalexaColorMode : uint8_t { none = 0, ct = 1, hs = 2, xy = 3 };
1213
enum class EspalexaDeviceType : uint8_t { onoff = 0, dimmable = 1, whitespectrum = 2, color = 3, extendedcolor = 4 };
@@ -15,22 +16,24 @@ enum class EspalexaDeviceProperty : uint8_t { none = 0, on = 1, off = 2, bri = 3
1516
class EspalexaDevice {
1617
private:
1718
String _deviceName;
18-
BrightnessCallbackFunction _callback;
19-
DeviceCallbackFunction _callbackDev;
19+
BrightnessCallbackFunction _callback = nullptr;
20+
DeviceCallbackFunction _callbackDev = nullptr;
21+
ColorCallbackFunction _callbackCol = nullptr;
2022
uint8_t _val, _val_last, _sat = 0;
2123
uint16_t _hue = 0, _ct = 0;
22-
float _x = 0, _y = 0;
24+
float _x = 0.5, _y = 0.5;
2325
uint32_t _rgb = 0;
2426
uint8_t _id = 0;
2527
EspalexaDeviceType _type;
2628
EspalexaDeviceProperty _changed = EspalexaDeviceProperty::none;
27-
EspalexaColorMode _mode;
29+
EspalexaColorMode _mode = EspalexaColorMode::xy;
2830

2931
public:
3032
EspalexaDevice();
3133
~EspalexaDevice();
3234
EspalexaDevice(String deviceName, BrightnessCallbackFunction bcb, uint8_t initialValue =0);
3335
EspalexaDevice(String deviceName, DeviceCallbackFunction dcb, EspalexaDeviceType t =EspalexaDeviceType::dimmable, uint8_t initialValue =0);
36+
EspalexaDevice(String deviceName, ColorCallbackFunction ccb, uint8_t initialValue =0);
3437

3538
String getName();
3639
uint8_t getId();

0 commit comments

Comments
 (0)