| Digital Power Starter Kit 3 Firmware dsPIC33C Boost Converter Voltage Mode Control Example |

app_push_button.c
1 /*
2 * File: app_push_button.c
3 * Author: M91406
4 *
5 * Created on March 12, 2020, 12:10 PM
6 */
7
8
9 #include "config/hal.h"
10 #include "app_push_button.h"
11 #include "lcd/app_lcd.h"
12
13 // PRIVATE VARIABLE DELARATIONS
14
15 #define PUSH_BUTTON_DEBOUNCE_DELAY_DEFAULT 1 // Push Button needs to be pressed >20ms to trip a PUSH BUTTON switch event
16 #define PUSH_BUTTON_LONG_PRESS_DELAY_DEFAULT 24 // Push Button needs to be pressed >500ms to trip a PUSH BUTTON switch event
17
18 volatile PUSH_BUTTON_OBJECT_t push_button;
19
20 // Push Button Events
21 volatile uint16_t appPushButton_EventButtonDown(void);
22 volatile uint16_t appPushButton_EventButtonPressed(void);
23 volatile uint16_t appPushButton_EventButtonLongPress(void);
24 volatile uint16_t appPushButton_EventButtonUp(void);
25
26 /*********************************************************************************
27 * @ingroup app-layer-push-button-functions-public
28 * @fn volatile uint16_t appPushButton_Initialize(void)
29 * @brief Initializes the USER push button on DPSK3
30 * @param void
31 * @return unsigned integer (0=failure, 1=success)
32 *
33 * @details
34 * DPSK3 has one on-board push button labeled 'USER' which is used in this
35 * application example to switch between different LCD screens allowing the
36 * user to view runtime data.
37 *
38 * This function initializes the push button function driver used by the
39 * push button monitoring function appPushButton_Execute() by setting
40 * user defined delays to debounce the switching edge and set up filters
41 * for short and long press events.
42 *
43 **********************************************************************************/
44
45 volatile uint16_t appPushButton_Initialize(void)
46 {
47 volatile uint16_t retval = 1;
48
49 retval &= drv_PushButton_Initialize(&push_button);
50
51 push_button.status.bits.enabled = false;
52 push_button.debounce_delay = PUSH_BUTTON_DEBOUNCE_DELAY_DEFAULT;
53 push_button.long_press_delay = PUSH_BUTTON_LONG_PRESS_DELAY_DEFAULT;
54
55 push_button.event_btn_down = &appPushButton_EventButtonDown;
56 push_button.event_btn_up = &appPushButton_EventButtonUp;
57 push_button.event_pressed = NULL; // Event not used
58 push_button.event_long_press = &appPushButton_EventButtonLongPress;
59
60 return(retval);
61 }
62
63 /*********************************************************************************
64 * @ingroup app-layer-push-button-functions-public
65 * @fn volatile uint16_t appPushButton_Execute(void)
66 * @brief Executes the USER push button monitor
67 * @param void
68 * @return unsigned integer (0=failure, 1=success)
69 *
70 * @details
71 * DPSK3 has one on-board push button labeled 'USER' which is used in this
72 * application example to switch between different LCD screens allowing the
73 * user to view runtime data.
74 *
75 * All user settings will get reset. The PUSH_BUTTON_OBJECT_t data
76 * object holding all user-defined settings of the push-button object need
77 * to be re-initialized before this function driver can be used again. *
78 *
79 **********************************************************************************/
80
81 volatile uint16_t appPushButton_Execute(void)
82 {
83 volatile uint16_t retval = 1;
84
85 DBGPIN1_Set();
86
87 retval &= drv_PushButton_Execute(&push_button);
88
89 DBGPIN1_Clear();
90
91 return(retval);
92 }
93
94 /*********************************************************************************
95 * @ingroup app-layer-push-button-functions-public
96 * @fn volatile uint16_t appPushButton_Start(void)
97 * @brief Starts the push button data object
98 * @param void
99 * @return unsigned integer (0=failure, 1=success)
100 *
101 * @details
102 * This function is used to unload all push button function driver data
103 * objects and free their resources.
104 *
105 * This function enables the pre-configured general purpose I/O monitoring
106 * software function detecting the status of the on-board push button 'USER',
107 * scanning for Long Press events, which will trigger the switch-over between
108 * different LCD screens.
109 *
110 * **********************************************************************************/
111
112 volatile uint16_t appPushButton_Start(void) {
113
114 volatile uint16_t retval = 1;
115
116 push_button.status.bits.enabled = true;
117 retval &= (uint16_t)(push_button.status.bits.enabled);
118
119 return(retval);
120 }
121
122 /*********************************************************************************
123 * @ingroup app-layer-push-button-functions-public
124 * @fn volatile uint16_t appPushButton_Dispose(void)
125 * @brief Unloads the push button data object and frees its resources
126 * @param void
127 * @return unsigned integer (0=failure, 1=success)
128 *
129 * @details
130 * This function is used to unload all push button function driver data
131 * objects and free their resources.
132 *
133 * All user settings will get reset. The PUSH_BUTTON_OBJECT_t data
134 * object holding all user-defined settings of the push-button object need
135 * to be re-initialized before this function driver can be used again. *
136 **********************************************************************************/
137
138 volatile uint16_t appPushButton_Dispose(void)
139 {
140 volatile uint16_t retval = 1;
141
142 retval &= drv_PushButton_Dispose(&push_button);
143
144 return(retval);
145 }
146
147 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
148 // PRIVATE FUNCTIONS
149 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
150
151 /*********************************************************************************
152 * @ingroup app-layer-push-button-functions-private
153 * @fn volatile uint16_t appPushButton_EventButtonDown(void)
154 * @brief Push button event raised at the "pressed" transition
155 * @param void
156 * @return unsigned integer (0=failure, 1=success)
157 *
158 * @details
159 * This function is called when the push button detects a transition event from
160 * 'not being pressed' to 'being pressed'. Enabling this interrupt requires to
161 * declare the function pointer to this function in the push button data object
162 * event_btn_down data field. If this data field remains uninitialized or is set
163 * to NULL, this even is disabled.
164 *
165 **********************************************************************************/
166
167 volatile uint16_t appPushButton_EventButtonDown(void)
168 {
169 Nop();
170 return(1);
171 }
172
173 /*********************************************************************************
174 * @ingroup app-layer-push-button-functions-private
175 * @fn volatile uint16_t appPushButton_EventButtonUp(void)
176 * @brief Push button event raised at the "not pressed" transition
177 * @param void
178 * @return unsigned int (0=failure, 1=success)
179 *
180 * @details
181 * This function is called when the push button detects a transition event from
182 * 'being pressed' to 'not being pressed'. Enabling this interrupt requires to
183 * declare the function pointer to this function in the push button data object
184 * event_btn_up data field. If this data field remains uninitialized or is set
185 * to NULL, this even is disabled.
186 *
187 **********************************************************************************/
188
189 volatile uint16_t appPushButton_EventButtonUp(void)
190 {
191 Nop();
192 return(1);
193 }
194
195 /*********************************************************************************
196 * @ingroup app-layer-push-button-functions-private
197 * @fn volatile uint16_t appPushButton_EventButtonPressed(void)
198 * @brief Push button event raised at the "is pressed" detection
199 * @param void
200 * @return unsigned int (0=failure, 1=success)
201 *
202 * @details
203 * This function is called after the push button detects a transition event from
204 * 'not being pressed' to 'being pressed'. Enabling this interrupt requires to
205 * declare the function pointer to this function in the push button data object
206 * event_btn_pressed data field. If this data field remains uninitialized or is set
207 * to NULL, this even is disabled.
208 *
209 **********************************************************************************/
210
211 volatile uint16_t appPushButton_EventButtonPressed(void)
212 {
213 Nop();
214 return(1);
215 }
216
217 /*********************************************************************************
218 * @ingroup app-layer-push-button-functions-private
219 * @fn volatile uint16_t appPushButton_EventButtonLongPress(void)
220 * @brief Push button event raised at the "is long pressed" detection
221 * @param void
222 * @return unsigned int (0=failure, 1=success)
223 *
224 * @details
225 * This function is called after the push button event PRESSED has been detected
226 * and the Long Press Delay has expired without detecting a Button Up event.
227 * Enabling this interrupt requires to declare the function pointer to this
228 * function in the push button data object event_long_press data field. If this
229 * data field remains uninitialized or is set to NULL, this even is disabled.
230 *
231 **********************************************************************************/
232
233 volatile uint16_t appPushButton_EventButtonLongPress(void) {
234
235 lcd.screen += 1; // increment screen index
236 if (lcd.screen >= lcd.screens) // Roll-over after last screen
237 lcd.screen = 0; // Reset to default view
238 return(1);
239 }
240
241
242 // end of file
PUSH_BUTTON_OBJECT_s::long_press_delay
volatile uint16_t long_press_delay
Number of call cycles until a "long press" switch event is triggered.
Definition: drv_push_button.h:112
LCD_s::screens
volatile uint16_t screens
Definition: app_lcd.h:58
LCD_s::screen
volatile uint16_t screen
Definition: app_lcd.h:57
PUSH_BUTTON_OBJECT_s::event_pressed
volatile uint16_t(* event_pressed)(void)
Function pointer to user function triggering a LONG_PRESS event.
Definition: drv_push_button.h:115
PUSH_BUTTON_OBJECT_s::event_long_press
volatile uint16_t(* event_long_press)(void)
Function pointer to user function triggering a LONG_PRESS event.
Definition: drv_push_button.h:114
PUSH_BUTTON_OBJECT_s::debounce_delay
volatile uint16_t debounce_delay
Number of call cycles until a switch event is triggered.
Definition: drv_push_button.h:111
PUSH_BUTTON_OBJECT_s::status
volatile struct PUSH_BUTTON_STATUS_s status
Status word of the switch object.
Definition: drv_push_button.h:110
PUSH_BUTTON_OBJECT_s::event_btn_up
volatile uint16_t(* event_btn_up)(void)
Function pointer to user function triggering a RELEASE event.
Definition: drv_push_button.h:116
PUSH_BUTTON_STATUS_s::enabled
volatile bool enabled
Bit 15: Enables/disables the Switch button object.
Definition: drv_push_button.h:89
PUSH_BUTTON_OBJECT_s
Definition: drv_push_button.h:108
PUSH_BUTTON_OBJECT_s::event_btn_down
volatile uint16_t(* event_btn_down)(void)
Function pointer to user function triggering a PRESSED event.
Definition: drv_push_button.h:113