@@ -19,6 +19,7 @@ static const int pulseDelayTime = 6;
1919#define TRK0_PIN A4
2020
2121#define MAX_FLUX_PULSE_PER_TRACK (500000 / 5 ) // 500khz / 5 hz per track rotation
22+ #define MAX_TRACKS 80
2223#define STEP_OUT HIGH
2324#define STEP_IN LOW
2425
@@ -81,14 +82,13 @@ void setup() {
8182 // Select drive
8283 digitalWrite (SELECT_PIN, LOW);
8384 delay (1000 );
84- /*
85+
8586 Serial.print (" Seeking track 00..." );
8687 if (! goto_track (0 )) {
87- Serial.println("Failed to get to track 0 ");
88- while (1);
88+ Serial.println (" Failed to seek to track" );
89+ while (1 ) yield () ;
8990 }
9091 Serial.println (" done!" );
91- */
9292}
9393uint32_t time_stamp = 0 ;
9494
@@ -107,6 +107,51 @@ void wait_for_index_pulse_low(void) {
107107 }
108108}
109109
110+ void print_pulses (uint8_t *pulses, uint32_t num_pulses) {
111+ for (uint32_t i=0 ; i<num_pulses; i++) {
112+ Serial.print (pulses[i]);
113+ Serial.print (" , " );
114+ }
115+ Serial.println ();
116+ }
117+
118+ void print_pulse_bins (uint8_t *pulses, uint32_t num_pulses) {
119+ // lets bin em!
120+ uint8_t bins[32 ][2 ];
121+ memset (bins, 0 , 32 *2 );
122+
123+ // we'll add each pulse to a bin so we can figure out the 3 buckets
124+ for (uint32_t i=0 ; i<num_pulses; i++) {
125+ uint8_t p = pulses[i];
126+ // find a bin for this pulse
127+ uint8_t bin = 0 ;
128+ for (bin=0 ; bin<32 ; bin++) {
129+ // bin already exists? increment the count!
130+ if (bins[bin][0 ] == p) {
131+ bins[bin][1 ] ++;
132+ break ;
133+ }
134+ if (bins[bin][0 ] == 0 ) {
135+ // ok we never found the bin, so lets make it this one!
136+ bins[bin][0 ] = p;
137+ bins[bin][1 ] = 1 ;
138+ break ;
139+ }
140+ }
141+ if (bin == 32 ) Serial.println (" oof we ran out of bins but we'll keep going" );
142+ }
143+ // this is a very lazy way to print the bins sorted
144+ for (uint8_t pulse_w=1 ; pulse_w<255 ; pulse_w++) {
145+ for (uint8_t b=0 ; b<32 ; b++) {
146+ if (bins[b][0 ] == pulse_w) {
147+ Serial.print (bins[b][0 ]);
148+ Serial.print (" : " );
149+ Serial.println (bins[b][1 ]);
150+ }
151+ }
152+ }
153+ }
154+
110155void capture_track (void ) {
111156 uint8_t pulse_count;
112157 uint8_t pulses[MAX_FLUX_PULSE_PER_TRACK];
@@ -148,11 +193,8 @@ void capture_track(void) {
148193 Serial.print (num_pulses);
149194 Serial.println (" flux transitions" );
150195
151- for (uint32_t i=0 ; i<num_pulses; i++) {
152- Serial.print (pulses[i]);
153- Serial.print (" , " );
154- }
155- Serial.println ();
196+ // print_pulses(pulses, num_pulses);
197+ // print_pulse_bins(pulses, num_pulses);
156198}
157199
158200void loop () {
@@ -167,37 +209,35 @@ void loop() {
167209 Serial.println (digitalRead (TRK0_PIN) ? " No" : " Yes" );
168210 time_stamp = millis ();
169211 }
170-
171212}
172213
173214
174215
175-
176-
177-
178-
179-
180216void step (bool dir, uint8_t times) {
181217 digitalWrite (DIR_PIN, dir);
182218 delayMicroseconds (10 ); // 1 microsecond, but we're generous
183219
184220 while (times--) {
185221 digitalWrite (STEP_PIN, HIGH);
186- delay (2 ); // 3ms min per step
222+ delay (3 ); // 3ms min per step
187223 digitalWrite (STEP_PIN, LOW);
188- delay (2 ); // 3ms min per step
224+ delay (3 ); // 3ms min per step
189225 digitalWrite (STEP_PIN, HIGH); // end high
190226 }
191227}
192228
193229bool goto_track (uint8_t track_num) {
230+ // track 0 is a very special case because its the only one we actually know we got to
194231 if (track_num == 0 ) {
195- uint8_t max_steps = 30 ;
196- while (max_steps-- && digitalRead (TRK0_PIN)) {
232+ uint8_t max_steps = MAX_TRACKS;
233+ while (max_steps--) {
234+ if (!digitalRead (TRK0_PIN))
235+ return true ;
197236 step (STEP_OUT, 1 );
198237 }
199- return (max_steps == 0 );
238+ return false ; // we 'timed' out!
200239 }
201240 if (!goto_track (0 )) return false ;
202- step (STEP_IN, max (track_num, 159 ));
241+ step (STEP_IN, max (track_num, MAX_TRACKS-1 ));
242+ return true ;
203243}
0 commit comments