text.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import constants
  2. from packet import Packet
  3. class Text(object):
  4. def __init__(self, msg=None, label=None, position=None, mode=None):
  5. if label is None:
  6. label = "A"
  7. self.label = label
  8. self.msg = msg
  9. # TODO(ms): need support for position and mode
  10. def __str__(self):
  11. # [WRITE_TEXT][File Label][ESC][Display Position][Mode Code]
  12. # [Special Specifier][ASCII Message]
  13. packet = Packet("%s%s%s%s%s%s" % (constants.WRITE_TEXT, self.label,
  14. constants.ESC,
  15. constants.positions["middle_line"],
  16. constants.modes["rotate"],
  17. self.msg))
  18. return str(packet)
  19. def __repr__(self):
  20. return repr(self.__str__())
  21. def color(color="autocolor"):
  22. """Returns color code for a specified color.
  23. Args:
  24. color: color string
  25. Returns:
  26. FIXME
  27. """
  28. if color not in constants.colors:
  29. color = "autocolor"
  30. return "%s%s" % ("\x1C", constants.colors[color])
  31. def charset(charset="five_high_std"):
  32. """Returns control code for a specified character set.
  33. Args:
  34. charset: charset name string
  35. Returns:
  36. FIXME
  37. """
  38. if charset not in constants.charsets:
  39. charset = "five_high_std"
  40. return "%s%s" % ("\x1A", constants.charsets[charset])
  41. def extchar(extchar="left_arrow"):
  42. """Returns control code for a specified extended character.
  43. Args:
  44. extchar: extended character name
  45. Returns:
  46. FIXME
  47. """
  48. if extchar not in constants.extchars:
  49. extchar = "left_arrow"
  50. return "%s%s" % ("\x08", constants.extchars[extchar])
  51. def spacing(option=0):
  52. """Returns control code to set the character spacing.
  53. Args:
  54. option: 0 - set proportional characters
  55. 1 - fixed width left justified characters
  56. Returns:
  57. FIXME
  58. """
  59. byte = (option == 0) and "0" or "1"
  60. return "\x1E%s" % byte
  61. def speed(speed):
  62. """Set the speed of the scrolling text.
  63. Args:
  64. speed: integer 1 (slowest) through 5 (fastest) inclusive
  65. Returns:
  66. FIXME
  67. """
  68. if speed < 1:
  69. speed = 1
  70. elif speed > 5:
  71. speed = 5
  72. n = 20 + speed
  73. return chr(n)