ButtonManager.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright (c) 2021-2023 Project CHIP Authors
  3. * All rights reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #pragma once
  18. #include <vector>
  19. #include <zephyr/device.h>
  20. #define STATE_HIGH 1
  21. #define STATE_LOW 0
  22. class Button
  23. {
  24. public:
  25. void Configure(const gpio_dt_spec * input_button_dt, const gpio_dt_spec * output_button_dt, void (*callback)(void));
  26. void Configure(const gpio_dt_spec * input_button_dt, void (*callback)(void));
  27. void Poll(Button * previous);
  28. void PollIRQ(const struct device * dev, uint32_t pins);
  29. void SetCallback(void (*callback)(void));
  30. private:
  31. int Init(void);
  32. int Deinit(void);
  33. const struct gpio_dt_spec * mInput_button;
  34. const struct gpio_dt_spec * mOutput_matrix_pin;
  35. int mPreviousState = STATE_LOW;
  36. struct gpio_callback mButton_cb_data;
  37. void (*mCallback)(void) = NULL;
  38. };
  39. class ButtonManager
  40. {
  41. public:
  42. void Init(void);
  43. void Poll(void);
  44. void PollIRQ(const struct device * dev, uint32_t pins);
  45. void AddButton(Button & button);
  46. void SetCallback(unsigned int index, void (*callback)(void));
  47. private:
  48. std::vector<Button> mButtons;
  49. friend ButtonManager & ButtonManagerInst(void);
  50. static ButtonManager sInstance;
  51. };
  52. /**
  53. * Returns the KeyManager singleton object.
  54. */
  55. inline ButtonManager & ButtonManagerInst(void)
  56. {
  57. return ButtonManager::sInstance;
  58. }