-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathESPboy_PT3Play.ino
818 lines (578 loc) · 15.3 KB
/
ESPboy_PT3Play.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
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
//v1.3 22.10.2019 gfx to TFT_eSPI lib, fixed 80/160Mhz bug
//v1.2 14.12.2019 linear interp for better quality, analyzer display fix, backlight off during startup
//v1.1 14.12.2019 hardware init fix, stereo and i2s support
//v1.0 13.12.2019 initial version
//by Shiru
//shiru@mail.ru
//https://www.patreon.com/shiru8bit
//configure output device
//if the i2s DAC is selected, but not connected, ESPboy crashes
//#define OUTPUT_DEVICE OUT_SPEAKER
#define OUTPUT_DEVICE OUT_I2S
enum {
OUT_SPEAKER = 0,
OUT_I2S
};
#include <TFT_eSPI.h>
#include <Adafruit_MCP23017.h>
#include <Adafruit_MCP4725.h>
#include <ESP8266WiFi.h>
#include <sigma_delta.h>
#include <i2s.h>
#include <i2s_reg.h>
#include "glcdfont.c"
#include "gfx/espboy.h"
#define MCP23017address 0 // actually it's 0x20 but in <Adafruit_MCP23017.h> lib there is (x|0x20) :)
//PINS
#define LEDPIN D4
#define SOUNDPIN D3
#define csTFTMCP23017pin 8
TFT_eSPI tft;
Adafruit_MCP23017 mcp;
#define MCP4725address 0x60
Adafruit_MCP4725 dac;
#define AY_CLOCK 1773400 //pitch
#define SAMPLE_RATE 44010 //quality of the sound, i2s DAC can't handle more than 44100 by some reason (not even 48000)
#define FRAME_RATE 50 //speed
volatile uint32_t sound_stereo_dac;
int pad_state;
int pad_state_prev;
int pad_state_t;
#define PAD_LEFT 0x01
#define PAD_UP 0x02
#define PAD_DOWN 0x04
#define PAD_RIGHT 0x08
#define PAD_A 0x10
#define PAD_B 0x20
#define PAD_ANY (PAD_UP|PAD_DOWN|PAD_LEFT|PAD_RIGHT|PAD_A|PAD_B)
#define SPEC_BANDS 42
#define SPEC_HEIGHT 48
#define SPEC_RANGE 1000
#define SPEC_SX 0
#define SPEC_SY 64
#define SPEC_BAND_WIDTH 3
#define SPEC_DECAY 3
#define SPEC_CHA_COL 0x041F
#define SPEC_CHB_COL 0xFC18
#define SPEC_CHC_COL 0xFC1F
#define SPEC_ENV_COL 0x87F0
volatile int16_t spec_levels[SPEC_BANDS];
volatile int16_t spec_levels_prev[SPEC_BANDS];
volatile uint16_t spec_colors[SPEC_BANDS];
uint8_t music_data[32768];
int music_data_size;
int output_device;
#include "ay_emu.h"
struct PT3_Channel_Parameters
{
unsigned short Address_In_Pattern, OrnamentPointer, SamplePointer, Ton;
unsigned char Loop_Ornament_Position, Ornament_Length, Position_In_Ornament, Loop_Sample_Position, Sample_Length, Position_In_Sample, Volume, Number_Of_Notes_To_Skip, Note, Slide_To_Note, Amplitude;
bool Envelope_Enabled, Enabled, SimpleGliss;
short Current_Amplitude_Sliding, Current_Noise_Sliding, Current_Envelope_Sliding, Ton_Slide_Count, Current_OnOff, OnOff_Delay, OffOn_Delay, Ton_Slide_Delay, Current_Ton_Sliding, Ton_Accumulator, Ton_Slide_Step, Ton_Delta;
signed char Note_Skip_Counter;
};
struct PT3_Parameters
{
unsigned char Env_Base_lo;
unsigned char Env_Base_hi;
short Cur_Env_Slide, Env_Slide_Add;
signed char Cur_Env_Delay, Env_Delay;
unsigned char Noise_Base, Delay, AddToNoise, DelayCounter, CurrentPosition;
int Version;
};
struct PT3_SongInfo
{
PT3_Parameters PT3;
PT3_Channel_Parameters PT3_A, PT3_B, PT3_C;
};
struct AYSongInfo
{
unsigned char* module;
unsigned char* module1;
int module_len;
PT3_SongInfo data;
PT3_SongInfo data1;
bool is_ts;
AYChipStruct chip0;
AYChipStruct chip1;
};
struct AYSongInfo AYInfo;
void ay_resetay(AYSongInfo* info, int chipnum)
{
if (!chipnum) ay_init(&info->chip0); else ay_init(&info->chip1);
}
void ay_writeay(AYSongInfo* info, int reg, int val, int chipnum)
{
if (!chipnum) ay_out(&info->chip0, reg, val); else ay_out(&info->chip1, reg, val);
}
#include "PT3Play.h"
int interruptCnt;
void spec_add(int hz, int level, uint16_t color)
{
int i, off;
const int curve[5] = {SPEC_HEIGHT / 10, SPEC_HEIGHT / 5, SPEC_HEIGHT / 2, SPEC_HEIGHT / 5, SPEC_HEIGHT / 10};
if (hz)
{
off = hz / (SPEC_RANGE / SPEC_BANDS) - 2;
if (off > SPEC_BANDS - 1) off = SPEC_BANDS - 1;
for (i = 0; i < 5; ++i)
{
if (off >= 0 && off < SPEC_BANDS)
{
spec_levels[off] += curve[i] * level / 16;
if (spec_levels[off] > SPEC_HEIGHT) spec_levels[off] = SPEC_HEIGHT;
spec_colors[off] = color;
}
++off;
}
}
}
void spec_update()
{
int i;
for (i = 0; i < SPEC_BANDS; ++i)
{
spec_levels[i] -= SPEC_DECAY;
if (spec_levels[i] < 0) spec_levels[i] = 0;
}
}
void spec_add_ay(AYChipStruct* chip)
{
int period;
if (!(chip->reg[7] & 0x01) && !(chip->reg[8] & 0x10))
{
period = chip->reg[0] + chip->reg[1] * 256;
if (period) spec_add(AY_CLOCK / 16 / period, chip->reg[8], SPEC_CHA_COL);
}
if (!(chip->reg[7] & 0x02) && !(chip->reg[9] & 0x10))
{
period = chip->reg[2] + chip->reg[3] * 256;
if (period) spec_add(AY_CLOCK / 16 / period, chip->reg[9], SPEC_CHB_COL);
}
if (!(chip->reg[7] & 0x04) && !(chip->reg[10] & 0x10))
{
period = chip->reg[4] + chip->reg[5] * 256;
if (period) spec_add(AY_CLOCK / 16 / period, chip->reg[10], SPEC_CHC_COL);
}
if ((chip->reg[8] & 0x10) || (chip->reg[9] & 0x10) || (chip->reg[10] & 0x10))
{
period = chip->reg[11] + chip->reg[12] * 256;
if (period) spec_add(AY_CLOCK / 16 / 16 / period, 12, SPEC_ENV_COL);
}
}
uint32_t emulate_sample(void)
{
uint32_t out_l, out_r;
if (interruptCnt++ >= (SAMPLE_RATE / FRAME_RATE))
{
spec_update();
spec_add_ay(&AYInfo.chip0);
if (AYInfo.is_ts) spec_add_ay(&AYInfo.chip1);
PT3_Play_Chip(AYInfo, 0);
interruptCnt = 0;
}
ay_tick(&AYInfo.chip0, (AY_CLOCK / SAMPLE_RATE / 8));
out_l = AYInfo.chip0.out[0] + AYInfo.chip0.out[1] / 2;
out_r = AYInfo.chip0.out[2] + AYInfo.chip0.out[1] / 2;
if (AYInfo.is_ts)
{
ay_tick(&AYInfo.chip1, (AY_CLOCK / SAMPLE_RATE / 8));
out_l += AYInfo.chip0.out[0] + AYInfo.chip0.out[1] / 2;
out_r += AYInfo.chip0.out[2] + AYInfo.chip0.out[1] / 2;
}
if (out_l > 32767) out_l = 32767;
if (out_r > 32767) out_r = 32767;
return out_l | (out_r << 16);
}
void ICACHE_RAM_ATTR sound_speaker_ISR()
{
sigmaDeltaWrite(0, sound_stereo_dac);
uint32_t out = emulate_sample();
sound_stereo_dac = ((out & 0xff00) >> 8) + ((out & 0xff000000) >> 24); //convert to 8-bit mono
}
void ICACHE_RAM_ATTR sound_i2s_ISR()
{
i2s_write_sample_nb(sound_stereo_dac);
sound_stereo_dac = emulate_sample();
}
int check_key()
{
pad_state_prev = pad_state;
pad_state = 0;
for (int i = 0; i < 8; i++)
{
if (!mcp.digitalRead(i)) pad_state |= (1 << i);
}
pad_state_t = pad_state ^ pad_state_prev & pad_state;
return pad_state;
}
//0 no timeout, otherwise timeout in ms
void wait_any_key(int timeout)
{
timeout /= 100;
while (1)
{
check_key();
if (pad_state_t&PAD_ANY) break;
if (timeout)
{
--timeout;
if (timeout <= 0) break;
}
delay(100);
}
}
//render part of a 8-bit uncompressed BMP file
//no clipping
//uses line buffer to draw it much faster than through writePixel
void drawBMP8Part(int16_t x, int16_t y, const uint8_t bitmap[], int16_t dx, int16_t dy, int16_t w, int16_t h)
{
int32_t i, j, bw, bh, wa, off, col, rgb;
static uint16_t buf[128];
bw = pgm_read_dword(&bitmap[0x12]);
bh = pgm_read_dword(&bitmap[0x16]);
wa = (bw + 3) & ~3;
if (w >= h)
{
for (i = 0; i < h; ++i)
{
off = 54 + 256 * 4 + (bh - 1 - (i + dy)) * wa + dx;
for (j = 0; j < w; ++j)
{
col = pgm_read_byte(&bitmap[off++]);
rgb = pgm_read_dword(&bitmap[54 + col * 4]);
buf[j] = ((rgb & 0xf8) >> 3) | ((rgb & 0xfc00) >> 5) | ((rgb & 0xf80000) >> 8);
}
tft.pushImage(x, y + i, w, 1, buf);
}
}
else
{
for (i = 0; i < w; ++i)
{
off = 54 + 256 * 4 + (bh - 1 - dy) * wa + i + dx;
for (j = 0; j < h; ++j)
{
col = pgm_read_byte(&bitmap[off]);
rgb = pgm_read_dword(&bitmap[54 + col * 4]);
buf[j] = ((rgb & 0xf8) >> 3) | ((rgb & 0xfc00) >> 5) | ((rgb & 0xf80000) >> 8);
off -= wa;
}
tft.pushImage(x + i, y, 1, h, buf);
}
}
}
void drawCharFast(int x, int y, int c, int16_t color, int16_t bg)
{
int i, j, line;
static uint16_t buf[5 * 8];
for (i = 0; i < 5; ++i)
{
line = pgm_read_byte(&font[c * 5 + i]);
for (j = 0; j < 8; ++j)
{
buf[j * 5 + i] = (line & 1) ? color : bg;
line >>= 1;
}
}
tft.pushImage(x, y, 5, 8, buf);
}
void printFast(int x, int y, char* str, int16_t color)
{
char c;
while (1)
{
c = *str++;
if (!c) break;
drawCharFast(x, y, c, color, 0);
x += 6;
}
}
bool espboy_logo_effect(int out)
{
int i, j, w, h, sx, sy, off, st, anim;
sx = 32;
sy = 28;
w = 64;
h = 72;
st = 8;
for (anim = 0; anim < st; ++anim)
{
if (check_key()&PAD_ANY) return false;
//if (!out) set_speaker(200 + anim * 50, 5);
for (i = 0; i < w / st; ++i)
{
for (j = 0; j < st; ++j)
{
off = anim - (7 - j);
if (out) off += 8;
if (off < 0 || off >= st) off = 0; else off += i * st;
drawBMP8Part(sx + i * st + j, sy, g_espboy, off, 0, 1, h);
}
}
delay(1000 / 30);
}
return true;
}
void music_open(const char* filename)
{
fs::File f = SPIFFS.open(filename, "r");
if (!f) return;
music_data_size = f.size();
f.readBytes((char*)music_data, music_data_size);
f.close();
}
void music_play()
{
memset(&AYInfo, 0, sizeof(AYInfo));
ay_init(&AYInfo.chip0);
ay_init(&AYInfo.chip1);
AYInfo.module = music_data;
AYInfo.module_len = music_data_size;
PT3_Init(AYInfo);
sound_stereo_dac = 0;
interruptCnt = 0;
switch (output_device)
{
case OUT_SPEAKER:
noInterrupts();
sigmaDeltaSetup(0, SAMPLE_RATE);
sigmaDeltaAttachPin(SOUNDPIN);
sigmaDeltaEnable();
timer1_attachInterrupt(sound_speaker_ISR);
timer1_enable(TIM_DIV1, TIM_EDGE, TIM_LOOP);
timer1_write(80 * 1000000 / SAMPLE_RATE);
interrupts();
break;
case OUT_I2S:
i2s_begin();
i2s_set_rate(SAMPLE_RATE);
timer1_attachInterrupt(sound_i2s_ISR);
timer1_enable(TIM_DIV1, TIM_EDGE, TIM_LOOP);
timer1_write(80 * 1000000 / SAMPLE_RATE);
break;
}
}
void music_stop()
{
noInterrupts();
timer1_disable();
switch (output_device)
{
case OUT_SPEAKER: sigmaDeltaDisable(); break;
case OUT_I2S: i2s_end(); break;
}
interrupts();
delay(10);
}
void playing_screen(const char* filename)
{
int i, h, sx, sy, off, frame;
char str[21];
for (i = 0; i < SPEC_BANDS; ++i)
{
spec_levels[i] = 0;
spec_levels_prev[i] = -1;
spec_colors[i] = TFT_BLACK;
}
music_open(filename);
tft.fillScreen(TFT_BLACK);
printFast(4, 16, "Now playing...", TFT_YELLOW);
tft.fillRect(0, 24, 128, 1, TFT_WHITE);
memset(str, 0, sizeof(str));
memcpy(str, &music_data[0x1e], 20);
printFast(4, 26, str, TFT_WHITE);
memcpy(str, &music_data[0x3f], 20);
printFast(4, 34, str, TFT_WHITE);
sx = SPEC_SX;
for (i = 0; i < SPEC_BANDS; ++i)
{
tft.fillRect(sx, SPEC_SY + SPEC_HEIGHT + 1, SPEC_BAND_WIDTH - 1, 1, TFT_WHITE);
sx += SPEC_BAND_WIDTH;
}
music_play();
frame = 0;
while (music_data)
{
sx = SPEC_SX;
sy = SPEC_SY;
for (i = 0; i < SPEC_BANDS; ++i)
{
h = spec_levels[i];
if (spec_levels_prev[i] != h)
{
spec_levels_prev[i] = h;
if (h > SPEC_HEIGHT) h = SPEC_HEIGHT;
tft.fillRect(sx, sy, 5, SPEC_HEIGHT - h, TFT_BLACK);
tft.fillRect(sx, sy + SPEC_HEIGHT - h, SPEC_BAND_WIDTH - 1, h, spec_colors[i]);
}
sx += SPEC_BAND_WIDTH;
}
check_key();
if (pad_state_t) break;
delay(1);
}
music_stop();
}
#define FILE_HEIGHT 14
#define FILE_FILTER "pt3"
int file_cursor;
bool file_browser_ext(const char* name)
{
while (1) if (*name++ == '.') break;
return (strcasecmp(name, FILE_FILTER) == 0) ? true : false;
}
void file_browser(String path, const char* header, char* filename, int filename_len)
{
int i, j, sy, pos, frame, file_count;
bool change, filter;
fs::Dir dir;
fs::File entry;
char name[19 + 1];
const char* str;
memset(filename, 0, filename_len);
memset(name, 0, sizeof(name));
tft.fillScreen(TFT_BLACK);
dir = SPIFFS.openDir(path);
file_count = 0;
while (dir.next())
{
entry = dir.openFile("r");
filter = file_browser_ext(entry.name());
entry.close();
if (filter) ++file_count;
}
if (!file_count)
{
printFast(24, 60, "No files found", TFT_RED);
while (1) delay(1000);
}
printFast(4, 4, (char*)header, TFT_GREEN);
tft.fillRect(0, 12, 128, 1, TFT_WHITE);
change = true;
frame = 0;
while (1)
{
if (change)
{
pos = file_cursor - FILE_HEIGHT / 2;
if (pos > file_count - FILE_HEIGHT) pos = file_count - FILE_HEIGHT;
if (pos < 0) pos = 0;
dir = SPIFFS.openDir(path);
i = pos;
while (dir.next())
{
entry = dir.openFile("r");
filter = file_browser_ext(entry.name());
entry.close();
if (!filter) continue;
--i;
if (i <= 0) break;
}
sy = 14;
i = 0;
while (1)
{
entry = dir.openFile("r");
filter = file_browser_ext(entry.name());
if (filter)
{
str = entry.name() + 1;
for (j = 0; j < sizeof(name) - 1; ++j)
{
if (*str != 0 && *str != '.') name[j] = *str++; else name[j] = ' ';
}
printFast(8, sy, name, TFT_WHITE);
drawCharFast(2, sy, ' ', TFT_WHITE, TFT_BLACK);
if (pos == file_cursor)
{
strncpy(filename, entry.name(), filename_len);
if (frame & 32) drawCharFast(2, sy, 0xdb, TFT_WHITE, TFT_BLACK);
}
}
entry.close();
if (!dir.next()) break;
if (filter)
{
sy += 8;
++pos;
++i;
if (i >= FILE_HEIGHT) break;
}
}
change = false;
}
check_key();
if (pad_state_t & PAD_UP)
{
--file_cursor;
if (file_cursor < 0) file_cursor = file_count - 1;
change = true;
frame = 32;
}
if (pad_state_t & PAD_DOWN)
{
++file_cursor;
if (file_cursor >= file_count) file_cursor = 0;
change = true;
frame = 32;
}
if (pad_state_t & (PAD_A | PAD_B)) break;
delay(1);
++frame;
if (!(frame & 31)) change = true;
}
}
void setup()
{
//serial init
Serial.begin(115200);
//disable wifi to save some battery power
WiFi.mode(WIFI_OFF);
WiFi.forceSleepBegin();
//DAC init, LCD backlit off
dac.begin(MCP4725address);
delay(100);
dac.setVoltage(0, false);
//mcp23017 and buttons init, should preceed the TFT init
mcp.begin(MCP23017address);
delay(100);
for (int i = 0; i < 8; i++)
{
mcp.pinMode(i, INPUT);
mcp.pullUp(i, HIGH);
}
pad_state = 0;
pad_state_prev = 0;
pad_state_t = 0;
//TFT init
mcp.pinMode(csTFTMCP23017pin, OUTPUT);
mcp.digitalWrite(csTFTMCP23017pin, LOW);
tft.begin();
tft.setSwapBytes(true);
tft.setRotation(0);
tft.fillScreen(TFT_BLACK);
delay(200);
dac.setVoltage(4095, true);
//filesystem init
SPIFFS.begin();
delay(100);
output_device = OUTPUT_DEVICE;
}
void loop()
{
char filename[64];
file_cursor = 0;
//logo (skippable)
if (espboy_logo_effect(0))
{
wait_any_key(1000);
espboy_logo_effect(1);
}
//main loop
while (1)
{
file_browser("/", "Select PT3 file:", filename, sizeof(filename));
playing_screen(filename);
}
}