-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmephisto.cpp
138 lines (113 loc) · 3.34 KB
/
mephisto.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
127
128
129
130
131
132
133
134
135
136
137
138
/*
Copyright 2021, 2022 Andreas Petersik ([email protected])
This file is part of the Open Mephisto Project.
Open Mephisto is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Open Mephisto is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Open Mephisto. If not, see <https://www.gnu.org/licenses/>.
*/
#include "mephisto.h"
#include <driver/rtc_io.h>
void Mephisto::initPorts()
{
rtc_gpio_hold_dis(gpio_num_t(ROW_LE));
rtc_gpio_hold_dis(gpio_num_t(LDC_LE));
rtc_gpio_hold_dis(gpio_num_t(LDC_EN));
rtc_gpio_hold_dis(gpio_num_t(CB_EN));
pinMode(ROW_LE, OUTPUT);
pinMode(LDC_LE, OUTPUT);
pinMode(LDC_EN, OUTPUT);
pinMode(CB_EN, OUTPUT);
digitalWrite(LDC_EN, LOW);
digitalWrite(ROW_LE, LOW);
digitalWrite(LDC_LE, LOW);
digitalWrite(CB_EN, HIGH); // only low to read out reed switches!
}
byte Mephisto::readRow(byte row)
{
byte rowResult;
digitalWrite(LDC_EN, LOW);
digitalWrite(ROW_LE, LOW);
digitalWrite(LDC_LE, LOW);
digitalWrite(CB_EN, HIGH);
delayMicroseconds(LATCH_WAIT);
// select row to read:
for (byte i = 0; i < 8; i++)
{
pinMode(bytePort[i], OUTPUT);
if (i == row)
{
digitalWrite(bytePort[i], LOW);
}
else
{
digitalWrite(bytePort[i], HIGH);
}
}
digitalWrite(ROW_LE, HIGH);
delayMicroseconds(LATCH_WAIT);
digitalWrite(ROW_LE, LOW);
delayMicroseconds(LATCH_WAIT);
// set all columns to LOW:
for (byte i = 0; i < 8; i++)
{
digitalWrite(bytePort[i], LOW);
}
delayMicroseconds(LATCH_WAIT);
digitalWrite(LDC_LE, HIGH);
delayMicroseconds(LATCH_WAIT);
digitalWrite(LDC_LE, LOW);
delayMicroseconds(LATCH_WAIT);
digitalWrite(LDC_EN, HIGH);
delayMicroseconds(LATCH_WAIT); // fix for older Exclusive boards
digitalWrite(LDC_EN, LOW);
delayMicroseconds(LATCH_WAIT);
digitalWrite(CB_EN, LOW);
delayMicroseconds(LATCH_WAIT); // Mandatory to work!
rowResult = 0;
for (byte i = 0; i < 8; i++)
{
pinMode(bytePort[i], INPUT);
rowResult += (digitalRead(bytePort[i])) << i;
}
digitalWrite(CB_EN, HIGH);
return ~rowResult;
}
void Mephisto::writeRow(byte row, byte value)
{
digitalWrite(CB_EN, HIGH);
digitalWrite(ROW_LE, LOW);
digitalWrite(LDC_LE, LOW);
digitalWrite(LDC_EN, HIGH);
// select row:
for (byte i = 0; i < 8; i++)
{
pinMode(bytePort[i], OUTPUT);
if (i == row)
{
digitalWrite(bytePort[i], LOW);
}
else
{
digitalWrite(bytePort[i], HIGH);
}
}
digitalWrite(ROW_LE, HIGH);
delayMicroseconds(LATCH_WAIT);
digitalWrite(ROW_LE, LOW);
// select column:
for (byte i = 0; i < 8; i++)
{
digitalWrite(bytePort[i], ((value >> i) & 0x1));
}
digitalWrite(LDC_LE, HIGH);
delayMicroseconds(LATCH_WAIT);
digitalWrite(LDC_LE, LOW);
digitalWrite(LDC_EN, LOW);
}