-
Notifications
You must be signed in to change notification settings - Fork 0
/
i-ching.ino
409 lines (290 loc) · 7.2 KB
/
i-ching.ino
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
#include <MD_MAX72xx.h>
#include <SPI.h>
#include <MD_Parola.h>
#include "chords.h"
#include "hexagram.h"
/*
I-Ching oracle consultation device.
by Ferrie Bank - Amsterdam, 8 July 2020
*/
#define BUTTON_PIN 8
boolean but;
/* 8x8 display init */
#define HARDWARE_TYPE MD_MAX72XX::ICSTATION_HW
#define MAX_DEVICES 1
#define DATA_PIN 10
#define CS_PIN 11
#define CLK_PIN 12
MD_Parola P = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
MD_MAX72XX *MAX = P.getGraphicObject();
#define PAUSE_TIME 1000
uint8_t OPEN = B01100110;
uint8_t CLOSED = B01111110;
uint8_t hexagram[6];
uint8_t hexaValue = 0;
uint8_t transHexagram[6];
uint8_t transHexaValue = 0;
uint8_t segIndex = 0;
const uint8_t messageLength = 105;
char today[messageLength];
char tomorrow[messageLength];
boolean busyRolling = false;
uint8_t ledPattern = 0;
uint8_t blinkIndex = 0;
uint16_t blinkCount = 0;
uint8_t yinyang[] = {
B00111100,
B01110010,
B11100101,
B11110001,
B11111001,
B11011001,
B01110010,
B00111100
};
/* side LEDs init */
byte pat = 0;
const byte LED6_PIN = 7;
const byte LED5_PIN = 6;
const byte LED4_PIN = 5;
const byte LED3_PIN = 4;
const byte LED2_PIN = 3;
const byte LED1_PIN = 2;
/* music library init */
const int whole_note_millis = 1000;
const int score_element_resolution = 16; // each element in the score array represents a 1/16 (= 1/resolution value) note duration.
const int score_note_millis = (whole_note_millis / score_element_resolution);
const int BUZZER_PIN = 9;
const byte effect_complete[] = {
C, CS, D, DS, R, C, CS, D, DS, R, C, CS, D, DS
};
const byte effect_roll_stable[] = {
C, C
};
const byte effect_roll_transient[] = {
DS, DS
};
const byte music_iching[] = {
E, E, E, E, R, R,
CS, CS, CS, CS, CS, CS, R, R,
CS, CS, CS, CS, CS, CS, CS, CS,
R, R, R, R, R, R, R, R,
CS, CS, CS, R, R, E,
E, E, R, R, FS, FS, FS,
R, R, FS, FS, FS, FS, R, R,
E, E, E, E, E, E, E, E
};
void setup() {
digitalWrite(LED_BUILTIN, LOW);
P.begin();
P.setSpeed(100);
P.setPause(2000);
P.setIntensity(0);
P.setTextEffect(PA_SCROLL_LEFT, PA_SCROLL_LEFT);
MAX->clear();
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode (LED6_PIN, OUTPUT);
pinMode (LED5_PIN, OUTPUT);
pinMode (LED4_PIN, OUTPUT);
pinMode (LED3_PIN, OUTPUT);
pinMode (LED2_PIN, OUTPUT);
pinMode (LED1_PIN, OUTPUT);
setLEDs(0);
printLogo(yinyang);
delay(1200);
play_complete();
}
void setRandom() {
unsigned long analog = analogRead(0);
unsigned long timeCount = millis();
randomSeed(analog ^ timeCount);
}
void loop() {
but = digitalRead(BUTTON_PIN);
if (!but && !busyRolling) {
rollSegment();
delay(800);
}
updateLEDs();
}
void reset() {
segIndex = 0;
hexaValue = 0;
transHexaValue = 0;
ledPattern = 0;
blinkIndex = 0;
updateLEDs();
printHexagram(hexagram);
busyRolling = false;
play_complete();
}
void rollSegment() {
busyRolling = true;
setRandom();
if (segIndex > 5) {
reset();
return;
}
uint8_t segment = random(0, 64);
boolean isClosed;
if (segment >= 32) {
hexaValue = setBit (hexaValue, segIndex, false);
hexagram[segIndex] = OPEN;
isClosed = false;
} else {
hexaValue = setBit (hexaValue, segIndex, true);
hexagram[segIndex] = CLOSED;
isClosed = true;
}
uint8_t transcendence = random(0,64);
if (transcendence >= 32) {
ledPattern = (ledPattern | (1 << segIndex));
transHexaValue = setBit (transHexaValue, segIndex, !isClosed);
transHexagram[segIndex] = ((isClosed) ? OPEN : CLOSED);
play_transient();
} else {
transHexaValue = setBit (transHexaValue, segIndex, isClosed);
transHexagram[segIndex] = ((isClosed) ? CLOSED : OPEN);
play_stable();
}
segIndex++;
blinkIndex++;
updateLEDs();
printHexagram(hexagram);
busyRolling = false;
if (segIndex > 5) {
delay(800);
setDescription();
play_iching();
scrollDescription();
printHexagram(transHexagram);
play_iching();
scrollTransientDescription();
cycleHexagrams();
}
}
uint8_t setBit (uint8_t origValue, uint8_t index, boolean setToOne) {
uint8_t result;
if (setToOne) {
result = origValue | (1 << index);
} else {
result = origValue & ~(1 << index);
}
return result;
}
void printLogo(uint8_t logo[]) {
MAX->clear();
for (uint8_t i = 0; i < 8; i++) {
MAX->setRow(0, 0, i, logo[i]);
}
}
void printHexagram(uint8_t hexagram[]) {
MAX->clear();
for (uint8_t i = 0; i < segIndex; i++) {
MAX->setRow(0, 0, (7 - (i + 1)), hexagram[i]);
}
}
void setDescription() {
strcpy_P(today, (char *) pgm_read_word(&(descriptions[hexaValue])));
strcpy_P(tomorrow, (char *) pgm_read_word(&(descriptions[transHexaValue])));
}
void scrollDescription() {
MAX->clear();
char text[messageLength] = " NOW: ";
strcat(text, today);
P.setTextBuffer(text);
P.displayReset();
while (!P.displayAnimate()){;};
}
void scrollTransientDescription() {
MAX->clear();
char text[messageLength] = " LATER: ";
strcat(text, tomorrow);
P.setTextBuffer(text);
P.displayReset();
while (!P.displayAnimate()){;};
}
void cycleHexagrams() {
but = digitalRead(BUTTON_PIN);
char kwhex[4];
kwhex[0] = ' ';
kwhex[1] = today[0];
kwhex[2] = today[1];
kwhex[3] = '\0';
char kwtrans[4];
kwtrans[0] = ' ';
kwtrans[1] = tomorrow[0];
kwtrans[2] = tomorrow[1];
kwtrans[3] = '\0';
char buff[] = "*** ";
while(but) {
printHexagram(hexagram);
if(!digitalRead(BUTTON_PIN)) {
reset();
return;
}
delay(2000);
if(!digitalRead(BUTTON_PIN)) {
reset();
return;
}
showText(kwhex);
if(!digitalRead(BUTTON_PIN)) {
reset();
return;
}
printHexagram(transHexagram);
if(!digitalRead(BUTTON_PIN)) {
reset();
return;
}
delay(2000);
if(!digitalRead(BUTTON_PIN)) {
reset();
return;
}
showText(kwtrans);
if(!digitalRead(BUTTON_PIN)) {
reset();
return;
}
showText(buff);
but = digitalRead(BUTTON_PIN);
}
}
void showText(char output[]) {
P.setTextBuffer(output);
P.displayReset();
while (!P.displayAnimate()){;};
}
void updateLEDs() {
uint8_t burnPattern = ledPattern;
if (blinkCount > 10000) {
burnPattern = burnPattern | (1 << blinkIndex);
} else {
burnPattern = burnPattern & ~(1 << blinkIndex);
}
if (blinkCount > 16000) blinkCount = 0;
blinkCount++;
setLEDs(burnPattern);
}
void setLEDs (byte pattern) {
digitalWrite (LED6_PIN, (pattern & 32));
digitalWrite (LED5_PIN, (pattern & 16));
digitalWrite (LED4_PIN, (pattern & 8));
digitalWrite (LED3_PIN, (pattern & 4));
digitalWrite (LED2_PIN, (pattern & 2));
digitalWrite (LED1_PIN, (pattern & 1));
}
void play_iching () {
play (music_iching, sizeof(music_iching), score_note_millis, BUZZER_PIN);
}
void play_stable () {
play (effect_roll_stable, sizeof(effect_roll_stable), score_note_millis, BUZZER_PIN);
}
void play_transient () {
play (effect_roll_transient, sizeof(effect_roll_transient), score_note_millis, BUZZER_PIN);
}
void play_complete () {
play (effect_complete, sizeof(effect_complete), score_note_millis, BUZZER_PIN);
}