-
Notifications
You must be signed in to change notification settings - Fork 5
/
Quick.h
262 lines (225 loc) · 6.34 KB
/
Quick.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#ifndef QUICK_H
#define QUICK_H
#include <WProgram.h>
#include <Wire.h>
#include "Core.h"
#include "TokenParser.h"
#warning "COMPILING FOR REV A"
us8 led1 = 37;
us8 led2 = 81;
// Kard 0
#define c0p0 68
#define c0p1 58
#define c0p2 62
#define c0p3 55
#define c0p4 82
#define c0p5 32
// Kard 1
#define c1p0 57
#define c1p1 56
#define c1p2 63
#define c1p3 54
#define c1p4 83
#define c1p5 31
// Kard 2
#define c2p0 86
#define c2p1 64
#define c2p2 5
#define c2p3 70
#define c2p4 84
#define c2p5 30
// Kard 3
#define c3p0 22
#define c3p1 76
#define c3p2 9
#define c3p3 2
#define c3p4 35
#define c3p5 52
// Kard 4
#define c4p0 23
#define c4p1 39
#define c4p2 8
#define c4p3 21
#define c4p4 34
#define c4p5 50
// Kard 5
#define c5p0 78
#define c5p1 79
#define c5p2 10
#define c5p3 20
#define c5p4 33
#define c5p5 85
// Kard Com
#define c6p0 71
#define c6p1 71
#define c6p2 71
#define c6p3 71
#define c6p4 71
#define c6p5 44
us8 KardIO[7][6] = {
{ 68, 58, 62, 55, 82, 32 }, // Kard 0
{ 57, 56, 63, 54, 83, 31 }, // Kard 1
{ 86, 64, 5, 70, 84, 30 }, // Kard 2
{ 22, 76, 9, 2, 35, 52 }, // Kard 3
{ 23, 39, 8, 21, 34, 50 }, // Kard 4
{ 78, 79, 10, 20, 33, 85 }, // Kard 5
{ 71, 71, 71, 71, 71, 44 } // Kard Com
};
class Quick {
public:
Quick()
{
Wire.begin();
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
}
void kardConfig(us8 c, us8 config)
{
for(us8 i = 0; i <= 5; i++) {
digitalWrite(KardIO[c][i], LOW);
pinMode(KardIO[c][i], (config & 0x01) ? INPUT : OUTPUT);
config >>= 1;
}
}
bool pin(us8 c, us8 p)
{
if(c <= 7 && p <= 6) {
return digitalRead(KardIO[c][p]);
}
return false;
}
void pin(us8 c, us8 p, bool state)
{
if(c < 7 && p < 6) {
digitalWrite(KardIO[c][p], state);
}
}
us8 card(us8 c)
{
us8 value = 0;
if(c <= 7) {
for(int i = 6; i > 0;) {
Serial.print(digitalRead(KardIO[c][i]), DEC);
value <<= 1;
value |= digitalRead(KardIO[c][i--]);
}
Serial.println();
}
return value;
}
void card(us8 c, us8 state)
{
if(c < 7) {
for(int i = 0; i < 6; i++) {
digitalWrite(KardIO[c][i], state & 1);
state >>= 1;
}
}
}
void printEE(us8 address)
{
Wire.beginTransmission((int)address); // 0x53 quicK 0x50 Kard
Wire.send(0xFA);
Wire.endTransmission();
Wire.requestFrom((int)address, 10); // request 6 bytes from slave device #2
while(Wire.available()) { // slave may send less than requested
us8 data = Wire.receive(); // receive a byte as character
Serial.print(data, HEX); // print the character
Serial.print(".");
}
Serial.println();
}
void cardData(us8 c)
{
Serial.print("QuicK0: ");
printEE(0x53);
us8 kard;
for(kard = 0; kard < 7; kard++) {
pinMode(KardIO[kard][5], OUTPUT);
digitalWrite(KardIO[kard][5], HIGH);
}
for(kard = 0; kard < 7; kard++) {
Serial.print("KARD-");
Serial.print(kard, DEC);
Serial.print(": ");
digitalWrite(KardIO[kard][5], LOW);
printEE(0x50);
digitalWrite(KardIO[kard][5], HIGH);
}
for(kard = 0; kard < 7; kard++) {
pinMode(KardIO[kard][5], INPUT);
}
}
void command(TokenParser &parser)
{
if(parser.compare("qc.ee")) {
cardData(0);
}
else if(parser.compare("pin")) {
parser.nextToken();
us8 pinNumber = parser.toVariant().toInt();
us8 cardNumber = 0;
while(pinNumber >= 4) {
pinNumber -= 4;
cardNumber++;
}
Serial.println(cardNumber, DEC);
Serial.println(pinNumber, DEC);
}
else if(parser.compare("c?p?", true)) {
parser.save();
parser.advanceTail(1);
parser.reverseHead(2);
us8 c = parser.toVariant().toInt();
parser.restore();
parser.advanceTail(3);
us8 p = parser.toVariant().toInt();
if(parser.nextToken()) {
pin(c, p, parser.toVariant().toBool());
Serial.println("OK");
}
else {
Serial.println(pin(c, p) ? "ON" : "OFF");
}
}
else if(parser.compare("c?", true)) {
Serial.println(parser.toString());
parser.advanceTail(1);
us8 c = parser.toVariant().toInt();
if(parser.nextToken()) {
card(c, parser.toVariant().toInt());
Serial.println("OK");
}
else {
Serial.println(card(c), HEX);
}
}
if(parser.compare("reset")) {
Serial.println("Close serial terminal, resetting board in...");
for(us8 sec = 5; sec >= 1; sec-- ) {
Serial.print(sec, DEC);
Serial.println(" seconds...");
delay(1000);
}
// Example 7-1 from Section 7 of PIC32 Family Manual
/* The following code illustrates a software Reset */
// assume interrupts are disabled
// assume the DMA controller is suspended
// assume the device is locked
/* perform a system unlock sequence */
// starting critical sequence
SYSKEY = 0x00000000; //write invalid key to force lock
SYSKEY = 0xAA996655; //write key1 to SYSKEY
SYSKEY = 0x556699AA; //write key2 to SYSKEY
// OSCCON is now unlocked
/* set SWRST bit to arm reset */
RSWRSTSET = 1;
/* read RSWRST register to trigger reset */
unsigned int dummy;
dummy = RSWRST;
/* prevent any unwanted code execution until reset occurs*/
while(1);
}
}
};
#endif // QUICK_H