KLL Compiler
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

schedule.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #!/usr/bin/env python3
  2. '''
  3. KLL Schedule Containers
  4. '''
  5. # Copyright (C) 2016 by Jacob Alexander
  6. #
  7. # This file is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This file is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this file. If not, see <http://www.gnu.org/licenses/>.
  19. ### Imports ###
  20. ### Decorators ###
  21. ## Print Decorator Variables
  22. ERROR = '\033[5;1;31mERROR\033[0m:'
  23. WARNING = '\033[5;1;33mWARNING\033[0m:'
  24. ### Classes ###
  25. class Time:
  26. '''
  27. Time parameter
  28. '''
  29. def __init__( self, time, unit ):
  30. self.time = time
  31. self.unit = unit
  32. def __repr__( self ):
  33. return "{0}{1}".format( self.time, self.unit )
  34. class Schedule:
  35. '''
  36. Identifier schedule
  37. Each schedule may have multiple parameters configuring how the element is scheduled
  38. Used for trigger and result elements
  39. '''
  40. def __init__( self ):
  41. self.parameters = None
  42. def setSchedule( self, parameters ):
  43. '''
  44. Applies given list of Schedule Parameters to Schedule
  45. None signifies an undefined schedule which allows free-form scheduling
  46. at either a later stage or at the convenience of the device firmware/driver
  47. If schedule is already set, do not overwrite, expressions are read inside->out
  48. '''
  49. # Ignore if already set
  50. if self.parameters is not None:
  51. return
  52. self.parameters = parameters
  53. def strSchedule( self ):
  54. '''
  55. __repr__ of Schedule when multiple inheritance is used
  56. '''
  57. output = ""
  58. if self.parameters is not None:
  59. for index, param in enumerate( self.parameters ):
  60. if index > 0:
  61. output += ","
  62. output += "{0}".format( param )
  63. return output
  64. def __repr__( self ):
  65. return self.strSchedule()
  66. class ScheduleParam:
  67. '''
  68. Schedule parameter
  69. In the case of a Timing parameter, the base type is unknown and must be inferred later
  70. '''
  71. def __init__( self, state, timing=None ):
  72. self.state = state
  73. self.timing = timing
  74. # Mutate class into the desired type
  75. if self.state in ['P', 'H', 'R', 'O', 'UP', 'UR']:
  76. self.__class__ = ButtonScheduleParam
  77. elif self.state in ['A', 'On', 'D', 'Off']:
  78. self.__class__ = IndicatorScheduleParam
  79. elif self.state is None and self.timing is not None:
  80. pass
  81. else:
  82. print( "{0} Invalid ScheduleParam state '{1}'".format( ERROR, self.state ) )
  83. def setTiming( self, timing ):
  84. '''
  85. Set parameter timing
  86. '''
  87. self.timing = timing
  88. def __repr__( self ):
  89. output = ""
  90. if self.state is None and self.timing is not None:
  91. output += "{0}".format( self.timing )
  92. else:
  93. output += "??"
  94. print( "{0} Unknown ScheduleParam state '{1}'".format( ERROR, self.state ) )
  95. return output
  96. class ButtonScheduleParam( ScheduleParam ):
  97. '''
  98. Button Schedule Parameter
  99. Accepts:
  100. P - Press
  101. H - Hold
  102. R - Release
  103. O - Off
  104. UP - Unique Press
  105. UR - Unique Release
  106. Timing specifiers are valid.
  107. Validity of specifiers are context dependent, and may error at a later stage, or be stripped altogether
  108. '''
  109. def __repr__( self ):
  110. output = "{0}".format( self.state )
  111. if self.timing is not None:
  112. output += ":{0}".format( self.timing )
  113. return output
  114. class AnalogScheduleParam( ScheduleParam ):
  115. '''
  116. Analog Schedule Parameter
  117. Accepts:
  118. Value from 0 to 100, indicating a percentage pressed
  119. XXX: Might be useful to accept decimal percentages
  120. '''
  121. def __init__( self, state ):
  122. self.state = state
  123. def __repr__( self ):
  124. return "Analog({0})".format( self.state )
  125. class IndicatorScheduleParam( ScheduleParam ):
  126. '''
  127. Indicator Schedule Parameter
  128. Accepts:
  129. A - Activate
  130. On
  131. D - Deactivate
  132. Off
  133. Timing specifiers are valid.
  134. Validity of specifiers are context dependent, and may error at a later stage, or be stripped altogether
  135. '''
  136. def __repr__( self ):
  137. output = "{0}".format( self.state )
  138. if self.timing is not None:
  139. output += ":{0}".format( self.timing )
  140. return output