samples: hid-mouse: cleanups

Similar cleanups as done to other samples.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
This commit is contained in:
Martí Bolívar 2020-05-04 14:08:09 -07:00 committed by Carles Cufí
commit f83ecb7bc6
2 changed files with 79 additions and 72 deletions

View file

@ -22,17 +22,33 @@ Requirements
This project requires an USB device driver, and there must has at least one
GPIO button in your board.
To use this sample, you will require a board that defines the user switch in its
header file. The :file:`board.h` must define the following variables:
The board hardware must have a push button connected via a GPIO pin. These are
called "User buttons" on many of Zephyr's :ref:`boards`.
- DT_GPIO_LABEL(DT_ALIAS(sw0), gpios)
- DT_GPIO_PIN(DT_ALIAS(sw0), gpios)
The button must be configured using the ``sw0`` :ref:`devicetree <dt-guide>`
alias, usually in the :ref:`BOARD.dts file <devicetree-in-out-files>`. You will
see this error if you try to build this sample for an unsupported board:
The following variables are optional and depend on available board buttons:
.. code-block:: none
- For right-button: DT_GPIO_LABEL(DT_ALIAS(sw1), gpios), DT_GPIO_PIN(DT_ALIAS(sw1), gpios)
- For X-axis: DT_GPIO_LABEL(DT_ALIAS(sw2), gpios), DT_GPIO_PIN(DT_ALIAS(sw2), gpios)
- For Y-axis: DT_GPIO_LABEL(DT_ALIAS(sw3), gpios), DT_GPIO_PIN(DT_ALIAS(sw3), gpios)
Unsupported board: sw0 devicetree alias is not defined
You may see additional build errors if the ``sw0`` alias exists, but is not
properly defined.
If the devicetree aliases ``sw1``, ``sw2``, and ``sw3`` are defined, they will
also be used as follows:
- ``sw1``: right button
- ``sw2``: move the mouse along the x-axis
- ``sw3``: move the mouse along the y-axis
An LED must also be configured via the ``led0`` devicetree alias. You may also
see this error if you try to build this sample for an unsupported board:
.. code-block:: none
Unsupported board: led0 devicetree alias is not defined
Building and Running
********************

View file

@ -14,70 +14,61 @@
#define LOG_LEVEL LOG_LEVEL_DBG
LOG_MODULE_REGISTER(main);
/* change this to use another GPIO port */
#if DT_NODE_HAS_PROP(DT_ALIAS(sw0), gpios)
#define PORT0 DT_GPIO_LABEL(DT_ALIAS(sw0), gpios)
#define FLAGS_OR_ZERO(node) \
COND_CODE_1(DT_PHA_HAS_CELL(node, gpios, flags), \
(DT_GPIO_FLAGS(node, gpios)), \
(0))
#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 FLAGS_OR_ZERO(SW0_NODE)
#else
#error DT_GPIO_LABEL(DT_ALIAS(sw0), gpios) needs to be set
#error "Unsupported board: sw0 devicetree alias is not defined"
#define PORT0 ""
#define PIN0 0
#define PIN0_FLAGS 0
#endif
/* change this to use another GPIO pin */
#if DT_PHA_HAS_CELL(DT_ALIAS(sw0), gpios, pin)
#define PIN0 DT_GPIO_PIN(DT_ALIAS(sw0), gpios)
#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 FLAGS_OR_ZERO(SW1_NODE)
#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 FLAGS_OR_ZERO(SW2_NODE)
#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 FLAGS_OR_ZERO(SW3_NODE)
#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 DT_GPIO_PIN(DT_ALIAS(sw0), gpios) needs to be set
#error "Unsupported board: led0 devicetree alias is not defined"
#define LED_PORT ""
#define LED 0
#define LED_FLAGS 0
#endif
/* The switch pin pull-up/down flags */
#if DT_PHA_HAS_CELL(DT_ALIAS(sw0), gpios, flags)
#define PIN0_FLAGS DT_GPIO_FLAGS(DT_ALIAS(sw0), gpios)
#else
#error DT_GPIO_FLAGS(DT_ALIAS(sw0), gpios) needs to be set
#endif
/* If second button exists, use it as right-click. */
#if DT_PHA_HAS_CELL(DT_ALIAS(sw1), gpios, pin)
#define PIN1 DT_GPIO_PIN(DT_ALIAS(sw1), gpios)
#endif
#if DT_NODE_HAS_PROP(DT_ALIAS(sw1), gpios)
#define PORT1 DT_GPIO_LABEL(DT_ALIAS(sw1), gpios)
#endif
#if DT_PHA_HAS_CELL(DT_ALIAS(sw1), gpios, flags)
#define PIN1_FLAGS DT_GPIO_FLAGS(DT_ALIAS(sw1), gpios)
#endif
/* If third button exists, use it as X axis movement. */
#if DT_PHA_HAS_CELL(DT_ALIAS(sw2), gpios, pin)
#define PIN2 DT_GPIO_PIN(DT_ALIAS(sw2), gpios)
#endif
#if DT_NODE_HAS_PROP(DT_ALIAS(sw2), gpios)
#define PORT2 DT_GPIO_LABEL(DT_ALIAS(sw2), gpios)
#endif
#if DT_PHA_HAS_CELL(DT_ALIAS(sw2), gpios, flags)
#define PIN2_FLAGS DT_GPIO_FLAGS(DT_ALIAS(sw2), gpios)
#endif
/* If fourth button exists, use it as Y axis movement. */
#if DT_PHA_HAS_CELL(DT_ALIAS(sw3), gpios, pin)
#define PIN3 DT_GPIO_PIN(DT_ALIAS(sw3), gpios)
#endif
#if DT_NODE_HAS_PROP(DT_ALIAS(sw3), gpios)
#define PORT3 DT_GPIO_LABEL(DT_ALIAS(sw3), gpios)
#endif
#if DT_PHA_HAS_CELL(DT_ALIAS(sw3), gpios, flags)
#define PIN3_FLAGS DT_GPIO_FLAGS(DT_ALIAS(sw3), gpios)
#endif
#define LED_PORT DT_GPIO_LABEL(DT_ALIAS(led0), gpios)
#define LED DT_GPIO_PIN(DT_ALIAS(led0), gpios)
#define LED_FLAGS DT_GPIO_FLAGS(DT_ALIAS(led0), gpios)
static const u8_t hid_report_desc[] = HID_MOUSE_REPORT_DESC(2);
static u8_t def_val[4];
@ -133,7 +124,7 @@ static void left_button(struct device *gpio, struct gpio_callback *cb,
}
}
#if DT_PHA_HAS_CELL(DT_ALIAS(sw1), gpios, pin)
#if DT_NODE_HAS_STATUS(SW1_NODE, okay)
static void right_button(struct device *gpio, struct gpio_callback *cb,
u32_t pins)
{
@ -167,7 +158,7 @@ static void right_button(struct device *gpio, struct gpio_callback *cb,
}
#endif
#if DT_PHA_HAS_CELL(DT_ALIAS(sw2), gpios, pin)
#if DT_NODE_HAS_STATUS(SW2_NODE, okay)
static void x_move(struct device *gpio, struct gpio_callback *cb, u32_t pins)
{
int ret;
@ -191,7 +182,7 @@ static void x_move(struct device *gpio, struct gpio_callback *cb, u32_t pins)
}
#endif
#if DT_PHA_HAS_CELL(DT_ALIAS(sw3), gpios, pin)
#if DT_NODE_HAS_STATUS(SW3_NODE, okay)
static void y_move(struct device *gpio, struct gpio_callback *cb, u32_t pins)
{
int ret;
@ -290,7 +281,7 @@ void main(void)
return;
}
#if DT_PHA_HAS_CELL(DT_ALIAS(sw1), gpios, pin)
#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])) {
LOG_ERR("Failed configuring right button callback.");
@ -298,7 +289,7 @@ void main(void)
}
#endif
#if DT_PHA_HAS_CELL(DT_ALIAS(sw2), gpios, pin)
#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])) {
LOG_ERR("Failed configuring X axis movement callback.");
@ -306,7 +297,7 @@ void main(void)
}
#endif
#if DT_PHA_HAS_CELL(DT_ALIAS(sw3), gpios, pin)
#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])) {
LOG_ERR("Failed configuring Y axis movement callback.");