CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
host_api_keypad.cpp
Go to the documentation of this file.
1
8
9// IKeypad.h first: host_api.h defines KEY_* macros that would otherwise
10// clobber the cdc::hal::Key enumerators during preprocessing.
11#include "cdc_hal/IKeypad.h"
13
14namespace {
15
16cdc::hal::Key code_to_key(uint8_t code) {
17 using cdc::hal::Key;
18 if (code <= 9) return static_cast<Key>('0' + code);
19 if (code == 10) return Key::KEY_YES;
20 if (code == 11) return Key::KEY_NO;
21 return Key::KEY_NONE;
22}
23
24int key_to_code(cdc::hal::Key k) {
25 char c = static_cast<char>(k);
26 if (c >= '0' && c <= '9') return c - '0';
27 if (c == 'Y') return 10;
28 if (c == 'N') return 11;
29 return -1;
30}
31
32} // namespace
33
34extern "C" {
35
36bool host_key_pressed(uint8_t key)
37{
38 auto* kp = cdc::hal::getKeypadInstance();
39 if (!kp) return false;
40 cdc::hal::Key k = code_to_key(key);
41 return k != cdc::hal::Key::KEY_NONE && kp->isKeyPressed(k);
42}
43
44int host_key_consume_next(uint8_t* out_key)
45{
46 if (!out_key) return HOST_ERR_INVALID_ARG;
47 auto* kp = cdc::hal::getKeypadInstance();
48 if (!kp) return HOST_ERR_GENERIC;
49 cdc::hal::Key k = kp->getNextKey();
51 int code = key_to_code(k);
52 if (code < 0) return HOST_ERR_NOT_FOUND;
53 *out_key = static_cast<uint8_t>(code);
54 return HOST_OK;
55}
56
57} // extern "C"
int host_key_consume_next(uint8_t *out_key)
Pop the next queued key press, if any.
bool host_key_pressed(uint8_t key)
True while key is currently held down.
CDC Badge OS plugin host API - canonical C ABI contract.
#define HOST_OK
Definition host_api.h:37
#define HOST_ERR_INVALID_ARG
Definition host_api.h:39
#define HOST_ERR_NOT_FOUND
Definition host_api.h:41
#define HOST_ERR_GENERIC
Definition host_api.h:38
IKeypad * getKeypadInstance()
Returns the singleton keypad service instance.