3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2018 Nest Labs, Inc.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 * Describes a Button tied to a GPIO that provides debouncing and polling APIs
29 #include "driver/gpio.h"
31 #include "esp_system.h"
32 #include "freertos/FreeRTOS.h"
33 #include "freertos/task.h"
42 * @param gpioNum The GPIO pin this button should keep track of
43 * @param debouncePeriod The debouncing period in FreeRTOS ticks
46 esp_err_t Init(gpio_num_t gpioNum, uint16_t debouncePeriod);
49 * Poll on the button and read its current state
51 * @return true If a button event occurred
52 * @return false If no button event occurred
57 * Returns the state of the button
59 * @return true If the button is pressed
60 * @return false If the button is not pressed or released if poll() is true.
65 * Get the time timestamp since the button entered its current state
67 * @return uint32_t The time in milliseconds since the app started
69 uint32_t GetStateStartTime();
72 * Get the duration in milliseconds since the button entered its current state
74 * @return uint32_t The time in milliseconds
76 uint32_t GetStateDuration();
79 * Get the duration in milliseconds the button spent in its previous state
81 * @return uint32_t The time in milliseconds
83 uint32_t GetPrevStateDuration();
87 uint32_t mLastReadTime;
89 uint32_t mStateStartTime;
91 uint32_t mPrevStateDur;
94 uint16_t mDebouncePeriod;
95 // true when button is pressed
97 bool mLastPolledState;
100 inline bool Button::IsPressed()
105 inline uint32_t Button::GetStateStartTime()
107 return mStateStartTime * portTICK_PERIOD_MS;
110 inline uint32_t Button::GetPrevStateDuration()
112 return mPrevStateDur * portTICK_PERIOD_MS;