Skip to content

Commit

Permalink
Move MAX_MILLIWATTS out of pio file
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidSchinazi committed Dec 13, 2023
1 parent 5538321 commit 09eb583
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
8 changes: 0 additions & 8 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ monitor_port = ${this.upload_port}
lib_deps =
fastled/FastLED
build_flags =
-DMAX_MILLIWATTS=9200 # Default power limit for USB-powered devices is 9W
# Power levels used for MAX_MILLIWATTS are approximate.
# The FastLED power calculation algorithm is not entirely accurate,
# and probably not calibrated for the LED strips we use.

# Main ATOM Matrix production build.
[env:atom_matrix]
Expand Down Expand Up @@ -62,7 +58,6 @@ extends = env:atom_matrix
build_flags =
${env:atom_matrix.build_flags}
-DJL_DEV=1
'-UMAX_MILLIWATTS -DMAX_MILLIWATTS=4200' # Reduce power limit to 4W for host-connected development boards
'-UBOOT_NAME -DBOOT_NAME=DEV'

# ATOM Matrix debug build for stepping though code.
Expand Down Expand Up @@ -109,7 +104,6 @@ build_flags =
${env:atom_matrix.build_flags}
'-UJL_CONFIG -DJL_CONFIG=STAFF'
'-UBOOT_NAME -DBOOT_NAME=STAFF'
'-UMAX_MILLIWATTS -DMAX_MILLIWATTS=300' # Significantly reduce power limit since this is 24V and 6 LEDs per pixel.
-DBRIGHTER2=1

# ATOM Matrix production configuration for the captain hat.
Expand All @@ -127,7 +121,6 @@ build_flags =
${env:atom_matrix.build_flags}
'-UJL_CONFIG -DJL_CONFIG=ROPELIGHT'
'-UBOOT_NAME -DBOOT_NAME=ROPE'
'-UMAX_MILLIWATTS'

# ATOM Matrix production configuration for hammer.
[env:atom_matrix_hammer]
Expand Down Expand Up @@ -182,7 +175,6 @@ extends = env:core2aws
build_flags =
${env:core2aws.build_flags}
-DJL_DEV=1
'-UMAX_MILLIWATTS -DMAX_MILLIWATTS=4200' # Reduce power limit to 4W for host-connected development boards
'-UBOOT_NAME -DBOOT_NAME=GNTLT-DEV'

# M5Stamp Pico
Expand Down
36 changes: 27 additions & 9 deletions src/jazzlights/fastled_runner.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "jazzlights/fastled_runner.h"

#include "jazzlights/config.h"

#ifdef ARDUINO

#include "jazzlights/instrumentation.h"
Expand All @@ -8,6 +10,22 @@

namespace jazzlights {

#if JL_IS_CONFIG(STAFF)
// Significantly reduce power limit since this is 24V and 6 LEDs per pixel.
#define JL_MAX_MILLIWATTS 300
#elif JL_IS_CONFIG(ROPELIGHT)
// Ropelight is generally connected to an independent large power source.
#define JL_MAX_MILLIWATTS 0
#elif JL_DEV
// Reduce power limit to 4W for host-connected development to avoid overloading USB port.
#define JL_MAX_MILLIWATTS 4200
#else
// Default power limit for USB-powered devices is 9W.
#define JL_MAX_MILLIWATTS 9200
// Note that these power levels are approximate. The FastLED power calculation algorithm is not entirely accurate, and
// probably not calibrated for the LED strips we use.
#endif

// Index 0 is normally reserved by FreeRTOS for stream and message buffers. However, the default precompiled FreeRTOS
// kernel for arduino/esp-idf only allows a single notification, so we use index 0 here. We don't use stream or message
// buffers on this specific task so we should be fine.
Expand All @@ -27,23 +45,23 @@ void FastLedRunner::SendLedsToFastLed() {
SAVE_COUNT_POINT(LedPrintLoop);
if (!shouldWriteToAtLeastOne) { return; }

uint32_t brightness = getBrightness(); // May be reduced if this exceeds our power budget with the current pattern
uint32_t brightness = getBrightness(); // May be reduced if this exceeds our power budget with the current pattern.

#if MAX_MILLIWATTS
#if JL_MAX_MILLIWATTS > 0
uint32_t powerAtFullBrightness = 0;
for (auto& renderer : renderers_) { powerAtFullBrightness += renderer->GetPowerAtFullBrightness(); }
const uint32_t powerAtDesiredBrightness =
powerAtFullBrightness * brightness / 256; // Forecast power at our current desired brightness
player_->powerLimited = (powerAtDesiredBrightness > MAX_MILLIWATTS);
if (player_->powerLimited) { brightness = brightness * MAX_MILLIWATTS / powerAtDesiredBrightness; }
powerAtFullBrightness * brightness / 256; // Forecast power at our current desired brightness.
player_->powerLimited = (powerAtDesiredBrightness > JL_MAX_MILLIWATTS);
if (player_->powerLimited) { brightness = brightness * JL_MAX_MILLIWATTS / powerAtDesiredBrightness; }

jll_debug("pf%6u pd%5u bu%4u bs%4u mW%5u mA%5u%s", powerAtFullBrightness,
powerAtDesiredBrightness, // Full-brightness power, desired-brightness power
getBrightness(), brightness, // Desired and selected brightness
powerAtDesiredBrightness, // Full-brightness power, desired-brightness power.
getBrightness(), brightness, // Desired and selected brightness.
powerAtFullBrightness * brightness / 256,
powerAtFullBrightness * brightness / 256 / 5, // Selected power & current
powerAtFullBrightness * brightness / 256 / 5, // Selected power & current.
player_->powerLimited ? " (limited)" : "");
#endif // MAX_MILLIWATTS
#endif // JL_MAX_MILLIWATTS
SAVE_TIME_POINT(Brightness);

ledWriteStart();
Expand Down

0 comments on commit 09eb583

Please sign in to comment.