-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpinInits.cpp
126 lines (108 loc) · 4.36 KB
/
pinInits.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "pinInits.h"
void setupPinInit() {
//Initialize all the rows as outputs
pinModeFast(PIN_PB0, OUTPUT);
digitalWriteFast(PIN_PB0, HIGH);
pinModeFast(PIN_PB1, OUTPUT);
digitalWriteFast(PIN_PB1, HIGH);
pinModeFast(PIN_PA1, OUTPUT);
digitalWriteFast(PIN_PA1, HIGH);
pinModeFast(PIN_PA5, OUTPUT);
digitalWriteFast(PIN_PA5, HIGH);
pinModeFast(PIN_PA3, OUTPUT);
digitalWriteFast(PIN_PA3, HIGH);
// Make the columns OUTPUT and HIGH, otherwise, Serial will interfer with them and activate the IR LED
pinModeFast(PIN_PB3, OUTPUT);
digitalWriteFast(PIN_PB3, HIGH);
pinModeFast(PIN_PA2, OUTPUT);
digitalWriteFast(PIN_PA2, HIGH);
pinModeFast(PIN_PA4, OUTPUT);
digitalWriteFast(PIN_PA4, HIGH);
pinModeFast(PIN_PA6, OUTPUT);
digitalWriteFast(PIN_PA6, HIGH);
}
void I2CPinInit() {
// Initialize all the columns as inputs. Necessary if a button is pressed and connects one of the I2C pins (that is also a row) to a column
pinModeFast(PIN_PB3, INPUT_PULLUP);
pinModeFast(PIN_PA2, INPUT_PULLUP);
pinModeFast(PIN_PA4, INPUT_PULLUP);
pinModeFast(PIN_PA6, INPUT_PULLUP);
//Initialize all the rows as INPUTS
pinModeFast(PIN_PB0, INPUT_PULLUP);
pinModeFast(PIN_PB1, INPUT_PULLUP);
pinModeFast(PIN_PA1, INPUT_PULLUP);
pinModeFast(PIN_PA5, INPUT_PULLUP);
pinModeFast(PIN_PA3, INPUT_PULLUP);
// Activate SHORT_COLUMNS so that we can use the pull up from the IR LED.
pinModeFast(SHORT_COLUMNS, OUTPUT);
digitalWriteFast(SHORT_COLUMNS, LOW); // Active low
Wire.swap(0);
Wire.usePullups();
Wire.begin();
}
#ifdef DEBUG_PRINTING
void serialPinInit() {
// Make the columns OUTPUT and HIGH, otherwise, Serial will interfer with them and activate the IR LED since SHORT_COLUMNS is serial TX.
pinModeFast(PIN_PB3, OUTPUT);
digitalWriteFast(PIN_PB3, HIGH);
pinModeFast(PIN_PA4, OUTPUT);
digitalWriteFast(PIN_PA4, HIGH);
pinModeFast(PIN_PA6, OUTPUT);
digitalWriteFast(PIN_PA6, HIGH);
Serial.swap(0); // Use serial interface 0
Serial.begin(SERIAL_SPEED, SERIAL_TX_ONLY);
}
#endif
void serialPinDeInit() {
Serial.end();
// Correct for silicon errata. If these lines were not here the USART peripheral would not release SHORT_COLUMNs
USART0.CTRLB = 0; // Try to disable UART TX
USART0.CTRLC |= 0; // Random USART register needs to be written to in order to keep the peripheral running long enough for the previuos write to take effect
}
void buttonsPinInit() {
// SHORT_COLUMS is active low and needs to be disabled to read individual button presses.
pinModeFast(SHORT_COLUMNS, OUTPUT);
digitalWriteFast(SHORT_COLUMNS, HIGH); // Active low
// Initialize all the columns as inputs
pinModeFast(PIN_PB3, INPUT_PULLUP);
pinModeFast(PIN_PA2, INPUT_PULLUP);
pinModeFast(PIN_PA4, INPUT_PULLUP);
pinModeFast(PIN_PA6, INPUT_PULLUP);
// PIN_PA4 is used for both button matrix and sending IR. When sending IR-codes it needs to be inverted since the IR LED is active low. Make sure it is not inverted now.
PORTA.PIN4CTRL &= ~PORT_INVEN_bm;
//Initialize all the rows as outputs
pinModeFast(PIN_PB0, OUTPUT);
digitalWriteFast(PIN_PB0, HIGH);
pinModeFast(PIN_PB1, OUTPUT);
digitalWriteFast(PIN_PB1, HIGH);
pinModeFast(PIN_PA1, OUTPUT);
digitalWriteFast(PIN_PA1, HIGH);
pinModeFast(PIN_PA5, OUTPUT);
digitalWriteFast(PIN_PA5, HIGH);
pinModeFast(PIN_PA3, OUTPUT);
digitalWriteFast(PIN_PA3, HIGH);
}
void receivePinInit(){
#ifdef DEBUG_PRINTING
// Make the columns OUTPUT and HIGH, otherwise, Serial will interfer with them and activate the IR LED since SHORT_COLUMNS is serial TX.
pinModeFast(PIN_PB3, OUTPUT);
digitalWriteFast(PIN_PB3, HIGH);
pinModeFast(PIN_PA4, OUTPUT);
digitalWriteFast(PIN_PA4, HIGH);
pinModeFast(PIN_PA6, OUTPUT);
digitalWriteFast(PIN_PA6, HIGH);
#else
// SHORT_COLUMS can disconnect the IR LED if it is not used for serial
pinModeFast(SHORT_COLUMNS, OUTPUT);
digitalWriteFast(SHORT_COLUMNS, HIGH); // Active low
#endif
}
void sendPinInit(){
// Disable the shorting of columns since this will also short the IR LED
pinModeFast(SHORT_COLUMNS, OUTPUT);
digitalWriteFast(SHORT_COLUMNS, HIGH);
IrSender.enableIROut(38);
IrSender.begin(IR_SEND_PIN);
PORTA.PIN4CTRL |= PORT_INVEN_bm; // The LED is controlled by a MOSFET that is active low. Therefore the signal needs to be inverted.
digitalWriteFast(IR_SEND_PIN, LOW);
}