Cascoda SDK
Cascoda SDK for building software to run with CA-821x transceivers
cascoda_btn.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023, Cascoda Ltd.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * 3. Neither the name of the copyright holder nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 /*
29  * Button / LED Functions for Cascoda UI
30 */
31 
32 #ifndef CASCODA_BTN_H
33 #define CASCODA_BTN_H
34 
36 #include "ca821x_api.h"
37 #include "ca821x_error.h"
38 
39 /* max. number of LED/Button pairs on board */
40 #define NUM_LEDBTN 4
41 
42 /* minimum press time for additional de-bouncing in [ms] */
43 #define BTN_MIN_PRESS_TIME 5
44 
45 #ifndef BTN_SHARED_SENSE_DELAY
46 #define BTN_SHARED_SENSE_DELAY 2
47 #endif
48 
49 /* Execute short press callback when pressed or on release
50  * Note: BTN_SHORTPRESS_RELEASED should only be used when
51  * the same button is being registered with a long press
52  * callback.
53  */
55 {
58 };
59 
60 /* Button state */
62 {
64  BTN_RELEASED = 1
65 };
66 
67 /* pin types */
69 {
74 };
75 
76 /* Whether an interrupt is registered for the pin or not */
78 {
81 };
82 
83 /* dvbd callback type definition */
84 typedef void (*btn_callback)(void* context);
85 
87 typedef struct btn_callback_info
88 {
96  void* holdContext;
98  uint32_t currentPressTime;
100  uint32_t holdTimeInterval;
101  uint32_t holdTimeLast;
102  uint8_t lastState;
103  uint8_t shortPressMode;
105 
106 /* Map from Button declaration to actual module pin number */
107 extern uint8_t registeredPinMappings[];
108 
109 /* functions */
110 
117 ca_error Btn_RegisterLEDOutput(uint8_t ledBtn);
118 
125 ca_error Btn_RegisterButtonInput(uint8_t ledBtn);
126 
133 ca_error Btn_RegisterButtonIRQInput(uint8_t ledBtn);
134 
141 ca_error Btn_RegisterSharedButtonLED(uint8_t ledBtn);
142 
150 
157 ca_error Btn_DeRegister(uint8_t ledBtn);
158 
168 ca_error Btn_SetButtonShortPressCallback(uint8_t ledBtn, btn_callback callback, void* context, uint8_t shortPressMode);
169 
179 ca_error Btn_SetButtonLongPressCallback(uint8_t ledBtn, btn_callback callback, void* context, uint32_t timeThreshold);
180 
190 ca_error Btn_SetButtonHoldCallback(uint8_t ledBtn, btn_callback callback, void* context, uint32_t TimeInterval);
191 
199 ca_error Btn_SetLED(uint8_t ledBtn, uint8_t val);
200 
208 ca_error Btn_Sense(uint8_t ledBtn, uint8_t* val);
209 
217 ca_error Btn_SenseOutput(uint8_t ledBtn, uint8_t* val);
218 
225 
232 ca_error Btn_HandleButtonCallbacks(btn_callback_info* callback, uint8_t pressed);
233 
238 void Btn_IncrementGPIOWakeup(void);
239 
246 
251 void Btn_SetSleepPermanently(void);
252 
258 bool Btn_CanSleep(void);
259 
265 ca_error Btn_DevboardSleep(uint32_t aSleepTime, struct ca821x_dev* pDeviceRef);
266 
267 #endif // CASCODA_BTN_H
The main ca821x-api include file.
Global error declarations for use across the Cascoda SDK.
ca_error Btn_RegisterButtonInput(uint8_t ledBtn)
Register button input.
Definition: cascoda_btn.c:89
ca_error Btn_HandleButtonCallbacks(btn_callback_info *callback, uint8_t pressed)
Process the button callbacks and timing.
Definition: cascoda_btn.c:324
ca_error Btn_DeRegister(uint8_t ledBtn)
De-Register an LED or Button Pin.
Definition: cascoda_btn.c:181
ca_error Btn_SenseOutput(uint8_t ledBtn, uint8_t *val)
Get the output state of the LED.
Definition: cascoda_btn.c:273
ca_error Btn_DecrementGPIOWakeup(void)
One fewer GPIO is now being used for wakeup.
Definition: cascoda_btn.c:389
ca_error Btn_SetButtonShortPressCallback(uint8_t ledBtn, btn_callback callback, void *context, uint8_t shortPressMode)
Set a callback function to a button when it is short pressed.
Definition: cascoda_btn.c:201
bool Btn_CanSleep(void)
Check if all buttons have been handled.
Definition: cascoda_btn.c:412
ca_error Btn_RegisterSharedButtonLED(uint8_t ledBtn)
Register button as shared input/output.
Definition: cascoda_btn.c:135
ca_error Btn_PollButtons(void)
Main polling function to activate callbacks for any buttons that are currently being pressed.
Definition: cascoda_btn.c:280
btn_shortpress_mode
Definition: cascoda_btn.h:55
@ BTN_SHORTPRESS_RELEASED
Definition: cascoda_btn.h:57
@ BTN_SHORTPRESS_PRESSED
Definition: cascoda_btn.h:56
void Btn_SetSleepPermanently(void)
Register that the device will be put to sleep permanently.
Definition: cascoda_btn.c:405
void Btn_IncrementGPIOWakeup(void)
One additional GPIO is now being used for wakeup.
Definition: cascoda_btn.c:382
ca_error Btn_SetLED(uint8_t ledBtn, uint8_t val)
Set the state of the LED.
Definition: cascoda_btn.c:228
struct btn_callback_info btn_callback_info
Callbacks and associated timers for the buttons.
void(* btn_callback)(void *context)
Definition: cascoda_btn.h:84
btn_pin_int
Definition: cascoda_btn.h:78
@ PIN_INTERRUPT
Definition: cascoda_btn.h:80
@ PIN_NO_INTERRUPT
Definition: cascoda_btn.h:79
ca_error Btn_RegisterLEDOutput(uint8_t ledBtn)
Register LED output (open drain)
Definition: cascoda_btn.c:74
ca_error Btn_SetButtonHoldCallback(uint8_t ledBtn, btn_callback callback, void *context, uint32_t TimeInterval)
Set a callback function to a button when it is held.
Definition: cascoda_btn.c:219
ca_error Btn_DevboardSleep(uint32_t aSleepTime, struct ca821x_dev *pDeviceRef)
Put board to sleep / powerdown.
Definition: cascoda_btn.c:427
uint8_t registeredPinMappings[]
Definition: cascoda_btn.c:41
ca_error Btn_Sense(uint8_t ledBtn, uint8_t *val)
Get the state of the LED/Button.
Definition: cascoda_btn.c:235
ca_error Btn_RegisterSharedIRQButtonLED(uint8_t ledBtn)
Register button as shared input/output with interrupt (for sleepy devices)
Definition: cascoda_btn.c:157
btn_button_state
Definition: cascoda_btn.h:62
@ BTN_PRESSED
Definition: cascoda_btn.h:63
@ BTN_RELEASED
Definition: cascoda_btn.h:64
ca_error Btn_RegisterButtonIRQInput(uint8_t ledBtn)
Register button input with interrupt (for sleepy devices)
Definition: cascoda_btn.c:111
ca_error Btn_SetButtonLongPressCallback(uint8_t ledBtn, btn_callback callback, void *context, uint32_t timeThreshold)
Set a callback function to a button when it is long pressed.
Definition: cascoda_btn.c:210
btn_pin_type
Definition: cascoda_btn.h:69
@ PINTYPE_BTN
Definition: cascoda_btn.h:72
@ PINTYPE_SHARED
Definition: cascoda_btn.h:73
@ PINTYPE_LED
Definition: cascoda_btn.h:71
@ PINTYPE_NONE
Definition: cascoda_btn.h:70
Type definitions used by Cascoda baremetal drivers.
ca_error
Cascoda error type.
Definition: ca821x_error.h:51
Callbacks and associated timers for the buttons.
Definition: cascoda_btn.h:88
void * shortPressContext
Context for shortPressCallback.
Definition: cascoda_btn.h:90
void * holdContext
Context for holdCallback.
Definition: cascoda_btn.h:96
btn_callback shortPressCallback
Callback function for a short button press.
Definition: cascoda_btn.h:89
uint32_t longPressTimeThreshold
Time limit [ms] above which a button press is considered a long press.
Definition: cascoda_btn.h:99
uint32_t holdTimeLast
Time [ms] when hold callback was last called.
Definition: cascoda_btn.h:101
uint8_t lastState
Last button state.
Definition: cascoda_btn.h:102
uint32_t currentPressTime
Time [ms] when the button has been pressed.
Definition: cascoda_btn.h:98
void * longPressContext
Context for longPressCallback.
Definition: cascoda_btn.h:93
btn_callback holdCallback
Callback function for when the button is held.
Definition: cascoda_btn.h:95
btn_callback longPressCallback
Callback function for a long button press.
Definition: cascoda_btn.h:92
uint8_t shortPressMode
Short Press callback when pressed or on release.
Definition: cascoda_btn.h:103
uint32_t holdTimeInterval
Time interval [ms] for triggering the hold function.
Definition: cascoda_btn.h:100
CA-821x Device reference struct.
Definition: ca821x_api.h:123