|
| 1 | +/* |
| 2 | + * Copyright 2019, Cypress Semiconductor Corporation or a subsidiary of |
| 3 | + * Cypress Semiconductor Corporation. All Rights Reserved. |
| 4 | + * |
| 5 | + * This software, including source code, documentation and related |
| 6 | + * materials ("Software"), is owned by Cypress Semiconductor Corporation |
| 7 | + * or one of its subsidiaries ("Cypress") and is protected by and subject to |
| 8 | + * worldwide patent protection (United States and foreign), |
| 9 | + * United States copyright laws and international treaty provisions. |
| 10 | + * Therefore, you may use this Software only as provided in the license |
| 11 | + * agreement accompanying the software package from which you |
| 12 | + * obtained this Software ("EULA"). |
| 13 | + * If no EULA applies, Cypress hereby grants you a personal, non-exclusive, |
| 14 | + * non-transferable license to copy, modify, and compile the Software |
| 15 | + * source code solely for use in connection with Cypress's |
| 16 | + * integrated circuit products. Any reproduction, modification, translation, |
| 17 | + * compilation, or representation of this Software except as specified |
| 18 | + * above is prohibited without the express written permission of Cypress. |
| 19 | + * |
| 20 | + * Disclaimer: THIS SOFTWARE IS PROVIDED AS-IS, WITH NO WARRANTY OF ANY KIND, |
| 21 | + * EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT, IMPLIED |
| 22 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Cypress |
| 23 | + * reserves the right to make changes to the Software without notice. Cypress |
| 24 | + * does not assume any liability arising out of the application or use of the |
| 25 | + * Software or any product or circuit described in the Software. Cypress does |
| 26 | + * not authorize its products for use in any products where a malfunction or |
| 27 | + * failure of the Cypress product may reasonably be expected to result in |
| 28 | + * significant property damage, injury or death ("High Risk Product"). By |
| 29 | + * including Cypress's product in a High Risk Product, the manufacturer |
| 30 | + * of such system or application assumes all risk of such use and in doing |
| 31 | + * so agrees to indemnify Cypress against all liability. |
| 32 | + */ |
| 33 | + |
| 34 | +/** @file |
| 35 | + * |
| 36 | + * Button control functionality for a dimmer |
| 37 | + * |
| 38 | + */ |
| 39 | + |
| 40 | +#include "sparcommon.h" |
| 41 | + |
| 42 | +#include "wiced_bt_mesh_models.h" |
| 43 | +#include "wiced_bt_mesh_app.h" |
| 44 | +#include "wiced_bt_trace.h" |
| 45 | +#include "wiced_hal_gpio.h" |
| 46 | +#include "wiced_hal_aclk.h" |
| 47 | +#include "wiced_platform.h" |
| 48 | +#include "button_control.h" |
| 49 | + |
| 50 | +/****************************************************************************** |
| 51 | + * Constants |
| 52 | + ******************************************************************************/ |
| 53 | +#define NUM_STEPS 9 |
| 54 | +static uint16_t button_level_step[NUM_STEPS] = |
| 55 | +{ |
| 56 | + 0x8000, 0xa000, 0xC000, 0xE000, 0x0000, 0x2000, 0x4000, 0x6000, 0x7FFF, |
| 57 | +}; |
| 58 | + |
| 59 | +extern wiced_platform_button_config_t platform_button[]; |
| 60 | +/****************************************************************************** |
| 61 | + * Function Declarations |
| 62 | + ******************************************************************************/ |
| 63 | +static void button_interrupt_handler(void* user_data, uint8_t value); |
| 64 | +static void button_set_level(wiced_bool_t is_instant, wiced_bool_t is_final); |
| 65 | +static void button_timer_callback(uint32_t arg); |
| 66 | + |
| 67 | +/****************************************************************************** |
| 68 | + * Variables Definitions |
| 69 | + ******************************************************************************/ |
| 70 | +uint8_t button_step = 0; |
| 71 | +uint64_t button_pushed_time = 0; |
| 72 | +wiced_bool_t button_direction = WICED_FALSE; |
| 73 | +wiced_bool_t button_level_moving = WICED_FALSE; |
| 74 | +uint32_t transation_start_time = 0; |
| 75 | +wiced_timer_t button_timer; |
| 76 | +uint32_t button_previous_value; |
| 77 | +/****************************************************************************** |
| 78 | + * Function Definitions |
| 79 | + ******************************************************************************/ |
| 80 | +void button_control_init(void) |
| 81 | +{ |
| 82 | + wiced_init_timer(&button_timer, &button_timer_callback, 0, WICED_MILLI_SECONDS_TIMER); |
| 83 | + button_previous_value = platform_button[WICED_PLATFORM_BUTTON_1].default_state; |
| 84 | +} |
| 85 | + |
| 86 | +void button_hardware_init(void) |
| 87 | +{ |
| 88 | + /* Configure buttons available on the platform */ |
| 89 | +#if defined(CYW20706A2) |
| 90 | + wiced_hal_gpio_configure_pin(WICED_GPIO_BUTTON, WICED_GPIO_BUTTON_SETTINGS(GPIO_EN_INT_BOTH_EDGE), WICED_GPIO_BUTTON_DEFAULT_STATE); |
| 91 | + wiced_hal_gpio_register_pin_for_interrupt(WICED_GPIO_BUTTON, button_interrupt_handler, NULL); |
| 92 | +#elif (defined(CYW20735B0) || defined(CYW20719B0) || defined(CYW20721B0)) |
| 93 | + wiced_hal_gpio_register_pin_for_interrupt(WICED_GPIO_PIN_BUTTON, button_interrupt_handler, NULL); |
| 94 | + wiced_hal_gpio_configure_pin(WICED_GPIO_PIN_BUTTON, WICED_GPIO_BUTTON_SETTINGS, GPIO_PIN_OUTPUT_LOW); |
| 95 | +#else |
| 96 | + wiced_platform_register_button_callback(WICED_PLATFORM_BUTTON_1, button_interrupt_handler, NULL, GPIO_EN_INT_BOTH_EDGE); |
| 97 | +#endif |
| 98 | +} |
| 99 | + |
| 100 | + |
| 101 | +/* |
| 102 | + * Process interrupts from the button. |
| 103 | + */ |
| 104 | +void button_interrupt_handler(void* user_data, uint8_t pin) |
| 105 | +{ |
| 106 | + uint32_t value = wiced_hal_gpio_get_pin_input_status(pin); |
| 107 | + uint32_t current_time = wiced_bt_mesh_core_get_tick_count(); |
| 108 | + uint32_t button_pushed_duration; |
| 109 | + |
| 110 | + if (value == button_previous_value) |
| 111 | + { |
| 112 | + WICED_BT_TRACE("interrupt_handler: duplicate pin:%d value:%d current_time:%d\n", pin, value, current_time); |
| 113 | + return; |
| 114 | + } |
| 115 | + button_previous_value = value; |
| 116 | + |
| 117 | + WICED_BT_TRACE("interrupt_handler: pin:%d value:%d current_time:%d\n", pin, value, current_time); |
| 118 | + |
| 119 | + if (value == platform_button[WICED_PLATFORM_BUTTON_1].button_pressed_value) |
| 120 | + { |
| 121 | + button_pushed_time = current_time; |
| 122 | + |
| 123 | + // if button is not released within 500ms, we will start sending move events |
| 124 | + wiced_start_timer(&button_timer, 500); |
| 125 | + return; |
| 126 | + } |
| 127 | + wiced_stop_timer(&button_timer); |
| 128 | + |
| 129 | + // button is released |
| 130 | + button_pushed_duration = current_time - button_pushed_time; |
| 131 | + if (button_pushed_duration < 500) |
| 132 | + { |
| 133 | + button_level_moving = WICED_FALSE; |
| 134 | + |
| 135 | + if (button_step == 0) |
| 136 | + button_step = NUM_STEPS - 1; |
| 137 | + else if (button_step == NUM_STEPS - 1) |
| 138 | + button_step = 0; |
| 139 | + else |
| 140 | + button_step = (button_direction ? NUM_STEPS - 1 : 0); |
| 141 | + button_set_level(WICED_TRUE, WICED_TRUE); |
| 142 | + return; |
| 143 | + } |
| 144 | + else if (button_pushed_duration < 15000) |
| 145 | + { |
| 146 | + // we were moving the level and button is released. |
| 147 | + // set message with ack |
| 148 | + if ((button_step != NUM_STEPS - 1) && (button_step != 0)) |
| 149 | + button_set_level(WICED_FALSE, WICED_TRUE); |
| 150 | + return; |
| 151 | + } |
| 152 | + // More than 15 seconds means factory reset |
| 153 | + mesh_application_factory_reset(); |
| 154 | +} |
| 155 | + |
| 156 | +void button_timer_callback(uint32_t arg) |
| 157 | +{ |
| 158 | + if (!button_level_moving) |
| 159 | + { |
| 160 | + if (button_step == 0) |
| 161 | + { |
| 162 | + button_direction = WICED_TRUE; |
| 163 | + button_step++; |
| 164 | + } |
| 165 | + else if (button_step == NUM_STEPS - 1) |
| 166 | + { |
| 167 | + button_direction = WICED_FALSE; |
| 168 | + button_step--; |
| 169 | + } |
| 170 | + else |
| 171 | + { |
| 172 | + if (button_direction) |
| 173 | + { |
| 174 | + if (button_step != NUM_STEPS - 1) |
| 175 | + button_step++; |
| 176 | + } |
| 177 | + else |
| 178 | + { |
| 179 | + if (button_step != 0) |
| 180 | + button_step--; |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + else |
| 185 | + { |
| 186 | + if (button_direction) |
| 187 | + { |
| 188 | + if (button_step != NUM_STEPS - 1) |
| 189 | + button_step++; |
| 190 | + } |
| 191 | + else |
| 192 | + { |
| 193 | + if (button_step != 0) |
| 194 | + button_step--; |
| 195 | + } |
| 196 | + } |
| 197 | + if ((button_step != NUM_STEPS - 1) && (button_step != 0)) |
| 198 | + { |
| 199 | + button_level_moving = WICED_TRUE; |
| 200 | + wiced_start_timer(&button_timer, 500); |
| 201 | + button_set_level(WICED_FALSE, WICED_FALSE); |
| 202 | + } |
| 203 | + else |
| 204 | + { |
| 205 | + button_level_moving = WICED_FALSE; |
| 206 | + button_set_level(WICED_FALSE, WICED_TRUE); |
| 207 | + } |
| 208 | +} |
| 209 | + |
| 210 | +/* |
| 211 | + * This function tells peer to set the level. Instant transition means that the button |
| 212 | + * is pushed and release, otherwise the button is being pushed, so the transition |
| 213 | + * time is set to 500ms which is the duration between 2 consecutive commands. I.e. we |
| 214 | + * send command to go to the next level every 500ms and duration is 500ms, which should |
| 215 | + * make the transition go smooth. The final parameter indicates that the transition is |
| 216 | + * complete, so we ask peer to send the ack. |
| 217 | + */ |
| 218 | +void button_set_level(wiced_bool_t is_instant, wiced_bool_t is_final) |
| 219 | +{ |
| 220 | + wiced_bt_mesh_level_set_level_t set_data; |
| 221 | + |
| 222 | + set_data.level = button_level_step[button_step]; |
| 223 | + set_data.transition_time = is_instant ? 100 : 500; |
| 224 | + set_data.delay = 0; |
| 225 | + |
| 226 | + WICED_BT_TRACE("Set level:%d transition time:%d final:%d\n", set_data.level, set_data.transition_time, is_final); |
| 227 | + |
| 228 | + wiced_bt_mesh_model_level_client_set(0, is_final, &set_data); |
| 229 | +} |
0 commit comments