samples: hid-mouse: various cleanups
Use the new GPIO_DT_SPEC_GET_OR() macro to clean up the node detection code by eliminating conditional compilation. This increases code coverage on platforms where not all the devicetree nodes are available. Clean up some whitespace while we're at it. Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
This commit is contained in:
parent
58617fbec7
commit
c48bcaae0a
1 changed files with 74 additions and 96 deletions
|
@ -14,56 +14,42 @@
|
|||
#define LOG_LEVEL LOG_LEVEL_DBG
|
||||
LOG_MODULE_REGISTER(main);
|
||||
|
||||
/*
|
||||
* Devicetree node identifiers for the buttons and LED this sample
|
||||
* supports.
|
||||
*/
|
||||
#define SW0_NODE DT_ALIAS(sw0)
|
||||
|
||||
#if DT_NODE_HAS_STATUS(SW0_NODE, okay)
|
||||
#define PORT0 DT_GPIO_LABEL(SW0_NODE, gpios)
|
||||
#define PIN0 DT_GPIO_PIN(SW0_NODE, gpios)
|
||||
#define PIN0_FLAGS DT_GPIO_FLAGS(SW0_NODE, gpios)
|
||||
#else
|
||||
#error "Unsupported board: sw0 devicetree alias is not defined"
|
||||
#define PORT0 ""
|
||||
#define PIN0 0
|
||||
#define PIN0_FLAGS 0
|
||||
#endif
|
||||
|
||||
#define SW1_NODE DT_ALIAS(sw1)
|
||||
|
||||
#if DT_NODE_HAS_STATUS(SW1_NODE, okay)
|
||||
#define PORT1 DT_GPIO_LABEL(SW1_NODE, gpios)
|
||||
#define PIN1 DT_GPIO_PIN(SW1_NODE, gpios)
|
||||
#define PIN1_FLAGS DT_GPIO_FLAGS(SW1_NODE, gpios)
|
||||
#endif
|
||||
|
||||
#define SW2_NODE DT_ALIAS(sw2)
|
||||
|
||||
#if DT_NODE_HAS_STATUS(SW2_NODE, okay)
|
||||
#define PORT2 DT_GPIO_LABEL(SW2_NODE, gpios)
|
||||
#define PIN2 DT_GPIO_PIN(SW2_NODE, gpios)
|
||||
#define PIN2_FLAGS DT_GPIO_FLAGS(SW2_NODE, gpios)
|
||||
#endif
|
||||
|
||||
#define SW3_NODE DT_ALIAS(sw3)
|
||||
|
||||
#if DT_NODE_HAS_STATUS(SW3_NODE, okay)
|
||||
#define PORT3 DT_GPIO_LABEL(SW3_NODE, gpios)
|
||||
#define PIN3 DT_GPIO_PIN(SW3_NODE, gpios)
|
||||
#define PIN3_FLAGS DT_GPIO_FLAGS(SW3_NODE, gpios)
|
||||
#endif
|
||||
|
||||
#define LED0_NODE DT_ALIAS(led0)
|
||||
|
||||
#if DT_NODE_HAS_STATUS(LED0_NODE, okay)
|
||||
#define LED_PORT DT_GPIO_LABEL(LED0_NODE, gpios)
|
||||
#define LED DT_GPIO_PIN(LED0_NODE, gpios)
|
||||
#define LED_FLAGS DT_GPIO_FLAGS(LED0_NODE, gpios)
|
||||
#else
|
||||
#error "Unsupported board: led0 devicetree alias is not defined"
|
||||
#define LED_PORT ""
|
||||
#define LED 0
|
||||
#define LED_FLAGS 0
|
||||
/*
|
||||
* Button sw0 and LED led0 are required.
|
||||
*/
|
||||
#if !DT_NODE_EXISTS(SW0_NODE)
|
||||
#error "Unsupported board: sw0 devicetree alias is not defined"
|
||||
#endif
|
||||
|
||||
#if !DT_NODE_EXISTS(LED0_NODE)
|
||||
#error "Unsupported board: led0 devicetree alias is not defined"
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Helper macro for initializing a gpio_dt_spec from the devicetree
|
||||
* with fallback values when the nodes are missing.
|
||||
*/
|
||||
#define GPIO_SPEC(node_id) GPIO_DT_SPEC_GET_OR(node_id, gpios, {0})
|
||||
|
||||
/*
|
||||
* Create gpio_dt_spec structures from the devicetree.
|
||||
*/
|
||||
static const struct gpio_dt_spec sw0 = GPIO_SPEC(SW0_NODE),
|
||||
sw1 = GPIO_SPEC(SW1_NODE),
|
||||
sw2 = GPIO_SPEC(SW2_NODE),
|
||||
sw3 = GPIO_SPEC(SW3_NODE),
|
||||
led0 = GPIO_SPEC(LED0_NODE);
|
||||
|
||||
static const uint8_t hid_report_desc[] = HID_MOUSE_REPORT_DESC(2);
|
||||
|
||||
static uint8_t def_val[4];
|
||||
|
@ -80,8 +66,6 @@ static enum usb_dc_status_code usb_status;
|
|||
#define MOUSE_BTN_RIGHT BIT(1)
|
||||
#define MOUSE_BTN_MIDDLE BIT(2)
|
||||
|
||||
|
||||
|
||||
static void status_cb(enum usb_dc_status_code status, const uint8_t *param)
|
||||
{
|
||||
usb_status = status;
|
||||
|
@ -100,10 +84,10 @@ static void left_button(const struct device *gpio, struct gpio_callback *cb,
|
|||
}
|
||||
}
|
||||
|
||||
ret = gpio_pin_get(gpio, PIN0);
|
||||
ret = gpio_pin_get(gpio, sw0.pin);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Failed to get the state of pin %u, error: %d",
|
||||
PIN0, ret);
|
||||
LOG_ERR("Failed to get the state of port %s pin %u, error: %d",
|
||||
gpio->name, sw0.pin, ret);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -119,7 +103,6 @@ static void left_button(const struct device *gpio, struct gpio_callback *cb,
|
|||
}
|
||||
}
|
||||
|
||||
#if DT_NODE_HAS_STATUS(SW1_NODE, okay)
|
||||
static void right_button(const struct device *gpio, struct gpio_callback *cb,
|
||||
uint32_t pins)
|
||||
{
|
||||
|
@ -133,10 +116,10 @@ static void right_button(const struct device *gpio, struct gpio_callback *cb,
|
|||
}
|
||||
}
|
||||
|
||||
ret = gpio_pin_get(gpio, PIN1);
|
||||
ret = gpio_pin_get(gpio, sw1.pin);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Failed to get the state of pin %u, error: %d",
|
||||
PIN1, ret);
|
||||
LOG_ERR("Failed to get the state of port %s pin %u, error: %d",
|
||||
gpio->name, sw1.pin, ret);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -151,19 +134,17 @@ static void right_button(const struct device *gpio, struct gpio_callback *cb,
|
|||
k_sem_give(&sem);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if DT_NODE_HAS_STATUS(SW2_NODE, okay)
|
||||
static void x_move(const struct device *gpio, struct gpio_callback *cb,
|
||||
uint32_t pins)
|
||||
{
|
||||
int ret;
|
||||
uint8_t state = status[MOUSE_X_REPORT_POS];
|
||||
|
||||
ret = gpio_pin_get(gpio, PIN2);
|
||||
ret = gpio_pin_get(gpio, sw2.pin);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Failed to get the state of pin %u, error: %d",
|
||||
PIN2, ret);
|
||||
LOG_ERR("Failed to get the state of port %s pin %u, error: %d",
|
||||
gpio->name, sw2.pin, ret);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -176,19 +157,17 @@ static void x_move(const struct device *gpio, struct gpio_callback *cb,
|
|||
k_sem_give(&sem);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if DT_NODE_HAS_STATUS(SW3_NODE, okay)
|
||||
static void y_move(const struct device *gpio, struct gpio_callback *cb,
|
||||
uint32_t pins)
|
||||
{
|
||||
int ret;
|
||||
uint8_t state = status[MOUSE_Y_REPORT_POS];
|
||||
|
||||
ret = gpio_pin_get(gpio, PIN3);
|
||||
ret = gpio_pin_get(gpio, sw3.pin);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Failed to get the state of pin %u, error: %d",
|
||||
PIN3, ret);
|
||||
LOG_ERR("Failed to get the state of port %s pin %u, error: %d",
|
||||
gpio->name, sw3.pin, ret);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -201,30 +180,36 @@ static void y_move(const struct device *gpio, struct gpio_callback *cb,
|
|||
k_sem_give(&sem);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
int callbacks_configure(const struct device *gpio, uint32_t pin, int flags,
|
||||
int callbacks_configure(const struct gpio_dt_spec *spec,
|
||||
gpio_callback_handler_t handler,
|
||||
struct gpio_callback *callback, uint8_t *val)
|
||||
{
|
||||
const struct device *gpio = spec->port;
|
||||
gpio_pin_t pin = spec->pin;
|
||||
int ret;
|
||||
|
||||
if (!gpio) {
|
||||
LOG_ERR("Could not find PORT");
|
||||
return -ENXIO;
|
||||
if (gpio == NULL) {
|
||||
/* Optional GPIO is missing. */
|
||||
return 0;
|
||||
}
|
||||
|
||||
ret = gpio_pin_configure(gpio, pin, GPIO_INPUT | flags);
|
||||
if (!device_is_ready(gpio)) {
|
||||
LOG_ERR("GPIO port %s is not ready", gpio->name);
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
ret = gpio_pin_configure_dt(spec, GPIO_INPUT);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Failed to configure pin %u, error: %d",
|
||||
pin, ret);
|
||||
LOG_ERR("Failed to configure port %s pin %u, error: %d",
|
||||
gpio->name, pin, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = gpio_pin_get(gpio, pin);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Failed to get the state of pin %u, error: %d",
|
||||
pin, ret);
|
||||
LOG_ERR("Failed to get the state of port %s pin %u, error: %d",
|
||||
gpio->name, pin, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -233,15 +218,17 @@ int callbacks_configure(const struct device *gpio, uint32_t pin, int flags,
|
|||
gpio_init_callback(callback, handler, BIT(pin));
|
||||
ret = gpio_add_callback(gpio, callback);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Failed to add the callback for pin %u, error: %d",
|
||||
pin, ret);
|
||||
LOG_ERR("Failed to add the callback for port %s pin %u, "
|
||||
"error: %d",
|
||||
gpio->name, pin, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = gpio_pin_interrupt_configure(gpio, pin, GPIO_INT_EDGE_BOTH);
|
||||
ret = gpio_pin_interrupt_configure_dt(spec, GPIO_INT_EDGE_BOTH);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Failed to configure interrupt for pin %u, error: %d",
|
||||
pin, ret);
|
||||
LOG_ERR("Failed to configure interrupt for port %s pin %u, "
|
||||
"error: %d",
|
||||
gpio->name, pin, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -252,11 +239,10 @@ void main(void)
|
|||
{
|
||||
int ret;
|
||||
uint8_t report[4] = { 0x00 };
|
||||
const struct device *led_dev, *hid_dev;
|
||||
const struct device *hid_dev;
|
||||
|
||||
led_dev = device_get_binding(LED_PORT);
|
||||
if (led_dev == NULL) {
|
||||
LOG_ERR("Cannot get LED");
|
||||
if (!device_is_ready(led0.port)) {
|
||||
LOG_ERR("LED device %s is not ready", led0.port->name);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -266,41 +252,33 @@ void main(void)
|
|||
return;
|
||||
}
|
||||
|
||||
ret = gpio_pin_configure(led_dev, LED, GPIO_OUTPUT | LED_FLAGS);
|
||||
ret = gpio_pin_configure_dt(&led0, GPIO_OUTPUT);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Failed to configure the LED pin, error: %d", ret);
|
||||
return;
|
||||
}
|
||||
|
||||
if (callbacks_configure(device_get_binding(PORT0), PIN0, PIN0_FLAGS,
|
||||
&left_button, &callback[0], &def_val[0])) {
|
||||
if (callbacks_configure(&sw0, &left_button, &callback[0],
|
||||
&def_val[0])) {
|
||||
LOG_ERR("Failed configuring left button callback.");
|
||||
return;
|
||||
}
|
||||
|
||||
#if DT_NODE_HAS_STATUS(SW1_NODE, okay)
|
||||
if (callbacks_configure(device_get_binding(PORT1), PIN1, PIN1_FLAGS,
|
||||
&right_button, &callback[1], &def_val[1])) {
|
||||
if (callbacks_configure(&sw1, &right_button, &callback[1],
|
||||
&def_val[1])) {
|
||||
LOG_ERR("Failed configuring right button callback.");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if DT_NODE_HAS_STATUS(SW2_NODE, okay)
|
||||
if (callbacks_configure(device_get_binding(PORT2), PIN2, PIN2_FLAGS,
|
||||
&x_move, &callback[2], &def_val[2])) {
|
||||
if (callbacks_configure(&sw2, &x_move, &callback[2], &def_val[2])) {
|
||||
LOG_ERR("Failed configuring X axis movement callback.");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if DT_NODE_HAS_STATUS(SW3_NODE, okay)
|
||||
if (callbacks_configure(device_get_binding(PORT3), PIN3, PIN3_FLAGS,
|
||||
&y_move, &callback[3], &def_val[3])) {
|
||||
if (callbacks_configure(&sw3, &y_move, &callback[3], &def_val[3])) {
|
||||
LOG_ERR("Failed configuring Y axis movement callback.");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
usb_hid_register_device(hid_dev,
|
||||
hid_report_desc, sizeof(hid_report_desc),
|
||||
|
@ -328,7 +306,7 @@ void main(void)
|
|||
}
|
||||
|
||||
/* Toggle LED on sent report */
|
||||
ret = gpio_pin_toggle(led_dev, LED);
|
||||
ret = gpio_pin_toggle(led0.port, led0.pin);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Failed to toggle the LED pin, error: %d", ret);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue