Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / examples / all-clusters-app / esp32 / main / include / Button.h
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    Copyright (c) 2018 Nest Labs, Inc.
5  *    All rights reserved.
6  *
7  *    Licensed under the Apache License, Version 2.0 (the "License");
8  *    you may not use this file except in compliance with the License.
9  *    You may obtain a copy of the License at
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *    Unless required by applicable law or agreed to in writing, software
14  *    distributed under the License is distributed on an "AS IS" BASIS,
15  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *    See the License for the specific language governing permissions and
17  *    limitations under the License.
18  */
19
20 /**
21  * @file Button.h
22  *
23  * Describes a Button tied to a GPIO that provides debouncing and polling APIs
24  *
25  **/
26
27 #pragma once
28
29 #include "driver/gpio.h"
30 #include "esp_log.h"
31 #include "esp_system.h"
32 #include "freertos/FreeRTOS.h"
33 #include "freertos/task.h"
34
35 class Button
36 {
37 public:
38     /**
39      * @brief
40      *  Initialize a button
41      *
42      * @param gpioNum           The GPIO pin this button should keep track of
43      * @param debouncePeriod    The debouncing period in FreeRTOS ticks
44      * @return esp_err_t
45      */
46     esp_err_t Init(gpio_num_t gpioNum, uint16_t debouncePeriod);
47     /**
48      * @brief
49      *  Poll on the button and read its current state
50      *
51      * @return true     If a button event occurred
52      * @return false    If no button event occurred
53      */
54     bool Poll();
55     /**
56      * @brief
57      *  Returns the state of the button
58      *
59      * @return true     If the button is pressed
60      * @return false    If the button is not pressed or released if poll() is true.
61      */
62     bool IsPressed();
63     /**
64      * @brief
65      *  Get the time timestamp since the button entered its current state
66      *
67      * @return uint32_t The time in milliseconds since the app started
68      */
69     uint32_t GetStateStartTime();
70     /**
71      * @brief
72      *  Get the duration in milliseconds since the button entered its current state
73      *
74      * @return uint32_t The time in milliseconds
75      */
76     uint32_t GetStateDuration();
77     /**
78      * @brief
79      *  Get the duration in milliseconds the button spent in its previous state
80      *
81      * @return uint32_t The time in milliseconds
82      */
83     uint32_t GetPrevStateDuration();
84
85 private:
86     // in ticks
87     uint32_t mLastReadTime;
88     // in ticks
89     uint32_t mStateStartTime;
90     // in ticks
91     uint32_t mPrevStateDur;
92     gpio_num_t mGPIONum;
93     // in ticks
94     uint16_t mDebouncePeriod;
95     // true when button is pressed
96     bool mState;
97     bool mLastPolledState;
98 };
99
100 inline bool Button::IsPressed()
101 {
102     return mState;
103 }
104
105 inline uint32_t Button::GetStateStartTime()
106 {
107     return mStateStartTime * portTICK_PERIOD_MS;
108 }
109
110 inline uint32_t Button::GetPrevStateDuration()
111 {
112     return mPrevStateDur * portTICK_PERIOD_MS;
113 }