source: trunk/firmware/SES/Src/tast.c@ 1

Last change on this file since 1 was 1, checked in by f.jahn, 3 years ago
File size: 2.6 KB
Line 
1/************************************************************************/
2/* */
3/* Debouncing 8 Keys */
4/* Sampling 4 Times */
5/* With Repeat Function */
6/* */
7/* Author: Peter Dannegger */
8/* danni@specs.de */
9/* */
10/************************************************************************/
11
12#include "tast.h"
13
14
15uint32_t key_state; // debounced and inverted key state:
16 // bit = 1: key pressed
17uint32_t key_press; // key press detect
18uint32_t key_rpt; // key long press and repeat
19
20
21void checkKeys()
22{
23 static uint32_t ct0, ct1, rpt;
24 uint32_t i;
25
26 // ATTENTION! if your pins from button are inverted, change KEY_PIN to ~KEY_PIN
27 i = key_state ^ KEY_PIN; // key changed ?
28 ct0 = ~( ct0 & i ); // reset or count ct0
29 ct1 = ct0 ^ (ct1 & i); // reset or count ct1
30 i &= ct0 & ct1; // count until roll over ?
31 key_state ^= i; // then toggle debounced state
32 key_press |= key_state & i; // 0->1: key press detect
33
34 if( (key_state & REPEAT_MASK) == 0 ) // check repeat function
35 rpt = REPEAT_START; // start delay
36 if( --rpt == 0 ){
37 rpt = REPEAT_NEXT; // repeat delay
38 key_rpt |= key_state & REPEAT_MASK;
39 }
40}
41
42
43uint32_t get_key_press( uint32_t key_mask )
44{
45// cli(); // read and clear atomic !
46 key_mask &= key_press; // read key(s)
47 key_press ^= key_mask; // clear key(s)
48// sei();
49 return key_mask;
50}
51
52
53uint32_t get_key_rpt( uint32_t key_mask )
54{
55// cli(); // read and clear atomic !
56 key_mask &= key_rpt; // read key(s)
57 key_rpt ^= key_mask; // clear key(s)
58// sei();
59 return key_mask;
60}
61
62
63uint32_t get_key_short( uint32_t key_mask )
64{
65// cli(); // read key state and key press atomic !
66 return get_key_press( ~key_state & key_mask );
67}
68
69
70uint32_t get_key_long( uint32_t key_mask )
71{
72 return get_key_press( get_key_rpt( key_mask ));
73}
74
75void clear_input(void)
76{
77 key_state = 0;
78 key_press = 0;
79 key_rpt = 0;
80}
81
82
83
84//void initTast()
85//{
86// key1_CTRL = 0x18; // Enable pull-up on
87// key2_CTRL = 0x18; // Enable pull-up on
88// key3_CTRL = 0x18; // Enable pull-up on
89// key4_CTRL = 0x18; // Enable pull-up on
90// key5_CTRL = 0x18; // Enable pull-up on
91//}
Note: See TracBrowser for help on using the repository browser.