Skip to content

Commit

Permalink
optimization: only use "float" math functions
Browse files Browse the repository at this point in the history
- saves 5KB flash and some RAM
-allow to build with -D WLED_USE_UNREAL_MATH, to restore old behaviour and save another 6KB flash
  • Loading branch information
softhack007 committed Oct 6, 2023
1 parent 6fdd182 commit 15cea2b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 2 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ lib_deps = ${esp8266.lib_deps}
; board_build.ldscript = ${common.ldscript_1m128k}
; build_unflags = ${common.build_unflags}
; build_flags = ${common.build_flags_esp8266} -D WLED_RELEASE_NAME=ESP01 -D WLED_DISABLE_OTA
; ; -D WLED_USE_UNREAL_MATH ;; may cause wrong sunset/sunrise times, but saves 7064 bytes FLASH and 975 bytes RAM
; lib_deps = ${esp8266.lib_deps}

[env:esp07]
Expand Down Expand Up @@ -691,6 +692,7 @@ build_flags = ${common.build_flags} ${esp32s2.build_flags} #-D WLED_RELEASE_NAME
-DARDUINO_USB_DFU_ON_BOOT=0
-DLOLIN_WIFI_FIX ; seems to work much better with this
-D WLED_USE_PSRAM
; -D WLED_USE_UNREAL_MATH ;; may cause wrong sunset/sunrise times, but saves 6792 bytes FLASH
-D WLED_WATCHDOG_TIMEOUT=0
-D CONFIG_ASYNC_TCP_USE_WDT=0
-D LEDPIN=16
Expand Down
17 changes: 16 additions & 1 deletion wled00/ntp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@
#include "wled.h"
#include "fcn_declare.h"

// on esp8266, building with `-D WLED_USE_UNREAL_MATH` saves around 7Kb flash and 1KB RAM
// warning: causes errors in sunset calculations, see #3400
#if defined(WLED_USE_UNREAL_MATH)
#define sinf sin_t
#define asinf asin_t
#define cosf cos_t
#define acosf acos_t
#define tanf tan_t
#define atanf atan_t
#define fmodf fmod_t
#define floorf floor_t
#else
#include <math.h>
#endif

/*
* Acquires time from NTP server
*/
Expand Down Expand Up @@ -433,7 +448,7 @@ int getSunriseUTC(int year, int month, int day, float lat, float lon, bool sunse
float L = fmodf(M + (1.916f * sinf(DEG_TO_RAD*M)) + (0.02f * sinf(2*DEG_TO_RAD*M)) + 282.634f, 360.0f);

//5a. calculate the Sun's right ascension
float RA = fmodf(RAD_TO_DEG*atan(0.91764f * tan(DEG_TO_RAD*L)), 360.0f);
float RA = fmodf(RAD_TO_DEG*atanf(0.91764f * tanf(DEG_TO_RAD*L)), 360.0f);

//5b. right ascension value needs to be in the same quadrant as L
float Lquadrant = floorf( L/90) * 90;
Expand Down

0 comments on commit 15cea2b

Please sign in to comment.