Skip to content

Commit 551c136

Browse files
fabiobaltierikartben
authored andcommitted
led: add and use a LED_BRIGTHNESS_MAX definition
The led_set_brightness API says that brightness is defined as percentage, add a macro from the max and use it everywhere. Signed-off-by: Fabio Baltieri <[email protected]>
1 parent 9dfeaf2 commit 551c136

File tree

16 files changed

+36
-35
lines changed

16 files changed

+36
-35
lines changed

drivers/led/ht16k33.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ static int ht16k33_init(const struct device *dev)
305305
dev_data->min_period = 0U;
306306
dev_data->max_period = 2000U;
307307
dev_data->min_brightness = 0U;
308-
dev_data->max_brightness = 100U;
308+
dev_data->max_brightness = LED_BRIGTHNESS_MAX;
309309

310310
/* System oscillator on */
311311
cmd[0] = HT16K33_CMD_SYSTEM_SETUP | HT16K33_OPT_S;

drivers/led/is31fl3194.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,12 @@ static int is31fl3194_set_brightness(const struct device *dev, uint32_t led, uin
145145
return -ENOTSUP;
146146
}
147147

148-
if (value > 100) {
148+
if (value > LED_BRIGTHNESS_MAX) {
149149
return -EINVAL;
150150
}
151151

152152
/* Rescale 0..100 to 0..255 */
153-
value = value * 255 / 100;
153+
value = value * 255 / LED_BRIGTHNESS_MAX;
154154

155155
ret = i2c_reg_write_byte_dt(&config->bus, led_channels[led], value);
156156
if (ret == 0) {
@@ -168,7 +168,7 @@ static int is31fl3194_set_brightness(const struct device *dev, uint32_t led, uin
168168

169169
static inline int is31fl3194_led_on(const struct device *dev, uint32_t led)
170170
{
171-
return is31fl3194_set_brightness(dev, led, 100);
171+
return is31fl3194_set_brightness(dev, led, LED_BRIGTHNESS_MAX);
172172
}
173173

174174
static inline int is31fl3194_led_off(const struct device *dev, uint32_t led)

drivers/led/is31fl3216a.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ static int is31fl3216a_update_pwm(const struct i2c_dt_spec *i2c)
6262

6363
static uint8_t is31fl3216a_brightness_to_pwm(uint8_t brightness)
6464
{
65-
return (0xFFU * brightness) / 100;
65+
return (0xFFU * brightness) / LED_BRIGTHNESS_MAX;
6666
}
6767

6868
static int is31fl3216a_led_write_channels(const struct device *dev,
@@ -87,7 +87,7 @@ static int is31fl3216a_led_write_channels(const struct device *dev,
8787
i2c_buffer[0] = IS31FL3216A_REG_PWM_LAST - start_channel -
8888
(num_channels - 1);
8989
for (i = 0; i < num_channels; i++) {
90-
if (buf[num_channels - i - 1] > 100) {
90+
if (buf[num_channels - i - 1] > LED_BRIGTHNESS_MAX) {
9191
return -EINVAL;
9292
}
9393
i2c_buffer[i + 1] = is31fl3216a_brightness_to_pwm(
@@ -111,7 +111,7 @@ static int is31fl3216a_led_set_brightness(const struct device *dev,
111111
int status;
112112
uint8_t pwm_value;
113113

114-
if (led > IS31FL3216A_MAX_LEDS - 1 || value > 100) {
114+
if (led > IS31FL3216A_MAX_LEDS - 1 || value > LED_BRIGTHNESS_MAX) {
115115
return -EINVAL;
116116
}
117117

@@ -126,7 +126,7 @@ static int is31fl3216a_led_set_brightness(const struct device *dev,
126126

127127
static int is31fl3216a_led_on(const struct device *dev, uint32_t led)
128128
{
129-
return is31fl3216a_led_set_brightness(dev, led, 100);
129+
return is31fl3216a_led_set_brightness(dev, led, LED_BRIGTHNESS_MAX);
130130
}
131131

132132
static int is31fl3216a_led_off(const struct device *dev, uint32_t led)

drivers/led/is31fl3733.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ LOG_MODULE_REGISTER(is31fl3733, CONFIG_LED_LOG_LEVEL);
4343
#define IS31FL3733_MAX_LED (IS31FL3733_ROW_COUNT * IS31FL3733_COL_COUNT)
4444

4545
/* Max brightness */
46-
#define IS31FL3733_MAX_BRIGHTNESS 100
47-
4846
struct is31fl3733_config {
4947
struct i2c_dt_spec bus;
5048
struct gpio_dt_spec sdb;
@@ -98,7 +96,7 @@ static int is31fl3733_led_set_brightness(const struct device *dev, uint32_t led,
9896
{
9997
const struct is31fl3733_config *config = dev->config;
10098
int ret;
101-
uint8_t led_brightness = (uint8_t)(((uint32_t)value * 255) / 100);
99+
uint8_t led_brightness = (uint8_t)(((uint32_t)value * 255) / LED_BRIGTHNESS_MAX);
102100

103101
if (led >= IS31FL3733_MAX_LED) {
104102
return -EINVAL;
@@ -115,7 +113,7 @@ static int is31fl3733_led_set_brightness(const struct device *dev, uint32_t led,
115113

116114
static int is31fl3733_led_on(const struct device *dev, uint32_t led)
117115
{
118-
return is31fl3733_led_set_brightness(dev, led, IS31FL3733_MAX_BRIGHTNESS);
116+
return is31fl3733_led_set_brightness(dev, led, LED_BRIGTHNESS_MAX);
119117
}
120118

121119
static int is31fl3733_led_off(const struct device *dev, uint32_t led)

drivers/led/led_dac.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ static int led_dac_set_brightness(const struct device *dev, uint32_t led, uint8_
4343
value = pct > 0 ? config->leds[led].dac_min_brightness +
4444
(uint64_t)(config->leds[led].dac_max_brightness -
4545
config->leds[led].dac_min_brightness) *
46-
pct / 100
46+
pct / LED_BRIGTHNESS_MAX
4747
: 0;
4848

4949
return led_dac_set_raw(dev, led, value);

drivers/led/led_gpio.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ static int led_gpio_set_brightness(const struct device *dev, uint32_t led, uint8
3030
const struct led_gpio_config *config = dev->config;
3131
const struct gpio_dt_spec *led_gpio;
3232

33-
if ((led >= config->num_leds) || (value > 100)) {
33+
if ((led >= config->num_leds) || (value > LED_BRIGTHNESS_MAX)) {
3434
return -EINVAL;
3535
}
3636

@@ -41,7 +41,7 @@ static int led_gpio_set_brightness(const struct device *dev, uint32_t led, uint8
4141

4242
static int led_gpio_on(const struct device *dev, uint32_t led)
4343
{
44-
return led_gpio_set_brightness(dev, led, 100);
44+
return led_gpio_set_brightness(dev, led, LED_BRIGTHNESS_MAX);
4545
}
4646

4747
static int led_gpio_off(const struct device *dev, uint32_t led)

drivers/led/led_pwm.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,19 @@ static int led_pwm_set_brightness(const struct device *dev,
5858
const struct led_pwm_config *config = dev->config;
5959
const struct pwm_dt_spec *dt_led;
6060

61-
if (led >= config->num_leds || value > 100) {
61+
if (led >= config->num_leds || value > LED_BRIGTHNESS_MAX) {
6262
return -EINVAL;
6363
}
6464

6565
dt_led = &config->led[led];
6666

6767
return pwm_set_pulse_dt(&config->led[led],
68-
(uint32_t) ((uint64_t) dt_led->period * value / 100));
68+
(uint32_t) ((uint64_t) dt_led->period * value / LED_BRIGTHNESS_MAX));
6969
}
7070

7171
static int led_pwm_on(const struct device *dev, uint32_t led)
7272
{
73-
return led_pwm_set_brightness(dev, led, 100);
73+
return led_pwm_set_brightness(dev, led, LED_BRIGTHNESS_MAX);
7474
}
7575

7676
static int led_pwm_off(const struct device *dev, uint32_t led)

drivers/led/led_shell.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ static int cmd_set_brightness(const struct shell *sh,
164164
argv[arg_idx_value]);
165165
return -EINVAL;
166166
}
167-
if (value > 100) {
167+
if (value > LED_BRIGTHNESS_MAX) {
168168
shell_error(sh, "Invalid LED brightness value %lu (max 100)",
169169
value);
170170
return -EINVAL;

drivers/led/lp3943.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ static int lp3943_led_init(const struct device *dev)
264264
dev_data->min_period = 0U;
265265
dev_data->max_period = 1600U;
266266
dev_data->min_brightness = 0U;
267-
dev_data->max_brightness = 100U;
267+
dev_data->max_brightness = LED_BRIGTHNESS_MAX;
268268

269269
return 0;
270270
}

drivers/led/lp50xx.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
#include <zephyr/logging/log.h>
2424
LOG_MODULE_REGISTER(lp50xx, CONFIG_LED_LOG_LEVEL);
2525

26-
#define LP50XX_MAX_BRIGHTNESS 100U
27-
2826
/*
2927
* Number of supported RGB led modules per chipset.
3028
*
@@ -126,21 +124,21 @@ static int lp50xx_set_brightness(const struct device *dev,
126124
return -ENODEV;
127125
}
128126

129-
if (value > LP50XX_MAX_BRIGHTNESS) {
127+
if (value > LED_BRIGTHNESS_MAX) {
130128
LOG_ERR("%s: brightness value out of bounds: val=%d, max=%d",
131-
dev->name, value, LP50XX_MAX_BRIGHTNESS);
129+
dev->name, value, LED_BRIGTHNESS_MAX);
132130
return -EINVAL;
133131
}
134132

135133
buf[0] = LP50XX_LED0_BRIGHTNESS(config->num_modules) + led_info->index;
136-
buf[1] = (value * 0xff) / 100;
134+
buf[1] = (value * 0xff) / LED_BRIGTHNESS_MAX;
137135

138136
return i2c_write_dt(&config->bus, buf, sizeof(buf));
139137
}
140138

141139
static int lp50xx_on(const struct device *dev, uint32_t led)
142140
{
143-
return lp50xx_set_brightness(dev, led, 100);
141+
return lp50xx_set_brightness(dev, led, LED_BRIGTHNESS_MAX);
144142
}
145143

146144
static int lp50xx_off(const struct device *dev, uint32_t led)

0 commit comments

Comments
 (0)