-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
ascii2scan.cpp
71 lines (67 loc) · 2.42 KB
/
ascii2scan.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include"ascii2scan.h"
// Convert an ASCII character from [0x20-0x7e] (space to ~) a HID scancode)
// Upper byte of return value indicats if shift is needed (a 0x00=>No, 0x01=>Yes)
// Lower byte is the scan code to return
// 0x0000 => No valid scan code
// Based on https://usb.org/sites/default/files/hut1_21.pdf chapter 10, page 82
uint16_t ascii2scan(char c) {
bool shift = false;
uint8_t code = 0x00;
if(c<0x20) {
// Before space / ' '...Nothing
} else
if(c<0x30) { // Space up <= '0'
const uint8_t codes[] =
// SPACE ! " # $ % & ' ( ) * + , - . /
{ 0x2c, 0x1e, 0x34, 0x20, 0x21, 0x22, 0x24, 0x34, 0x26, 0x27, 0x25, 0x2e, 0x36, 0x2d, 0x37, 0x38};
const bool shifts[] =
{false, true, true, true, true, true, true, false, true, true, true, true, false, false, false, false};
uint8_t index = c-0x20;
shift = shifts[index];
code = codes[index];
} else
if(c<0x3a) { // [0..9]
if(c=='0') // 0x30
code = 0x27;
else
code = 0x1e + (c-'1'); // 1-9
} else
if(c<0x41) { // 9<c<A
const uint8_t codes[] =
// : ; < = > ? @
{ 0x33, 0x33, 0x36, 0x2e, 0x37, 0x38, 0x1f};
const bool shifts[] =
{true, false, true, false, true, true, true};
uint8_t index = c-':';
shift = shifts[index];
code = codes[index];
} else
if(c<0x5B) { // [A..Z]
shift = true;
code = 0x04 + c-'A';
} else
if(c<0x61) { // Before 'a'
const uint8_t codes[] =
// [ \ ] ^ _ `
{ 0x2f, 0x31, 0x30, 0x23, 0x2D, 0x35};
const bool shifts[] =
{false, false, false, true, true, false};
uint8_t index = c-'[';
shift = shifts[index];
code = codes[index];
} else
if(c<0x7b) { // [a..z]
code = 0x04 + c-'a';
} else
if(c<0x7f) { // Before del
const uint8_t codes[] =
// { | } ~
{ 0x2f, 0x31, 0x30, 0x35};
const bool shifts[] =
{true, true, true, true};
uint8_t index = c-'{';
shift = shifts[index];
code = codes[index];
}
return (shift ? 0x0100 : 0x0000) | code;
}