From c8e81310cc2ae9985e4a516c6bba1fe7c7cd9db8 Mon Sep 17 00:00:00 2001 From: Ollo Date: Wed, 4 Dec 2024 19:08:17 +0100 Subject: [PATCH] Refactored to single instance of Adafruit_NeoPixel --- include/controller.h | 8 ++++---- src/main.cpp | 33 +++++++++++++++++++++------------ 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/include/controller.h b/include/controller.h index d8fb967..614ad39 100644 --- a/include/controller.h +++ b/include/controller.h @@ -16,10 +16,10 @@ #define GPIO_DS18B20 D2 /**< One-Wire used for Dallas temperature sensor */ -#define GPIO_WS2812_PIN1 D3 -#define WS2812_LEDS1 1 +#define WS2812SINGLE_GPIO_PIN D3 +#define WS2812SINGLE_LEDS 1 -#define GPIO_WS2812_PIN2 D4 -#define WS2812_LEDS2 10 +#define WS2812STRIP_GPIO_PIN D4 +#define WS2812STRIP_LEDS 10 #endif /* End FANLEDCTL_PINS */ \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 75f6fa9..0646faa 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,7 +3,10 @@ #include #include #include "controller.h" - +/****************************************************************************** + * MAKROS + ******************************************************************************/ +#define MAX(x, y) (((x) > (y)) ? (x) : (y)) /****************************************************************************** * DEFINES @@ -20,8 +23,7 @@ /****************************************************************************** * LOCAL VARIABLES ******************************************************************************/ -Adafruit_NeoPixel* pPixelFan = NULL; -Adafruit_NeoPixel* pPixel = NULL; +Adafruit_NeoPixel pixels(MAX(WS2812SINGLE_LEDS, WS2812STRIP_GPIO_PIN), 0, NEO_RGB + NEO_KHZ800); OneWire oneWire(GPIO_DS18B20); DallasTemperature sensors(&oneWire); @@ -38,13 +40,13 @@ void setup() { pinMode(SIGNAL_PIN, INPUT); - pPixelFan = new Adafruit_NeoPixel(WS2812_LEDS1, GPIO_WS2812_PIN1, NEO_RGB + NEO_KHZ800); + pixels.setPin(WS2812SINGLE_GPIO_PIN); + pixels.begin(); + pixels.clear(); + pixels.setBrightness(100); + pixels.fill(pixels.Color(255,0,0)); + pixels.show(); - pPixelFan->begin(); - pPixelFan->clear(); - pPixelFan->fill(pPixelFan->Color(255,0,0)); - pPixelFan->setBrightness(100); - pPixelFan->show(); /* Initialize Temperature sensor */ sensors.begin(); @@ -54,6 +56,10 @@ void setup() { sensors.begin(); Serial.println("Reset 1-Wire Bus"); } + + pixels.fill(pixels.Color(0,255,0)); + pixels.setBrightness(50); + pixels.show(); } /** @@ -117,9 +123,12 @@ void loop() { fanSpeedPercent = (100 - MIN_FAN_SPEED_PERCENT) * (temp - MIN_TEMP) / (MAX_TEMP - MIN_TEMP) + MIN_FAN_SPEED_PERCENT; } - pPixelFan->fill(pPixelFan->Color(0,255,0)); - pPixelFan->setBrightness(fanSpeedPercent); - pPixelFan->show(); + pixels.setPin(WS2812STRIP_GPIO_PIN); + pixels.begin(); + pixels.clear(); + pixels.fill(pixels.Color(0,255,0)); + pixels.setBrightness(fanSpeedPercent); + pixels.show(); Serial.print(fanSpeedPercent); Serial.print("%;");