Electronic Load  1.0
Programmable Constant Current Sink
keyboard.h
Go to the documentation of this file.
1 // keyboard.h
2 
3 #define KEYBOARD_WITH_PRESSED
4 #define KEYBOARD_WITH_REPEAT
5 #define KEYBOARD_WITH_LONG
6 #define KEYBOARD_WITH_RELEASE
7 #define KEYBUFSIZE 16
8 
9 static const uint8_t KEY_REPEAT_DELAY=60;
10 static const uint8_t KEY_REPEAT_RATE=5;
11 static const uint8_t KEY_ACTION_MASK=0xc0;
12 static const uint8_t KEY_PRESSED=0;
13 static const uint8_t KEY_REPEAT=0x40;
14 static const uint8_t KEY_PRESSED_LONG=0x80;
15 static const uint8_t KEY_RELEASED=0xc0;
16 
17 typedef uint8_t KEY_CHAR_t;
18 
19 extern KEY_CHAR_t GetKey(void);
20 
21 extern volatile unsigned char keyPressed;
22 extern volatile uint8_t keyboardLocked;
23 
24 #define KEYSCANPORT PORTB
25 #define KEYSCANDDR DDRB
26 #define KEYRETPORT PORTA
27 #define KEYRETPIN PINA
28 #define KEYRETDDR DDRA
29 #define KEY_SCANLINES 3
30 #define KEY_RETLINES 4
31 #define ROTPORT PORTD
32 #define ROTPIN PIND
33 #define ROTBUTTON (PIND & (1<<PIND5))
34 
35 /* Definition of Key-Names.
36 
37  Caution: We use the two upper bits of a Key to
38  indicate KEY_PRESS, KEY_PRESSED_LONG and KEY_RELEASE,
39  so all key codes must be below 0x40!
40 */
41 #define KEY_INVALID 1
42 #define KEY_ENTER 2
43 #define KEY_CANCEL 3
44 #define KEY_ROTUP 4
45 #define KEY_ROTDOWN 5
46 #define KEY_NONE 6
47 
48 /* Definition of Putkey-Return-Values */
49 typedef enum {
54 
55 extern uint8_t bufReadPtr,bufWritePtr;
56 
60 static inline bool keyStat(void)
61 {
62  return bufReadPtr!=bufWritePtr;
63 }
64 
65 extern uint8_t keynum;
66 
71 static inline void InitKeyboard()
72 {
73 KEYSCANDDR|=0b00000111; // scan lines are output, other input
74 KEYSCANPORT|=0b00000110; // and set only first scanline low
75 KEYRETPORT|=0b00001111; // enable pullups at return lines
76 ROTPIN &= 0b11100111; // set rotary pins as input...
77 ROTPORT|=0b00011000; // ...with pullup
78 keynum=9; // no key
79 }
80 
81 extern uint8_t rot,lastRot,rotButton,lastRotButton;
82 extern const char rotMatrix[16];
84 
94 static inline void scanRotaryEncoder(void)
95 {
96  uint8_t rotIndex;
97 
98  rot=(ROTPIN & 0b00011000) >> 3;
99  rotIndex=rot | (lastRot<<2);
100  char rotChar=pgm_read_byte(&(rotMatrix[rotIndex]));
101  if (rotChar!=0)
102  {
103  PutKey(rotChar);
104  }
105  lastRot=rot;
108  {
109  if (rotButton==0)
110  {
111  PutKey(KEY_ENTER);
112  }
114  }
115 }
116 
117 extern uint8_t scancnt;
118 extern uint8_t keyTimer[KEY_RETLINES*KEY_SCANLINES];
119 extern const char decodeMatrix[];
120 extern uint8_t repeatRateCnt;
121 
133 static inline void scanKeyboard()
134 {
135 unsigned char i;
136 char c;
137 
138 /* The following switch() is hardware dependant and converts
139 the active return line into a number bewteen 0 and KEY_RETLINES
140 or 255 if no or more than one key is pressed */
141 switch(KEYRETPIN&0x0f) {
142  case 0x0f: keynum=255; // All lines high, no key is pressed
143  break;
144  case 0x0e: keynum=0;
145  break;
146  case 0x0d: keynum=1;
147  break;
148  case 0x0b: keynum=2;
149  break;
150  case 0x07: keynum=3;
151  break;
152  default: keynum=255; // more than one key pressed. We treat this as if no key is pressed.
153  }
154 if (keynum<KEY_RETLINES) { // a key is pressed
155  keynum+=scancnt<<2; // add the value of the current scan line
156  c=pgm_read_byte(&(decodeMatrix[keynum]));
157  if (keyTimer[keynum]==0) { // key has just been pressed
158  PutKey(c|KEY_PRESSED);
159  }
161  {
162  keyTimer[keynum]++;
163  } else
165  {
166  keyTimer[keynum]++;
167 #ifdef KEYBOARD_WITH_LONG
169 #endif
170  } else
171  {
172  {
173 #ifdef KEYBOARD_WITH_REPEAT
174  repeatRateCnt++;
176  {
177  repeatRateCnt=0;
178  PutKey(c|KEY_REPEAT);
179  }
180 #endif
181  }
182  }
183  }
184 else if (keynum==255) {
185  for (i=scancnt<<2;i<(scancnt<<2)+KEY_RETLINES;i++)
186  {
187 #ifdef KEYBOARD_WITH_RELEASE
188  if (keyTimer[i])
189  {
190  c=pgm_read_byte(&(decodeMatrix[i]));
191  PutKey(c|KEY_RELEASED);
192  }
193 #endif
194  keyTimer[i]=0;
195  }
196  }
197 scancnt++;
199  scancnt=0;
200 /* The following lines are hardware dependant and set all
201 scan lines high and the active one low. */
202 KEYSCANPORT|=0b00000111; // all scanlines high
203 KEYSCANPORT&=~(0b00000001<<scancnt); // active scanline low
204 }
205 
PutCharReturn_t PutKey(KEY_CHAR_t c)
Puts a key into the keyboard buffer.
Definition: keyboard.c:152
uint8_t rot
Current value of the rotary encoder (0..3)
Definition: keyboard.c:86
#define KEY_SCANLINES
Number of Scan Lines.
Definition: keyboard.h:29
#define KEYRETPIN
Definition: keyboard.h:27
The return value of PutKey() if no space available. The Character has been discarded.
Definition: keyboard.h:51
static const uint8_t KEY_PRESSED_LONG
The Key is pressed for at least KEY_REPEAT_DELAY.
Definition: keyboard.h:14
uint8_t keyTimer[KEY_RETLINES *KEY_SCANLINES]
Starts counting at 0 as soon as a Key is pressed. Topped at KEY_REPEAT_DELAY.
Definition: keyboard.c:84
#define KEYSCANPORT
Definition: keyboard.h:24
The return value of Putkey() if the Key has been successfully placed in the Keyboard Queue.
Definition: keyboard.h:50
static const uint8_t KEY_ACTION_MASK
The upper two Bits of a Key are Flags.
Definition: keyboard.h:11
static const uint8_t KEY_REPEAT_RATE
Key is Repeated every n Calls to scanKeyboard()/KEY_SCANLINES.
Definition: keyboard.h:10
uint8_t keynum
Scancode of a key (0..11 or 255)
Definition: keyboard.c:78
uint8_t lastRot
Last value of the rotary encoder (0..3)
Definition: keyboard.c:85
#define ROTBUTTON
Definition: keyboard.h:33
uint8_t bufReadPtr
Read Pointer Index of the keyboard buffer. The buffer is defined to be empty if Read PTR == Write PTR...
Definition: keyboard.c:81
static void InitKeyboard()
Initializes the keyboard.
Definition: keyboard.h:71
uint8_t lastRotButton
Last value of the rotary encoder pushbutton (0 or 1)
Definition: keyboard.c:87
#define KEY_ENTER
The Enter/Yes Key.
Definition: keyboard.h:42
static void scanKeyboard()
Reads the Return Lines and sets the next Scan Line active.
Definition: keyboard.h:133
uint8_t scancnt
Counter for current scan line.
Definition: keyboard.c:77
static const uint8_t KEY_PRESSED
The Key has just been pressed.
Definition: keyboard.h:12
#define ROTPORT
Definition: keyboard.h:31
KEY_CHAR_t GetKey(void)
Gets the next key from the keyboard buffer.
Definition: keyboard.c:186
static const uint8_t KEY_REPEAT_DELAY
Key Repetition starts after n Calls to scanKeyboard()/KEY_SCANLINES.
Definition: keyboard.h:9
uint8_t KEY_CHAR_t
Definition: keyboard.h:17
const char decodeMatrix[]
uint8_t bufWritePtr
Write Pointer Index of the keyboard buffer. The buffer is defined to be empty if Read PTR == Write PT...
Definition: keyboard.c:82
static bool keyStat(void)
Returns True if Keystrokes are pending.
Definition: keyboard.h:60
volatile uint8_t keyboardLocked
Lock status of the keyboard.
Definition: keyboard.c:136
#define ROTPIN
Definition: keyboard.h:32
#define KEYSCANDDR
Definition: keyboard.h:25
const char rotMatrix[16]
Matrix for decoding the rotary encoder.
Definition: keyboard.c:99
static const uint8_t KEY_REPEAT
The Key is repeated due to long press time.
Definition: keyboard.h:13
uint8_t rotButton
Current value of the rotary encoder pushbutton (0 or 1)
Definition: keyboard.c:88
#define KEYRETPORT
Definition: keyboard.h:26
uint8_t repeatRateCnt
Counts as long as a Key is pressed from 0 to KEY_REPEAT_RATE and issues the key again on overflow.
Definition: keyboard.c:91
The return value of PutKey() if the Keyboard is locked. The Character has been discarded.
Definition: keyboard.h:52
static const uint8_t KEY_RELEASED
The Key has just been released.
Definition: keyboard.h:15
volatile unsigned char keyPressed
Contains the key pressed. Set it to KEY_NONE after usage.
Definition: keyboard.c:125
PutCharReturn_t
Definition: keyboard.h:49
static void scanRotaryEncoder(void)
Scans the rotary encoder.
Definition: keyboard.h:94
#define KEY_RETLINES
Number of Return Lines.
Definition: keyboard.h:30