Numworks Epsilon  1.4.1
Graphing Calculator Operating System
keyboard.cpp
Go to the documentation of this file.
1 /* Keyboard initialization code
2  *
3  * The job of this code is to implement the "ion_key_state" function.
4  *
5  * The keyboard is a matrix that is laid out as follow:
6  *
7  * | PC0 | PC1 | PC2 | PC3 | PC4 | PC5 |
8  * -----+------+------+------+------+------+------+
9  * PE0 | K_A1 | K_A2 | K_A3 | K_A4 | K_A5 | K_A6 |
10  * -----+------+------+------+------+------+------+
11  * PE1 | K_B1 | K_B2 | | | | |
12  * -----+------+------+------+------+------+------+
13  * PE2 | K_C1 | K_C2 | K_C3 | K_C4 | K_C5 | K_C6 |
14  * -----+------+------+------+------+------+------+
15  * PE3 | K_D1 | K_D2 | K_D3 | K_D4 | K_D5 | K_D6 |
16  * -----+------+------+------+------+------+------+
17  * PE4 | K_E1 | K_E2 | K_E3 | K_E4 | K_E5 | K_E6 |
18  * -----+------+------+------+------+------+------+
19  * PE5 | K_F1 | K_F2 | K_F3 | K_F4 | K_F5 | |
20  * -----+------+------+------+------+------+------+
21  * PE6 | K_G1 | K_G2 | K_G3 | K_G4 | K_G5 | |
22  * -----+------+------+------+------+------+------+
23  * PE7 | K_H1 | K_H2 | K_H3 | K_H4 | K_H5 | |
24  * -----+------+------+------+------+------+------+
25  * PE8 | K_I1 | K_I2 | K_I3 | K_I4 | K_I5 | |
26  * -----+------+------+------+------+------+------|
27  *
28  * We decide to drive the rows (PE0-8) and read the columns (PC0-5).
29  *
30  * To avoid short-circuits, the pins E0-E8 will not be standard outputs but
31  * only open-drain. Open drain means the pin is either driven low or left
32  * floating.
33  * When a user presses multiple keys, a connection between two rows can happen.
34  * If we don't use open drain outputs, this situation could trigger a short
35  * circuit between an output driving high and another driving low.
36  *
37  * If the outputs are open-drain, this means that the input must be pulled up.
38  * So if the input reads "1", this means the key is in fact *not* pressed, and
39  * if it reads "0" it means that there's a short to an open-drain output. Which
40  * means the corresponding key is pressed.
41  */
42 
43 #include "keyboard.h"
44 
45 // Public Ion::Keyboard methods
46 
47 namespace Ion {
48 namespace Keyboard {
49 
51  uint64_t state = 0;
52 
53  for (uint8_t i=0; i<Device::numberOfRows; i++) {
55 
56  // TODO: Assert pin numbers are sequentials and dynamically find 8 and 0
57  uint8_t columns = Device::ColumnGPIO.IDR()->getBitRange(5,0);
58 
59  /* The key is down if the input is brought low by the output. In other
60  * words, we want to return true if the input is low (false). So we need to
61  * append 6 bits of (not columns) to state. */
62  state = (state << 6) | (~columns & 0x3F);
63  }
64 
65  /* Last but not least, keys number 8, 9, 10, 11, 35, 41, 47 and 53 are not
66  * defined. Therefore we want to make sure those bits are forced to zero in
67  * whatever value we return. */
68  state = state & 0x1F7DF7FFFFF0FF;
69 
70  return State(state);
71 }
72 
73 }
74 }
75 
76 // Private Ion::Keyboard::Device methods
77 
78 namespace Ion {
79 namespace Keyboard {
80 namespace Device {
81 
82 void init() {
83  for (uint8_t i=0; i<numberOfRows; i++) {
84  uint8_t pin = RowPins[i];
87  }
88 
89  for (uint8_t i=0; i<numberOfColumns; i++) {
90  uint8_t pin = ColumnPins[i];
93  }
94 }
95 
96 void shutdown() {
97  for (uint8_t i=0; i<numberOfRows; i++) {
98  uint8_t pin = RowPins[i];
101  }
102 
103  for (uint8_t i=0; i<numberOfColumns; i++) {
104  uint8_t pin = ColumnPins[i];
107  }
108 }
109 
110 }
111 }
112 }
unsigned char uint8_t
Definition: stdint.h:4
void setType(int index, Type type) volatile
Definition: gpio.h:27
unsigned long long uint64_t
Definition: stdint.h:7
void activateRow(uint8_t row)
Definition: keyboard.h:49
constexpr uint8_t numberOfColumns
Definition: keyboard.h:39
void setPull(int index, Pull pull) volatile
Definition: gpio.h:39
State scan()
Definition: keyboard.cpp:50
constexpr GPIO ColumnGPIO
Definition: keyboard.h:38
constexpr uint8_t RowPins[numberOfRows]
Definition: keyboard.h:36
constexpr uint8_t ColumnPins[numberOfColumns]
Definition: keyboard.h:40
Definition: gpio.h:42
void Keyboard(const char *input)
Definition: keyboard.cpp:9
constexpr uint8_t numberOfRows
Definition: keyboard.h:35
void setMode(int index, Mode mode) volatile
Definition: gpio.h:17
Definition: backlight.h:6
T getBitRange(uint8_t high, uint8_t low) volatile
Definition: register.h:24
constexpr GPIO RowGPIO
Definition: keyboard.h:34