base.py 2.9 KB

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