Skip to content

Commit af6f9ba

Browse files
modules: led: gather global variables in a common struct
Gather led state variables into a struct. Signed-off-by: Giacomo Dematteis <giacomo.dematteis@nordicsemi.no>
1 parent 206c0aa commit af6f9ba

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

app/src/modules/led/led.c

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,15 @@ ZBUS_CHAN_DEFINE(LED_CHAN,
5757
ZBUS_CHAN_ADD_OBS(LED_CHAN, led, 0);
5858

5959
static struct k_work_delayable blink_work;
60-
static struct led_msg current_led_state;
61-
static bool led_is_on;
62-
static int repetitions;
60+
61+
/* Structure to hold all LED state variables */
62+
struct led_state {
63+
struct led_msg current_state;
64+
bool is_on;
65+
int repetitions;
66+
};
67+
68+
static struct led_state led_state;
6369
static void blink_timer_handler(struct k_work *work);
6470

6571
static int pwm_out(const struct led_msg *led_msg, bool force_off)
@@ -109,28 +115,28 @@ static void blink_timer_handler(struct k_work *work)
109115

110116
ARG_UNUSED(work);
111117

112-
led_is_on = !led_is_on;
118+
led_state.is_on = !led_state.is_on;
113119

114120
/* Update LED state */
115-
err = pwm_out(&current_led_state, !led_is_on);
121+
err = pwm_out(&led_state.current_state, !led_state.is_on);
116122
if (err) {
117123
LOG_ERR("pwm_out, error: %d", err);
118124
SEND_FATAL_ERROR();
119125
}
120126

121127
/* If LED just turned off, we completed one cycle */
122-
if (!led_is_on && repetitions > 0) {
123-
repetitions--;
124-
if (repetitions == 0) {
128+
if (!led_state.is_on && led_state.repetitions > 0) {
129+
led_state.repetitions--;
130+
if (led_state.repetitions == 0) {
125131
/* We're done, don't schedule next toggle */
126132
return;
127133
}
128134
}
129135

130136
/* Schedule next toggle */
131-
uint32_t next_delay = led_is_on ?
132-
current_led_state.duration_on_msec :
133-
current_led_state.duration_off_msec;
137+
uint32_t next_delay = led_state.is_on ?
138+
led_state.current_state.duration_on_msec :
139+
led_state.current_state.duration_off_msec;
134140

135141
err = k_work_schedule(&blink_work, K_MSEC(next_delay));
136142
if (err < 0) {
@@ -150,22 +156,22 @@ static void led_callback(const struct zbus_channel *chan)
150156
(void)k_work_cancel_delayable(&blink_work);
151157

152158
/* Store the new LED state */
153-
memcpy(&current_led_state, led_msg, sizeof(struct led_msg));
159+
memcpy(&led_state.current_state, led_msg, sizeof(struct led_msg));
154160

155161
/* Set up repetitions */
156-
repetitions = led_msg->repetitions;
162+
led_state.repetitions = led_msg->repetitions;
157163

158164
/* If repetitions is 0, turn LED off. Otherwise LED on */
159-
led_is_on = (repetitions != 0);
165+
led_state.is_on = (led_state.repetitions != 0);
160166

161-
err = pwm_out(led_msg, !led_is_on);
167+
err = pwm_out(led_msg, !led_state.is_on);
162168
if (err) {
163169
LOG_ERR("pwm_out, error: %d", err);
164170
SEND_FATAL_ERROR();
165171
}
166172

167173
/* Schedule first toggle if LED should be blinking */
168-
if (led_is_on) {
174+
if (led_state.is_on) {
169175
err = k_work_schedule(&blink_work, K_MSEC(led_msg->duration_on_msec));
170176
if (err < 0) {
171177
LOG_ERR("k_work_schedule, error: %d", err);

0 commit comments

Comments
 (0)