LevelMeter-Display  2.0
An ultrasonic Level Meter for Stormwater Cisterns
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
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 
21 static inline bool GetKeyState(void)
22 {
23  return !(PINB&0x01);
24 }
25 
26 
38 static inline void KeyInt(void)
39 {
40  static bool lastKeyState=false; // Key state at last interrupt
41  static uint8_t keyPressedTime=0; // Counts the time the key is pressed
42  static uint8_t cnt100ms=0; // Counts the 100ms-intervals
43  bool keyState; // current key state
44 
45  cnt100ms++;
46  if (cnt100ms>=10)
47  {
48  cnt100ms=0;
49  } else
50  {
51  return;
52  }
53  // The following is executed every 100ms!
54  keyState=GetKeyState();
55  if (lastKeyState!=keyState) // Something has changed
56  {
57  lastKeyState=keyState; // Keep the key state for next interrupt
58  if (keyState) // Key has just been pressed
59  {
61  keyPressedTime=0;
62  return;
63  } else // Key has just been released
64  {
66  if (keyPressedTime<25)
67  {
69  }
70  }
71  }
72  if (keyState) {
73  if (keyPressedTime<255)
74  {
75  keyPressedTime++;
76  }
77  if (keyPressedTime==25) // More than 2.5 seconds
78  {
80  }
81  }
82 }
83 
84 #endif /* KEY_H_ */