raspberry pi zero based drum machine
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.

sNewDir.py 5.5KB

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