flux.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 summary_string(self):
  24. return ("Raw Flux (%u flux in %.2fms)"
  25. % (len(self.list), sum(self.list)*1000/self.sample_freq))
  26. def flux_for_writeout(self):
  27. error.check(self.splice == 0 or len(self.index_list) > 1,
  28. "Cannot write single-revolution unaligned raw flux")
  29. splice_at_index = (self.splice == 0)
  30. # Copy the required amount of flux to a fresh list.
  31. flux_list = []
  32. to_index = self.index_list[0]
  33. remain = to_index + self.splice
  34. for f in self.list:
  35. if f > remain:
  36. break
  37. flux_list.append(f)
  38. remain -= f
  39. if splice_at_index:
  40. # Extend with "safe" 4us sample values, to avoid unformatted area
  41. # at end of track if drive motor is a little slow.
  42. four_us = max(self.sample_freq * 4e-6, 1)
  43. if remain > four_us:
  44. flux_list.append(remain)
  45. for i in range(round(to_index/(10*four_us))):
  46. flux_list.append(four_us)
  47. elif remain > 0:
  48. # End the write exactly where specified.
  49. flux_list.append(remain)
  50. return WriteoutFlux(to_index, flux_list, self.sample_freq,
  51. terminate_at_index = (self.splice == 0))
  52. def flux(self):
  53. return self
  54. def scale(self, factor):
  55. """Scale up all flux and index timings by specified factor."""
  56. self.sample_freq /= factor
  57. @property
  58. def mean_index_time(self):
  59. """Mean time between index pulses, in seconds (float)"""
  60. return sum(self.index_list) / (len(self.index_list) * self.sample_freq)
  61. class WriteoutFlux(Flux):
  62. def __init__(self, ticks_to_index, flux_list, sample_freq,
  63. terminate_at_index):
  64. super().__init__([ticks_to_index], flux_list, sample_freq)
  65. self.terminate_at_index = terminate_at_index
  66. def __str__(self):
  67. s = ("\nWriteoutFlux: %.2f MHz, %.2fms to index, %s\n"
  68. " Total: %u samples, %.2fms"
  69. % (self.sample_freq*1e-6,
  70. self.index_list[0]*1000/self.sample_freq,
  71. ("Write all", "Terminate at index")[self.terminate_at_index],
  72. len(self.list), sum(self.list)*1000/self.sample_freq))
  73. return s
  74. def flux_for_writeout(self):
  75. return self
  76. # Local variables:
  77. # python-indent: 4
  78. # End: