-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFloppyDiskCast.ino
More file actions
149 lines (117 loc) · 3.11 KB
/
FloppyDiskCast.ino
File metadata and controls
149 lines (117 loc) · 3.11 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
#include "ff.h"
#include "ArduinoFDC.h"
#include "floppyfuncs.h"
#include "LowPower.h"
static FIL FatFsFile;
const int wakeUpPin = 2;
const int espRSTPin = A0;
const int powerEnable5VPin = A1;
const int espToArduinoPin = A3;
const int arduinoToEspPin = A4;
void wakeUp()
{
// Just a handler for the pin interrupt.
}
void setup() {
pinMode(wakeUpPin, INPUT_PULLUP);
//pinMode(espRSTPin, INPUT);
pinMode(powerEnable5VPin, OUTPUT);
digitalWrite(powerEnable5VPin, 0);
pinMode(espToArduinoPin, INPUT);
pinMode(arduinoToEspPin, INPUT);
Serial.begin(115200);
}
void resetESP() {
digitalWrite(espRSTPin, LOW);
pinMode(espRSTPin, OUTPUT);
delay(10);
pinMode(espRSTPin, INPUT);
delay(500);
}
bool isESPSleeping() {
return digitalRead(espToArduinoPin);
}
void resetESP_ifNeeded() {
if (isESPSleeping()) {
resetESP();
}
}
void sleep_untilInterrupt() {
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
}
UINT readFileNoPrint(char *fname) {
UINT count;
FRESULT fr;
ArduinoFDC.motorOn();
fr = f_open(&FatFsFile, fname, FA_READ);
if( fr == FR_OK )
{
count = 1;
//TODO: at one point need to simplify code to not overflow TEMPBUFFER
//while( count>0 )
{
fr = f_read(&FatFsFile, tempbuffer, TEMPBUFFER_SIZE, &count);
/*if( fr == FR_OK )
Serial.write(tempbuffer, count);
else
print_ff_error(fr);*/
}
f_close(&FatFsFile);
return count;
}
else
print_ff_error(fr);
}
void loop() {
UINT count = 0;
//Wait for disk
//NOTE: PIN34 could be hooked up; https://en.wikipedia.org/wiki/Floppy_disk_drive_interface
// however, in practice it never is. So jerry-rig a switch instead.
// Allow wake up pin to trigger interrupt on low.
attachInterrupt(0, wakeUp, LOW); // 0 = pin D2
sleep_untilInterrupt();
detachInterrupt(0);
//Disk in, power up stuff
resetESP_ifNeeded();
//XXX, determine needed delay
delay(50);
Serial.println("diskin\n");
Serial.print("\n");
Serial.flush();
delay(50);
digitalWrite(powerEnable5VPin, 1);
delay(500);
ArduinoFDC.begin(ArduinoFDCClass::DT_3_HD, ArduinoFDCClass::DT_3_HD);
f_mount(&FatFs, "0:", 0);
//Debug purposes:
//listDir();
//Read autoexec.sh
count = readFile("autoexec.sh"); //also prints on serial
//Send contents over UDP broadcast WIFI, assuming network is hooked up on Serial
//Serial.write(tempbuffer, count);
Serial.print("\n");
Serial.flush();
//Read diskmelo.dy
count = readFileNoPrint("diskmelo.dy");
//Play melody using floppy drive
//TODO
// Move head a couple of tracks, to protect track0
ArduinoFDC.readSector(20, 0, 1, tempbuffer);
ArduinoFDC.motorOff();
delay(50);
ArduinoFDC.end();
delay(200);
digitalWrite(powerEnable5VPin, 0);
//If disk is still in, wait for it to be ejected
if (digitalRead(wakeUpPin) == LOW) {
attachInterrupt(0, wakeUp, RISING);
sleep_untilInterrupt();
detachInterrupt(0);
}
resetESP_ifNeeded();
Serial.println("diskout\n");
Serial.print("\n");
Serial.flush();
//debounce the switch a bit
delay(50);
}