diff --git a/.gitignore b/.gitignore index c61f8f21..c77d9175 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ clients/web/node_modules/ clients/web/\.vscode/ +*.DS_Store diff --git a/Arduino/McLighting/GY33_MCU.cpp b/Arduino/McLighting/GY33_MCU.cpp index 99dd4161..2f84d68a 100644 --- a/Arduino/McLighting/GY33_MCU.cpp +++ b/Arduino/McLighting/GY33_MCU.cpp @@ -17,11 +17,7 @@ v1.0 - First release */ /**************************************************************************/ -#ifdef __AVR - #include -#elif defined(ESP8266) - #include -#endif + #include #include @@ -52,7 +48,7 @@ uint8_t GY33_MCU::write8 (uint8_t reg, uint8_t val) brzo_i2c_start_transaction(MCU_ADDRESS, SCL_SPEED); buf[0]=reg; buf[1]=val; - brzo_i2c_write(buf, 2, true); + brzo_i2c_write(buf, 2, false); return brzo_i2c_end_transaction(); } @@ -140,26 +136,28 @@ boolean GY33_MCU::begin(void) @brief Reads the raw red, green, blue and clear channel values */ /**************************************************************************/ -void GY33_MCU::getRawData (uint16_t *r, uint16_t *g, uint16_t *b, uint16_t *c, uint16_t *ct) +void GY33_MCU::getRawData (uint16_t *r, uint16_t *g, uint16_t *b, uint16_t *c, uint16_t *lux, uint16_t *ct) { if (!_MCUInitialised) begin(); - *r = read16(MCU_RDATAH); - *g = read16(MCU_GDATAH); - *b = read16(MCU_BDATAH); - *c = read16(MCU_CDATAH); - *ct = read16(MCU_CTDATAH); + *r = read16(MCU_RDATAH); + *g = read16(MCU_GDATAH); + *b = read16(MCU_BDATAH); + *c = read16(MCU_CDATAH); + *lux = read16(MCU_LDATAH); + *ct = read16(MCU_CTDATAH); } -void GY33_MCU::getData (uint8_t *r, uint8_t *g, uint8_t *b, uint8_t *c) +void GY33_MCU::getData (uint8_t *r, uint8_t *g, uint8_t *b, uint8_t *c, uint8_t *conf) { if (!_MCUInitialised) begin(); *r = read8(MCU_RDATA); *g = read8(MCU_GDATA); *b = read8(MCU_BDATA); - *c = read8(MCU_COLDATA); + *c = read8(MCU_COLDATA); + *conf = read8(MCU_CONFIG); } /**************************************************************************/ @@ -214,10 +212,9 @@ uint16_t GY33_MCU::calculateLux(uint16_t r, uint16_t g, uint16_t b) } void GY33_MCU::setConfig(uint8_t high, uint8_t low) { -// write8(MCU_CONFIG, high | low); Serial.println("GY-33: "); Serial.println(high | low, HEX); - write8(MCU_CONFIG, 0x11); + write8(MCU_CONFIG, high | low); } uint8_t GY33_MCU::getConfig(void) { diff --git a/Arduino/McLighting/GY33_MCU.h b/Arduino/McLighting/GY33_MCU.h index 8c339588..07481c26 100644 --- a/Arduino/McLighting/GY33_MCU.h +++ b/Arduino/McLighting/GY33_MCU.h @@ -38,24 +38,24 @@ #ifndef _MCU_H_ #define _MCU_H_ -#include +#include // https://github.com/pasko-zh/brzo_i2c #define MCU_ADDRESS (0x5A) #define SCL_SPEED 100 #define SCL_STRETCH_TIMEOUT 50000 -#define MCU_LED_OFF (0x00) -#define MCU_LED_1 (0x10) -#define MCU_LED_2 (0x20) -#define MCU_LED_3 (0x30) -#define MCU_LED_4 (0x40) -#define MCU_LED_5 (0x50) -#define MCU_LED_6 (0x60) -#define MCU_LED_7 (0x70) -#define MCU_LED_8 (0x80) -#define MCU_LED_9 (0x90) -#define MCU_LED_10 (0xA0) +#define MCU_LED_OFF (0xA0) +#define MCU_LED_01 (0x90) +#define MCU_LED_02 (0x80) +#define MCU_LED_03 (0x70) +#define MCU_LED_04 (0x60) +#define MCU_LED_05 (0x50) +#define MCU_LED_06 (0x40) +#define MCU_LED_07 (0x30) +#define MCU_LED_08 (0x20) +#define MCU_LED_09 (0x10) +#define MCU_LED_10 (0x00) #define MCU_WHITE_OFF (0x00) /* No Whitebalance */ #define MCU_WHITE_ON (0x01) /* Whitebalance */ @@ -75,15 +75,15 @@ #define MCU_GDATA (0x0D) /* Green channel data */ #define MCU_BDATA (0x0E) /* Blue channel data */ #define MCU_COLDATA (0x0F) /* Blue channel data */ -#define MCU_CONFIG (0x10) +#define MCU_CONFIG (0x10) /* Config channel data */ class GY33_MCU { public: GY33_MCU(); boolean begin(void); - void getRawData(uint16_t *r, uint16_t *g, uint16_t *b, uint16_t *c, uint16_t *ct); - void getData(uint8_t *r, uint8_t *g, uint8_t *b, uint8_t *c); + void getRawData(uint16_t *r, uint16_t *g, uint16_t *b, uint16_t *c, uint16_t *lux, uint16_t *ct); + void getData(uint8_t *r, uint8_t *g, uint8_t *b, uint8_t *c, uint8_t *conf); uint16_t calculateColorTemperature(uint16_t r, uint16_t g, uint16_t b); uint16_t calculateLux(uint16_t r, uint16_t g, uint16_t b); uint8_t write8 (uint8_t reg, uint8_t val); diff --git a/Arduino/McLighting/McLighting.ino b/Arduino/McLighting/McLighting.ino index 3aa44c03..24d55693 100644 --- a/Arduino/McLighting/McLighting.ino +++ b/Arduino/McLighting/McLighting.ino @@ -25,7 +25,24 @@ // Initialize Color Sensor // *************************************************************************** byte gammatable[256]; - GY33_MCU tcs; + GY33_MCU tcs; + byte gamma8[] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, + 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, + 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, + 10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16, + 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25, + 25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36, + 37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50, + 51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68, + 69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89, + 90, 92, 93, 95, 96, 98, 99,101,102,104,105,107,109,110,112,114, + 115,117,119,120,122,124,126,127,129,131,133,135,137,138,140,142, + 144,146,148,150,152,154,156,158,160,162,164,167,169,171,173,175, + 177,180,182,184,186,189,191,193,196,198,200,203,205,208,210,213, + 215,218,220,223,225,228,231,233,236,239,241,244,247,249,252,255 }; #endif // OTA @@ -565,18 +582,8 @@ DBG_OUTPUT_PORT.println("Starting...."); // Setup: SPIFFS Webserver handler // *************************************************************************** server.on("/set_brightness", []() { - if (server.arg("c").toInt() > 0) { - brightness = (int) server.arg("c").toInt() * 2.55; - } else { - brightness = server.arg("p").toInt(); - } - if (brightness > 255) { - brightness = 255; - } - if (brightness < 0) { - brightness = 0; - } - strip.setBrightness(brightness); + getArgs(); + mode = BRIGHTNESS; #ifdef ENABLE_MQTT mqtt_client.publish(mqtt_outtopic, String(String("OK %") + String(brightness)).c_str()); #endif @@ -632,7 +639,9 @@ DBG_OUTPUT_PORT.println("Starting...."); }); server.on("/get_color", []() { - String rgbcolor = String(main_color.white, HEX) + String(main_color.red, HEX) + String(main_color.green, HEX) + String(main_color.blue, HEX); + //String rgbcolor = String(main_color.white, HEX) + String(main_color.red, HEX) + String(main_color.green, HEX) + String(main_color.blue, HEX); + char rgbcolor[9]; + snprintf(rgbcolor, sizeof(rgbcolor), "%02X%02X%02X%02X", main_color.white, main_color.red, main_color.green, main_color.blue); server.send(200, "text/plain", rgbcolor ); DBG_OUTPUT_PORT.print("/get_color: "); DBG_OUTPUT_PORT.println(rgbcolor); @@ -860,9 +869,7 @@ DBG_OUTPUT_PORT.println("Starting...."); } sprintf(last_state, "STA|%2d|%3d|%3d|%3d|%3d|%3d|%3d|%3d", mode, ws2812fx_mode, ws2812fx_speed, brightness, main_color.white, main_color.red, main_color.green, main_color.blue); #endif - tcs.setConfig(MCU_LED_10,MCU_WHITE_ON); - DBG_OUTPUT_PORT.println("Config is:"); - DBG_OUTPUT_PORT.println( tcs.getConfig()); + tcs.setConfig(MCU_LED_05, MCU_WHITE_ON); } void loop() { @@ -870,7 +877,7 @@ void loop() { button(); #endif #ifdef ENABLE_BUTTON_GY33 - button2(); + button_gy33(); #endif server.handleClient(); webSocket.loop(); @@ -913,8 +920,7 @@ void loop() { DBG_OUTPUT_PORT.printf("SET_MODE: %d %d\n", ws2812fx_mode, mode); strip.setColor(main_color.white, main_color.red, main_color.green, main_color.blue); strip.setMode(ws2812fx_mode); - strip.setSpeed(convertSpeed(ws2812fx_speed)); - mode = HOLD; + mode = SETSPEED; } if (mode == OFF) { // strip.setColor(0,0,0,0); @@ -933,7 +939,7 @@ void loop() { } if (mode == SETSPEED) { strip.setSpeed(convertSpeed(ws2812fx_speed)); - mode = HOLD; + mode = BRIGHTNESS; } if (mode == BRIGHTNESS) { strip.setBrightness(brightness); diff --git a/Arduino/McLighting/definitions.h b/Arduino/McLighting/definitions.h index 526d238b..a524ecc9 100644 --- a/Arduino/McLighting/definitions.h +++ b/Arduino/McLighting/definitions.h @@ -150,11 +150,11 @@ LEDState main_color = { 0, 255, 0, 0}; // Store the "main color" of the strip u #define BTN_MODE_MEDIUM "STA| 1| 48|245|196| 0|255|102| 0" // Fire flicker #define BTN_MODE_LONG "STA| 1| 46|253|196| 0|255|102| 0" // Fireworks random - unsigned long keyPrevMillis2 = 0; - const unsigned long keySampleIntervalMs2 = 25; - byte longKeyPressCountMax2 = 80; // 80 * 25 = 2000 ms - byte mediumKeyPressCountMin2 = 20; // 20 * 25 = 500 ms - byte KeyPressCount2 = 0; - byte prevKeyState2 = HIGH; // button is active low + unsigned long keyPrevMillis_gy33 = 0; + const unsigned long keySampleIntervalMs_gy33 = 25; + byte longKeyPressCountMax_gy33 = 80; // 80 * 25 = 2000 ms + byte mediumKeyPressCountMin_gy33 = 20; // 20 * 25 = 500 ms + byte KeyPressCount_gy33 = 0; + byte prevKeyState_gy33 = HIGH; // button is active low #endif diff --git a/Arduino/McLighting/request_handlers.h b/Arduino/McLighting/request_handlers.h index 7affa224..11ceab9f 100644 --- a/Arduino/McLighting/request_handlers.h +++ b/Arduino/McLighting/request_handlers.h @@ -33,6 +33,13 @@ void getArgs() { if (server.arg("m") != "") { ws2812fx_mode = constrain(server.arg("m").toInt(), 0, strip.getModeCount() - 1); } + + if (server.arg("c").toInt() > 0) { + brightness = (int) server.arg("c").toInt() * 2.55; + } else { + brightness = server.arg("p").toInt(); + } + brightness = constrain(brightness, 0, 255); main_color.white = constrain(main_color.white, 0, 255); main_color.red = constrain(main_color.red, 0, 255); @@ -1270,26 +1277,15 @@ void checkForRequests() { #endif #ifdef ENABLE_BUTTON_GY33 - void shortKeyPress2() { - DBG_OUTPUT_PORT.printf("Short button2 press\n"); + void shortKeyPress_gy33() { + DBG_OUTPUT_PORT.printf("Short GY-33 button press\n"); if (buttonState == false) { uint16_t red, green, blue, cl, ct, lux; -// tcs.setInterrupt(false); // turn on LED - delay(60); // takes 50ms to read - tcs.getRawData(&red, &green, &blue, &cl, &ct); -// ct = tcs.calculateColorTemperature(red, green, blue); - lux = tcs.calculateLux(red, green, blue); -// tcs.setInterrupt(true); // turn off LED - // Figure out some basic hex code for visualization - uint32_t sum = cl; -/* float r, g, b, col; - r = red; r /= sum; - g = green; g /= sum; - b = blue; b /= sum; - r *= 256; g *= 256; b *= 256;*/ - uint8_t r, g, b, col; - tcs.getData(&r, &g, &b, &col); - DBG_OUTPUT_PORT.printf("Colors: R: [%d] G: [%d] B: [%d] Clear: [%d] Colortemp: [%d] Lux: [%d]\n", (int)r, (int)g, (int)b, (int)cl, (int)ct, (int)lux ); + tcs.getRawData(&red, &green, &blue, &cl, &lux, &ct); + DBG_OUTPUT_PORT.printf("Raw Colors: R: [%d] G: [%d] B: [%d] Clear: [%d] Lux: [%d] Colortemp: [%d]\n", (int)red, (int)green, (int)blue, (int)cl, (int)lux, (int)ct); + uint8_t r, g, b, col, conf; + tcs.getData(&r, &g, &b, &col, &conf); + DBG_OUTPUT_PORT.printf("Colors: R: [%d] G: [%d] B: [%d] Color: [%d] Conf: [%d]\n", (int)r, (int)g, (int)b, (int)col, (int)conf); char newmode[38]; sprintf(newmode, "STA| 1| 0|245|196|0|%3d|%3d|%3d", (int)r, (int)g, (int)b); DBG_OUTPUT_PORT.println(newmode); @@ -1297,12 +1293,11 @@ void checkForRequests() { main_color.white = 0; main_color.red = gammatable[(int)r]; main_color.green = gammatable[(int)g]; main_color.blue = gammatable[(int)b]; mode = HOLD; strip.setMode(0); - strip.setSpeed(convertSpeed(245)); - strip.setBrightness(196); + //strip.setBrightness(196); strip.setColor(main_color.white, main_color.red, main_color.green, main_color.blue); buttonState = false; #ifdef ENABLE_MQTT - mqtt_client.publish(mqtt_outtopic, String("OK =static white").c_str()); + mqtt_client.publish(mqtt_outtopic, String("OK =static GY-33").c_str()); #endif } else { mode = OFF; @@ -1324,70 +1319,42 @@ void checkForRequests() { } // called when button is kept pressed for less than 2 seconds - void mediumKeyPress2() { - DBG_OUTPUT_PORT.printf("Medium button2 press\n"); - setModeByStateString(BTN_MODE_MEDIUM); - #ifdef ENABLE_MQTT - mqtt_client.publish(mqtt_outtopic, String("OK =fire flicker").c_str()); - #endif - #ifdef ENABLE_AMQTT - amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String("OK =fire flicker").c_str()); - #endif - #ifdef ENABLE_HOMEASSISTANT - stateOn = true; - if(!ha_send_data.active()) ha_send_data.once(5, tickerSendState); - #endif - #ifdef ENABLE_STATE_SAVE_SPIFFS - if(!spiffs_save_state.active()) spiffs_save_state.once(3, tickerSpiffsSaveState); - #endif + void mediumKeyPress_gy33() { + tcs.setConfig(MCU_LED_02, MCU_WHITE_OFF); } // called when button is kept pressed for 2 seconds or more - void longKeyPress2() { - DBG_OUTPUT_PORT.printf("Long button2 press\n"); - setModeByStateString(BTN_MODE_LONG); - #ifdef ENABLE_MQTT - mqtt_client.publish(mqtt_outtopic, String("OK =fireworks random").c_str()); - #endif - #ifdef ENABLE_AMQTT - amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String("OK =fireworks random").c_str()); - #endif - #ifdef ENABLE_HOMEASSISTANT - stateOn = true; - if(!ha_send_data.active()) ha_send_data.once(5, tickerSendState); - #endif - #ifdef ENABLE_STATE_SAVE_SPIFFS - if(!spiffs_save_state.active()) spiffs_save_state.once(3, tickerSpiffsSaveState); - #endif + void longKeyPress_gy33() { + tcs.setConfig(MCU_LED_OFF, MCU_WHITE_OFF); } - void button2() { - if (millis() - keyPrevMillis2 >= keySampleIntervalMs2) { - keyPrevMillis2 = millis(); + void button_gy33() { + if (millis() - keyPrevMillis_gy33 >= keySampleIntervalMs_gy33) { + keyPrevMillis_gy33 = millis(); - byte currKeyState2 = digitalRead(BUTTON_GY33); + byte currKeyState_gy33 = digitalRead(BUTTON_GY33); - if ((prevKeyState2 == HIGH) && (currKeyState2 == LOW)) { + if ((prevKeyState_gy33 == HIGH) && (currKeyState_gy33 == LOW)) { // key goes from not pressed to pressed - KeyPressCount2 = 0; + KeyPressCount_gy33 = 0; } - else if ((prevKeyState2 == LOW) && (currKeyState2 == HIGH)) { - if (KeyPressCount2 < longKeyPressCountMax2 && KeyPressCount2 >= mediumKeyPressCountMin2) { - mediumKeyPress2(); + else if ((prevKeyState_gy33 == LOW) && (currKeyState_gy33 == HIGH)) { + if (KeyPressCount_gy33 < longKeyPressCountMax_gy33 && KeyPressCount_gy33 >= mediumKeyPressCountMin_gy33) { + mediumKeyPress_gy33(); } else { - if (KeyPressCount2 < mediumKeyPressCountMin2) { - shortKeyPress2(); + if (KeyPressCount_gy33 < mediumKeyPressCountMin_gy33) { + shortKeyPress_gy33(); } } } - else if (currKeyState2 == LOW) { - KeyPressCount2++; - if (KeyPressCount2 >= longKeyPressCountMax2) { - longKeyPress2(); + else if (currKeyState_gy33 == LOW) { + KeyPressCount_gy33++; + if (KeyPressCount_gy33 >= longKeyPressCountMax_gy33) { + longKeyPress_gy33(); } } - prevKeyState2 = currKeyState2; + prevKeyState_gy33 = currKeyState_gy33; } } #endif