-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPIUIO_leonardo.c
More file actions
executable file
·466 lines (432 loc) · 11.8 KB
/
PIUIO_leonardo.c
File metadata and controls
executable file
·466 lines (432 loc) · 11.8 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/***********************************************************/
/* ____ ___ _ _ ___ ___ ____ _ */
/* | _ \_ _| | | |_ _/ _ \ / ___| | ___ _ __ ___ */
/* | |_) | || | | || | | | | | | | |/ _ \| '_ \ / _ \ */
/* | __/| || |_| || | |_| | | |___| | (_) | | | | __/ */
/* |_| |___|\___/|___\___/ \____|_|\___/|_| |_|\___| */
/* */
/* By: USB-ON-ATMEGAU4 BY CKDUR */
/***********************************************************/
/* Basicly this is an PIUIO Clone with an ATMEGA32U2 */
/* interfaced on ARDUINO Leonardo */
/***********************************************************/
/* This is main code from PIUIO Clone */
/***********************************************************/
/* Please look at LICENSE for details */
/* Please consult https://github.com/ckdur/PIUIO_arduino */
/***********************************************************/
#include "PIUIO.h"
#include "PIUIO_ctrl.h"
#define PIUIO_CTL_REQ 0xAE
#define GETBIT(port,bit) ((unsigned char)(((unsigned char)(port)) & ((unsigned char)(0x01 << ((unsigned char)(bit))))))
#define SETBIT(port,bit) port |= (0x01 << (bit))
#define CLRBIT(port,bit) port &= ~(0x01 << (bit))
// This turns on one of the LEDs hooked up to the chip
// NOTE: use the enum for the port
enum IO_PORTS {
IO_PORT_B,
IO_PORT_C,
IO_PORT_D,
IO_PORT_E,
IO_PORT_F
};
// Sets or unsets the output mode
void MODEon(char ledNumber, int port) {
if(port == IO_PORT_B) {
DDRB |= 1 << ledNumber;
}
if(port == IO_PORT_C) {
DDRC |= 1 << ledNumber;
}
if(port == IO_PORT_D) {
DDRD |= 1 << ledNumber;
}
if(port == IO_PORT_E) {
DDRE |= 1 << ledNumber;
}
if(port == IO_PORT_F) {
DDRF |= 1 << ledNumber;
}
}
void MODEoff(char ledNumber, int port) {
if(port == IO_PORT_B) {
DDRB &= ~(1 << ledNumber);
}
if(port == IO_PORT_C) {
DDRC &= ~(1 << ledNumber);
}
if(port == IO_PORT_D) {
DDRD &= ~(1 << ledNumber);
}
if(port == IO_PORT_E) {
DDRE &= ~(1 << ledNumber);
}
if(port == IO_PORT_F) {
DDRF &= ~(1 << ledNumber);
}
}
// Sets or unsets the output (remember to set the mode first)
void LEDon(char ledNumber, int port){
if(port == IO_PORT_B) {
PORTB |= 1 << ledNumber;
}
if(port == IO_PORT_C) {
PORTC |= 1 << ledNumber;
}
if(port == IO_PORT_D) {
PORTD |= 1 << ledNumber;
}
if(port == IO_PORT_E) {
PORTE |= 1 << ledNumber;
}
if(port == IO_PORT_F) {
PORTF |= 1 << ledNumber;
}
}
// And this turns it off
void LEDoff(char ledNumber, int port){
if(port == IO_PORT_B) {
PORTB &= ~(1 << ledNumber);
}
if(port == IO_PORT_C) {
PORTC &= ~(1 << ledNumber);
}
if(port == IO_PORT_D) {
PORTD &= ~(1 << ledNumber);
}
if(port == IO_PORT_E) {
PORTE &= ~(1 << ledNumber);
}
if(port == IO_PORT_F) {
PORTF &= ~(1 << ledNumber);
}
}
int READfrom(char ledNumber, int port){
int st = 0;
if(port == IO_PORT_B) {
st = (PINB & (1 << ledNumber)) ? 1 : 0;
}
if(port == IO_PORT_C) {
st = (PINC & (1 << ledNumber)) ? 1 : 0;
}
if(port == IO_PORT_D) {
st = (PIND & (1 << ledNumber)) ? 1 : 0;
}
if(port == IO_PORT_E) {
st = (PINE & (1 << ledNumber)) ? 1 : 0;
}
if(port == IO_PORT_F) {
st = (PINF & (1 << ledNumber)) ? 1 : 0;
}
return st;
}
// Configurations of all
#define RXLED 0, IO_PORT_B
#define TXLED 5, IO_PORT_D
// As for the rest...
// Check https://www.arduino.cc/en/uploads/Main/arduino-leonardo-schematic_3b.pdf for details
#define DIO0 PD2, IO_PORT_D
#define DIO1 PD3, IO_PORT_D
#define DIO2 PD1, IO_PORT_D
#define DIO3 PD0, IO_PORT_D
#define DIO4 PD4, IO_PORT_D
#define DIO5 PC6, IO_PORT_C
#define DIO6 PD7, IO_PORT_D
#define DIO7 PE6, IO_PORT_E
#define DIO8 PB4, IO_PORT_B
#define DIO9 PB5, IO_PORT_B
#define DIO10 PB6, IO_PORT_B
#define DIO11 PB7, IO_PORT_B
#define DIO12 PD6, IO_PORT_D
#define DIO13 PC7, IO_PORT_C
#define DIOA0 PF7, IO_PORT_F
#define DIOA1 PF6, IO_PORT_F
#define DIOA2 PF5, IO_PORT_F
#define DIOA3 PF4, IO_PORT_F
#define DIOA4 PF1, IO_PORT_F
#define DIOA5 PF0, IO_PORT_F
// Now, assigning the IO according to ColombiaStep's stuff
#define P1_PAD
#define P2_PAD
//#define DO_LED
#if defined(P1_PAD) && defined(P2_PAD) && defined(DO_LED)
#error "This implementation does not support P1 & P2 and Lights. You need to disable one of them"
#endif
#ifdef P1_PAD
#define KEY_Q DIO8
#define KEY_E DIO4
#define KEY_S DIO10
#define KEY_Z DIO6
#define KEY_C DIO2
#ifdef DO_LED
#define LED_Q DIO7
#define LED_E DIO3
#define LED_S DIO9
#define LED_Z DIO5
#define LED_C DIO1
#endif // DO_LED
#endif
#ifdef P2_PAD
#ifndef P1_PAD
#define KEY_7 DIO8
#define KEY_9 DIO4
#define KEY_5 DIO10
#define KEY_1 DIO6
#define KEY_3 DIO2
#else
#define KEY_7 DIO7
#define KEY_9 DIO3
#define KEY_5 DIO9
#define KEY_1 DIO5
#define KEY_3 DIO1
#endif
#ifdef DO_LED
#define LED_7 DIO7
#define LED_9 DIO3
#define LED_5 DIO9
#define LED_1 DIO5
#define LED_3 DIO1
#endif // DO_LED
#endif
#define KEY_COIN1 DIO13
//#define KEY_COIN2 DIO13
//#define KEY_CLEAR DIO13
#define KEY_SERVICE DIO12
#define KEY_TEST DIO11
#define LED_NEON DIOA0
#define LED_UL DIOA1
#define LED_DL DIOA2
#define LED_UR DIOA3
#define LED_DL DIOA4
#define KEY_SWITCH DIO0
/** Main program entry point. This routine contains the overall program flow, including initial
* setup of all components and the main program loop.
*/
int main(void)
{
nControl = 0;
SetupHardware();
GlobalInterruptEnable();
int i;
for(i=0;i<8;i++){
InputData[i] = 0xFF;
LampData[i] = 0x0;
}
#ifdef KEY_Q
MODEoff(KEY_Q); LEDon(KEY_Q);
#endif
#ifdef KEY_E
MODEoff(KEY_E); LEDon(KEY_E);
#endif
#ifdef KEY_S
MODEoff(KEY_S); LEDon(KEY_S);
#endif
#ifdef KEY_Z
MODEoff(KEY_Z); LEDon(KEY_Z);
#endif
#ifdef KEY_C
MODEoff(KEY_C); LEDon(KEY_C);
#endif
#ifdef KEY_7
MODEoff(KEY_7); LEDon(KEY_7);
#endif
#ifdef KEY_9
MODEoff(KEY_9); LEDon(KEY_9);
#endif
#ifdef KEY_5
MODEoff(KEY_5); LEDon(KEY_5);
#endif
#ifdef KEY_1
MODEoff(KEY_1); LEDon(KEY_1);
#endif
#ifdef KEY_3
MODEoff(KEY_3); LEDon(KEY_3);
#endif
#ifdef KEY_CLEAR
MODEoff(KEY_CLEAR); LEDon(KEY_CLEAR);
#endif
#ifdef KEY_SERVICE
MODEoff(KEY_SERVICE); LEDon(KEY_SERVICE);
#endif
#ifdef KEY_TEST
MODEoff(KEY_TEST); LEDon(KEY_TEST);
#endif
#ifdef KEY_COIN1
MODEoff(KEY_COIN1); LEDon(KEY_COIN1);
#endif
#ifdef KEY_COIN2
MODEoff(KEY_COIN2); LEDon(KEY_COIN2);
#endif
#ifdef KEY_SWITCH
MODEoff(KEY_SWITCH); LEDon(KEY_SWITCH);
#endif
#ifdef LED_Q
MODEon(LED_Q);
#endif
#ifdef LED_E
MODEon(LED_E);
#endif
#ifdef LED_S
MODEon(LED_S);
#endif
#ifdef LED_Z
MODEon(LED_Z);
#endif
#ifdef LED_C
MODEon(LED_C);
#endif
#ifdef LED_7
MODEon(LED_7);
#endif
#ifdef LED_9
MODEon(LED_9);
#endif
#ifdef LED_5
MODEon(LED_5);
#endif
#ifdef LED_1
MODEon(LED_1);
#endif
#ifdef LED_3
MODEon(LED_3);
#endif
#ifdef LED_NEON
MODEon(LED_NEON);
#endif
#ifdef LED_UL
MODEon(LED_UL);
#endif
#ifdef LED_DL
MODEon(LED_DL);
#endif
#ifdef LED_UR
MODEon(LED_UR);
#endif
#ifdef LED_DR
MODEon(LED_DR);
#endif
#ifdef KEY_SWITCH
int prev_switch;
prev_switch = READfrom(KEY_SWITCH)?1:0;
for(int i = 1; i < 4; i++) {
prev_switch |= (prev_switch & 1) << i;
}
int prev_switch_state = prev_switch == 0xF?1:0;
#endif
for (;;)
{
RESTART:
USB_Loop();
USB_USBTask();
if(nControl > 1000) LEDon(RXLED);
if(nControl > 2000) {LEDoff(RXLED); nControl = 0;}
#ifdef KEY_SWITCH
prev_switch = prev_switch << 1;
prev_switch |= READfrom(KEY_SWITCH)?1:0;
prev_switch &= 0xF;
int cur_switch_state = prev_switch_state;
if(prev_switch == 0xF) cur_switch_state = 1;
else if(prev_switch == 0x0) cur_switch_state = 0;
if(prev_switch_state != cur_switch_state && !cur_switch_state) {
go_next_device();
}
prev_switch_state = cur_switch_state;
#endif
#ifdef KEY_Q
if(READfrom(KEY_Q)) {SETBIT(InputData[0],0);} else {CLRBIT(InputData[0],0);}
#endif
#ifdef KEY_E
if(READfrom(KEY_E)) {SETBIT(InputData[0],1);} else {CLRBIT(InputData[0],1);}
#endif
#ifdef KEY_S
if(READfrom(KEY_S)) {SETBIT(InputData[0],2);} else {CLRBIT(InputData[0],2);}
#endif
#ifdef KEY_Z
if(READfrom(KEY_Z)) {SETBIT(InputData[0],3);} else {CLRBIT(InputData[0],3);}
#endif
#ifdef KEY_C
if(READfrom(KEY_C)) {SETBIT(InputData[0],4);} else {CLRBIT(InputData[0],4);}
#endif
#ifdef KEY_7
if(READfrom(KEY_7)) {SETBIT(InputData[2],0);} else {CLRBIT(InputData[2],0);}
#endif
#ifdef KEY_9
if(READfrom(KEY_9)) {SETBIT(InputData[2],1);} else {CLRBIT(InputData[2],1);}
#endif
#ifdef KEY_5
if(READfrom(KEY_5)) {SETBIT(InputData[2],2);} else {CLRBIT(InputData[2],2);}
#endif
#ifdef KEY_1
if(READfrom(KEY_1)) {SETBIT(InputData[2],3);} else {CLRBIT(InputData[2],3);}
#endif
#ifdef KEY_3
if(READfrom(KEY_3)) {SETBIT(InputData[2],4);} else {CLRBIT(InputData[2],4);}
#endif
#ifdef KEY_CLEAR
if(READfrom(KEY_CLEAR)) {SETBIT(InputData[1],7);} else {CLRBIT(InputData[1],7);}
if(READfrom(KEY_CLEAR)) {SETBIT(InputData[3],7);} else {CLRBIT(InputData[3],7);}
#endif
#ifdef KEY_SERVICE
if(READfrom(KEY_SERVICE)) {SETBIT(InputData[1],6);} else {CLRBIT(InputData[1],6);}
if(READfrom(KEY_SERVICE)) {SETBIT(InputData[3],6);} else {CLRBIT(InputData[3],6);}
#endif
#ifdef KEY_TEST
if(READfrom(KEY_TEST)) {SETBIT(InputData[1],1);} else {CLRBIT(InputData[1],1);}
if(READfrom(KEY_TEST)) {SETBIT(InputData[3],1);} else {CLRBIT(InputData[3],1);}
#endif
#ifdef KEY_COIN1
if(READfrom(KEY_COIN1)) {SETBIT(InputData[1],2);} else {CLRBIT(InputData[1],2);}
#endif
#ifdef KEY_COIN2
if(READfrom(KEY_COIN2)) {SETBIT(InputData[3],2);} else {CLRBIT(InputData[3],2);}
#endif
#ifdef LED_Q
if(GETBIT(LampData[0],2)) {LEDon(LED_Q);} else {LEDoff(LED_Q);}
#endif
#ifdef LED_E
if(GETBIT(LampData[0],3)) {LEDon(LED_E);} else {LEDoff(LED_E);}
#endif
#ifdef LED_S
if(GETBIT(LampData[0],4)) {LEDon(LED_Q);} else {LEDoff(LED_Q);}
#endif
#ifdef LED_Z
if(GETBIT(LampData[0],5)) {LEDon(LED_Z);} else {LEDoff(LED_Z);}
#endif
#ifdef LED_C
if(GETBIT(LampData[0],6)) {LEDon(LED_C);} else {LEDoff(LED_C);}
#endif
#ifdef LED_7
if(GETBIT(LampData[2],2)) {LEDon(LED_7);} else {LEDoff(LED_7);}
#endif
#ifdef LED_9
if(GETBIT(LampData[2],3)) {LEDon(LED_9);} else {LEDoff(LED_9);}
#endif
#ifdef LED_5
if(GETBIT(LampData[2],4)) {LEDon(LED_5);} else {LEDoff(LED_5);}
#endif
#ifdef LED_1
if(GETBIT(LampData[2],5)) {LEDon(LED_1);} else {LEDoff(LED_1);}
#endif
#ifdef LED_3
if(GETBIT(LampData[2],6)) {LEDon(LED_3);} else {LEDoff(LED_3);}
#endif
#ifdef LED_NEON
if(GETBIT(LampData[1],2)) {LEDon(LED_NEON);} else {LEDoff(LED_NEON);}
#endif
#ifdef LED_UL
if(GETBIT(LampData[2],7)) {LEDon(LED_UL);} else {LEDoff(LED_UL);}
#endif
#ifdef LED_DL
if(GETBIT(LampData[3],0)) {LEDon(LED_DL);} else {LEDoff(LED_DL);}
#endif
#ifdef LED_UR
if(GETBIT(LampData[3],1)) {LEDon(LED_UR);} else {LEDoff(LED_UR);}
#endif
#ifdef LED_DR
if(GETBIT(LampData[3],2)) {LEDon(LED_DR);} else {LEDoff(LED_DR);}
#endif
// Note: Logic is inverted in these leds
if(InputData[0] != 0xFF || InputData[1] != 0xFF || InputData[2] != 0xFF || InputData[3] != 0xFF) LEDoff(TXLED); else LEDon(TXLED);
}
return 0;
}