sterlinghughes commented on a change in pull request #889: implementation of
common button events
URL: https://github.com/apache/mynewt-core/pull/889#discussion_r173254642
##########
File path: util/btn/include/btn/btn.h
##########
@@ -0,0 +1,148 @@
+#ifndef _BTN_H_
+#define _BTN_H_
+
+#include <stdbool.h>
+#include <os/os.h>
+#include <hal/hal_gpio.h>
+#include <debounce/debounce.h>
+
+
+/*
+
+Example:
+
+btn_t btns[] = {
+ { .id = 1,
+ .children = (btn_t *[]){ &btns[2], NULL },
+ .reader = { .mode = BTN_READER_MODE_PIN_DEBOUNCED,
+ .inverted = true,
+ .pin = BUTTON_1,
+ .pull = HAL_GPIO_PULL_UP,
+ .timer = 5 },
+ .notify = { .mode = BTN_NOTIFY_MODE_MOUSE },
+ },
+ { .id = 2,
+ .children = (btn_t *[]){ &btns[2], NULL },
+ .reader = { .mode = BTN_READER_MODE_PIN_DEBOUNCED,
+ .inverted = true,
+ .pin = BUTTON_2,
+ .pull = HAL_GPIO_PULL_UP,
+ .timer = 5 },
+ .notify = { .mode = BTN_NOTIFY_MODE_TOUCH, },
+ },
+ { .id = 3,
+ .reader = { .mode = BTN_READER_MODE_EMULATED,
+ .emulated = { .from = { &btns[0], &btns[1] } } },
+ .notify = { .mode = BTN_NOTIFY_MODE_BUTTON },
+ },
+};
+
+btn_init(btns, 3, btn_callback);
+
+ */
+
+#define BTN_FLG_PRESSED 0x01
+#if MYNEWT_VAL(BTN_USE_DOUBLE)
+#define BTN_FLG_DOUBLED 0x02
+#endif
+#if MYNEWT_VAL(BTN_USE_LONG)
+#define BTN_FLG_LONG 0x04
+#endif
+#if MYNEWT_VAL(BTN_USE_REPEAT)
+#define BTN_FLG_REPEATING 0x08
+#endif
+#define BTN_FLG_MISSED 0x40
+#define BTN_FLG_FAILED 0x80
+
+#define BTN_EVT_STATE 0x01
+#define BTN_EVT_ACTION 0x02
+
+#define BTN_READER_MODE_PIN_DEBOUNCED 1
+#if MYNEWT_VAL(BTN_USE_EMULATION)
+#define BTN_READER_MODE_EMULATED 2
+#endif
+
+#define BTN_NOTIFY_MODE_DISABLED (0)
+#define BTN_NOTIFY_MODE_BUTTON (BTN_FLG_PRESSED)
+
+#if MYNEWT_VAL(BTN_USE_DOUBLE)
+#define BTN_NOTIFY_MODE_MOUSE (BTN_NOTIFY_MODE_BUTTON | BTN_FLG_DOUBLED)
+#endif
+
+#if MYNEWT_VAL(BTN_USE_LONG)
+#define BTN_NOTIFY_MODE_PEN (BTN_NOTIFY_MODE_BUTTON | BTN_FLG_LONG)
+#endif
+#if MYNEWT_VAL(BTN_USE_DOUBLE) && MYNEWT_VAL(BTN_USE_LONG)
+#define BTN_NOTIFY_MODE_TOUCH (BTN_NOTIFY_MODE_MOUSE | BTN_NOTIFY_MODE_PEN)
+#endif
+
+typedef uint8_t btn_id_t;
+
+
+
+
+typedef struct btn {
+ btn_id_t id;
+ uint8_t state;
+#if MYNEWT_VAL(BTN_USE_EMULATION)
+ struct btn **children;
+#endif
+ struct {
+ uint8_t mode;
+ bool inverted;
+ union {
+ struct {
+ int pin;
+ hal_gpio_pull_t pull;
+ int timer;
+ debounce_pin_t debounce;
+ };
+#if MYNEWT_VAL(BTN_USE_EMULATION)
+ struct {
+ struct btn *from[2];
+ bool emulating;
+ } emulated;
+ };
+#endif
+ } reader;
+
+ struct {
+ uint8_t mode;
+ uint8_t fsm_state;
+#if MYNEWT_VAL(BTN_USE_EVENT_FILTERING) && \
+ ( MYNEWT_VAL(BTN_GENERATE_EVENT_STATE) || \
+ MYNEWT_VAL(BTN_GENERATE_EVENT_ACTION) )
+ struct {
+ bool enabled;
+#if MYNEWT_VAL(BTN_GENERATE_EVENT_STATE)
+ uint8_t state;
+#endif
Review comment:
For smaller variables like this, I'd prefer to just have them always
declared in the struct itself. You are only wasting a couple of bytes for a
few buttons, and it makes it easier to debug not to have variable elements in
structures. I think the button emulation above makes sense to #ifdef out, but
these variables should probably just be there IMO.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services