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.

utils.py 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import glob
  2. import os
  3. import pygame
  4. def draw_header(o):
  5. o.draw.rectangle((0, 0, o.width, o.height), outline=0, fill=o.blue)
  6. o.center_text(o.header_text, o.h1, o.width, 25, o.light_grey)
  7. o.center_text("----------------------", o.h2, o.width, 55, o.light_grey)
  8. def play_seq(o, message):
  9. _id = 0
  10. o.clear_notes_on()
  11. for s in o.soundSlots:
  12. if s.notes[message[0]][message[1]][0] == 1:
  13. if o.erase:
  14. if _id in o.erase_buf:
  15. s.notes[message[0]][message[1]][0] = 0
  16. else:
  17. o.notes_on[_id] = 1
  18. o.soundSlots[_id].play(s.notes[message[0]][message[1]][1])
  19. elif o.mute:
  20. if _id in o.mute_buf:
  21. pass
  22. else:
  23. o.notes_on[_id] = 1
  24. o.soundSlots[_id].play(s.notes[message[0]][message[1]][1])
  25. else:
  26. o.notes_on[_id] = 1
  27. o.soundSlots[_id].play(s.notes[message[0]][message[1]][1])
  28. _id += 1
  29. def erase_note(o, message, pressed):
  30. _id = 0
  31. for s in o.soundSlots:
  32. if s.notes[message[0]][message[1]][0] == 1:
  33. o.notes_on[_id] = 1
  34. o.soundSlots[_id].play(s.notes[message[0]][message[1]][1])
  35. _id += 1
  36. def text_box1(o, a, b):
  37. w1 = 0
  38. w2 = (o.width / 2) - 50
  39. h1 = 45
  40. h2 = h1 + 10
  41. o.center_block(a, o.h2, [w1,h1,w2,h2], o.light_grey)
  42. o.center_block(b, o.h2, [w1,h1+10,w2,h2+25], o.light_grey)
  43. def text_box2(o, a, b):
  44. w1 = 0
  45. w2 = (o.width / 2) - 50
  46. h1 = 90
  47. h2 = h1 + 10
  48. o.center_block(a, o.h2, [w1,h1,w2,h2], o.light_grey)
  49. o.center_block(b, o.h2, [w1,h1+10,w2,h2+25], o.light_grey)
  50. def text_box3(o, a, b):
  51. w1 = (o.width / 2) + 40
  52. w2 = o.width
  53. h1 = 45
  54. h2 = h1 + 10
  55. o.center_block(a, o.h2, [w1,h1,w2,h2], o.light_grey)
  56. o.center_block(b, o.h2, [w1,h1+10,w2,h2+25], o.light_grey)
  57. def text_box4(o, a, b):
  58. w1 = (o.width / 2) + 40
  59. w2 = o.width
  60. h1 = 90
  61. h2 = h1 + 10
  62. o.center_block(a, o.h2, [w1,h1,w2,h2], o.light_grey)
  63. o.center_block(b, o.h2, [w1,h1+10,w2,h2+25], o.light_grey)
  64. def text_center(o, a, b):
  65. w1 = 0
  66. w2 = o.width
  67. h1 = 45
  68. h2 = h1 + 10
  69. o.draw.rectangle((0, 0, o.width, o.height), outline=0, fill=o.blue)
  70. o.center_block(a, o.h1, [w1,h1,w2,h2], o.light_grey)
  71. o.center_block(b, o.h1, [w1,h1+20,w2,h2+45], o.light_grey)
  72. def breakup_song_text(song):
  73. line1 = ""
  74. line2 = ""
  75. line3 = ""
  76. line4 = ""
  77. if len(song) < 9:
  78. for x in song:
  79. line1 += str(x) + '.'
  80. elif len(song) < 17:
  81. _iter = 0
  82. for x in song:
  83. if _iter < 8:
  84. line1 += str(x) + '.'
  85. else:
  86. line2 += str(x) + '.'
  87. _iter += 1
  88. elif len(song) < 25:
  89. _iter = 0
  90. for x in song:
  91. if _iter < 8:
  92. line1 += str(x) + '.'
  93. elif _iter < 16:
  94. line2 += str(x) + '.'
  95. else:
  96. line3 += str(x) + '.'
  97. _iter += 1
  98. elif len(song) < 33:
  99. _iter = 0
  100. for x in song:
  101. if _iter < 8:
  102. line1 += str(x) + '.'
  103. elif _iter < 16:
  104. line2 += str(x) + '.'
  105. elif _iter < 24:
  106. line3 += str(x) + '.'
  107. else:
  108. line4 += str(x) + '.'
  109. _iter += 1
  110. else:
  111. _iter = 0
  112. cur_id = len(song) - 1
  113. _iter = 0
  114. ns = song.copy()
  115. while len(ns) > 31:
  116. del ns[0]
  117. for x in ns:
  118. if _iter < 7:
  119. line1 += str(x) + '.'
  120. elif _iter < 15:
  121. line2 += str(x) + '.'
  122. elif _iter < 23:
  123. line3 += str(x) + '.'
  124. else:
  125. line4 += str(x) + '.'
  126. _iter += 1
  127. line1 = ".." + line1
  128. return [line1, line2, line3, line4]
  129. def get_folders(path):
  130. globbed = glob.glob(path + "/*/")
  131. globbed.sort()
  132. return globbed
  133. def get_files(path, ext):
  134. path += '/'
  135. out = []
  136. files = os.listdir(path)
  137. files.sort()
  138. for file in files:
  139. if os.path.isdir(path + file):
  140. out.append(file)
  141. else:
  142. pass
  143. for file in files:
  144. if file.lower().endswith(ext):
  145. if os.path.isdir(path + file):
  146. pass
  147. else:
  148. out.append(file)
  149. else:
  150. pass
  151. out2 = []
  152. for f in out:
  153. if f[0] == '.':
  154. pass
  155. else:
  156. out2.append(f)
  157. return out2
  158. def scroll_ud(o, self):
  159. since_up = 0
  160. if o.keyState[0] == 2:
  161. if o.press_ticks_up == None:
  162. o.press_ticks_up = pygame.time.get_ticks()
  163. since_up = pygame.time.get_ticks() - o.press_ticks_up
  164. else:
  165. o.press_ticks_up = None
  166. o.press_ticks_up = pygame.time.get_ticks()
  167. if o.keyState[0] == 1 or since_up > 250:
  168. if self.cur_el > 0:
  169. self.cur_el -= 1
  170. self.draw_main(o)
  171. o.update_display(0)
  172. o.press_ticks_up = pygame.time.get_ticks()
  173. since_down = 0
  174. if o.keyState[1] == 2:
  175. if o.press_ticks_down == None:
  176. o.press_ticks_down = pygame.time.get_ticks()
  177. since_down = pygame.time.get_ticks() - o.press_ticks_down
  178. else:
  179. o.press_ticks_down = None
  180. o.press_ticks_down = pygame.time.get_ticks()
  181. if o.keyState[1] == 1 or since_down > 250:
  182. if self.cur_el < (len(self.menu) - 1):
  183. self.cur_el += 1
  184. self.draw_main(o)
  185. o.update_display(0)
  186. o.press_ticks_down = pygame.time.get_ticks()