| 1 | /* |
| 2 | * Generic button ops |
| 3 | * |
| 4 | * Copyright (C) 2019 - 2020 Andy Green <andy@warmcat.com> |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 22 | * IN THE SOFTWARE. |
| 23 | * |
| 24 | * Leverages the lws generic gpio pieces to bind gpio buttons to smd events |
| 25 | */ |
| 26 | |
| 27 | #if !defined(__LWS_BUTTON_H__) |
| 28 | #define __LWS_BUTTON_H__ |
| 29 | |
| 30 | typedef uint16_t lws_button_idx_t; |
| 31 | |
| 32 | /* actual minimum may be 1 x RTOS tick depending on platform */ |
| 33 | #define LWS_BUTTON_MON_TIMER_MS 5 |
| 34 | |
| 35 | typedef void (*lws_button_cb_t)(void *opaque, lws_button_idx_t idx, int state); |
| 36 | |
| 37 | /* These are specified in ms but the granularity is LWS_BUTTON_MON_TIMER_MS, |
| 38 | * which may have been rounded up to an RTOS tick depending on platform */ |
| 39 | |
| 40 | enum { |
| 41 | LWSBTNRGMFLAG_CLASSIFY_DOUBLECLICK = (1 << 0) |
| 42 | }; |
| 43 | |
| 44 | typedef struct lws_button_regime { |
| 45 | uint16_t ms_min_down; |
| 46 | uint16_t ms_min_down_longpress; |
| 47 | uint16_t ms_up_settle; |
| 48 | uint16_t ms_doubleclick_grace; |
| 49 | uint16_t ms_repeat_down; |
| 50 | uint8_t flags; |
| 51 | /**< when double-click classification is enabled, clicks are delayed |
| 52 | * by ms_min_down + ms_doubleclick_grace to wait and see if it will |
| 53 | * become a double-click. Set LWSBTNRGMFLAG_CLASSIFY_DOUBLECLICK to |
| 54 | * enable it or leave that bit at 0 to get faster single-click |
| 55 | * classification. |
| 56 | */ |
| 57 | } lws_button_regime_t; |
| 58 | |
| 59 | /* |
| 60 | * This is the const part of the button controller, describing the static |
| 61 | * bindings to gpio, and lws_smd event name information |
| 62 | */ |
| 63 | |
| 64 | typedef struct lws_button_map { |
| 65 | _lws_plat_gpio_t gpio; |
| 66 | const char *smd_interaction_name; |
| 67 | const lws_button_regime_t *regime; |
| 68 | /**< a default regime is applied if this is left NULL */ |
| 69 | } lws_button_map_t; |
| 70 | |
| 71 | typedef struct lws_button_controller { |
| 72 | const char *smd_bc_name; |
| 73 | const lws_gpio_ops_t *gpio_ops; |
| 74 | const lws_button_map_t *button_map; |
| 75 | lws_button_idx_t active_state_bitmap; |
| 76 | uint8_t count_buttons; |
| 77 | } lws_button_controller_t; |
| 78 | |
| 79 | struct lws_button_state; /* opaque */ |
| 80 | |
| 81 | /** |
| 82 | * lws_button_controller_create() - instantiate a button controller |
| 83 | * |
| 84 | * \param ctx: the lws_context |
| 85 | * \param controller: the static controller definition |
| 86 | * |
| 87 | * Instantiates a button controller from a static definition of the buttons |
| 88 | * and their smd names, and active levels, and binds it to a gpio implementation |
| 89 | */ |
| 90 | |
| 91 | LWS_VISIBLE LWS_EXTERN struct lws_button_state * |
| 92 | lws_button_controller_create(struct lws_context *ctx, |
| 93 | const lws_button_controller_t *controller); |
| 94 | |
| 95 | /** |
| 96 | * lws_button_controller_destroy() - destroys a button controller |
| 97 | * |
| 98 | * \param bcs: button controller state previously created |
| 99 | * |
| 100 | * Disables all buttons and then destroys and frees a previously created |
| 101 | * button controller. |
| 102 | */ |
| 103 | |
| 104 | LWS_VISIBLE LWS_EXTERN void |
| 105 | lws_button_controller_destroy(struct lws_button_state *bcs); |
| 106 | |
| 107 | |
| 108 | LWS_VISIBLE LWS_EXTERN lws_button_idx_t |
| 109 | lws_button_get_bit(struct lws_button_state *bcs, const char *name); |
| 110 | |
| 111 | /* |
| 112 | * lws_button_enable() - enable and disable buttons |
| 113 | */ |
| 114 | |
| 115 | LWS_VISIBLE LWS_EXTERN void |
| 116 | lws_button_enable(struct lws_button_state *bcs, |
| 117 | lws_button_idx_t _reset, lws_button_idx_t _set); |
| 118 | |
| 119 | #endif |
| 120 | |
| 121 | |