Skip to content

Commit

Permalink
Change brightness transition to use quad easing function for smoother…
Browse files Browse the repository at this point in the history
… effects.
  • Loading branch information
DrA1ex committed Sep 28, 2024
1 parent c77bfbb commit b0d7f50
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/app/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ void Application::_app_loop() {
PWM_MAX_VALUE
);

uint16_t brightness = _brightness() * cubic_wave16(factor, PWM_MAX_VALUE) / PWM_MAX_VALUE;
uint16_t brightness = _brightness() * quad_wave16(factor, PWM_MAX_VALUE) / PWM_MAX_VALUE;
_led->set_brightness(brightness);
}
}
Expand All @@ -187,7 +187,7 @@ void Application::_app_loop() {
case AppState::TURNING_ON: {
uint16_t factor = std::min<unsigned long>(PWM_MAX_VALUE,
(millis() - _state_change_time) * PWM_MAX_VALUE / sys_config().power_change_timeout);
uint16_t brightness = (uint16_t) _brightness() * ease_cubic16(factor, PWM_MAX_VALUE) / PWM_MAX_VALUE;
uint16_t brightness = (uint16_t) _brightness() * ease_quad16(factor, PWM_MAX_VALUE) / PWM_MAX_VALUE;
_led->set_brightness(brightness);

if (factor == PWM_MAX_VALUE) change_state(AppState::STAND_BY);
Expand All @@ -197,7 +197,7 @@ void Application::_app_loop() {
case AppState::TURNING_OFF: {
uint16_t factor = PWM_MAX_VALUE - std::min<unsigned long>(PWM_MAX_VALUE,
(millis() - _state_change_time) * PWM_MAX_VALUE / sys_config().power_change_timeout);
uint16_t brightness = (uint16_t) _brightness() * ease_cubic16(factor, PWM_MAX_VALUE) / PWM_MAX_VALUE;
uint16_t brightness = (uint16_t) _brightness() * ease_quad16(factor, PWM_MAX_VALUE) / PWM_MAX_VALUE;
_led->set_brightness(brightness);

if (factor == 0) change_state(AppState::STAND_BY);
Expand Down
4 changes: 1 addition & 3 deletions src/misc/night_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ void NightModeManager::handle_night(const NtpTime &ntp_time) {
uint16_t NightModeManager::get_brightness() const {
if (!is_night_time()) { return {}; }

const uint16_t brightness = smooth16(_config.brightness, _config.night_mode.brightness, _fade_factor);

return brightness;
return smooth16(_config.brightness, _config.night_mode.brightness, _fade_factor);
}

void NightModeManager::reset() {
Expand Down
15 changes: 11 additions & 4 deletions src/utils/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,20 @@ inline uint16_t tri_wave16(uint16_t value, uint16_t max_value) {
return std::min(max_value, (uint16_t) (value * 2));
}

inline uint16_t ease_cubic16(uint16_t value, uint16_t max_value) {
return (uint64_t) value * value * value / ((uint64_t) max_value * max_value);
inline uint16_t ease_quad16(uint16_t value, uint16_t max_value) {
if (max_value == 0) return 0;

if (value < max_value / 2) {
return 2 * (uint32_t) value * value / max_value;
} else {
uint64_t temp = 2 * max_value - 2 * value;
return max_value - temp * temp / (2 * max_value);
}
}

inline uint16_t cubic_wave16(uint16_t value, uint16_t max_value) {
inline uint16_t quad_wave16(uint16_t value, uint16_t max_value) {
auto v = tri_wave16(value, max_value);
return ease_cubic16(v, max_value);
return ease_quad16(v, max_value);
}

inline uint16_t map16(uint16_t value, uint16_t limit_src, uint16_t limit_dst) {
Expand Down

0 comments on commit b0d7f50

Please sign in to comment.