Skip to content

Commit a8c6fa4

Browse files
committed
Update to the latest ESP32 core
As a consiquance removed the analogueWrite (not compatible) lib and moved over to the built in ledc. Also fixed #352 LED Brightness slider bug.
1 parent 54db4e8 commit a8c6fa4

File tree

3 files changed

+70
-79
lines changed

3 files changed

+70
-79
lines changed

platformio.ini

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,14 @@ default_envs = openevse_wifi_v1
3131
[common]
3232
version = -D BUILD_TAG=4.1.7
3333
lib_deps =
34-
bblanchon/ArduinoJson@6.19.1
34+
bblanchon/ArduinoJson@6.20.1
3535
jeremypoulter/[email protected]
3636
jeremypoulter/Micro [email protected]
3737
jeremypoulter/[email protected]
3838
jeremypoulter/[email protected]
3939
jeremypoulter/[email protected]
4040
jeremypoulter/[email protected]
4141
jeremypoulter/[email protected]
42-
erropix/ESP32 [email protected]
43-
4442
4543
4644
lib_ignore = WebSockets ; ArduinoOcpp: don't compile built-in WS library
@@ -98,7 +96,7 @@ build_flags =
9896
build_partitions = min_spiffs.csv
9997
build_partitions_debug = min_spiffs_debug.csv
10098

101-
neopixel_lib = adafruit/Adafruit NeoPixel@1.7.0
99+
neopixel_lib = adafruit/Adafruit NeoPixel@1.11.0
102100

103101

104102
[env]
@@ -107,7 +105,7 @@ neopixel_lib = adafruit/Adafruit [email protected]
107105
#platform = https://github.com/platformio/platform-espressif32.git#feature/stage
108106
#platform = https://github.com/platformio/platform-espressif32.git#develop
109107
#platform = [email protected]
110-
platform = espressif32@5.0.0
108+
platform = espressif32@6.0.1
111109
#framework = arduino, espidf
112110
framework = arduino
113111
lib_deps = ${common.lib_deps}

src/LedManagerTask.cpp

Lines changed: 67 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,24 @@
66
#include <openevse.h>
77

88
#ifdef ESP32
9-
#include <analogWrite.h>
9+
//#include <analogWrite.h>
10+
#if defined(RED_LED) && defined(GREEN_LED) && defined(BLUE_LED)
11+
#ifndef RED_LEDC_CHANNEL
12+
#define RED_LEDC_CHANNEL 1
13+
#endif
14+
#ifndef GREEN_LEDC_CHANNEL
15+
#define GREEN_LEDC_CHANNEL 2
16+
#endif
17+
#ifndef BLUE_LEDC_CHANNEL
18+
#define BLUE_LEDC_CHANNEL 3
19+
#endif
20+
#ifndef LEDC_FREQUENCY
21+
#define LEDC_FREQUENCY 5000
22+
#endif
23+
#ifndef LEDC_RESOLUTION
24+
#define LEDC_RESOLUTION 8
25+
#endif
26+
#endif
1027
#endif
1128

1229
#include "debug.h"
@@ -29,7 +46,7 @@ Adafruit_NeoPixel strip = Adafruit_NeoPixel(NEO_PIXEL_LENGTH, NEO_PIXEL_PIN, NEO
2946
#if defined(RED_LED) && defined(GREEN_LED) && defined(BLUE_LED)
3047

3148
// https://learn.adafruit.com/led-tricks-gamma-correction/the-quick-fix
32-
const uint8_t PROGMEM gamma8[] = {
49+
const uint8_t gamma8[] = {
3350
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3451
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
3552
1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
@@ -55,10 +72,13 @@ const uint8_t PROGMEM gamma8[] = {
5572
#define WIFI_BUTTON_SHARE_LED WIFI_LED
5673
#elif defined(RED_LED) && RED_LED == WIFI_BUTTON
5774
#define WIFI_BUTTON_SHARE_LED RED_LED
75+
#define WIFI_BUTTON_SHARE_LEDC_CHANNEL RED_LEDC_CHANNEL
5876
#elif defined(GREEN_LED) && GREEN_LED == WIFI_BUTTON
5977
#define WIFI_BUTTON_SHARE_LED GREEN_LED
78+
#define WIFI_BUTTON_SHARE_LEDC_CHANNEL GREEN_LEDC_CHANNEL
6079
#elif defined(BLUE_LED) && BLUE_LED == WIFI_BUTTON
6180
#define WIFI_BUTTON_SHARE_LED BLUE_LED
81+
#define WIFI_BUTTON_SHARE_LLEDC_CHANNELBLUE_LEDC_CHANNEL
6282
#endif
6383

6484
#endif
@@ -114,12 +134,13 @@ void LedManagerTask::setup()
114134

115135
#if defined(RED_LED) && defined(GREEN_LED) && defined(BLUE_LED)
116136
DBUGF("Initialising RGB LEDs, %d, %d, %d", RED_LED, GREEN_LED, BLUE_LED);
117-
pinMode(RED_LED, OUTPUT);
118-
digitalWrite(RED_LED, LOW);
119-
pinMode(GREEN_LED, OUTPUT);
120-
digitalWrite(GREEN_LED, LOW);
121-
pinMode(BLUE_LED, OUTPUT);
122-
digitalWrite(BLUE_LED, LOW);
137+
// configure LED PWM functionalitites
138+
ledcSetup(RED_LEDC_CHANNEL, LEDC_FREQUENCY, LEDC_RESOLUTION);
139+
ledcAttachPin(RED_LED, RED_LEDC_CHANNEL);
140+
ledcSetup(GREEN_LEDC_CHANNEL, LEDC_FREQUENCY, LEDC_RESOLUTION);
141+
ledcAttachPin(GREEN_LED, GREEN_LEDC_CHANNEL);
142+
ledcSetup(BLUE_LEDC_CHANNEL, LEDC_FREQUENCY, LEDC_RESOLUTION);
143+
ledcAttachPin(BLUE_LED, BLUE_LEDC_CHANNEL);
123144
#endif
124145

125146
#ifdef WIFI_LED
@@ -320,41 +341,7 @@ int LedManagerTask::fadeLed(int fadeValue, int FadeDir)
320341
#if RGB_LED
321342
void LedManagerTask::setAllRGB(uint8_t red, uint8_t green, uint8_t blue)
322343
{
323-
DBUG("LED R:");
324-
DBUG(red);
325-
DBUG(" G:");
326-
DBUG(green);
327-
DBUG(" B:");
328-
DBUGLN(blue);
329-
330-
if(brightness) { // See notes in setBrightness()
331-
red = (red * brightness) >> 8;
332-
green = (green * brightness) >> 8;
333-
blue = (blue * brightness) >> 8;
334-
}
335-
336-
#if defined(NEO_PIXEL_PIN) && defined(NEO_PIXEL_LENGTH)
337-
uint32_t col = strip.gamma32(strip.Color(red, green, blue));
338-
DBUGVAR(col, HEX);
339-
strip.fill(col);
340-
strip.show();
341-
#endif
342-
343-
#if defined(RED_LED) && defined(GREEN_LED) && defined(BLUE_LED)
344-
analogWrite(RED_LED, pgm_read_byte(&gamma8[red]));
345-
analogWrite(GREEN_LED, pgm_read_byte(&gamma8[green]));
346-
analogWrite(BLUE_LED, pgm_read_byte(&gamma8[blue]));
347-
348-
#ifdef WIFI_BUTTON_SHARE_LED
349-
#if RED_LED == WIFI_BUTTON_SHARE_LED
350-
buttonShareState = red;
351-
#elif GREEN_LED == WIFI_BUTTON_SHARE_LED
352-
buttonShareState = green;
353-
#elif BLUE_LED == WIFI_BUTTON_SHARE_LED
354-
buttonShareState = blue;
355-
#endif
356-
#endif
357-
#endif
344+
setEvseAndWifiRGB(red, green, blue, red, green, blue);
358345
}
359346
#endif
360347

@@ -384,6 +371,20 @@ void LedManagerTask::setEvseAndWifiRGB(uint8_t evseRed, uint8_t evseGreen, uint8
384371
wifiBlue = (wifiBlue * brightness) >> 8;
385372
}
386373

374+
DBUG("EVSE LED R:");
375+
DBUG(evseRed);
376+
DBUG(" G:");
377+
DBUG(evseGreen);
378+
DBUG(" B:");
379+
DBUGLN(evseBlue);
380+
381+
DBUG("WiFi LED R:");
382+
DBUG(wifiRed);
383+
DBUG(" G:");
384+
DBUG(wifiGreen);
385+
DBUG(" B:");
386+
DBUGLN(wifiBlue);
387+
387388
#if defined(NEO_PIXEL_PIN) && defined(NEO_PIXEL_LENGTH)
388389
uint32_t col = strip.gamma32(strip.Color(evseRed, evseGreen, evseBlue));
389390
DBUGVAR(col, HEX);
@@ -393,18 +394,24 @@ void LedManagerTask::setEvseAndWifiRGB(uint8_t evseRed, uint8_t evseGreen, uint8
393394
#endif
394395

395396
#if defined(RED_LED) && defined(GREEN_LED) && defined(BLUE_LED)
396-
analogWrite(RED_LED, pgm_read_byte(&gamma8[wifiRed]));
397-
analogWrite(GREEN_LED, pgm_read_byte(&gamma8[wifiGreen]));
398-
analogWrite(BLUE_LED, pgm_read_byte(&gamma8[wifiBlue]));
397+
398+
DBUGVAR(gamma8[wifiRed]);
399+
DBUGVAR(gamma8[wifiGreen]);
400+
DBUGVAR(gamma8[wifiBlue]);
401+
402+
ledcWrite(RED_LEDC_CHANNEL, gamma8[wifiRed]);
403+
ledcWrite(GREEN_LEDC_CHANNEL, gamma8[wifiGreen]);
404+
ledcWrite(BLUE_LEDC_CHANNEL, gamma8[wifiBlue]);
399405

400406
#ifdef WIFI_BUTTON_SHARE_LED
401407
#if RED_LED == WIFI_BUTTON_SHARE_LED
402-
buttonShareState = wifiRed;
408+
buttonShareState = gamma8[wifiRed];
403409
#elif GREEN_LED == WIFI_BUTTON_SHARE_LED
404-
buttonShareState = wifiGreen;
410+
buttonShareState = gamma8[wifiGreen];
405411
#elif BLUE_LED == WIFI_BUTTON_SHARE_LED
406-
buttonShareState = wifiBlue;
412+
buttonShareState = gamma8[wifiBlue];
407413
#endif
414+
DBUGVAR(buttonShareState);
408415
#endif
409416
#endif
410417
}
@@ -507,34 +514,26 @@ void LedManagerTask::setWifiMode(bool client, bool connected)
507514
int LedManagerTask::getButtonPressed()
508515
{
509516
#if defined(WIFI_BUTTON_SHARE_LED)
510-
#ifdef ESP32
511-
int channel = analogWriteChannel(WIFI_BUTTON_SHARE_LED);
512-
if(-1 != channel) {
513-
ledcDetachPin(WIFI_BUTTON_SHARE_LED);
514-
}
517+
#ifdef RGB_LEDC_CHANNEL
518+
ledcDetachPin(WIFI_BUTTON_SHARE_LED);
519+
#else
520+
digitalWrite(WIFI_BUTTON_SHARE_LED, HIGH);
515521
#endif
516522

517-
digitalWrite(WIFI_BUTTON_SHARE_LED, HIGH);
518523
pinMode(WIFI_BUTTON_SHARE_LED, WIFI_BUTTON_PRESSED_PIN_MODE);
519524
#endif
520525

521526
// Pressing the boot button for 5 seconds will turn on AP mode, 10 seconds will factory reset
522527
int button = digitalRead(WIFI_BUTTON);
523528

524529
#if defined(WIFI_BUTTON_SHARE_LED)
525-
pinMode(WIFI_BUTTON, OUTPUT);
526-
527-
#ifdef ESP32
528-
if(-1 != channel) {
529-
ledcAttachPin(WIFI_BUTTON_SHARE_LED, channel);
530-
}
530+
#ifdef WIFI_BUTTON_SHARE_LEDC_CHANNEL
531+
ledcAttachPin(WIFI_BUTTON_SHARE_LED, WIFI_BUTTON_SHARE_LEDC_CHANNEL);
532+
ledcWrite(WIFI_BUTTON_SHARE_LED, buttonShareState);
533+
#else
534+
pinMode(WIFI_BUTTON_SHARE_LED, OUTPUT);
535+
digitalWrite(WIFI_BUTTON_SHARE_LED, buttonShareState ? HIGH : LOW);
531536
#endif
532-
533-
if(0 == buttonShareState || 255 == buttonShareState) {
534-
digitalWrite(WIFI_BUTTON_SHARE_LED, buttonShareState ? HIGH : LOW);
535-
} else {
536-
analogWrite(WIFI_BUTTON_SHARE_LED, buttonShareState);
537-
}
538537
#endif
539538

540539
return button;
@@ -548,11 +547,7 @@ void LedManagerTask::setBrightness(uint8_t brightness)
548547
// adding 1 here may (intentionally) roll over...so 0 = max brightness
549548
// (color values are interpreted ltimingiterally; no scaling), 1 = min
550549
// brightness (off), 255 = just below max brightness.
551-
this->brightness = brightness - 1;
552-
553-
//#if defined(NEO_PIXEL_PIN) && defined(NEO_PIXEL_LENGTH)
554-
// strip.setBrightness(LED_DEFAULT_BRIGHTNESS);
555-
//#endif
550+
this->brightness = brightness + 1;
556551

557552
DBUGVAR(this->brightness);
558553

src/LedManagerTask.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ class LedManagerTask : public MicroTasks::Task
4545

4646
#if RGB_LED
4747
void setAllRGB(uint8_t red, uint8_t green, uint8_t blue);
48-
#endif
49-
#if RGB_LED
5048
void setEvseAndWifiRGB(uint8_t evseRed, uint8_t evseGreen, uint8_t evseBlue, uint8_t wifiRed, uint8_t wifiGreen, uint8_t wifiBlue);
5149
#endif
5250

0 commit comments

Comments
 (0)