sterlinghughes commented on a change in pull request #889: implementation of
common button events
URL: https://github.com/apache/mynewt-core/pull/889#discussion_r173254791
##########
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
+#if MYNEWT_VAL(BTN_GENERATE_EVENT_ACTION)
+ uint8_t action;
+#endif
+ } filter;
+#endif
+#if MYNEWT_VAL(BTN_USE_DOUBLE) || \
+ MYNEWT_VAL(BTN_USE_LONG ) || \
+ MYNEWT_VAL(BTN_USE_REPEAT)
+ struct os_callout callout;
+#endif
+ } notify;
+} btn_t;
Review comment:
Can you add doxygen documentation to the header file, this will make it
easier to autogenerate this as a part of the docs.
----------------------------------------------------------------
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