Skip to content

Instantly share code, notes, and snippets.

@aschmidt75
Created April 25, 2018 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aschmidt75/c446c6f152a3b7f0b01b3678823e249f to your computer and use it in GitHub Desktop.
Save aschmidt75/c446c6f152a3b7f0b01b3678823e249f to your computer and use it in GitHub Desktop.
THNG:STRUCTION Application part for a DimmableColorLight on an ESP8266
// ------------------------------------------------------------------------------------------------------------------
// Application Handlers
// TODO: Implement application functionality here
// ------------------------------------------------------------------------------------------------------------------
/** Pushes a new event to the event FIFO */
void wot_events_push(const char *name, const char *description, const char *time);
/** TODO: Implement application-specific code here by filling the app_...(...) functions */
// HINT: #include your own libraries here if you need them (e.g. for sensor libraries)
#include <Adafruit_NeoPixel.h>
// HINT: declare/define your globals here
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(1, D2, NEO_GRB + NEO_KHZ800);
int dim_value = 0; // [%] 0=off, 100=on
int breathing_value = 100; // [%]
int breathing_dir = 1;
bool b_breathing = false;
uint32_t color = pixels.Color(255,255,255);
char color_buf[8];
bool b_update = false;
/**
* app_setup() is called by setup() after Serial and networking has been made
* available. This is a good place to initialize your own hardware parts like
* sensors etc., and to initialize your own variables.
*/
void app_setup() {
pixels.begin();
pixels.setPixelColor(0,color);
pixels.setBrightness(0);
pixels.show();
}
/**
* app_loop() gets called by loop() function in regular intervals. This is good
* place to check for conditions (e.g. from sensors) and raise events. Additionally,
* actions handling code should be performed here.
* It's not run in a separate thread, so please do not delay(..) in here, otherwise
* it can block the http procotol handling.
*/
void app_loop() {
if ( b_update || b_breathing) {
if ( b_breathing) {
pixels.setPixelColor(0,color);
pixels.setBrightness((dim_value*breathing_value)/100);
pixels.show();
delay(15);
} else {
pixels.setPixelColor(0,color);
pixels.setBrightness(dim_value);
pixels.show();
}
}
if (b_breathing) {
breathing_value += breathing_dir;
if ( breathing_value >= 100) {
breathing_value = 100;
breathing_dir *= -1;
}
if ( breathing_value <= 0) {
breathing_value = 0;
breathing_dir *= -1;
}
}
}
/**
* app_request_start_breathing is called by http handlers every time a client
* requests action "start_breathing". Returns true if action has been handled
* successfully, false otherwise.
* TODO: Implement custom action logic here. Remember this function blocks
* processing of http request. For longer running actions, remember
* action state and implement actions processing within app_loop().
*/
bool app_request_start_breathing() {
b_breathing = true;
return true;
}
/**
* app_request_stop_breathing is called by http handlers every time a client
* requests action "stop_breathing". Returns true if action has been handled
* successfully, false otherwise.
* TODO: Implement custom action logic here. Remember this function blocks
* processing of http request. For longer running actions, remember
* action state and implement actions processing within app_loop().
*/
bool app_request_stop_breathing() {
b_breathing = false;
return true;
}
/**
* app_get_color is called by http handlers every time property "color"
* is requested by a client in a read operation.
* TODO: Implement custom action logic here. Remember this function blocks
* processing of http request, so make its execution as short as possible.
*/
const char* app_get_color() {
return color_buf;
}
int hex_nib_to_int(char v) {
if (v >=65 && v <= 70) {
return (v-65)+10;
}
if (v >=97 && v <= 102) {
return (v-97)+10;
}
if (v >=48 && v <= 57) {
return (v-48);
}
return 0;
}
/**
* app_set_color is called by http handlers every time property "color"
* is requested by a client in a write operation.
* TODO: Implement custom action logic here. Remember this function blocks
* processing of http request, so make its execution as short as possible.
*/
void app_set_color(const char* v) {
if (strlen(v) >= 7 && v[0] == '#') {
strncpy(color_buf, v, sizeof(color_buf));
int r,g,b = 0;
r = (hex_nib_to_int(v[1])<<4)+hex_nib_to_int(v[2]);
g = (hex_nib_to_int(v[3])<<4)+hex_nib_to_int(v[4]);
b = (hex_nib_to_int(v[5])<<4)+hex_nib_to_int(v[6]);
color = pixels.Color(r,g,b);
b_update = true;
}
}
/**
* app_get_level is called by http handlers every time property "level"
* is requested by a client in a read operation.
* TODO: Implement custom action logic here. Remember this function blocks
* processing of http request, so make its execution as short as possible.
*/
int app_get_level() {
return dim_value;
}
/**
* app_set_level is called by http handlers every time property "level"
* is requested by a client in a write operation.
* TODO: Implement custom action logic here. Remember this function blocks
* processing of http request, so make its execution as short as possible.
*/
void app_set_level(int v) {
dim_value = v;
b_update = true;
}
/**
* app_get_on is called by http handlers every time property "on"
* is requested by a client in a read operation.
* TODO: Implement custom action logic here. Remember this function blocks
* processing of http request, so make its execution as short as possible.
*/
bool app_get_on() {
return (dim_value == 100);
}
/**
* app_set_on is called by http handlers every time property "on"
* is requested by a client in a write operation.
* TODO: Implement custom action logic here. Remember this function blocks
* processing of http request, so make its execution as short as possible.
*/
void app_set_on(bool v) {
dim_value = (v?100:0);
b_update = true;
}
// ------------------------------------------------------------------------------------------------------------------
// END Application Handlers
// ------------------------------------------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment