Skip to content

Commit

Permalink
(2.4.1) Re-added 2.2.0+ color callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Aircoookie committed Mar 1, 2019
1 parent 748949c commit 1b993b0
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 14 deletions.
96 changes: 96 additions & 0 deletions examples/EspalexaColor/EspalexaColor.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* This is a basic example on how to use Espalexa with RGB color devices.
*/
#ifdef ARDUINO_ARCH_ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#define ESPALEXA_ASYNC
#include <Espalexa.h>

// prototypes
boolean connectWifi();

//callback function prototype
void colorLightChanged(uint8_t brightness, uint32_t rgb);

// Change this!!
const char* ssid = "...";
const char* password = "wifipassword";

boolean wifiConnected = false;

Espalexa espalexa;

void setup()
{
Serial.begin(115200);
// Initialise wifi connection
wifiConnected = connectWifi();

if(wifiConnected){
espalexa.addDevice("Color Light", colorLightChanged);

espalexa.begin();

} else
{
while (1) {
Serial.println("Cannot connect to WiFi. Please check data and reset the ESP.");
delay(2500);
}
}
}

void loop()
{
espalexa.loop();
delay(1);
}

//the color device callback function has two parameters
void colorLightChanged(uint8_t brightness, uint32_t rgb) {
//do what you need to do here, for example control RGB LED strip
Serial.print("Brightness: ");
Serial.print(brightness);
Serial.print(", Red: ");
Serial.print((rgb >> 16) & 0xFF); //get red component
Serial.print(", Green: ");
Serial.print((rgb >> 8) & 0xFF); //get green
Serial.print(", Blue: ");
Serial.println(rgb & 0xFF); //get blue
}

// connect to wifi – returns true if successful or false if not
boolean connectWifi(){
boolean state = true;
int i = 0;

WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
Serial.println("Connecting to WiFi");

// Wait for connection
Serial.print("Connecting...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
if (i > 40){
state = false; break;
}
i++;
}
Serial.println("");
if (state){
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
else {
Serial.println("Connection failed.");
}
return state;
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Espalexa
version=2.4.0
version=2.4.1
author=Christian Schwinne
maintainer=Christian Schwinne
sentence=Library to control an ESP module with the Alexa voice assistant
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Now compatible with both ESP8266 and ESP32!

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

It allows you to set a ranged value (e.g. Brightness, Temperature) additionally to standard on/off control.
It allows you to set a ranged value (e.g. Brightness, Temperature) and optionally a color, additionally to standard on/off control.
For example, you can say "Alexa, turn the light to 75% / 21 degrees".
Alexa now finally supports colors with the local API! You can see how to add color devices in the EspalexaColor example.
Then, you can say "Alexa, turn the light to Blue". Color temperature (white shades) is also supported.
Expand Down
19 changes: 15 additions & 4 deletions src/Espalexa.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
/*
* @title Espalexa library
* @version 2.4.0
* @version 2.4.1
* @author Christian Schwinne
* @license MIT
* @contributors d-999
Expand Down Expand Up @@ -49,7 +49,7 @@
#include <WiFiUdp.h>

#ifdef ESPALEXA_DEBUG
#pragma message "Espalexa 2.4.0 debug mode"
#pragma message "Espalexa 2.4.1 debug mode"
#define EA_DEBUG(x) Serial.print (x)
#define EA_DEBUGLN(x) Serial.println (x)
#else
Expand Down Expand Up @@ -174,7 +174,7 @@ class Espalexa {
}
res += "\r\nFree Heap: " + (String)ESP.getFreeHeap();
res += "\r\nUptime: " + (String)millis();
res += "\r\n\r\nEspalexa library v2.4.0 by Christian Schwinne 2019";
res += "\r\n\r\nEspalexa library v2.4.1 by Christian Schwinne 2019";
server->send(200, "text/plain", res);
}
#endif
Expand Down Expand Up @@ -386,7 +386,7 @@ class Espalexa {
return true;
}

//deprecated brightness-only callback
//brightness-only callback
bool addDevice(String deviceName, BrightnessCallbackFunction callback, uint8_t initialValue = 0)
{
EA_DEBUG("Constructing device ");
Expand All @@ -395,6 +395,17 @@ class Espalexa {
EspalexaDevice* d = new EspalexaDevice(deviceName, callback, initialValue);
return addDevice(d);
}

//brightness-only callback
bool addDevice(String deviceName, ColorCallbackFunction callback, uint8_t initialValue = 0)
{
EA_DEBUG("Constructing device ");
EA_DEBUGLN((currentDeviceCount+1));
if (currentDeviceCount >= ESPALEXA_MAXDEVICES) return false;
EspalexaDevice* d = new EspalexaDevice(deviceName, callback, initialValue);
return addDevice(d);
}


bool addDevice(String deviceName, DeviceCallbackFunction callback, EspalexaDeviceType t = EspalexaDeviceType::dimmable, uint8_t initialValue = 0)
{
Expand Down
18 changes: 14 additions & 4 deletions src/EspalexaDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

EspalexaDevice::EspalexaDevice(){}

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

_deviceName = deviceName;
_callback = gnCallback;
Expand All @@ -13,11 +13,19 @@ EspalexaDevice::EspalexaDevice(String deviceName, BrightnessCallbackFunction gnC
_type = EspalexaDeviceType::dimmable;
}

EspalexaDevice::EspalexaDevice(String deviceName, DeviceCallbackFunction gnCallback, EspalexaDeviceType t, uint8_t initialValue) { //constructor for color device
EspalexaDevice::EspalexaDevice(String deviceName, ColorCallbackFunction gnCallback, uint8_t initialValue) { //constructor for color device

_deviceName = deviceName;
_callbackCol = gnCallback;
_val = initialValue;
_val_last = _val;
_type = EspalexaDeviceType::extendedcolor;
}

EspalexaDevice::EspalexaDevice(String deviceName, DeviceCallbackFunction gnCallback, EspalexaDeviceType t, uint8_t initialValue) { //constructor for general device

_deviceName = deviceName;
_callbackDev = gnCallback;
_callback = nullptr;
_type = t;
_val = initialValue;
_val_last = _val;
Expand Down Expand Up @@ -305,5 +313,7 @@ void EspalexaDevice::setColor(uint8_t r, uint8_t g, uint8_t b)

void EspalexaDevice::doCallback()
{
(_callback != nullptr) ? _callback(_val) : _callbackDev(this);
if (_callback != nullptr) {_callback(_val); return;}
if (_callbackDev != nullptr) {_callbackDev(this); return;}
if (_callbackCol != nullptr) _callbackCol(_val, getRGB());
}
11 changes: 7 additions & 4 deletions src/EspalexaDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ typedef class EspalexaDevice;

typedef void (*BrightnessCallbackFunction) (uint8_t b);
typedef void (*DeviceCallbackFunction) (EspalexaDevice* d);
typedef void (*ColorCallbackFunction) (uint8_t br, uint32_t col);

enum class EspalexaColorMode : uint8_t { none = 0, ct = 1, hs = 2, xy = 3 };
enum class EspalexaDeviceType : uint8_t { onoff = 0, dimmable = 1, whitespectrum = 2, color = 3, extendedcolor = 4 };
Expand All @@ -15,22 +16,24 @@ enum class EspalexaDeviceProperty : uint8_t { none = 0, on = 1, off = 2, bri = 3
class EspalexaDevice {
private:
String _deviceName;
BrightnessCallbackFunction _callback;
DeviceCallbackFunction _callbackDev;
BrightnessCallbackFunction _callback = nullptr;
DeviceCallbackFunction _callbackDev = nullptr;
ColorCallbackFunction _callbackCol = nullptr;
uint8_t _val, _val_last, _sat = 0;
uint16_t _hue = 0, _ct = 0;
float _x = 0, _y = 0;
float _x = 0.5, _y = 0.5;
uint32_t _rgb = 0;
uint8_t _id = 0;
EspalexaDeviceType _type;
EspalexaDeviceProperty _changed = EspalexaDeviceProperty::none;
EspalexaColorMode _mode;
EspalexaColorMode _mode = EspalexaColorMode::xy;

public:
EspalexaDevice();
~EspalexaDevice();
EspalexaDevice(String deviceName, BrightnessCallbackFunction bcb, uint8_t initialValue =0);
EspalexaDevice(String deviceName, DeviceCallbackFunction dcb, EspalexaDeviceType t =EspalexaDeviceType::dimmable, uint8_t initialValue =0);
EspalexaDevice(String deviceName, ColorCallbackFunction ccb, uint8_t initialValue =0);

String getName();
uint8_t getId();
Expand Down

0 comments on commit 1b993b0

Please sign in to comment.