-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.c
184 lines (162 loc) · 3.83 KB
/
snake.c
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
/* SPDX-License-Identifier: GPL-2.0+ */
/*
* 3D "snake" program for ATmega16-driven 5x5x5 LED cube.
* Lubomir Rintel <[email protected]>
*
* This program is free software.
* Repository: <https://github.com/hackerspace/led5x5x5>
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <avr/io.h>
#define F_CPU 1000000UL
#include <util/delay.h>
static inline uint8_t
bitfield (uint8_t a, uint8_t b, uint8_t c, uint8_t d,
uint8_t e, uint8_t f, uint8_t g, uint8_t h)
{
return (a ? 0x01 : 0) |
(b ? 0x02 : 0) |
(c ? 0x04 : 0) |
(d ? 0x08 : 0) |
(e ? 0x10 : 0) |
(f ? 0x20 : 0) |
(g ? 0x40 : 0) |
(h ? 0x80 : 0);
}
/*
* Field buffer. Non-zero number is the "age" of the position that decays
* towards zero on every update.
*/
static uint8_t field[5][5][5] = { 0, };
/*
* Draw one row from the field buffer.
*/
static inline void
zrow (int z)
{
PORTA = bitfield (
field[0][3][z], field[0][1][z], field[0][4][z], field[1][3][z],
field[1][2][z], field[1][1][z], field[1][0][z], field[2][0][z]);
PORTB = bitfield (
field[1][4][z], field[0][2][z], field[0][0][z], z == 1,
z == 4, z == 0, 0, 0);
PORTC = bitfield (
field[4][4][z], field[3][4][z], field[3][2][z], field[3][0][z],
field[2][4][z], field[2][3][z], field[2][2][z], field[2][1][z]);
PORTD = bitfield (
z == 3, z == 2, field[3][1][z], field[3][3][z],
field[4][2][z], field[4][1][z], field[4][0][z], field[4][3][z]);
_delay_ms (3);
PORTA = PORTB = PORTC = PORTD = 0;
}
int
main (int argc, char *argv[])
{
struct { uint8_t x, y, z; } tgt = { 0, }, cur = { 0, };
uint8_t x, y, z;
uint8_t len = 0;
uint8_t mcucsr = MCUCSR;
/*
* Nuke JTAG, so that we can use its ports.
* Hardware requires that this register is wrote twice.
*/
mcucsr |= 0x80;
MCUCSR = mcucsr;
MCUCSR = mcucsr;
/*
* Pin directions.
*/
DDRA = 0xff;
DDRB = 0xff;
DDRC = 0xff;
DDRD = 0xff;
while (1) {
/*
* Age all tiles.
*/
for (x = 0; x < 5; x++) {
for (y = 0; y < 5; y++) {
for (z = 0; z < 5; z++) {
if (field[x][y][z] > 0)
field[x][y][z]--;
}
}
}
/*
* Add a new one at head.
*/
field[cur.x][cur.y][cur.z] = len;
/*
* Successfully hit the target?
*/
if (tgt.x == cur.x && tgt.y == cur.y && tgt.z == cur.z) {
len++;
do {
tgt.x = rand () % 5;
tgt.y = rand () % 5;
tgt.z = rand () % 5;
} while (field[tgt.x][tgt.y][tgt.z] != 0);
/*
* Can move in a good direction?
*/
} else if (tgt.x > cur.x && field[cur.x + 1][cur.y][cur.z] == 0) {
cur.x++;
} else if (tgt.x < cur.x && field[cur.x - 1][cur.y][cur.z] == 0) {
cur.x--;
} else if (tgt.y > cur.y && field[cur.x][cur.y + 1][cur.z] == 0) {
cur.y++;
} else if (tgt.y < cur.y && field[cur.x][cur.y - 1][cur.z] == 0) {
cur.y--;
} else if (tgt.z > cur.z && field[cur.x][cur.y][cur.z + 1] == 0) {
cur.z++;
} else if (tgt.z < cur.z && field[cur.x][cur.y][cur.z - 1] == 0) {
cur.z--;
/*
* Can move anywhere?
*/
} else if (cur.x < 4 && field[cur.x + 1][cur.y][cur.z] == 0) {
cur.x++;
} else if (cur.x > 0 && field[cur.x - 1][cur.y][cur.z] == 0) {
cur.x--;
} else if (cur.y < 4 && field[cur.x][cur.y + 1][cur.z] == 0) {
cur.y++;
} else if (cur.y > 0 && field[cur.x][cur.y - 1][cur.z] == 0) {
cur.y--;
} else if (cur.z < 4 && field[cur.x][cur.y][cur.z + 1] == 0) {
cur.z++;
} else if (cur.z > 0 && field[cur.x][cur.y][cur.z - 1] == 0) {
cur.z--;
/*
* Stuck?
*/
} else {
cur.x = rand () % 5;
cur.y = rand () % 5;
cur.z = rand () % 5;
len = 1;
for (x = 0; x < 5; x++) {
for (y = 0; y < 5; y++) {
for (z = 0; z < 5; z++) {
field[x][y][z] = 0;
}
}
}
}
/*
* Show target.
*/
field[tgt.x][tgt.y][tgt.z] = 1;
/*
* Output (interlaced)
*/
for (x = 0; x < 10; x++) {
zrow (0);
zrow (2);
zrow (4);
zrow (1);
zrow (3);
}
}
}