Skip to content

Commit 1123d85

Browse files
committed
removed float math and log(), added max bit width
1 parent 5c247d2 commit 1123d85

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

wled00/bus_manager.cpp

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,6 @@ uint8_t realtimeBroadcast(uint8_t type, IPAddress client, uint16_t length, byte
4040
#define DEBUG_PRINTF_P(x...)
4141
#endif
4242

43-
// ESP8266 has 1 MHz clock
44-
#ifdef ESP8266
45-
#define CLOCK_FREQUENCY 1e6f
46-
#else
47-
// Use XTAL clock if possible to avoid timer frequency error when setting APB clock < 80 Mhz
48-
// https://github.com/espressif/arduino-esp32/blob/2.0.2/cores/esp32/esp32-hal-ledc.c
49-
#ifdef SOC_LEDC_SUPPORT_XTAL_CLOCK
50-
#define CLOCK_FREQUENCY 40e6f
51-
#else
52-
#define CLOCK_FREQUENCY 80e6f
53-
#endif
54-
#endif
55-
5643
//color mangling macros
5744
#define RGBW32(r,g,b,w) (uint32_t((byte(w) << 24) | (byte(r) << 16) | (byte(g) << 8) | (byte(b))))
5845
#define R(c) (byte((c) >> 16))
@@ -398,7 +385,7 @@ BusPwm::BusPwm(BusConfig &bc)
398385
unsigned numPins = NUM_PWM_PINS(bc.type);
399386
_frequency = bc.frequency ? bc.frequency : WLED_PWM_FREQ;
400387
// duty cycle resolution (_depth) can be extracted from this formula: CLOCK_FREQUENCY > _frequency * 2^_depth
401-
_depth = uint8_t(log((float)CLOCK_FREQUENCY / (float)_frequency) / log(2.0));
388+
for (_depth=MAX_BIT_WIDTH; _depth>8; _depth--) if (((uint32_t(CLOCK_FREQUENCY)/_frequency)>>_depth) > 0) break;
402389

403390
#ifdef ESP8266
404391
analogWriteRange((1<<_depth)-1);

wled00/const.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,35 @@
521521
#endif
522522
#endif
523523

524+
#ifndef CLOCK_FREQUENCY
525+
#ifdef ESP8266
526+
// 1 MHz clock
527+
#define CLOCK_FREQUENCY 1e6f
528+
#else
529+
// Use XTAL clock if possible to avoid timer frequency error when setting APB clock < 80 Mhz
530+
// https://github.com/espressif/arduino-esp32/blob/2.0.2/cores/esp32/esp32-hal-ledc.c
531+
#ifdef SOC_LEDC_SUPPORT_XTAL_CLOCK
532+
#define CLOCK_FREQUENCY 40e6f
533+
#else
534+
#define CLOCK_FREQUENCY 80e6f
535+
#endif
536+
#endif
537+
#endif
538+
539+
#ifndef MAX_BIT_WIDTH
540+
#ifdef ESP8266
541+
#define MAX_BIT_WIDTH 10
542+
#else
543+
#ifdef SOC_LEDC_TIMER_BIT_WIDE_NUM
544+
// C6/H2/P4: 20 bit, S2/S3/C2/C3: 14 bit
545+
#define MAX_BIT_WIDTH SOC_LEDC_TIMER_BIT_WIDE_NUM
546+
#else
547+
// ESP32: 32 bit
548+
#define MAX_BIT_WIDTH 20
549+
#endif
550+
#endif
551+
#endif
552+
524553
#define TOUCH_THRESHOLD 32 // limit to recognize a touch, higher value means more sensitive
525554

526555
// Size of buffer for API JSON object (increase for more segments)

0 commit comments

Comments
 (0)