LevelMeter-Display  2.0
An ultrasonic Level Meter for Stormwater Cisterns
key.h
Go to the documentation of this file.
1 /*
2  * Key.h
3  *
4  * Created: 25.11.2012 20:54:52
5  * Author: Robert
6  */
7 
8 #include <stdint.h>
9 #include <stdbool.h>
10 #include <avr/io.h>
11 
12 #ifndef KEY_H_
13 #define KEY_H_
14 
15 extern volatile uint8_t kflags;
16 static const uint8_t KFLAG_KEYPRESSED=1;
17 static const uint8_t KFLAG_KEYRELEASED=2;
18 static const uint8_t KFLAG_KEYPRESSEDSHORT=4;
19 static const uint8_t KFLAG_KEYPRESSEDLONG=8;
20 
24 static inline bool GetKeyState(void)
25 {
26  return !(PINB&0x01);
27 }
28 
29 
43 static inline void KeyInt(void)
44 {
45  static bool lastKeyState=false; // Key state at last interrupt
46  static uint8_t keyPressedTime=0; // Counts the time the key is pressed
47  static uint8_t cnt100ms=0; // Counts the 100ms-intervals
48  bool keyState; // current key state
49 
50  cnt100ms++;
51  if (cnt100ms>=10)
52  {
53  cnt100ms=0;
54  } else
55  {
56  return;
57  }
58  // The following is executed every 100ms!
59  keyState=GetKeyState();
60  if (lastKeyState!=keyState) // Something has changed
61  {
62  lastKeyState=keyState; // Keep the key state for next interrupt
63  if (keyState) // Key has just been pressed
64  {
66  keyPressedTime=0;
67  return;
68  } else // Key has just been released
69  {
71  if (keyPressedTime<25)
72  {
74  }
75  }
76  }
77  if (keyState) {
78  if (keyPressedTime<255)
79  {
80  keyPressedTime++;
81  }
82  if (keyPressedTime==25) // More than 2.5 seconds
83  {
85  }
86  }
87 }
88 
89 #endif /* KEY_H_ */
static const uint8_t KFLAG_KEYPRESSED
The key has been pressed.
Definition: key.h:16
static void KeyInt(void)
Must be called from the 100Hz-Timer Interrupt.
Definition: key.h:43
static const uint8_t KFLAG_KEYPRESSEDSHORT
The key has been pressed and released within 2 seconds.
Definition: key.h:18
static const uint8_t KFLAG_KEYPRESSEDLONG
The key has been pressed for more than 2 seconds.
Definition: key.h:19
volatile uint8_t kflags
Flags set by KeyInt() for evaluation by the main loop.
Definition: key.c:24
static bool GetKeyState(void)
Returns the current state of the Pushbutton Key.
Definition: key.h:24
static const uint8_t KFLAG_KEYRELEASED
The key has been released.
Definition: key.h:17