Electronic Load 1.0 Programmable Constant Current Sink

Macros | Typedefs | Enumerations | Functions | Variables
keyboard.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

## Macros
#define | KEY_CANCEL 3
| The Cancel/No Key. More...
#define | KEY_ENTER 2
| The Enter/Yes Key. More...
#define | KEY_INVALID 1
| more than one key pressed More...
#define | KEY_NONE 6
| No Key pressed. Due to the nature of scanKeyboard(), this value has to be >3. More...
#define | KEY_RETLINES 4
| Number of Return Lines. More...
#define | KEY_ROTDOWN 5
| Rotary Encoder turned left. More...
#define | KEY_ROTUP 4
| Rotary Encoder turned right. More...
#define | KEY_SCANLINES 3
| Number of Scan Lines. More...
#define | KEYBOARD_WITH_LONG
| Enables KEY_PRESSED_LONG Event. More...
#define | KEYBOARD_WITH_PRESSED
| Enables the KEY_PRESSED Event. More...
#define | KEYBOARD_WITH_RELEASE
| Enables KEY_RELEASED Event. More...
#define | KEYBOARD_WITH_REPEAT
| Enables the KEY_REPEAT Event. More...
#define | KEYBUFSIZE 16
| The Size of the Keyboad Buffer. It can keep up to KEYBUFSIZE-1 keys pending. More...
#define | KEYRETDDR DDRA
#define | KEYRETPIN PINA
#define | KEYRETPORT PORTA
#define | KEYSCANDDR DDRB
#define | KEYSCANPORT PORTB
#define | ROTBUTTON (PIND & (1<<PIND5))
#define | ROTPIN PIND
#define | ROTPORT PORTD

## Typedefs
typedef uint8_t | KEY_CHAR_t

## Enumerations
enum | PutCharReturn_t { PUTKEY_DONE, PUTKEY_ERROR_BUFFER_FULL, PUTKEY_ERROR_KEYBOARD_LOCKED }

## Functions
KEY_CHAR_t | GetKey (void)
| Gets the next key from the keyboard buffer. More...
static void | InitKeyboard ()
| Initializes the keyboard. More...
static bool | keyStat (void)
| Returns True if Keystrokes are pending. More...
PutCharReturn_t | PutKey (KEY_CHAR_t c)
| Puts a key into the keyboard buffer. More...
static void | scanKeyboard ()
| Reads the Return Lines and sets the next Scan Line active. More...
static void | scanRotaryEncoder (void)
| Scans the rotary encoder. More...

## Variables
uint8_t | bufReadPtr
| Read Pointer Index of the keyboard buffer. The buffer is defined to be empty if Read PTR == Write PTR. More...
uint8_t | bufWritePtr
| Write Pointer Index of the keyboard buffer. The buffer is defined to be empty if Read PTR == Write PTR. More...
const char | decodeMatrix []
static const uint8_t | KEY_ACTION_MASK =0xc0
| The upper two Bits of a Key are Flags. More...
static const uint8_t | KEY_PRESSED =0
| The Key has just been pressed. More...
static const uint8_t | KEY_PRESSED_LONG =0x80
| The Key is pressed for at least KEY_REPEAT_DELAY. More...
static const uint8_t | KEY_RELEASED =0xc0
| The Key has just been released. More...
static const uint8_t | KEY_REPEAT =0x40
| The Key is repeated due to long press time. More...
static const uint8_t | KEY_REPEAT_DELAY =60
| Key Repetition starts after n Calls to scanKeyboard()/KEY_SCANLINES. More...
static const uint8_t | KEY_REPEAT_RATE =5
| Key is Repeated every n Calls to scanKeyboard()/KEY_SCANLINES. More...
volatile uint8_t | keyboardLocked
| Lock status of the keyboard. More...
uint8_t | keynum
| Scancode of a key (0..11 or 255) More...
volatile unsigned char | keyPressed
| Contains the key pressed. Set it to KEY_NONE after usage. More...
uint8_t | keyTimer [KEY_RETLINES *KEY_SCANLINES]
| Starts counting at 0 as soon as a Key is pressed. Topped at KEY_REPEAT_DELAY. More...
uint8_t | lastRot
| Last value of the rotary encoder (0..3) More...
uint8_t | lastRotButton
| Last value of the rotary encoder pushbutton (0 or 1) More...
uint8_t | repeatRateCnt
| Counts as long as a Key is pressed from 0 to KEY_REPEAT_RATE and issues the key again on overflow. More...
uint8_t | rot
| Current value of the rotary encoder (0..3) More...
uint8_t | rotButton
| Current value of the rotary encoder pushbutton (0 or 1) More...
const char | rotMatrix [16]
| Matrix for decoding the rotary encoder. More...
uint8_t | scancnt
| Counter for current scan line. More...

## Macro Definition Documentation

## ◆ KEYBOARD_WITH_PRESSED

#define KEYBOARD_WITH_PRESSED

Enables the KEY_PRESSED Event.

## ◆ KEYBOARD_WITH_REPEAT

#define KEYBOARD_WITH_REPEAT

Enables the KEY_REPEAT Event.

## ◆ KEYBOARD_WITH_LONG

#define KEYBOARD_WITH_LONG

Enables KEY_PRESSED_LONG Event.

## ◆ KEYBOARD_WITH_RELEASE

#define KEYBOARD_WITH_RELEASE

Enables KEY_RELEASED Event.

## ◆ KEYBUFSIZE

#define KEYBUFSIZE 16

The Size of the Keyboad Buffer. It can keep up to KEYBUFSIZE-1 keys pending.

## ◆ KEYSCANPORT

#define KEYSCANPORT PORTB

## ◆ KEYSCANDDR

#define KEYSCANDDR DDRB

## ◆ KEYRETPORT

#define KEYRETPORT PORTA

## ◆ KEYRETPIN

#define KEYRETPIN PINA

## ◆ KEYRETDDR

#define KEYRETDDR DDRA

## ◆ KEY_SCANLINES

#define KEY_SCANLINES 3

Number of Scan Lines.

## ◆ KEY_RETLINES

#define KEY_RETLINES 4

Number of Return Lines.

## ◆ ROTPORT

#define ROTPORT PORTD

## ◆ ROTPIN

#define ROTPIN PIND

## ◆ ROTBUTTON

#define ROTBUTTON (PIND & (1<<PIND5))

## ◆ KEY_INVALID

#define KEY_INVALID 1

more than one key pressed

## ◆ KEY_ENTER

#define KEY_ENTER 2

The Enter/Yes Key.

## ◆ KEY_CANCEL

#define KEY_CANCEL 3

The Cancel/No Key.

## ◆ KEY_ROTUP

#define KEY_ROTUP 4

Rotary Encoder turned right.

## ◆ KEY_ROTDOWN

#define KEY_ROTDOWN 5

Rotary Encoder turned left.

## ◆ KEY_NONE

#define KEY_NONE 6

No Key pressed. Due to the nature of scanKeyboard(), this value has to be >3.

## Typedef Documentation

## ◆ KEY_CHAR_t

typedef uint8_t KEY_CHAR_t

## Enumeration Type Documentation

## ◆ PutCharReturn_t

enum PutCharReturn_t

Enumerator
PUTKEY_DONE | The return value of Putkey() if the Key has been successfully placed in the Keyboard Queue.
PUTKEY_ERROR_BUFFER_FULL | The return value of PutKey() if no space available. The Character has been discarded.
PUTKEY_ERROR_KEYBOARD_LOCKED | The return value of PutKey() if the Keyboard is locked. The Character has been discarded.

## Function Documentation

## ◆ GetKey()

KEY_CHAR_t GetKey | ( | void |  | ) |

Gets the next key from the keyboard buffer.

Returns
  The key

If the keyboard buffer is empty the function returns KEY_NONE. Be sure to define KEY_NONE to a value not included in your decode matrix!

Note
  You can test the keyboard buffer with keyStat() wich will return true if keystrokes are pending without actually calling GetKey().

## ◆ keyStat()

static bool keyStat | ( | void |  | ) |
| inlinestatic

Returns True if Keystrokes are pending.

Returns
  False if Keyboard Buffer is empty, True if not.

Here is the caller graph for this function:

## ◆ InitKeyboard()

static void InitKeyboard | ( |  | ) |
| inlinestatic

Initializes the keyboard.

Must be called once on startup

Here is the caller graph for this function:

## ◆ PutKey()

PutCharReturn_t PutKey | ( | KEY_CHAR_t | c | ) |

Puts a key into the keyboard buffer.

Parameters

  c | The character

Returns
  PUTKEY_DONE on success, PUTKEY_ERROR_BUFFER_FULL if the character has been discarded due to buffer full, PUTKEY_ERROR_KEYBOARD_LOCKED if keyboard is locked.

If the buffer is full or the keyboard is locked, the character is discarded. Check the return value of PutKey() if desirable.
You can call this routine from anywhere in your program to simulate keyboard events but be aware of the keyboard buffer length. It is good policy to honor the return value to make sure the key has actually been placed in the queue.

Here is the call graph for this function:

## ◆ scanRotaryEncoder()

static void scanRotaryEncoder | ( | void |  | ) |
| inlinestatic

Scans the rotary encoder.

This routine is declared inline since it is only used once in your project inside the timer interrupt.
It is kept separate from scanKeyboard so you can call it asynchronously, let's say every 5ms you call scanRotaryEncoder and every other 5ms you call scanKeyboard to minimize the timer interrupt load. But both functions have to be called at frequent intervals!

Here is the call graph for this function:
Here is the caller graph for this function:

## ◆ scanKeyboard()

static void scanKeyboard | ( |  | ) |
| inlinestatic

Reads the Return Lines and sets the next Scan Line active.

This routine is declared inline since it is only used once in your project inside the timer interrupt.
If a key is newly pressed its code is placed into the keyboard buffer. The scan counter is advanced to the next scan line and the next scan line is set active. So, even with week pull-ups the return lines will stabilize until the next call. Must be called from 10ms-timer interrupt.

Here is the call graph for this function:
Here is the caller graph for this function:

## Variable Documentation

## ◆ KEY_REPEAT_DELAY

const uint8_t KEY_REPEAT_DELAY =60
| static

Key Repetition starts after n Calls to scanKeyboard()/KEY_SCANLINES.

## ◆ KEY_REPEAT_RATE

const uint8_t KEY_REPEAT_RATE =5
| static

Key is Repeated every n Calls to scanKeyboard()/KEY_SCANLINES.

## ◆ KEY_ACTION_MASK

const uint8_t KEY_ACTION_MASK =0xc0
| static

The upper two Bits of a Key are Flags.

## ◆ KEY_PRESSED

const uint8_t KEY_PRESSED =0
| static

The Key has just been pressed.

## ◆ KEY_REPEAT

const uint8_t KEY_REPEAT =0x40
| static

The Key is repeated due to long press time.

## ◆ KEY_PRESSED_LONG

const uint8_t KEY_PRESSED_LONG =0x80
| static

The Key is pressed for at least KEY_REPEAT_DELAY.

## ◆ KEY_RELEASED

const uint8_t KEY_RELEASED =0xc0
| static

The Key has just been released.

## ◆ keyPressed

volatile unsigned char keyPressed

Contains the key pressed. Set it to KEY_NONE after usage.

Normally, the getKey-function should be used. This variable has been introduced to wait for keys although the keyboard is locked. When locked, keys will not be written into the keyboard buffer but keyPressed is set to the value of the key.

## ◆ keyboardLocked

volatile uint8_t keyboardLocked

Lock status of the keyboard.

If !=0, keys and encoder actions are no longer placed into the keyboard buffer. Alternatively, the keyPressed- variable is set to allow a "wait-for-any-key".
This is used when running test programs to avoid user interference. The keyboard can be locked by USB-commands to prevent user actions to interfere with program activity.

## ◆ bufReadPtr

uint8_t bufReadPtr

Read Pointer Index of the keyboard buffer. The buffer is defined to be empty if Read PTR == Write PTR.

## ◆ bufWritePtr

uint8_t bufWritePtr

Write Pointer Index of the keyboard buffer. The buffer is defined to be empty if Read PTR == Write PTR.

## ◆ keynum

uint8_t keynum

Scancode of a key (0..11 or 255)

## ◆ rot

uint8_t rot

Current value of the rotary encoder (0..3)

## ◆ lastRot

uint8_t lastRot

Last value of the rotary encoder (0..3)

## ◆ rotButton

uint8_t rotButton

Current value of the rotary encoder pushbutton (0 or 1)

## ◆ lastRotButton

uint8_t lastRotButton

Last value of the rotary encoder pushbutton (0 or 1)

## ◆ rotMatrix

const char rotMatrix

Initial value:
= {
0,
KEY_ROTUP,
KEY_ROTDOWN,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
KEY_ROTDOWN,
KEY_ROTUP,
0 }
KEY_ROTDOWN
#define KEY_ROTDOWN
Rotary Encoder turned left.
Definition: keyboard.h:45
KEY_ROTUP
#define KEY_ROTUP
Rotary Encoder turned right.
Definition: keyboard.h:44

Matrix for decoding the rotary encoder.

The index is (lastRot<<2 | rot). A 0 means no (valid) transition.

## ◆ scancnt

uint8_t scancnt

Counter for current scan line.

## ◆ keyTimer

uint8_t keyTimer[KEY_RETLINES *KEY_SCANLINES]

Starts counting at 0 as soon as a Key is pressed. Topped at KEY_REPEAT_DELAY.

## ◆ decodeMatrix

const char decodeMatrix[]

## ◆ repeatRateCnt

uint8_t repeatRateCnt

Counts as long as a Key is pressed from 0 to KEY_REPEAT_RATE and issues the key again on overflow.