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.1KB

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