-
Notifications
You must be signed in to change notification settings - Fork 0
/
coder.h
67 lines (55 loc) · 1.18 KB
/
coder.h
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
#ifndef CODER_H
#define CODER_H
#include "io.h"
namespace typotron{
byte matrixToKey(matrix_t matrix,bool* key_pressed_flag)
{
/*
* ENCODING:
* key = 0b00 000 000
* || | |
* CS byte bit
*/
bool shift = false;
bool code = false;
byte key = 000;
for(byte i=0;i<8;i++)
{
byte matrix_byte = matrix[i];
if(matrix_byte == 0) continue;
for(byte j=0;j<8;++j)
{
if ((matrix_byte >> j)&1)
{
*key_pressed_flag = true;
if(i==1 && j==7) code = true;
else if(i==2 && j==7) shift = true;
else key = (i << 3) | j;
}
}
}
if(shift) key |= 0b01000000;
if(code) key |= 0b10000000;
return key;
}
void keyToMatrix(byte key, matrix_t matrix)
{
/*
* ENCODING:
* key = 0b00 000 000
* || | |
* CS byte bit
*/
bool shift = (key >> 6) & 1;
bool code = (key >> 7) & 1;
key &= 0b00111111;
for(byte i=0;i<8;++i)
{
if((key & 0b00111000) >> 3 == i) matrix[i] = (1 << (key & 0b00000111));
else matrix[i] = 0;
}
if(shift) matrix[2] |= 0b00010000; //SHIFT = 024
if(code) matrix[1] |= 0b00010000; //CODE = 014
}
}//namespace
#endif