added NeoPixel support using Adafruit library

This commit is contained in:
Gareth Coleman 2024-04-22 14:42:52 +01:00
parent 45fd5e25ac
commit 5dd08e9533
3 changed files with 64 additions and 32 deletions

View File

@ -5,6 +5,11 @@
NCP5623 rgb; NCP5623 rgb;
#endif #endif
#ifdef HAS_NEOPIXEL
#include <graphics/NeoPixel.h>
Adafruit_NeoPixel pixels(NEOPIXEL_COUNT, NEOPIXEL_DATA, NEOPIXEL_TYPE);
#endif
#ifdef UNPHONE #ifdef UNPHONE
#include "unPhone.h" #include "unPhone.h"
extern unPhone unphone; extern unPhone unphone;
@ -33,7 +38,7 @@ class AmbientLightingThread : public concurrency::OSThread
return; return;
} }
#endif #endif
#if defined(HAS_NCP5623) || defined(UNPHONE) || defined(RGBLED_RED) #if defined(HAS_NCP5623) || defined(RGBLED_RED) || defined(HAS_NEOPIXEL) || defined(UNPHONE)
if (!moduleConfig.ambient_lighting.led_state) { if (!moduleConfig.ambient_lighting.led_state) {
LOG_DEBUG("AmbientLightingThread disabling due to moduleConfig.ambient_lighting.led_state OFF\n"); LOG_DEBUG("AmbientLightingThread disabling due to moduleConfig.ambient_lighting.led_state OFF\n");
disable(); disable();
@ -48,6 +53,11 @@ class AmbientLightingThread : public concurrency::OSThread
pinMode(RGBLED_RED, OUTPUT); pinMode(RGBLED_RED, OUTPUT);
pinMode(RGBLED_GREEN, OUTPUT); pinMode(RGBLED_GREEN, OUTPUT);
pinMode(RGBLED_BLUE, OUTPUT); pinMode(RGBLED_BLUE, OUTPUT);
#endif
#ifdef HAS_NEOPIXEL
pixels.begin(); // Initialise the pixel(s)
pixels.clear(); // Set all pixel colors to 'off'
pixels.setBrightness(moduleConfig.ambient_lighting.current);
#endif #endif
setLighting(); setLighting();
#endif #endif
@ -59,7 +69,7 @@ class AmbientLightingThread : public concurrency::OSThread
protected: protected:
int32_t runOnce() override int32_t runOnce() override
{ {
#if defined(HAS_NCP5623) || defined(UNPHONE) || defined(RGBLED_RED) #if defined(HAS_NCP5623) || defined(RGBLED_RED) || defined(HAS_NEOPIXEL) || defined(UNPHONE)
#ifdef HAS_NCP5623 #ifdef HAS_NCP5623
if (_type == ScanI2C::NCP5623 && moduleConfig.ambient_lighting.led_state) { if (_type == ScanI2C::NCP5623 && moduleConfig.ambient_lighting.led_state) {
#endif #endif
@ -86,10 +96,14 @@ class AmbientLightingThread : public concurrency::OSThread
moduleConfig.ambient_lighting.current, moduleConfig.ambient_lighting.red, moduleConfig.ambient_lighting.green, moduleConfig.ambient_lighting.current, moduleConfig.ambient_lighting.red, moduleConfig.ambient_lighting.green,
moduleConfig.ambient_lighting.blue); moduleConfig.ambient_lighting.blue);
#endif #endif
#ifdef UNPHONE #ifdef HAS_NEOPIXEL
unphone.rgb(moduleConfig.ambient_lighting.red, moduleConfig.ambient_lighting.green, moduleConfig.ambient_lighting.blue); pixels.fill(pixels.Color(moduleConfig.ambient_lighting.red, moduleConfig.ambient_lighting.green,
LOG_DEBUG("Initializing unPhone Ambient lighting w/ red=%d, green=%d, blue=%d\n", moduleConfig.ambient_lighting.red, moduleConfig.ambient_lighting.blue),
moduleConfig.ambient_lighting.green, moduleConfig.ambient_lighting.blue); 0, NEOPIXEL_COUNT);
pixels.show();
LOG_DEBUG("Initializing NeoPixel Ambient lighting w/ brightness(current)=%d, red=%d, green=%d, blue=%d\n",
moduleConfig.ambient_lighting.current, moduleConfig.ambient_lighting.red, moduleConfig.ambient_lighting.green,
moduleConfig.ambient_lighting.blue);
#endif #endif
#ifdef RGBLED_CA #ifdef RGBLED_CA
analogWrite(RGBLED_RED, 255 - moduleConfig.ambient_lighting.red); analogWrite(RGBLED_RED, 255 - moduleConfig.ambient_lighting.red);
@ -103,6 +117,11 @@ class AmbientLightingThread : public concurrency::OSThread
analogWrite(RGBLED_BLUE, moduleConfig.ambient_lighting.blue); analogWrite(RGBLED_BLUE, moduleConfig.ambient_lighting.blue);
LOG_DEBUG("Initializing Ambient lighting RGB Common Cathode w/ red=%d, green=%d, blue=%d\n", LOG_DEBUG("Initializing Ambient lighting RGB Common Cathode w/ red=%d, green=%d, blue=%d\n",
moduleConfig.ambient_lighting.red, moduleConfig.ambient_lighting.green, moduleConfig.ambient_lighting.blue); moduleConfig.ambient_lighting.red, moduleConfig.ambient_lighting.green, moduleConfig.ambient_lighting.blue);
#endif
#ifdef UNPHONE
unphone.rgb(moduleConfig.ambient_lighting.red, moduleConfig.ambient_lighting.green, moduleConfig.ambient_lighting.blue);
LOG_DEBUG("Initializing unPhone Ambient lighting w/ red=%d, green=%d, blue=%d\n", moduleConfig.ambient_lighting.red,
moduleConfig.ambient_lighting.green, moduleConfig.ambient_lighting.blue);
#endif #endif
} }
}; };

View File

@ -607,9 +607,7 @@ void setup()
} }
#endif #endif
#ifdef UNPHONE #if defined(HAS_NEOPIXEL) || defined(UNPHONE) || defined(RGBLED_RED)
ambientLightingThread = new AmbientLightingThread(ScanI2C::DeviceType::NONE);
#elif defined(RGBLED_RED)
ambientLightingThread = new AmbientLightingThread(ScanI2C::DeviceType::NONE); ambientLightingThread = new AmbientLightingThread(ScanI2C::DeviceType::NONE);
#elif !defined(ARCH_PORTDUINO) && !defined(ARCH_STM32WL) #elif !defined(ARCH_PORTDUINO) && !defined(ARCH_STM32WL)
if (rgb_found.type != ScanI2C::DeviceType::NONE) { if (rgb_found.type != ScanI2C::DeviceType::NONE) {

View File

@ -28,12 +28,16 @@
#include <graphics/RAKled.h> #include <graphics/RAKled.h>
#endif #endif
#ifdef HAS_NEOPIXEL
#include <graphics/NeoPixel.h>
#endif
#ifdef UNPHONE #ifdef UNPHONE
#include "unPhone.h" #include "unPhone.h"
extern unPhone unphone; extern unPhone unphone;
#endif #endif
#if defined(HAS_NCP5623) || defined(UNPHONE) || defined(RGBLED_RED) #if defined(HAS_NCP5623) || defined(RGBLED_RED) || defined(HAS_NEOPIXEL) || defined(UNPHONE)
uint8_t red = 0; uint8_t red = 0;
uint8_t green = 0; uint8_t green = 0;
uint8_t blue = 0; uint8_t blue = 0;
@ -115,7 +119,7 @@ int32_t ExternalNotificationModule::runOnce()
millis()) { millis()) {
getExternal(2) ? setExternalOff(2) : setExternalOn(2); getExternal(2) ? setExternalOff(2) : setExternalOn(2);
} }
#if defined(HAS_NCP5623) || defined(UNPHONE) || defined(RGBLED_RED) #if defined(HAS_NCP5623) || defined(RGBLED_RED) || defined(HAS_NEOPIXEL) || defined(UNPHONE)
red = (colorState & 4) ? brightnessValues[brightnessIndex] : 0; // Red enabled on colorState = 4,5,6,7 red = (colorState & 4) ? brightnessValues[brightnessIndex] : 0; // Red enabled on colorState = 4,5,6,7
green = (colorState & 2) ? brightnessValues[brightnessIndex] : 0; // Green enabled on colorState = 2,3,6,7 green = (colorState & 2) ? brightnessValues[brightnessIndex] : 0; // Green enabled on colorState = 2,3,6,7
blue = (colorState & 1) ? (brightnessValues[brightnessIndex] * 1.5) : 0; // Blue enabled on colorState = 1,3,5,7 blue = (colorState & 1) ? (brightnessValues[brightnessIndex] * 1.5) : 0; // Blue enabled on colorState = 1,3,5,7
@ -124,9 +128,6 @@ int32_t ExternalNotificationModule::runOnce()
rgb.setColor(red, green, blue); rgb.setColor(red, green, blue);
} }
#endif #endif
#ifdef UNPHONE
unphone.rgb(red, green, blue);
#endif
#ifdef RGBLED_CA #ifdef RGBLED_CA
analogWrite(RGBLED_RED, 255 - red); // CA type needs reverse logic analogWrite(RGBLED_RED, 255 - red); // CA type needs reverse logic
analogWrite(RGBLED_GREEN, 255 - green); analogWrite(RGBLED_GREEN, 255 - green);
@ -135,6 +136,13 @@ int32_t ExternalNotificationModule::runOnce()
analogWrite(RGBLED_RED, red); analogWrite(RGBLED_RED, red);
analogWrite(RGBLED_GREEN, green); analogWrite(RGBLED_GREEN, green);
analogWrite(RGBLED_BLUE, blue); analogWrite(RGBLED_BLUE, blue);
#endif
#ifdef HAS_NEOPIXEL
pixels.fill(pixels.Color(red, green, blue), 0, NEOPIXEL_COUNT);
pixels.show();
#endif
#ifdef UNPHONE
unphone.rgb(red, green, blue);
#endif #endif
if (ascending) { // fade in if (ascending) { // fade in
brightnessIndex++; brightnessIndex++;
@ -220,9 +228,6 @@ void ExternalNotificationModule::setExternalOn(uint8_t index)
rgb.setColor(red, green, blue); rgb.setColor(red, green, blue);
} }
#endif #endif
#ifdef UNPHONE
unphone.rgb(red, green, blue);
#endif
#ifdef RGBLED_CA #ifdef RGBLED_CA
analogWrite(RGBLED_RED, 255 - red); // CA type needs reverse logic analogWrite(RGBLED_RED, 255 - red); // CA type needs reverse logic
analogWrite(RGBLED_GREEN, 255 - green); analogWrite(RGBLED_GREEN, 255 - green);
@ -232,6 +237,13 @@ void ExternalNotificationModule::setExternalOn(uint8_t index)
analogWrite(RGBLED_GREEN, green); analogWrite(RGBLED_GREEN, green);
analogWrite(RGBLED_BLUE, blue); analogWrite(RGBLED_BLUE, blue);
#endif #endif
#ifdef HAS_NEOPIXEL
pixels.fill(pixels.Color(red, green, blue), 0, NEOPIXEL_COUNT);
pixels.show();
#endif
#ifdef UNPHONE
unphone.rgb(red, green, blue);
#endif
#ifdef T_WATCH_S3 #ifdef T_WATCH_S3
drv.go(); drv.go();
#endif #endif
@ -260,33 +272,31 @@ void ExternalNotificationModule::setExternalOff(uint8_t index)
break; break;
} }
#if defined(HAS_NCP5623) || defined(RGBLED_RED) || defined(HAS_NEOPIXEL) || defined(UNPHONE)
red = 0;
green = 0;
blue = 0;
#ifdef HAS_NCP5623 #ifdef HAS_NCP5623
if (rgb_found.type == ScanI2C::NCP5623) { if (rgb_found.type == ScanI2C::NCP5623) {
red = 0;
green = 0;
blue = 0;
rgb.setColor(red, green, blue); rgb.setColor(red, green, blue);
} }
#endif #endif
#ifdef UNPHONE
red = 0;
green = 0;
blue = 0;
unphone.rgb(red, green, blue);
#endif
#ifdef RGBLED_RED
red = 0;
green = 0;
blue = 0;
#ifdef RGBLED_CA #ifdef RGBLED_CA
analogWrite(RGBLED_RED, 255 - red); // CA type needs reverse logic analogWrite(RGBLED_RED, 255 - red); // CA type needs reverse logic
analogWrite(RGBLED_GREEN, 255 - green); analogWrite(RGBLED_GREEN, 255 - green);
analogWrite(RGBLED_BLUE, 255 - blue); analogWrite(RGBLED_BLUE, 255 - blue);
#else #elif defined(RGBLED_RED)
analogWrite(RGBLED_RED, red); analogWrite(RGBLED_RED, red);
analogWrite(RGBLED_GREEN, green); analogWrite(RGBLED_GREEN, green);
analogWrite(RGBLED_BLUE, blue); analogWrite(RGBLED_BLUE, blue);
#endif #endif
#ifdef HAS_NEOPIXEL
pixels.fill(pixels.Color(red, green, blue), 0, NEOPIXEL_COUNT);
pixels.show();
#endif
#ifdef UNPHONE
unphone.rgb(red, green, blue);
#endif
#endif #endif
#ifdef T_WATCH_S3 #ifdef T_WATCH_S3
@ -397,6 +407,11 @@ ExternalNotificationModule::ExternalNotificationModule()
analogWrite(RGBLED_RED, 255); // with a common anode type, logic is reversed analogWrite(RGBLED_RED, 255); // with a common anode type, logic is reversed
analogWrite(RGBLED_GREEN, 255); // so we want to initialise with lights off analogWrite(RGBLED_GREEN, 255); // so we want to initialise with lights off
analogWrite(RGBLED_BLUE, 255); analogWrite(RGBLED_BLUE, 255);
#endif
#ifdef HAS_NEOPIXEL
pixels.begin(); // Initialise the pixel(s)
pixels.clear(); // Set all pixel colors to 'off'
pixels.setBrightness(moduleConfig.ambient_lighting.current);
#endif #endif
} else { } else {
LOG_INFO("External Notification Module Disabled\n"); LOG_INFO("External Notification Module Disabled\n");
@ -578,4 +593,4 @@ void ExternalNotificationModule::handleSetRingtone(const char *from_msg)
if (changed) { if (changed) {
nodeDB->saveProto(rtttlConfigFile, meshtastic_RTTTLConfig_size, &meshtastic_RTTTLConfig_msg, &rtttlConfig); nodeDB->saveProto(rtttlConfigFile, meshtastic_RTTTLConfig_size, &meshtastic_RTTTLConfig_msg, &rtttlConfig);
} }
} }