Files
ZephCore/zephcore/helpers/led_gate.c
T
ZephCore T096andClaude Sonnet 5 fc0723a8ec led: one shared PWM write helper, shorter comments
The heartbeat and activity LED each computed the PWM pulse inline.
zephcore_led_pwm_write() in led_gate.c now does it once.

Comments added for the LED work described branch history, dates and
release notes; keep only what the code needs. led_gate.c still said the
brightness was RAM-only, which is no longer true, and the server decode
comment gave the old 10% default.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-08 18:27:01 +02:00

101 lines
2.6 KiB
C

/*
* ZephCore - LED master gate
* Copyright (c) 2025 ZephCore
* SPDX-License-Identifier: MIT
*/
#include "led_gate.h"
#include <zephyr/drivers/pwm.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/atomic.h>
/* Atomic because the readers are not all on one thread: the heartbeat work
* handler runs on the system work queue, while the TX LED is driven from the
* dispatcher's transmit path. Writers are the main thread (boot / CLI) and the
* UI task. */
static atomic_t s_leds_disabled;
/*
* Weak: overridden by helpers/ui/ui_common.c in builds that have a UI, so a
* change made from the CLI also extinguishes a lit heartbeat LED immediately
* and syncs the UI's LED page. No-op in headless builds, where ui_common.c
* isn't compiled at all — which is exactly why the gate lives here and not
* there.
*/
__weak void zephcore_leds_ui_sync(bool disabled) { ARG_UNUSED(disabled); }
bool zephcore_leds_disabled(void)
{
return atomic_get(&s_leds_disabled) != 0;
}
void zephcore_leds_set_disabled(bool disabled)
{
atomic_set(&s_leds_disabled, disabled ? 1 : 0);
zephcore_leds_ui_sync(disabled);
}
/*
* Both modes default to 0, which every LEDS_* enum defines as the behaviour the
* firmware had before these settings existed. That matters beyond tidiness: a
* role whose boot path forgets to apply the pref, or a build with no CLI at
* all, still lands on the historical behaviour rather than something new.
*/
static atomic_t s_radio_mode; /* LEDS_RADIO_TX */
static atomic_t s_hb_mode; /* LEDS_HB_ALL */
static atomic_t s_radio_holds_pin;
uint8_t zephcore_leds_radio_mode(void)
{
return (uint8_t)atomic_get(&s_radio_mode);
}
void zephcore_leds_set_radio_mode(uint8_t mode)
{
atomic_set(&s_radio_mode, mode);
}
uint8_t zephcore_leds_hb_mode(void)
{
return (uint8_t)atomic_get(&s_hb_mode);
}
void zephcore_leds_set_hb_mode(uint8_t mode)
{
atomic_set(&s_hb_mode, mode);
}
bool zephcore_led_radio_holds_pin(void)
{
return atomic_get(&s_radio_holds_pin) != 0;
}
void zephcore_led_radio_hold_pin(bool held)
{
atomic_set(&s_radio_holds_pin, held ? 1 : 0);
}
static atomic_t s_led_brightness_pct = ATOMIC_INIT(ZEPHCORE_LED_DEFAULT_BRIGHTNESS_PCT);
uint8_t zephcore_led_brightness_pct(void)
{
return (uint8_t)atomic_get(&s_led_brightness_pct);
}
void zephcore_led_set_brightness_pct(uint8_t pct)
{
if (pct > 100) {
pct = 100;
}
atomic_set(&s_led_brightness_pct, pct);
}
void zephcore_led_pwm_write(const struct pwm_dt_spec *led, bool on)
{
uint32_t pulse = on ? (uint32_t)((uint64_t)led->period *
zephcore_led_brightness_pct() / 100)
: 0;
pwm_set_pulse_dt(led, pulse);
}