base.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import time
  2. from alphasign import constants
  3. from alphasign import packet
  4. import alphasign.string
  5. import alphasign.text
  6. class BaseInterface(object):
  7. """Base interface from which all other interfaces inherit.
  8. This class contains utility methods for fundamental sign features.
  9. """
  10. def write(self, data):
  11. return False
  12. def clear_memory(self):
  13. """Clear the sign's memory.
  14. :rtype: None
  15. """
  16. pkt = packet.Packet("%s%s" % (constants.WRITE_SPECIAL, "$"))
  17. self.write(pkt)
  18. time.sleep(1)
  19. def beep(self, frequency=0, duration=0.1, repeat=0):
  20. """Make the sign beep.
  21. :param frequency: frequency integer (not in Hz), 0 - 254
  22. :param duration: beep duration, 0.1 - 1.5
  23. :param repeat: number of times to repeat, 0 - 15
  24. :rtype: None
  25. """
  26. if frequency < 0:
  27. frequency = 0
  28. elif frequency > 254:
  29. frequency = 254
  30. duration = int(duration / 0.1)
  31. if duration < 1:
  32. duration = 1
  33. elif duration > 15:
  34. duration = 15
  35. if repeat < 0:
  36. repeat = 0
  37. elif repeat > 15:
  38. repeat = 15
  39. pkt = packet.Packet("%s%s%02X%X%X" % (constants.WRITE_SPECIAL, "(2",
  40. frequency, duration, repeat))
  41. self.write(pkt)
  42. def soft_reset(self):
  43. """Perform a soft reset on the sign.
  44. This is non-destructive and does not clear the sign's memory.
  45. :rtype: None
  46. """
  47. pkt = packet.Packet("%s%s" % (constants.WRITE_SPECIAL, ","))
  48. self.write(pkt)
  49. def allocate(self, files):
  50. """Allocate a set of files on the device.
  51. :param files: list of file objects (:class:`alphasign.text.Text`,
  52. :class:`alphasign.string.String`, ...)
  53. :rtype: None
  54. """
  55. seq = ""
  56. for obj in files:
  57. size_hex = "%04x" % obj.size
  58. # format: FTPSIZEQQQQ
  59. if type(obj) == alphasign.string.String:
  60. file_type = "B"
  61. qqqq = "0000" # unused for strings
  62. lock = constants.LOCKED
  63. else: # if type(obj) == alphasign.text.Text:
  64. file_type = "A"
  65. qqqq = "FFFF" # TODO(ms): start/end times
  66. lock = constants.UNLOCKED
  67. alloc_str = ("%s%s%s%s%s" %
  68. (obj.label, # file label to allocate
  69. file_type, # file type
  70. lock,
  71. size_hex, # size in hex
  72. qqqq))
  73. seq += alloc_str
  74. # allocate special TARGET TEXT files 1 through 5
  75. for i in range(5):
  76. alloc_str = ("%s%s%s%s%s" %
  77. ("%d" % (i + 1),
  78. "A", # file type
  79. constants.UNLOCKED,
  80. "%04x" % 100,
  81. "FEFE"))
  82. seq += alloc_str
  83. pkt = packet.Packet("%s%s%s" % (constants.WRITE_SPECIAL, "$", seq))
  84. self.write(pkt)
  85. def set_run_sequence(self, files, locked=False):
  86. """Set the run sequence on the device.
  87. This determines the order in which the files are displayed on the device, if
  88. at all. This is useful when handling multiple TEXT files.
  89. :param files: list of file objects (:class:`alphasign.text.Text`,
  90. :class:`alphasign.string.String`, ...)
  91. :param locked: allow sequence to be changed with IR keyboard
  92. :rtype: None
  93. """
  94. seq_str = ".T"
  95. seq_str += locked and "L" or "U"
  96. for obj in files:
  97. seq_str += obj.label
  98. pkt = packet.Packet("%s%s" % (constants.WRITE_SPECIAL, seq_str))
  99. self.write(pkt)