-
Notifications
You must be signed in to change notification settings - Fork 50
/
FloppyDrives.cpp
326 lines (290 loc) · 9.61 KB
/
FloppyDrives.cpp
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
/*
* FloppyDrives.cpp
*
* Output for controlling floppy drives. The _original_ Moppy instrument!
*/
#include "MoppyInstrument.h"
#include "FloppyDrives.h"
// First drive being used for floppies, and the last drive. Used for calculating
// step and direction pins.
const byte FIRST_DRIVE = 1;
const byte LAST_DRIVE = 8; // This sketch can handle only up to 9 drives (the max for Arduino Uno)
/*NOTE: The arrays below contain unused zero-indexes to avoid having to do extra
* math to shift the 1-based subAddresses to 0-based indexes here. Unlike the previous
* version of Moppy, we *will* be doing math to calculate which drive maps to which pin,
* so there are as many values as drives (plus the extra zero-index)
*/
/*An array of maximum track positions for each floppy drive. 3.5" Floppies have
80 tracks, 5.25" have 50. These should be doubled, because each tick is now
half a position (use 158 and 98).
NOTE: Index zero of this array controls the "resetAll" function, and should be the
same as the largest value in this array
*/
unsigned int MAX_POSITION[] = {158,158,158,158,158,158,158,158,158,158};
//Array to track the current position of each floppy head.
unsigned int currentPosition[] = {0,0,0,0,0,0,0,0,0,0};
/*Array to keep track of state of each pin. Even indexes track the control-pins for toggle purposes. Odd indexes
track direction-pins. LOW = forward, HIGH=reverse
*/
int currentState[] = {0,0,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW};
// Current period assigned to each drive. 0 = off. Each period is two-ticks (as defined by
// TIMER_RESOLUTION in MoppyInstrument.h) long.
unsigned int currentPeriod[] = {0,0,0,0,0,0,0,0,0,0};
// Tracks the current tick-count for each drive (see FloppyDrives::tick() below)
unsigned int currentTick[] = {0,0,0,0,0,0,0,0,0,0};
// The period originally set by incoming messages (prior to any modifications from pitch-bending)
unsigned int originalPeriod[] = {0,0,0,0,0,0,0,0,0,0};
void FloppyDrives::setup() {
// Prepare pins (0 and 1 are reserved for Serial communications)
pinMode(2, OUTPUT); // Step control 1
pinMode(3, OUTPUT); // Direction 1
pinMode(4, OUTPUT); // Step control 2
pinMode(5, OUTPUT); // Direction 2
pinMode(6, OUTPUT); // Step control 3
pinMode(7, OUTPUT); // Direction 3
pinMode(8, OUTPUT); // Step control 4
pinMode(9, OUTPUT); // Direction 4
pinMode(10, OUTPUT); // Step control 5
pinMode(11, OUTPUT); // Direction 5
pinMode(12, OUTPUT); // Step control 6
pinMode(13, OUTPUT); // Direction 6
pinMode(14, OUTPUT); // Step control 7
pinMode(15, OUTPUT); // Direction 7
pinMode(16, OUTPUT); // Step control 8
pinMode(17, OUTPUT); // Direction 8
pinMode(18, OUTPUT); // Direction 9
pinMode(19, OUTPUT); // Step control 9
// With all pins setup, let's do a first run reset
resetAll();
delay(500); // Wait a half second for safety
// Setup timer to handle interrupts for floppy driving
Timer1.initialize(TIMER_RESOLUTION); // Set up a timer at the resolution defined in MoppyInstrument.h
Timer1.attachInterrupt(tick); // Attach the tick function
// If MoppyConfig wants a startup sound, play the startupSound on the
// first drive.
if (PLAY_STARTUP_SOUND) {
startupSound(FIRST_DRIVE);
delay(500);
resetAll();
}
}
// Play startup sound to confirm drive functionality
void FloppyDrives::startupSound(byte driveNum) {
unsigned int chargeNotes[] = {
noteDoubleTicks[31],
noteDoubleTicks[36],
noteDoubleTicks[38],
noteDoubleTicks[43],
0
};
byte i = 0;
unsigned long lastRun = 0;
while(i < 5) {
if (millis() - 200 > lastRun) {
lastRun = millis();
currentPeriod[driveNum] = chargeNotes[i++];
}
}
}
//
//// Message Handlers
//
// Handles system messages (e.g. sequence start and stop)
void FloppyDrives::systemMessage(uint8_t command, uint8_t payload[]) {
switch(command) {
// NETBYTE_SYS_PING is handled by the network adapter directly
case NETBYTE_SYS_RESET: // System reset
resetAll();
break;
case NETBYTE_SYS_START: // Sequence start
// Nothing to do here yet
break;
case NETBYTE_SYS_STOP: // Sequence stop
haltAllDrives();
break;
}
}
// Handles device-specific messages (e.g. playing notes)
void FloppyDrives::deviceMessage(uint8_t subAddress, uint8_t command, uint8_t payload[]) {
switch(command) {
case NETBYTE_DEV_RESET: // Reset
if (subAddress == 0x00) {
resetAll();
} else {
reset(subAddress);
}
break;
case NETBYTE_DEV_NOTEON: // Note On
// Set the current period to the new value to play it immediately
// Also set the originalPeriod in-case we pitch-bend
currentPeriod[subAddress] = originalPeriod[subAddress] = noteDoubleTicks[payload[0]];
break;
case NETBYTE_DEV_NOTEOFF: // Note Off
currentPeriod[subAddress] = originalPeriod[subAddress] = 0;
break;
case NETBYTE_DEV_BENDPITCH: //Pitch bend
// A value from -8192 to 8191 representing the pitch deflection
int16_t bendDeflection = payload[0] << 8 | payload[1];
// A whole octave of bend would double the frequency (halve the the period) of notes
// Calculate bend based on BEND_OCTAVES from MoppyInstrument.h and percentage of deflection
//currentPeriod[subAddress] = originalPeriod[subAddress] / 1.4;
currentPeriod[subAddress] = originalPeriod[subAddress] / pow(2.0, BEND_OCTAVES*(bendDeflection/(float)8192));
break;
}
}
//
//// Floppy driving functions
//
/*
Called by the timer interrupt at the specified resolution. Because this is called extremely often,
it's crucial that any computations here be kept to a minimum!
*/
void FloppyDrives::tick()
{
/*
If there is a period set for control pin 2, count the number of
ticks that pass, and toggle the pin if the current period is reached.
*/
if (currentPeriod[1]>0){
currentTick[1]++;
if (currentTick[1] >= currentPeriod[1]){
togglePin(1,2,3); // Drive 1 is on pins 2 and 3
currentTick[1]=0;
}
}
if (currentPeriod[2]>0){
currentTick[2]++;
if (currentTick[2] >= currentPeriod[2]){
togglePin(2,4,5);
currentTick[2]=0;
}
}
if (currentPeriod[3]>0){
currentTick[3]++;
if (currentTick[3] >= currentPeriod[3]){
togglePin(3,6,7);
currentTick[3]=0;
}
}
if (currentPeriod[4]>0){
currentTick[4]++;
if (currentTick[4] >= currentPeriod[4]){
togglePin(4,8,9);
currentTick[4]=0;
}
}
if (currentPeriod[5]>0){
currentTick[5]++;
if (currentTick[5] >= currentPeriod[5]){
togglePin(5,10,11);
currentTick[5]=0;
}
}
if (currentPeriod[6]>0){
currentTick[6]++;
if (currentTick[6] >= currentPeriod[6]){
togglePin(6,12,13);
currentTick[6]=0;
}
}
if (currentPeriod[7]>0){
currentTick[7]++;
if (currentTick[7] >= currentPeriod[7]){
togglePin(7,14,15);
currentTick[7]=0;
}
}
if (currentPeriod[8]>0){
currentTick[8]++;
if (currentTick[8] >= currentPeriod[8]){
togglePin(8,16,17);
currentTick[8]=0;
}
}
if (currentPeriod[9]>0){
currentTick[9]++;
if (currentTick[9] >= currentPeriod[9]){
togglePin(9,18,19);
currentTick[9]=0;
}
}
}
void FloppyDrives::togglePin(byte driveNum, byte pin, byte direction_pin) {
//Switch directions if end has been reached
if (currentPosition[driveNum] >= MAX_POSITION[driveNum]) {
currentState[direction_pin] = HIGH;
digitalWrite(direction_pin,HIGH);
}
else if (currentPosition[driveNum] <= 0) {
currentState[direction_pin] = LOW;
digitalWrite(direction_pin,LOW);
}
//Update currentPosition
if (currentState[direction_pin] == HIGH){
currentPosition[driveNum]--;
}
else {
currentPosition[driveNum]++;
}
//Pulse the control pin
digitalWrite(pin,currentState[pin]);
currentState[pin] = ~currentState[pin];
}
//
//// UTILITY FUNCTIONS
//
//Not used now, but good for debugging...
void FloppyDrives::blinkLED(){
digitalWrite(13, HIGH); // set the LED on
delay(250); // wait for a second
digitalWrite(13, LOW);
}
// Immediately stops all drives
void FloppyDrives::haltAllDrives() {
for (byte d=FIRST_DRIVE;d<=LAST_DRIVE;d++) {
currentPeriod[d] = 0;
}
}
//For a given floppy number, runs the read-head all the way back to 0
void FloppyDrives::reset(byte driveNum)
{
currentPeriod[driveNum] = 0; // Stop note
byte stepPin = driveNum * 2;
digitalWrite(stepPin+1,HIGH); // Go in reverse
for (unsigned int s=0;s<MAX_POSITION[driveNum];s+=2){ //Half max because we're stepping directly (no toggle)
digitalWrite(stepPin,HIGH);
digitalWrite(stepPin,LOW);
delay(5);
}
currentPosition[driveNum] = 0; // We're reset.
currentState[stepPin] = LOW;
digitalWrite(stepPin+1,LOW);
currentState[stepPin+1] = LOW; // Ready to go forward.
}
// Resets all the drives simultaneously
void FloppyDrives::resetAll()
{
// Stop all drives and set to reverse
for (byte d=FIRST_DRIVE;d<=LAST_DRIVE;d++) {
byte stepPin = d * 2;
currentPeriod[d] = 0;
digitalWrite(stepPin+1,HIGH);
}
// Reset all drives together
for (unsigned int s=0;s<MAX_POSITION[0];s+=2){ //Half max because we're stepping directly (no toggle); grab max from index 0
for (byte d=FIRST_DRIVE;d<=LAST_DRIVE;d++) {
byte stepPin = d * 2;
digitalWrite(stepPin,HIGH);
digitalWrite(stepPin,LOW);
}
delay(5);
}
// Return tracking to ready state
for (byte d=FIRST_DRIVE;d<=LAST_DRIVE;d++) {
byte stepPin = d * 2;
currentPosition[d] = 0; // We're reset.
currentState[stepPin] = LOW;
digitalWrite(stepPin+1,LOW);
currentState[stepPin+1] = LOW; // Ready to go forward.
}
}