From 4d78ddb4a643d22cc911ae0ba3048be5a272a097 Mon Sep 17 00:00:00 2001 From: BPoH_Voodoo Date: Thu, 12 Apr 2018 20:50:11 +0200 Subject: [PATCH] Bugfix and GY-33 progress Bugfix /set_mode?m=2&rgb=000000AA&s=200 will not set color GY-33 further progress --- Arduino/McLighting/GY33_MCU.cpp | 37 +++++++++------------------ Arduino/McLighting/GY33_MCU.h | 14 +++------- Arduino/McLighting/McLighting.ino | 26 ++++++++++++++----- Arduino/McLighting/definitions.h | 9 ++++--- Arduino/McLighting/request_handlers.h | 4 +-- 5 files changed, 41 insertions(+), 49 deletions(-) diff --git a/Arduino/McLighting/GY33_MCU.cpp b/Arduino/McLighting/GY33_MCU.cpp index ab0dafa..99dd416 100644 --- a/Arduino/McLighting/GY33_MCU.cpp +++ b/Arduino/McLighting/GY33_MCU.cpp @@ -46,14 +46,14 @@ float powf(const float x, const float y) @brief Writes a register and an 8 bit value over I2C */ /**************************************************************************/ -void GY33_MCU::write8 (uint8_t reg, uint32_t val) +uint8_t GY33_MCU::write8 (uint8_t reg, uint8_t val) { uint8_t buf[2]; brzo_i2c_start_transaction(MCU_ADDRESS, SCL_SPEED); buf[0]=reg; buf[1]=val; brzo_i2c_write(buf, 2, true); - brzo_i2c_end_transaction(); + return brzo_i2c_end_transaction(); } /**************************************************************************/ @@ -125,7 +125,7 @@ boolean GY33_MCU::begin(void) /* Make sure we're actually connected */ uint8_t x = read8(MCU_CONFIG); Serial.println(x, HEX); - if ((x != 0x00) && (x != 0xFF)) + if (x != 0x10) { return false; } @@ -161,6 +161,7 @@ void GY33_MCU::getData (uint8_t *r, uint8_t *g, uint8_t *b, uint8_t *c) *b = read8(MCU_BDATA); *c = read8(MCU_COLDATA); } + /**************************************************************************/ /*! @brief Converts the raw R/G/B values to color temperature in degrees @@ -212,29 +213,15 @@ uint16_t GY33_MCU::calculateLux(uint16_t r, uint16_t g, uint16_t b) return (uint16_t)illuminance; } - -/*void GY33_MCU::setInterrupt(boolean i) { - uint8_t r = read8(MCU_ENABLE); - if (i) { - r |= MCU_ENABLE_AIEN; - } else { - r &= ~MCU_ENABLE_AIEN; - } - write8(MCU_ENABLE, r); -} - -void GY33_MCU::clearInterrupt(void) { - Wire.beginTransmission(MCU_ADDRESS); - #if ARDUINO >= 100 - Wire.write(MCU_COMMAND_BIT | 0x66); - #else - Wire.send(MCU_COMMAND_BIT | 0x66); - #endif - Wire.endTransmission(); -} -*/ - 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); } +uint8_t GY33_MCU::getConfig(void) +{ + if (!_MCUInitialised) begin(); + + return read8(MCU_CONFIG); +} diff --git a/Arduino/McLighting/GY33_MCU.h b/Arduino/McLighting/GY33_MCU.h index 23f8f4b..8c33958 100644 --- a/Arduino/McLighting/GY33_MCU.h +++ b/Arduino/McLighting/GY33_MCU.h @@ -38,12 +38,6 @@ #ifndef _MCU_H_ #define _MCU_H_ -#if ARDUINO >= 100 - #include -#else - #include -#endif - #include #define MCU_ADDRESS (0x5A) @@ -92,13 +86,11 @@ class GY33_MCU { void getData(uint8_t *r, uint8_t *g, uint8_t *b, uint8_t *c); uint16_t calculateColorTemperature(uint16_t r, uint16_t g, uint16_t b); uint16_t calculateLux(uint16_t r, uint16_t g, uint16_t b); - void write8 (uint8_t reg, uint32_t val); + uint8_t write8 (uint8_t reg, uint8_t val); uint8_t read8 (uint8_t reg); uint16_t read16 (uint8_t reg); -/* void setInterrupt(boolean flag); - void clearInterrupt(void);*/ - void setConfig(uint8_t h, uint8_t l); - + void setConfig(uint8_t h, uint8_t l); + uint8_t getConfig(void); private: boolean _MCUInitialised; diff --git a/Arduino/McLighting/McLighting.ino b/Arduino/McLighting/McLighting.ino index 792d655..3aa44c0 100644 --- a/Arduino/McLighting/McLighting.ino +++ b/Arduino/McLighting/McLighting.ino @@ -18,7 +18,7 @@ #include //https://github.com/Links2004/arduinoWebSockets #include -#ifdef ENABLE_BUTTON2 +#ifdef ENABLE_BUTTON_GY33 // needed for MCU #include "GY33_MCU.h" // *************************************************************************** @@ -105,7 +105,12 @@ NeoAnimationFX strip(neoStrip); // *************************************************************************** // https://github.com/kitesurfer1404/WS2812FX #include "WS2812FX.h" + +#ifdef RGBW WS2812FX strip = WS2812FX(NUMLEDS, PIN, NEO_GRBW + NEO_KHZ800); +#else + WS2812FX strip = WS2812FX(NUMLEDS, PIN, NEO_GRB + NEO_KHZ800); +#endif // Parameter 1 = number of pixels in strip // Parameter 2 = Arduino pin number (most are valid) @@ -248,8 +253,11 @@ void setup() { pinMode(BUTTON, INPUT_PULLUP); #endif -#ifdef ENABLE_BUTTON2 - pinMode(BUTTON2, INPUT_PULLUP); +DBG_OUTPUT_PORT.println(""); +DBG_OUTPUT_PORT.println("Starting...."); + +#ifdef ENABLE_BUTTON_GY33 + pinMode(BUTTON_GY33, INPUT_PULLUP); for (int i=0; i<256; i++) { float x = i; x /= 255; @@ -258,8 +266,7 @@ void setup() { gammatable[i] = x; } if (tcs.begin()) { - DBG_OUTPUT_PORT.println("Found GY33 sensor"); - tcs.setConfig(MCU_LED_OFF,MCU_LED_OFF); + DBG_OUTPUT_PORT.println("Found GY-33 sensor"); } else { DBG_OUTPUT_PORT.println("No GY33 sensor found ... check your connections"); } @@ -853,13 +860,16 @@ void setup() { } 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()); } void loop() { #ifdef ENABLE_BUTTON button(); #endif - #ifdef ENABLE_BUTTON2 + #ifdef ENABLE_BUTTON_GY33 button2(); #endif server.handleClient(); @@ -901,8 +911,10 @@ void loop() { // Simple statemachine that handles the different modes if (mode == SET_MODE) { 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); - mode = SETSPEED; + strip.setSpeed(convertSpeed(ws2812fx_speed)); + mode = HOLD; } if (mode == OFF) { // strip.setColor(0,0,0,0); diff --git a/Arduino/McLighting/definitions.h b/Arduino/McLighting/definitions.h index 486e386..526d238 100644 --- a/Arduino/McLighting/definitions.h +++ b/Arduino/McLighting/definitions.h @@ -6,7 +6,8 @@ #define NUMLEDS 194 // Number of leds in the strip #define BUILTIN_LED 2 // ESP-12F has the built in LED on GPIO2, see https://github.com/esp8266/Arduino/issues/2192 #define BUTTON 14 // Input pin (14 / D5) for switching the LED strip on / off, connect this PIN to ground to trigger button. -#define BUTTON2 12 // Input pin (12 / D6) for read color data with RGB sensor, connect this PIN to ground to trigger button. +#define BUTTON_GY33 12 // Input pin (12 / D6) for read color data with RGB sensor, connect this PIN to ground to trigger button. +#define RGBW const char HOSTNAME[] = "ESPLightRGBW02"; // Friedly hostname @@ -16,7 +17,7 @@ const char HOSTNAME[] = "ESPLightRGBW02"; // Friedly hostname //#define ENABLE_MQTT // If defined, enable MQTT client code, see: https://github.com/toblum/McLighting/wiki/MQTT-API #define ENABLE_HOMEASSISTANT // If defined, enable Homeassistant integration, ENABLE_MQTT must be active #define ENABLE_BUTTON // If defined, enable button handling code, see: https://github.com/toblum/McLighting/wiki/Button-control -#define ENABLE_BUTTON2 // +#define ENABLE_BUTTON_GY33 // //#define MQTT_HOME_ASSISTANT_SUPPORT // If defined, use AMQTT and select Tools -> IwIP Variant -> Higher Bandwidth @@ -127,7 +128,7 @@ LEDState main_color = { 0, 255, 0, 0}; // Store the "main color" of the strip u // Button handling -#ifdef ENABLE_BUTTON || ENABLE_BUTTON2 +#ifdef ENABLE_BUTTON || ENABLE_BUTTON_GY33 boolean buttonState = false; #endif @@ -144,7 +145,7 @@ LEDState main_color = { 0, 255, 0, 0}; // Store the "main color" of the strip u byte prevKeyState = HIGH; // button is active low #endif -#ifdef ENABLE_BUTTON2 +#ifdef ENABLE_BUTTON_GY33 #define BTN_MODE_SHORT "STA| 1| 0|245|196|255| 0| 0| 0" // Static white #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 diff --git a/Arduino/McLighting/request_handlers.h b/Arduino/McLighting/request_handlers.h index 757a4a6..7affa22 100644 --- a/Arduino/McLighting/request_handlers.h +++ b/Arduino/McLighting/request_handlers.h @@ -1269,7 +1269,7 @@ void checkForRequests() { } #endif -#ifdef ENABLE_BUTTON2 +#ifdef ENABLE_BUTTON_GY33 void shortKeyPress2() { DBG_OUTPUT_PORT.printf("Short button2 press\n"); if (buttonState == false) { @@ -1365,7 +1365,7 @@ void checkForRequests() { if (millis() - keyPrevMillis2 >= keySampleIntervalMs2) { keyPrevMillis2 = millis(); - byte currKeyState2 = digitalRead(BUTTON2); + byte currKeyState2 = digitalRead(BUTTON_GY33); if ((prevKeyState2 == HIGH) && (currKeyState2 == LOW)) { // key goes from not pressed to pressed