samples: reel_board: mesh_badge: Convert to the new GPIO API
Convert to the new GPIO API using logical levels, and remove the duplicate implementation of LED control that existed. Signed-off-by: Carles Cufi <carles.cufi@nordicsemi.no>
This commit is contained in:
parent
cb24057845
commit
007557a61f
|
@ -5,15 +5,8 @@
|
|||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
enum led_periph_device {
|
||||
DEV_IDX_LED0 = 0,
|
||||
DEV_IDX_LED1,
|
||||
DEV_IDX_LED2,
|
||||
};
|
||||
|
||||
enum periph_device {
|
||||
DEV_IDX_BUTTON = 0,
|
||||
DEV_IDX_HDC1010,
|
||||
DEV_IDX_HDC1010 = 0,
|
||||
DEV_IDX_MMA8652,
|
||||
DEV_IDX_APDS9960,
|
||||
DEV_IDX_EPD,
|
||||
|
|
|
@ -62,7 +62,8 @@ static struct k_work mesh_start_work;
|
|||
|
||||
/* Definitions of models user data (Start) */
|
||||
static struct led_onoff_state led_onoff_state[] = {
|
||||
{ .dev_id = DEV_IDX_LED0 },
|
||||
/* Use LED 0 for this model */
|
||||
{ .dev_id = 0 },
|
||||
};
|
||||
|
||||
static void heartbeat(u8_t hops, u16_t feat)
|
||||
|
@ -155,7 +156,6 @@ static void gen_onoff_set_unack(struct bt_mesh_model *model,
|
|||
printk("addr 0x%02x state 0x%02x\n",
|
||||
bt_mesh_model_elem(model)->addr, state->current);
|
||||
|
||||
/* Pin set low turns on LED's on the reel board */
|
||||
if (set_led_state(state->dev_id, onoff)) {
|
||||
printk("Failed to set led state\n");
|
||||
|
||||
|
|
|
@ -17,54 +17,13 @@ struct device_info {
|
|||
char *name;
|
||||
};
|
||||
|
||||
struct led_device_info {
|
||||
struct device *dev;
|
||||
char *name;
|
||||
u32_t pin;
|
||||
};
|
||||
|
||||
static struct led_device_info led_dev_info[] = {
|
||||
/* red front LED */
|
||||
{ NULL, DT_ALIAS_LED0_GPIOS_CONTROLLER, DT_ALIAS_LED0_GPIOS_PIN },
|
||||
/* green front LED */
|
||||
{ NULL, DT_ALIAS_LED1_GPIOS_CONTROLLER, DT_ALIAS_LED1_GPIOS_PIN },
|
||||
/* blue front LED */
|
||||
{ NULL, DT_ALIAS_LED2_GPIOS_CONTROLLER, DT_ALIAS_LED2_GPIOS_PIN },
|
||||
};
|
||||
|
||||
static struct device_info dev_info[] = {
|
||||
{ NULL, DT_ALIAS_SW0_GPIOS_CONTROLLER },
|
||||
{ NULL, DT_INST_0_TI_HDC1010_LABEL },
|
||||
{ NULL, DT_INST_0_NXP_MMA8652FC_LABEL },
|
||||
{ NULL, DT_INST_0_AVAGO_APDS9960_LABEL },
|
||||
{ NULL, DT_INST_0_SOLOMON_SSD16XXFB_LABEL },
|
||||
};
|
||||
|
||||
static void configure_gpios(void)
|
||||
{
|
||||
gpio_pin_configure(led_dev_info[DEV_IDX_LED0].dev,
|
||||
led_dev_info[DEV_IDX_LED0].pin, GPIO_DIR_OUT);
|
||||
gpio_pin_write(led_dev_info[DEV_IDX_LED0].dev,
|
||||
led_dev_info[DEV_IDX_LED0].pin, 1);
|
||||
|
||||
gpio_pin_configure(led_dev_info[DEV_IDX_LED1].dev,
|
||||
led_dev_info[DEV_IDX_LED1].pin, GPIO_DIR_OUT);
|
||||
gpio_pin_write(led_dev_info[DEV_IDX_LED1].dev,
|
||||
led_dev_info[DEV_IDX_LED1].pin, 1);
|
||||
|
||||
gpio_pin_configure(led_dev_info[DEV_IDX_LED2].dev,
|
||||
led_dev_info[DEV_IDX_LED2].pin, GPIO_DIR_OUT);
|
||||
gpio_pin_write(led_dev_info[DEV_IDX_LED2].dev,
|
||||
led_dev_info[DEV_IDX_LED2].pin, 1);
|
||||
}
|
||||
|
||||
int set_led_state(u8_t id, bool state)
|
||||
{
|
||||
/* Invert state because of active low state for GPIO LED pins */
|
||||
return gpio_pin_write(led_dev_info[id].dev, led_dev_info[id].pin,
|
||||
!state);
|
||||
}
|
||||
|
||||
int get_hdc1010_val(struct sensor_value *val)
|
||||
{
|
||||
if (sensor_sample_fetch(dev_info[DEV_IDX_HDC1010].dev)) {
|
||||
|
@ -195,18 +154,6 @@ int periphs_init(void)
|
|||
}
|
||||
}
|
||||
|
||||
/* Bind leds */
|
||||
for (i = 0U; i < ARRAY_SIZE(led_dev_info); i++) {
|
||||
led_dev_info[i].dev = device_get_binding(led_dev_info[i].name);
|
||||
if (led_dev_info[i].dev == NULL) {
|
||||
printk("Failed to get %s led device\n",
|
||||
led_dev_info[i].name);
|
||||
return -EBUSY;
|
||||
}
|
||||
}
|
||||
|
||||
configure_gpios();
|
||||
|
||||
configure_accel();
|
||||
|
||||
return 0;
|
||||
|
|
|
@ -47,8 +47,6 @@ struct font_info {
|
|||
|
||||
#define STAT_COUNT 128
|
||||
|
||||
#define EDGE (GPIO_INT_EDGE | GPIO_INT_DOUBLE_EDGE)
|
||||
|
||||
#ifdef DT_ALIAS_SW0_GPIOS_FLAGS
|
||||
#define PULL_UP DT_ALIAS_SW0_GPIOS_FLAGS
|
||||
#else
|
||||
|
@ -66,11 +64,15 @@ static char str_buf[256];
|
|||
static struct {
|
||||
struct device *dev;
|
||||
const char *name;
|
||||
u32_t pin;
|
||||
gpio_pin_t pin;
|
||||
gpio_flags_t flags;
|
||||
} leds[] = {
|
||||
{ .name = DT_ALIAS_LED0_GPIOS_CONTROLLER, .pin = DT_ALIAS_LED0_GPIOS_PIN, },
|
||||
{ .name = DT_ALIAS_LED1_GPIOS_CONTROLLER, .pin = DT_ALIAS_LED1_GPIOS_PIN, },
|
||||
{ .name = DT_ALIAS_LED2_GPIOS_CONTROLLER, .pin = DT_ALIAS_LED2_GPIOS_PIN, },
|
||||
{ .name = DT_ALIAS_LED0_GPIOS_CONTROLLER, .pin = DT_ALIAS_LED0_GPIOS_PIN,
|
||||
.flags = DT_ALIAS_LED0_GPIOS_FLAGS},
|
||||
{ .name = DT_ALIAS_LED1_GPIOS_CONTROLLER, .pin = DT_ALIAS_LED1_GPIOS_PIN,
|
||||
.flags = DT_ALIAS_LED1_GPIOS_FLAGS},
|
||||
{ .name = DT_ALIAS_LED2_GPIOS_CONTROLLER, .pin = DT_ALIAS_LED2_GPIOS_PIN,
|
||||
.flags = DT_ALIAS_LED2_GPIOS_FLAGS}
|
||||
};
|
||||
|
||||
struct k_delayed_work led_timer;
|
||||
|
@ -444,11 +446,7 @@ static void long_press(struct k_work *work)
|
|||
|
||||
static bool button_is_pressed(void)
|
||||
{
|
||||
u32_t val;
|
||||
|
||||
gpio_pin_read(gpio, DT_ALIAS_SW0_GPIOS_PIN, &val);
|
||||
|
||||
return !val;
|
||||
return gpio_pin_get(gpio, DT_ALIAS_SW0_GPIOS_PIN) > 0;
|
||||
}
|
||||
|
||||
static void button_interrupt(struct device *dev, struct gpio_callback *cb,
|
||||
|
@ -517,16 +515,24 @@ static int configure_button(void)
|
|||
}
|
||||
|
||||
gpio_pin_configure(gpio, DT_ALIAS_SW0_GPIOS_PIN,
|
||||
(GPIO_DIR_IN | GPIO_INT | PULL_UP | EDGE));
|
||||
GPIO_INPUT | DT_ALIAS_SW0_GPIOS_FLAGS);
|
||||
|
||||
gpio_pin_interrupt_configure(gpio, DT_ALIAS_SW0_GPIOS_PIN,
|
||||
GPIO_INT_EDGE_BOTH);
|
||||
|
||||
gpio_init_callback(&button_cb, button_interrupt,
|
||||
BIT(DT_ALIAS_SW0_GPIOS_PIN));
|
||||
|
||||
gpio_init_callback(&button_cb, button_interrupt, BIT(DT_ALIAS_SW0_GPIOS_PIN));
|
||||
gpio_add_callback(gpio, &button_cb);
|
||||
|
||||
gpio_pin_enable_callback(gpio, DT_ALIAS_SW0_GPIOS_PIN);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int set_led_state(u8_t id, bool state)
|
||||
{
|
||||
return gpio_pin_set(leds[id].dev, leds[id].pin, state);
|
||||
}
|
||||
|
||||
static void led_timeout(struct k_work *work)
|
||||
{
|
||||
static int led_cntr;
|
||||
|
@ -534,7 +540,7 @@ static void led_timeout(struct k_work *work)
|
|||
|
||||
/* Disable all LEDs */
|
||||
for (i = 0; i < ARRAY_SIZE(leds); i++) {
|
||||
gpio_pin_write(leds[i].dev, leds[i].pin, 1);
|
||||
set_led_state(i, 0);
|
||||
}
|
||||
|
||||
/* Stop after 5 iterations */
|
||||
|
@ -545,7 +551,7 @@ static void led_timeout(struct k_work *work)
|
|||
|
||||
/* Select and enable current LED */
|
||||
i = led_cntr++ % ARRAY_SIZE(leds);
|
||||
gpio_pin_write(leds[i].dev, leds[i].pin, 0);
|
||||
set_led_state(i, 1);
|
||||
|
||||
k_delayed_work_submit(&led_timer, K_MSEC(100));
|
||||
}
|
||||
|
@ -561,9 +567,9 @@ static int configure_leds(void)
|
|||
return -ENODEV;
|
||||
}
|
||||
|
||||
gpio_pin_configure(leds[i].dev, leds[i].pin, GPIO_DIR_OUT);
|
||||
gpio_pin_write(leds[i].dev, leds[i].pin, 1);
|
||||
|
||||
gpio_pin_configure(leds[i].dev, leds[i].pin,
|
||||
leds[i].flags |
|
||||
GPIO_OUTPUT_INACTIVE);
|
||||
}
|
||||
|
||||
k_delayed_work_init(&led_timer, led_timeout);
|
||||
|
|
Loading…
Reference in a new issue