Digital Power Starter Kit 3 Firmware
dsPIC33C Buck Converter Voltage Mode Control Example
drv_push_button.c
1 /*
2  * File: switch.c
3  * Author: M91406
4  *
5  * Created on March 12, 2020, 12:10 PM
6  */
7 
8 
9 #include "push_button/app_push_button.h"
10 #include "config/hal.h"
11 
12 // PRIVATE VARIABLE DELARATIONS
13 // (none)
14 
15 
38 volatile uint16_t drv_PushButton_Initialize(volatile struct PUSH_BUTTON_OBJECT_s* pushbtn)
39 {
40  volatile uint16_t retval = 1;
41 
42  SW_USER_Init(); // Initialize GPIO used to read switch
43 
44  // Initializing switch object
45  pushbtn->debounce_delay = 0; // Clear debounce delay
46  pushbtn->long_press_delay = 0; // Clear 'long press" delay
47  pushbtn->status.bits.pressed = false; // Reset PRESSED status
48  pushbtn->status.bits.long_press = false; // Reset LONG_PRESS status
49  pushbtn->status.bits.enabled = false; // Turn off Switch Button
50  pushbtn->status.bits.sw_event = false; // Clear SWITCH EVENT flag
51 
52  return(retval);
53 }
54 
55 
70 volatile uint16_t drv_PushButton_Execute(volatile struct PUSH_BUTTON_OBJECT_s* pushbtn)
71 {
72  volatile uint16_t retval = 1; // Return value
73  static uint16_t press_cnt, release_cnt; // local counters of SWITCH_USER being pressed/released
74  static bool pre_pressed = false, pre_long_press = false;
75 
76  // If switch is disabled, exit here
77  if (!pushbtn->status.bits.enabled)
78  {
79  pushbtn->status.bits.pressed = false; // Mark Switch as RELEASED
80  return(1); // Exit function
81  }
82 
83  // Trigger on a PRESSED event with applied debouncing
84  if ((!SW_USER_Get()) && (!pushbtn->status.bits.pressed)) {
85 
86  // switch button PRESSED event
87  if (++press_cnt > pushbtn->debounce_delay) {
88  pushbtn->status.bits.pressed = true; // Set PRESSED flag
89  if (pushbtn->event_btn_down != NULL) // Raise Button Event
90  pushbtn->event_btn_down();
91  }
92  }
93  // Trigger on a LONG PRESS event with applied debouncing
94  else if ((!SW_USER_Get()) && (pushbtn->status.bits.pressed)) {
95 
96  // switch button LONG PRESS event
97  if (++press_cnt > pushbtn->long_press_delay) {
98 
99  if (!pushbtn->status.bits.long_press) // Long Press Event is triggered for the first time
100  if (pushbtn->event_long_press != NULL) // Raise Button Long Press Event
101  pushbtn->event_long_press();
102 
103  pushbtn->status.bits.long_press = true; // Set LONG_PRESS flag
104  press_cnt = pushbtn->long_press_delay; // Clamp counter to threshold
105 
106  if (pushbtn->event_pressed != NULL) // Raise Button Pressed Event
107  pushbtn->event_pressed();
108 
109  }
110 
111  }
112  // Trigger on a RELEASE event with applied debouncing
113  else if ((SW_USER_Get()) && (pushbtn->status.bits.pressed)) {
114 
115  // switch button RELEASE event
116  if (++release_cnt > pushbtn->debounce_delay) {
117  if (pushbtn->event_btn_up != NULL) // Raise Button Event
118  pushbtn->event_btn_up();
119  pushbtn->status.bits.pressed = false; // Clear PRESSED flag
120  pushbtn->status.bits.long_press = false; // Clear LONG_PRESS flag
121  release_cnt = pushbtn->debounce_delay; // Clamp counter to threshold
122  }
123  }
124  else {
125  press_cnt = 0; // Clear switch debounce counter PRESSED
126  release_cnt = 0; // Clear switch debounce counter RELEASE
127  }
128 
129  // Trigger on switch events
130  pushbtn->status.bits.sw_event = (bool) //(pre_pressed != pushbtn->status.pressed) ;
131  ((pre_pressed != pushbtn->status.bits.pressed) || (pre_long_press != pushbtn->status.bits.long_press));
132 
133  pre_pressed = pushbtn->status.bits.pressed;
134  pre_long_press = pushbtn->status.bits.long_press;
135 
136  return(retval);
137 }
138 
139 
153 volatile uint16_t drv_PushButton_Dispose(volatile struct PUSH_BUTTON_OBJECT_s* pushbtn)
154 {
155  volatile uint16_t retval = 1;
156 
157  pushbtn->debounce_delay = 0;
158  pushbtn->status.bits.pressed = false;
159  pushbtn->status.bits.long_press = false;
160  pushbtn->status.bits.enabled = false;
161  SW_USER_Init();
162 
163  return(retval);
164 }
165 
166 // end of file
drv_PushButton_Dispose
volatile uint16_t drv_PushButton_Dispose(volatile struct PUSH_BUTTON_OBJECT_s *pushbtn)
Initializes the push button device driver.
Definition: drv_push_button.c:153
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::pressed
volatile bool pressed
Bit 14: Indicates if the button is pressed or not.
Definition: drv_push_button.h:88
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
SW_USER_Init
#define SW_USER_Init()
Macro instruction initializing the specified GPIO as input.
Definition: dpsk3_hwdescr.h:244
SW_USER_Get
#define SW_USER_Get()
Macro instruction to set a pin state to logic HIGH.
Definition: dpsk3_hwdescr.h:240
PUSH_BUTTON_STATUS_s::long_press
volatile bool long_press
Bit 13: Indicates if switch has been pressed for a longer time.
Definition: drv_push_button.h:87
PUSH_BUTTON_OBJECT_s::status
volatile struct PUSH_BUTTON_STATUS_s status
Status word of the switch object.
Definition: drv_push_button.h:110
drv_PushButton_Initialize
volatile uint16_t drv_PushButton_Initialize(volatile struct PUSH_BUTTON_OBJECT_s *pushbtn)
Initializes the push button device driver.
Definition: drv_push_button.c:38
PUSH_BUTTON_OBJECT_s
Push button function driver data object.
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
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::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::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
PUSH_BUTTON_STATUS_s::sw_event
volatile bool sw_event
Bit 0: Event bit indicating a state has changed (cleared automatically)
Definition: drv_push_button.h:74
drv_PushButton_Execute
volatile uint16_t drv_PushButton_Execute(volatile struct PUSH_BUTTON_OBJECT_s *pushbtn)
Initializes the push button device driver.
Definition: drv_push_button.c:70