raspberry pi zero based drum machine
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

sEnterText.py 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import FSM
  2. import utils as u
  3. import menus as m
  4. import pygame
  5. from itertools import cycle
  6. from datetime import datetime
  7. from datetime import date
  8. #====================================
  9. State = type("State", (object,), {})
  10. #====================================
  11. class State(object):
  12. def __init__(self, FSM):
  13. self.FSM = FSM
  14. def Enter(self):
  15. pass
  16. def Execute(self):
  17. pass
  18. def Exit(self):
  19. pass
  20. class EnterText(State):
  21. def __init__(self,FSM):
  22. super(EnterText, self).__init__(FSM)
  23. def Enter(self):
  24. o = self.FSM.owner
  25. m.menu1_actions(self, o)
  26. if o.keyState[16] == 1:
  27. m.draw_menu1(o)
  28. o.update_display(0)
  29. elif o.keyState[16] == 4:
  30. #u.draw_header(o)
  31. #self.draw_footer(o)
  32. self.draw(o)
  33. o.update_display(0)
  34. self.input_string = ""
  35. if o.sconf['title'] == 'blank':
  36. self.input_string += str(datetime.today().strftime('%Y%m%d'))
  37. else:
  38. self.input_string += o.sconf['title']
  39. #if
  40. o.header_text = "EnterText"
  41. o.pub.register("beat", self)
  42. self.last_action = pygame.time.get_ticks()
  43. self.caps = False
  44. self.cur_but = 18
  45. self.cur_letter = ""
  46. if o.keyState[16] > 0 or o.keyState[17] > 0:
  47. pass
  48. else:
  49. pass
  50. self.skips = [7, 3, 11, 12, 13, 14, 15]
  51. self.lc_labels = [["p", "q", "r", "s"], ["t", "u", "v"], ["w", "x", "y", "z"], ["C", "A", "P", "S"],
  52. ["g", "h", "i"], ["j", "k", "l"], ["m", "n", "o"], ["D", "E", "L"],
  53. ["_", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-"], ["a", "b", "c"], ["d", "e", "f"], ["O", "K"],
  54. [], [], [], []]
  55. self.uc_labels = [["P", "Q", "R", "S"], ["T", "U", "V"], ["W", "X", "Y", "Z"], ["C", "A", "P", "S"],
  56. ["G", "H", "I"], ["J", "K", "L"], ["M", "N", "O"], ["D", "E", "L"],
  57. ["_", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-"], ["A", "B", "C"], ["D", "E", "F"], ["O", "K"],
  58. [], [], [], []]
  59. self.labels = self.lc_labels
  60. #self.songCycle = cycle(self.song)
  61. #self.curPattern = next(self.songCycle)
  62. self.cycles = []
  63. for l in self.labels:
  64. self.cycles.append(cycle(l))
  65. self.draw(o)
  66. o.update_display(0)
  67. super(EnterText, self).Enter()
  68. def Execute(self):
  69. o = self.FSM.owner
  70. y_size = o.height / 4
  71. if o.keyState[11] == 1:
  72. if self.input_string != 'blank':
  73. o.title = self.input_string
  74. #o.bpm = o.sconf['bpm']
  75. o.sconf['title'] = self.input_string
  76. print('fsm saving')
  77. o.save_song()
  78. self.FSM.ToTransition('toFile')
  79. m.menu1_actions(self, o)
  80. m.menu2_actions_saveas(self, o)
  81. if o.keyState[16] == 1:
  82. m.draw_menu1(o)
  83. o.update_display(0)
  84. elif o.keyState[16] == 4:
  85. u.draw_header(o)
  86. self.draw(o)
  87. #self.draw_main(o)
  88. o.update_display(0)
  89. if o.keyState[17] == 1:
  90. m.draw_menu_saveas(o)
  91. o.update_display(0)
  92. elif o.keyState[17] == 4:
  93. u.draw_header(o)
  94. self.draw(o)
  95. #self.draw_main(o)
  96. o.update_display(0)
  97. if o.keyState[0] == 1 or o.keyState[0] == 3 or o.keyState[1] == 1 or o.keyState[1] == 3 or o.keyState[2] == 1 or o.keyState[2] == 3 or o.keyState[3] == 1 or o.keyState[3] == 3:
  98. u.draw_header(o)
  99. self.draw(o)
  100. #self.draw_main(o)
  101. o.update_display(0)
  102. if o.keyState[16] > 0 or o.keyState[17] > 0:
  103. pass
  104. else:
  105. _iter = 0
  106. for k in o.keyState:
  107. if k == 1:
  108. cur_time = pygame.time.get_ticks()
  109. print(cur_time - self.last_action)
  110. #print('cur letter ', next(self.cycles[_iter]))
  111. if _iter not in self.skips:
  112. self.last_action = cur_time
  113. if _iter < len(self.cycles):
  114. self.cur_letter = next(self.cycles[_iter])
  115. self.cur_but = _iter
  116. self.draw(o)
  117. o.center_block(self.input_string + self.cur_letter, o.h2, [0, 0, o.width, y_size], o.light_grey)
  118. if _iter == 7:
  119. self.input_string = self.input_string[:-1]
  120. #self.input_string = self.input_string.rstrip(self.input_string[-1])
  121. print('deleting')
  122. self.last_action = 100000000
  123. self.draw(o)
  124. o.center_block(self.input_string, o.h2, [0, 0, o.width, y_size], o.light_grey)
  125. elif _iter == 3:
  126. print('caps')
  127. if self.caps:
  128. self.caps = False
  129. self.labels = self.lc_labels
  130. else:
  131. self.caps = True
  132. self.labels = self.uc_labels
  133. self.draw(o)
  134. o.center_block(self.input_string, o.h2, [0, 0, o.width, y_size], o.light_grey)
  135. self.cycles = []
  136. for l in self.labels:
  137. self.cycles.append(cycle(l))
  138. o.update_display(0)
  139. _iter += 1
  140. cur_time = pygame.time.get_ticks()
  141. delay = cur_time - self.last_action
  142. if delay > 1000 and delay < 100000:
  143. self.input_string += self.cur_letter
  144. self.last_action = 100000000
  145. self.reset_cycles()
  146. self.draw(o)
  147. o.center_block(self.input_string, o.h2, [0, 0, o.width, y_size], o.light_grey)
  148. o.update_display(0)
  149. def ReceiveMessage(self, message):
  150. o = self.FSM.owner
  151. u.play_seq(o, message)
  152. def draw(self, o):
  153. x_size = o.width / 4
  154. y_size = o.height / 4
  155. og_x = 0
  156. o_x = og_x
  157. o_y = o.height
  158. text_padding = 6
  159. _id = 0
  160. while _id < 16:
  161. lab = ""
  162. for i in self.labels[_id]:
  163. lab += i
  164. if _id == 8:
  165. lab = "_12"
  166. o.draw.rectangle((o_x, o_y, o_x + x_size, o_y - y_size), outline=0, fill=o.blue)
  167. o.center_block(lab, o.h2, [o_x, o_y, o_x + x_size, o_y - y_size], o.light_grey)
  168. o_x = o_x + x_size
  169. _id += 1
  170. if _id % 4 == 0:
  171. o_y -= y_size
  172. o_x = og_x
  173. o.draw.rectangle((0, 0, o.width, y_size), outline=0, fill=o.blue)
  174. def reset_cycles(self):
  175. self.cycles = []
  176. for l in self.labels:
  177. self.cycles.append(cycle(l))
  178. def Exit(self):
  179. self.FSM.owner.pub.unregister("beat", self)