flux.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # greaseweazle/flux.py
  2. #
  3. # Written & released by Keir Fraser <keir.xen@gmail.com>
  4. #
  5. # This is free and unencumbered software released into the public domain.
  6. # See the file COPYING for more details, or visit <http://unlicense.org>.
  7. from greaseweazle import error
  8. class Flux:
  9. def __init__(self, index_list, flux_list, sample_freq):
  10. self.index_list = index_list
  11. self.list = flux_list
  12. self.sample_freq = sample_freq
  13. self.splice = 0
  14. def __str__(self):
  15. s = "\nFlux: %.2f MHz" % (self.sample_freq*1e-6)
  16. s += ("\n Total: %u samples, %.2fms\n"
  17. % (len(self.list), sum(self.list)*1000/self.sample_freq))
  18. rev = 0
  19. for t in self.index_list:
  20. s += " Revolution %u: %.2fms\n" % (rev, t*1000/self.sample_freq)
  21. rev += 1
  22. return s[:-1]
  23. def flux_for_writeout(self):
  24. error.check(self.splice == 0,
  25. "Cannot write non-index-aligned raw flux")
  26. # Copy the first revolution only to a fresh flux list.
  27. flux_list = []
  28. to_index = remain = self.index_list[0]
  29. for f in self.list:
  30. if f > remain:
  31. break
  32. flux_list.append(f)
  33. remain -= f
  34. # Extend with "safe" 4us sample values, to avoid unformatted area
  35. # at end of track if drive motor is a little slow.
  36. four_us = max(self.sample_freq * 4e-6, 1)
  37. if remain > four_us:
  38. flux_list.append(remain)
  39. for i in range(round(to_index/(10*four_us))):
  40. flux_list.append(four_us)
  41. return WriteoutFlux(to_index, flux_list, self.sample_freq,
  42. terminate_at_index = True)
  43. def flux(self):
  44. return self
  45. def scale(self, factor):
  46. """Scale up all flux and index timings by specified factor."""
  47. self.sample_freq /= factor
  48. @property
  49. def mean_index_time(self):
  50. """Mean time between index pulses, in seconds (float)"""
  51. return sum(self.index_list) / (len(self.index_list) * self.sample_freq)
  52. class WriteoutFlux(Flux):
  53. def __init__(self, ticks_to_index, flux_list, sample_freq,
  54. terminate_at_index):
  55. super().__init__([ticks_to_index], flux_list, sample_freq)
  56. self.terminate_at_index = terminate_at_index
  57. def __str__(self):
  58. s = ("\nWriteoutFlux: %.2f MHz, %.2fms to index, %s\n"
  59. " Total: %u samples, %.2fms"
  60. % (self.sample_freq*1e-6,
  61. self.index_list[0]*1000/self.sample_freq,
  62. ("Write all", "Terminate at index")[self.terminate_at_index],
  63. len(self.list), sum(self.list)*1000/self.sample_freq))
  64. return s
  65. def flux_for_writeout(self):
  66. return self
  67. # Local variables:
  68. # python-indent: 4
  69. # End: