switch_driver.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2021 Espressif Systems (Shanghai) CO LTD
  3. * All rights reserved.
  4. *
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Espressif Systems
  13. * integrated circuit in a product or a software update for such product,
  14. * must reproduce the above copyright notice, this list of conditions and
  15. * the following disclaimer in the documentation and/or other materials
  16. * provided with the distribution.
  17. *
  18. * 3. Neither the name of the copyright holder nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software without
  20. * specific prior written permission.
  21. *
  22. * 4. Any software provided in binary form under this license must not be reverse
  23. * engineered, decompiled, modified and/or disassembled.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  29. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #pragma once
  38. #include "driver/gpio.h"
  39. #ifdef __cplusplus
  40. extern "C" {
  41. #endif
  42. /* user should configure which I/O port as toggle switch input, default is GPIO9 */
  43. #define GPIO_INPUT_IO_TOGGLE_SWITCH GPIO_NUM_9
  44. /* config button level depends on the pull up/down setting
  45. push button level is on level = 1 when pull-down enable
  46. push button level is on level = 0 when pull-up enable
  47. */
  48. #define GPIO_INPUT_LEVEL_ON 0
  49. #define ESP_INTR_FLAG_DEFAULT 0
  50. #define PAIR_SIZE(TYPE_STR_PAIR) (sizeof(TYPE_STR_PAIR) / sizeof(TYPE_STR_PAIR[0]))
  51. typedef enum {
  52. SWITCH_IDLE,
  53. SWITCH_PRESS_ARMED,
  54. SWITCH_PRESS_DETECTED,
  55. SWITCH_PRESSED,
  56. SWITCH_RELEASE_DETECTED,
  57. } switch_state_t;
  58. typedef enum {
  59. SWITCH_ON_CONTROL,
  60. SWITCH_OFF_CONTROL,
  61. SWITCH_ONOFF_TOGGLE_CONTROL,
  62. SWITCH_LEVEL_UP_CONTROL,
  63. SWITCH_LEVEL_DOWN_CONTROL,
  64. SWITCH_LEVEL_CYCLE_CONTROL,
  65. SWITCH_COLOR_CONTROL,
  66. } switch_func_t;
  67. typedef struct {
  68. uint32_t pin;
  69. switch_func_t func;
  70. } switch_func_pair_t;
  71. typedef void (*esp_switch_callback_t)(switch_func_pair_t param);
  72. /**
  73. * @brief init function for switch and callback setup
  74. *
  75. * @param button_func_pair pointer of the button pair.
  76. * @param button_num number of button pair.
  77. * @param cb callback pointer.
  78. */
  79. bool switch_driver_init(switch_func_pair_t *button_func_pair, uint8_t button_num, esp_switch_callback_t cb);
  80. #ifdef __cplusplus
  81. } // extern "C"
  82. #endif