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.

StatesProg.py 61KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249
  1. import random
  2. import time
  3. import pygame
  4. import sys
  5. import pyaudio
  6. import wave
  7. import glob
  8. import os
  9. from itertools import cycle
  10. from datetime import datetime
  11. import ast
  12. import random
  13. from pathlib import Path
  14. #====================================
  15. State = type("State", (object,), {})
  16. #====================================
  17. class State(object):
  18. def __init__(self, FSM):
  19. self.FSM = FSM
  20. self.timer = 0
  21. self.startTime = 0
  22. def Enter(self):
  23. self.timer = 0
  24. self.startTime = 0
  25. def Execute(self):
  26. print('Executing')
  27. def Exit(self):
  28. print('Exiting')
  29. #====================================
  30. def play_seq(o, message):
  31. _id = 0
  32. o.clear_notes_on()
  33. for s in o.soundSlots:
  34. if s.notes[message[0]][message[1]][0] == 1:
  35. o.notes_on[_id] = 1
  36. o.soundSlots[_id].play(s.notes[message[0]][message[1]][1])
  37. _id += 1
  38. #print(o.notes_on)
  39. def draw_header(o):
  40. o.draw.rectangle((0, 0, o.width, o.height), outline=0, fill=o.blue)
  41. o.center_text(o.header_text, o.h1, o.width, 25, o.light_grey)
  42. o.center_text("----------------------", o.h2, o.width, 55, o.light_grey)
  43. def text_box1(o, a, b):
  44. w1 = 0
  45. w2 = (o.width / 2) - 50
  46. h1 = 45
  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_box2(o, a, b):
  51. w1 = 0
  52. w2 = (o.width / 2) - 50
  53. h1 = 90
  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_box3(o, a, b):
  58. w1 = (o.width / 2) + 40
  59. w2 = o.width
  60. h1 = 45
  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_box4(o, a, b):
  65. w1 = (o.width / 2) + 40
  66. w2 = o.width
  67. h1 = 90
  68. h2 = h1 + 10
  69. o.center_block(a, o.h2, [w1,h1,w2,h2], o.light_grey)
  70. o.center_block(b, o.h2, [w1,h1+10,w2,h2+25], o.light_grey)
  71. def text_center(o, a, b):
  72. w1 = 0
  73. w2 = o.width
  74. h1 = 45
  75. h2 = h1 + 10
  76. o.draw.rectangle((0, 0, o.width, o.height), outline=0, fill=o.blue)
  77. o.center_block(a, o.h1, [w1,h1,w2,h2], o.light_grey)
  78. o.center_block(b, o.h1, [w1,h1+20,w2,h2+45], o.light_grey)
  79. def draw_menu1(o):
  80. bpm_inc = 4
  81. x_size = o.width / 4
  82. y_size = o.height / 4
  83. og_x = 0
  84. o_x = og_x
  85. o_y = o.height
  86. text_padding = 6
  87. _id = 0
  88. while _id < 16:
  89. o.draw.rectangle((o_x, o_y, o_x + x_size, o_y - y_size), outline=0, fill=o.blue)
  90. if _id == 0:
  91. o.center_block("Main", o.h2, [o_x, o_y, o_x + x_size, o_y - y_size], o.light_grey)
  92. if _id == 1:
  93. o.center_block("Note", o.h2, [o_x, o_y, o_x + x_size, o_y - y_size], o.light_grey)
  94. if _id == 2:
  95. o.center_block("Patt", o.h2, [o_x, o_y, o_x + x_size, o_y - y_size], o.light_grey)
  96. if _id == 3:
  97. o.center_block("Song", o.h2, [o_x, o_y, o_x + x_size, o_y - y_size], o.light_grey)
  98. if _id == 4:
  99. o.center_block("File", o.h2, [o_x, o_y, o_x + x_size, o_y - y_size], o.light_grey)
  100. if _id == 5:
  101. o.center_block("Util", o.h2, [o_x, o_y, o_x + x_size, o_y - y_size], o.light_grey)
  102. if _id == 6:
  103. o.center_block("Bank-", o.h2, [o_x, o_y, o_x + x_size, o_y - y_size], o.light_grey)
  104. if _id == 7:
  105. o.center_block("Bank+", o.h2, [o_x, o_y, o_x + x_size, o_y - y_size], o.light_grey)
  106. if _id == 8:
  107. o.center_block("Vol-", o.h2, [o_x, o_y, o_x + x_size, o_y - y_size], o.light_grey)
  108. if _id == 9:
  109. o.center_block("Vol+", o.h2, [o_x, o_y, o_x + x_size, o_y - y_size], o.light_grey)
  110. if _id == 11:
  111. o.center_block("", o.h2, [o_x, o_y, o_x + x_size, o_y - y_size], o.light_grey)
  112. if _id == 12:
  113. o.center_block("Play", o.h2, [o_x, o_y, o_x + x_size, o_y - y_size], o.light_grey)
  114. if _id == 13:
  115. o.center_block("Stop", o.h2, [o_x, o_y, o_x + x_size, o_y - y_size], o.light_grey)
  116. if _id == 14:
  117. o.center_block("BPM-", o.h2, [o_x, o_y, o_x + x_size, o_y - y_size], o.light_grey)
  118. if _id == 15:
  119. o.center_block("BPM+", o.h2, [o_x, o_y, o_x + x_size, o_y - y_size], o.light_grey)
  120. o_x = o_x + x_size
  121. _id += 1
  122. if _id % 4 == 0:
  123. o_y -= y_size
  124. o_x = og_x
  125. def draw_menu2_sample(o):
  126. bpm_inc = 4
  127. x_size = o.width / 4
  128. y_size = o.height / 4
  129. og_x = 0
  130. o_x = og_x
  131. o_y = o.height
  132. text_padding = 6
  133. _id = 0
  134. while _id < 16:
  135. o.draw.rectangle((o_x, o_y, o_x + x_size, o_y - y_size), outline=0, fill=o.dark_grey)
  136. if _id == 0:
  137. o.center_block("Copy", o.h2, [o_x, o_y, o_x + x_size, o_y - y_size], o.light_grey)
  138. if _id == 1:
  139. o.center_block("Paste", o.h2, [o_x, o_y, o_x + x_size, o_y - y_size], o.light_grey)
  140. if _id == 2:
  141. o.center_block("Del", o.h2, [o_x, o_y, o_x + x_size, o_y - y_size], o.light_grey)
  142. if _id == 3:
  143. o.center_block("Load", o.h2, [o_x, o_y, o_x + x_size, o_y - y_size], o.light_grey)
  144. if _id == 4:
  145. o.center_block("Vol-", o.h2, [o_x, o_y, o_x + x_size, o_y - y_size], o.light_grey)
  146. if _id == 5:
  147. o.center_block("Vol+", o.h2, [o_x, o_y, o_x + x_size, o_y - y_size], o.light_grey)
  148. if _id == 6:
  149. o.center_block("Pitch-", o.h2, [o_x, o_y, o_x + x_size, o_y - y_size], o.light_grey)
  150. if _id == 7:
  151. o.center_block("Pitch+", o.h2, [o_x, o_y, o_x + x_size, o_y - y_size], o.light_grey)
  152. o_x = o_x + x_size
  153. _id += 1
  154. if _id % 4 == 0:
  155. o_y -= y_size
  156. o_x = og_x
  157. def draw_menu2_pattern(o):
  158. bpm_inc = 4
  159. x_size = o.width / 4
  160. y_size = o.height / 4
  161. og_x = 0
  162. o_x = og_x
  163. o_y = o.height
  164. text_padding = 6
  165. _id = 0
  166. while _id < 16:
  167. o.draw.rectangle((o_x, o_y, o_x + x_size, o_y - y_size), outline=0, fill=o.dark_grey)
  168. if _id == 0:
  169. o.center_block("Copy", o.h2, [o_x, o_y, o_x + x_size, o_y - y_size], o.light_grey)
  170. if _id == 1:
  171. o.center_block("Paste", o.h2, [o_x, o_y, o_x + x_size, o_y - y_size], o.light_grey)
  172. if _id == 2:
  173. o.center_block("Clear", o.h2, [o_x, o_y, o_x + x_size, o_y - y_size], o.light_grey)
  174. if _id == 6:
  175. o.center_block("Bank -", o.h2, [o_x, o_y, o_x + x_size, o_y - y_size], o.light_grey)
  176. if _id == 7:
  177. o.center_block("Bank +", o.h2, [o_x, o_y, o_x + x_size, o_y - y_size], o.light_grey)
  178. o_x = o_x + x_size
  179. _id += 1
  180. if _id % 4 == 0:
  181. o_y -= y_size
  182. o_x = og_x
  183. def draw_menu2_note(o):
  184. bpm_inc = 4
  185. x_size = o.width / 4
  186. y_size = o.height / 4
  187. og_x = 0
  188. o_x = og_x
  189. o_y = o.height
  190. text_padding = 6
  191. _id = 0
  192. while _id < 16:
  193. o.draw.rectangle((o_x, o_y, o_x + x_size, o_y - y_size), outline=0, fill=o.dark_grey)
  194. if _id == 0:
  195. o.center_block("Vol -", o.h2, [o_x, o_y, o_x + x_size, o_y - y_size], o.light_grey)
  196. if _id == 1:
  197. o.center_block("Vol +", o.h2, [o_x, o_y, o_x + x_size, o_y - y_size], o.light_grey)
  198. o_x = o_x + x_size
  199. _id += 1
  200. if _id % 4 == 0:
  201. o_y -= y_size
  202. o_x = og_x
  203. def menu1_actions(self, o):
  204. if o.keyState[16] == 2:
  205. if o.keyState[0] == 1:
  206. self.FSM.ToTransition('toMain')
  207. elif o.keyState[1] == 1:
  208. self.FSM.ToTransition('toEditSoundSequence')
  209. elif o.keyState[2] == 1:
  210. self.FSM.ToTransition('toSelectPattern')
  211. elif o.keyState[3] == 1:
  212. self.FSM.ToTransition('toEditSong')
  213. elif o.keyState[4] == 1:
  214. self.FSM.ToTransition('toFile')
  215. elif o.keyState[5] == 1:
  216. self.FSM.ToTransition('toUtil')
  217. if o.keyState[12] == 1:
  218. o.start_playback()
  219. if o.keyState[13] == 1:
  220. o.stop_playback()
  221. if o.keyState[6] == 1:
  222. if o.note_bank > 0:
  223. o.note_bank -= 1
  224. if o.keyState[7] == 1:
  225. if o.note_bank < 4:
  226. o.note_bank += 1
  227. if o.keyState[8] == 1:
  228. if o.volume > 0:
  229. o.volume -= 1
  230. text_center(o, "Master Volume", str(o.volume))
  231. o.update_display(0)
  232. if o.keyState[9] == 1:
  233. if o.volume < 16:
  234. o.volume += 1
  235. text_center(o, "Master Volume", str(o.volume))
  236. o.update_display(0)
  237. if o.keyState[11] == 1:
  238. #o.save_song()
  239. pass
  240. if o.keyState[14] == 1:
  241. if o.sconf.as_int('bpm') > 60:
  242. o.sconf['bpm'] = o.sconf.as_int('bpm') - o.bpm_inc
  243. o.update_bpm()
  244. text_center(o, "Master BPM", str(o.sconf['bpm']))
  245. o.update_display(0)
  246. if o.keyState[15] == 1:
  247. if o.sconf.as_int('bpm') < 240:
  248. o.sconf['bpm'] = o.sconf.as_int('bpm') + o.bpm_inc
  249. o.update_bpm()
  250. text_center(o, "Master BPM", str(o.sconf['bpm']))
  251. o.update_display(0)
  252. def menu2_actions_sample(self, o):
  253. if o.keyState[17] == 2:
  254. if o.keyState[0] == 1:
  255. if o.soundSlots[o.eSound].volume > 0:
  256. print('copy')
  257. o.soundClipboard = [o.soundSlots[o.eSound].file, o.soundSlots[o.eSound].volume, o.soundSlots[o.eSound].pitch]
  258. print(o.soundClipboard)
  259. #text_center(o, "Sample Volume", str(o.soundSlots[o.eSound].volume))
  260. #o.update_display(0)
  261. elif o.keyState[1] == 1:
  262. print('pasting ', o.soundClipboard)
  263. o.soundSlots[o.eSound].file = o.soundClipboard[0]
  264. o.soundSlots[o.eSound].volume = o.soundClipboard[1]
  265. o.soundSlots[o.eSound].pitch = o.soundClipboard[2]
  266. o.soundSlots[o.eSound].create_sound()
  267. o.soundSlots[o.eSound].set_pitch()
  268. #s = o.SoundSlot(o.soundClipboard[0], o.eSound, o)
  269. #s.volume = o.soundClipboard[1]
  270. #s.pitch = o.soundClipboard[2]
  271. #s.notes = s.init_notes()
  272. #o.soundSlots[o.eSound] = s
  273. elif o.keyState[2] == 1:
  274. if o.soundSlots[o.eSound].pitch > -10:
  275. o.soundSlots[o.eSound].pitch -= 1
  276. text_center(o, "Sample Pitch", str(o.soundSlots[o.eSound].pitch))
  277. o.soundSlots[o.eSound].set_pitch()
  278. o.update_display(0)
  279. elif o.keyState[3] == 1:
  280. print('load')
  281. o.FSM.FSM.ToTransition("toLoadSound")
  282. elif o.keyState[4] == 1:
  283. if o.soundSlots[o.eSound].volume > 0:
  284. o.soundSlots[o.eSound].volume -= 1
  285. text_center(o, "Sample Volume", str(o.soundSlots[o.eSound].volume))
  286. o.update_display(0)
  287. elif o.keyState[5] == 1:
  288. if o.soundSlots[o.eSound].volume < 16:
  289. o.soundSlots[o.eSound].volume += 1
  290. text_center(o, "Sample Volume", str(o.soundSlots[o.eSound].volume))
  291. o.update_display(0)
  292. elif o.keyState[6] == 1:
  293. if o.soundSlots[o.eSound].pitch > -10:
  294. o.soundSlots[o.eSound].pitch -= 1
  295. text_center(o, "Sample Pitch", str(o.soundSlots[o.eSound].pitch))
  296. o.soundSlots[o.eSound].set_pitch()
  297. o.update_display(0)
  298. elif o.keyState[7] == 1:
  299. if o.soundSlots[o.eSound].pitch < 10:
  300. o.soundSlots[o.eSound].pitch += 1
  301. text_center(o, "Sample Pitch", str(o.soundSlots[o.eSound].pitch))
  302. o.soundSlots[o.eSound].set_pitch()
  303. o.update_display(0)
  304. def menu2_actions_pattern(self, o):
  305. if o.keyState[17] == 2:
  306. if o.keyState[0] == 1:
  307. print('copy')
  308. o.patternClipboard = []
  309. for s in o.soundSlots:
  310. notes = []
  311. for i in s.notes[o.ePattern]:
  312. notes.append([i[0], i[1]])
  313. #i[0] = 0
  314. #i[1] = 0
  315. o.patternClipboard.append(notes)
  316. print('adding to clip ', notes)
  317. # if o.soundSlots[o.eSound].volume > 0:
  318. # o.soundSlots[o.eSound].volume -= 1
  319. # text_center(o, "Sample Volume", str(o.soundSlots[o.eSound].volume))
  320. # o.update_display(0)
  321. elif o.keyState[1] == 1:
  322. print('paste')
  323. if o.patternClipboard != []:
  324. p = 0
  325. for s in o.soundSlots:
  326. n = 0
  327. for i in s.notes[o.ePattern]:
  328. i[0] = o.patternClipboard[p][n][0]
  329. i[1] = o.patternClipboard[p][n][1]
  330. n += 1
  331. print('added ', o.patternClipboard[p])
  332. p += 1
  333. else:
  334. print('nothing to paste')
  335. # if o.soundSlots[o.eSound].volume < 16:
  336. # o.soundSlots[o.eSound].volume += 1
  337. # text_center(o, "Sample Volume", str(o.soundSlots[o.eSound].volume))
  338. # o.update_display(0)
  339. elif o.keyState[2] == 1:
  340. print('clear')
  341. print(o.soundSlots[o.eSound].notes[o.ePattern])
  342. for s in o.soundSlots:
  343. for i in s.notes[o.ePattern]:
  344. i[0] = 0
  345. i[1] = 0
  346. #print(o.soundSlots[o.eSound].notes[o.ePattern])
  347. # if o.soundSlots[o.eSound].pitch > -10:
  348. # o.soundSlots[o.eSound].pitch -= 1
  349. # text_center(o, "Sample Pitch", str(o.soundSlots[o.eSound].pitch))
  350. # o.soundSlots[o.eSound].set_pitch()
  351. # o.update_display(0)
  352. elif o.keyState[6] == 1:
  353. if o.pat_bank > 0:
  354. o.pat_bank -= 1
  355. text_center(o, "Pattern Bank", str(o.pat_bank))
  356. o.update_display(0)
  357. elif o.keyState[7] == 1:
  358. if o.pat_bank < 4:
  359. o.pat_bank += 1
  360. text_center(o, "Pattern Bank", str(o.pat_bank))
  361. o.update_display(0)
  362. def menu2_actions_note(self, o):
  363. if o.keyState[17] == 2:
  364. if o.keyState[0] == 1:
  365. if o.note_vol > 0:
  366. o.note_vol -= 1
  367. text_center(o, "Note Volume", str(o.note_vol))
  368. o.update_display(0)
  369. elif o.keyState[1] == 1:
  370. if o.note_vol < 16:
  371. o.note_vol += 1
  372. text_center(o, "Note Volume", str(o.note_vol))
  373. o.update_display(0)
  374. def util_command(self, o, command):
  375. if command == "Clock":
  376. self.FSM.ToTransition('toClock')
  377. elif command == "Ball":
  378. self.FSM.ToTransition('toBall')
  379. elif command == "Exit":
  380. self.FSM.ToTransition('toExit')
  381. elif command == "Theme":
  382. self.FSM.ToTransition('toSelTheme')
  383. elif command == "Reboot":
  384. #self.FSM.ToTransition('toSelTheme')
  385. os.system("sudo reboot")
  386. def file_command(self, o, command):
  387. if command == "Save":
  388. o.save_song()
  389. elif command == "Open":
  390. self.FSM.ToTransition('toOpenSong')
  391. elif command == "Save As":
  392. self.FSM.ToTransition('toEnterText')
  393. #====================================
  394. class Example(State):
  395. def __init__(self,FSM):
  396. super(Example, self).__init__(FSM)
  397. def Enter(self):
  398. self.FSM.stateLife = 1
  399. super(Example, self).Enter()
  400. def Execute(self):
  401. self.FSM.stateLife += 1
  402. def Exit(self):
  403. pass
  404. #====================================
  405. class Startup(State):
  406. def __init__(self,FSM):
  407. super(Startup, self).__init__(FSM)
  408. def Enter(self):
  409. self.FSM.stateLife = 1
  410. self.birth = time.perf_counter()
  411. super(Startup, self).Enter()
  412. def Execute(self):
  413. self.FSM.stateLife += 1
  414. print('startup state')
  415. if hasattr(self, 'birth'):
  416. pass
  417. else:
  418. self.birth = time.perf_counter()
  419. self.FSM.ToTransition('toIntro')
  420. def Exit(self):
  421. pass
  422. #====================================
  423. class Intro(State):
  424. def __init__(self,FSM):
  425. super(Intro, self).__init__(FSM)
  426. def Enter(self):
  427. self.FSM.stateLife = 1
  428. o = self.FSM.owner
  429. o.header_text = "Starting up..."
  430. o.draw.rectangle((0, 0, self.FSM.owner.width, self.FSM.owner.height), outline=0, fill=o.blue)
  431. #draw_header(o)
  432. #o.draw.rectangle((0, 0, o.width, o.height), outline=0, fill=o.blue)
  433. o.center_block("Starting up...", o.h2, [0, 0, o.width, o.height], o.light_grey)
  434. #o.display_thread.start()
  435. o.update_display(0)
  436. #o.update_display(0)
  437. super(Intro, self).Enter()
  438. def Execute(self):
  439. self.FSM.stateLife += 1
  440. o = self.FSM.owner
  441. self.FSM.ToTransition('toLoadDefault')
  442. def Exit(self):
  443. pass
  444. #====================================
  445. class LoadDefault(State):
  446. def __init__(self,FSM):
  447. super(LoadDefault, self).__init__(FSM)
  448. def Enter(self):
  449. self.FSM.stateLife = 1
  450. o = self.FSM.owner
  451. o.init_slots()
  452. o.load_song()
  453. self.bank = 0
  454. self.sounds = []
  455. self.globbed = glob.glob('/home/pi/skull/*.wav') + glob.glob('/home/pi/skull/*.mp3')
  456. self.globbed.sort()
  457. obj_id = 0
  458. # for s in o.sconf['sounds']:
  459. # p1 = self.FSM.owner.SoundSlot(s, obj_id, o)
  460. # self.FSM.owner.soundSlots.append(p1)
  461. # obj_id += 1
  462. # _iter = 0
  463. # lst = ast.literal_eval(o.sconf['notes'][0])
  464. # print('length ', len(lst))
  465. # for n in o.soundSlots:
  466. # m = []
  467. # if _iter < (len(o.sconf.as_list('notes')) - 1):
  468. # lst = ast.literal_eval(o.sconf.as_list('notes')[_iter])
  469. # n.notes = lst
  470. # _iter += 1
  471. super(LoadDefault, self).Enter()
  472. def Execute(self):
  473. self.FSM.stateLife += 1
  474. self.FSM.ToTransition('toMain')
  475. def Exit(self):
  476. pass
  477. #====================================
  478. #====================================
  479. class Main(State):
  480. def __init__(self,FSM):
  481. super(Main, self).__init__(FSM)
  482. def Enter(self):
  483. print('hello main')
  484. self.FSM.owner.pub.register("beat", self)
  485. self.FSM.stateLife = 1
  486. o = self.FSM.owner
  487. o.header_text = "Sound Select"
  488. if o.keyState[16] > 0 or o.keyState[17] > 0:
  489. pass
  490. else:
  491. draw_header(o)
  492. self.draw_square()
  493. text_box1(o, "Vol", str(o.soundSlots[o.eSound].volume))
  494. text_box2(o, "BPM", str(o.sconf['bpm']))
  495. text_box4(o, "Bank", str(o.note_bank))
  496. text_box3(o, "Pitch", str(o.soundSlots[o.eSound].pitch))
  497. o.update_display(0)
  498. #o.soundSlots[2].volume = 2
  499. super(Main, self).Enter()
  500. def Execute(self):
  501. o = self.FSM.owner
  502. menu1_actions(self, o)
  503. menu2_actions_sample(self, o)
  504. if o.keyState[16] == 1:
  505. draw_menu1(o)
  506. o.update_display(0)
  507. elif o.keyState[16] == 4:
  508. draw_header(o)
  509. self.draw_square()
  510. #print('esound ', o.eSound)
  511. #print('oss ', o.soundSlots[o.eSound].volume)
  512. text_box1(o, "SVol", str(o.soundSlots[o.eSound].volume))
  513. text_box2(o, "BPM", str(o.sconf['bpm']))
  514. text_box4(o, "Bank", str(o.note_bank))
  515. text_box3(o, "Pitch", str(o.soundSlots[o.eSound].pitch))
  516. o.update_display(0)
  517. if o.keyState[17] == 1:
  518. draw_menu2_sample(o)
  519. o.update_display(0)
  520. elif o.keyState[17] == 4:
  521. draw_header(o)
  522. self.draw_square()
  523. text_box1(o, "SVol", str(o.soundSlots[o.eSound].volume))
  524. text_box2(o, "BPM", str(o.sconf['bpm']))
  525. text_box4(o, "Bank", str(o.note_bank))
  526. text_box3(o, "Pitch", str(o.soundSlots[o.eSound].pitch))
  527. o.update_display(0)
  528. _id = 0
  529. for k in o.keyState:
  530. if _id > 15:
  531. pass
  532. else:
  533. if o.keyState[16] > 0 or o.keyState[17] > 0:
  534. pass
  535. else:
  536. if k == 1:
  537. note = _id + o.note_bank * 16
  538. #if len(o.soundSlots) > note:
  539. if 12 == 12:
  540. #o.soundSlots[note].play(o.note_vol)
  541. o.soundSlots[note].play(o.note_vol)
  542. o.eSound = note
  543. print('now editing sound ', o.eSound)
  544. draw_header(o)
  545. #text_box1(o, "SVol", str(o.soundSlots[o.eSound].volume))
  546. text_box1(o, "SVol", str(o.slots[o.eSound].volume))
  547. text_box2(o, "BPM", str(o.sconf['bpm']))
  548. text_box4(o, "Bank", str(o.note_bank))
  549. #text_box3(o, "Pitch", str(o.soundSlots[o.eSound].pitch))
  550. text_box3(o, "Pitch", str(o.soundSlots[o.eSound].pitch))
  551. self.draw_square()
  552. o.update_display(0)
  553. _id += 1
  554. def ReceiveMessage(self, message):
  555. o = self.FSM.owner
  556. play_seq(o, message)
  557. if o.keyState[16] > 0 or o.keyState[17] > 0:
  558. pass
  559. else:
  560. draw_header(o)
  561. self.draw_square()
  562. text_box1(o, "SVol", str(o.soundSlots[o.eSound].volume))
  563. text_box2(o, "BPM", str(o.sconf['bpm']))
  564. text_box4(o, "Bank", str(o.note_bank))
  565. text_box3(o, "Pitch", str(o.soundSlots[o.eSound].pitch))
  566. o.update_display(0)
  567. def draw_square(self):
  568. o = self.FSM.owner
  569. size = 22
  570. og_x = (o.width / 2) - (size * 2)
  571. o_x = og_x
  572. o_y = 127
  573. _id = o.note_bank * 16
  574. while _id < ((o.note_bank * 16) + 16):
  575. if _id == o.eSound:
  576. o.draw.rectangle((o_x, o_y, o_x + size, o_y - size), outline=o.light_grey, fill=o.light_grey, width=1)
  577. elif o.notes_on[_id] == 1:
  578. o.draw.rectangle((o_x, o_y, o_x + size, o_y - size), outline=o.light_grey, fill=o.grey, width=1)
  579. #elif _id > (len(o.soundSlots) - 1):
  580. elif self.has_sample(o, _id) == False:
  581. o.draw.rectangle((o_x, o_y, o_x + size, o_y - size), outline=o.light_grey, fill=o.dark_blue, width=1)
  582. else:
  583. o.draw.rectangle((o_x, o_y, o_x + size, o_y - size), outline=o.light_grey, fill=o.blue, width=1)
  584. o_x = o_x + size
  585. _id += 1
  586. if _id % 4 == 0:
  587. o_y -= size
  588. o_x = og_x
  589. def has_sample(self, o, slot):
  590. if o.soundSlots[slot].file != None:
  591. return True
  592. else:
  593. return False
  594. def Exit(self):
  595. self.FSM.owner.pub.unregister("beat", self)
  596. #====================================
  597. #====================================
  598. class PadPlayer(State):
  599. def __init__(self,FSM):
  600. super(PadPlayer, self).__init__(FSM)
  601. def Enter(self):
  602. self.FSM.stateLife = 1
  603. print('entering pad player')
  604. super(PadPlayer, self).Enter()
  605. def Execute(self):
  606. self.FSM.stateLife += 1
  607. _id = 0
  608. for k in self.FSM.owner.keyState:
  609. if _id < 16 and k == 1:
  610. self.FSM.owner.soundSlots[_id].play()
  611. _id += 1
  612. if self.FSM.owner.keys:
  613. print("Pressed: ", self.FSM.owner.keys)
  614. #self.FSM.ToTransition('toLand')
  615. def Exit(self):
  616. pass
  617. #====================================
  618. class SeqPlayer(State):
  619. def __init__(self,FSM):
  620. super(SeqPlayer, self).__init__(FSM)
  621. def Enter(self):
  622. self.FSM.stateLife = 1
  623. self.beat = 0
  624. self.FSM.owner.pub.register("beat", self)
  625. print('entering seq player')
  626. o = self.FSM.owner
  627. o.draw.rectangle((0, 0, o.width, o.height), outline=0, fill=o.blue)
  628. o.draw.text((20, 0), 'SeqPlayer', font=o.h1, fill="#FFFFFF")
  629. o.update_display(0)
  630. super(SeqPlayer, self).Enter()
  631. def Execute(self):
  632. self.FSM.stateLife += 1
  633. o = self.FSM.owner
  634. if o.keyState[12] == 1:
  635. o.start_playback()
  636. if o.keyState[13] == 1:
  637. o.stop_playback()
  638. #self.beat = o.playhead
  639. #print('key0 ', o.keyState[16])
  640. if o.keyState[16] == 2:
  641. #print('key0 ', o.keyState[16])
  642. if o.keyState[0] == 1:
  643. print('to main')
  644. self.FSM.ToTransition('toMain')
  645. def ReceiveMessage(self, message):
  646. #print(self.name, 'got message', message)
  647. o = self.FSM.owner
  648. o.draw.rectangle((0, 0, o.width, o.height), outline=0, fill=o.blue)
  649. o.draw.text((20, 0), str(message[0]) + ' - ' + str(message[1]), font=o.h1, fill="#FFFFFF")
  650. o.update_display(0)
  651. play_seq(o, message)
  652. def Exit(self):
  653. self.FSM.owner.pub.unregister("beat", self)
  654. #====================================
  655. #====================================
  656. class SelectSound(State):
  657. def __init__(self,FSM):
  658. super(SelectSound, self).__init__(FSM)
  659. def Enter(self):
  660. self.FSM.stateLife = 1
  661. o = self.FSM.owner
  662. o.header_text = "SelSou"
  663. o.pub.register("beat", self)
  664. draw_header(o)
  665. self.draw_square()
  666. o.update_display(0)
  667. super(SelectSound, self).Enter()
  668. def Execute(self):
  669. o = self.FSM.owner
  670. if o.keyState[16] == 1:
  671. draw_menu1(o)
  672. o.update_display(0)
  673. elif o.keyState[16] == 4:
  674. draw_header(o)
  675. self.draw_square()
  676. o.update_display(0)
  677. if o.keyState[16] == 2:
  678. if o.keyState[0] == 1:
  679. self.FSM.ToTransition('toMain')
  680. else:
  681. _id = 0
  682. for k in o.keyState:
  683. if _id > 15:
  684. pass
  685. else:
  686. if k == 1:
  687. o.eSound = _id
  688. o.draw.rectangle((0, 0, o.width, o.height), outline=0, fill=o.blue)
  689. o.draw.text((0, 0), "SelSou", font=o.h1, fill=o.color_a)
  690. o.draw.text((20, 60), str(o.eSound), font=o.h2, fill=o.color_b)
  691. self.draw_square()
  692. o.update_display(0)
  693. self.FSM.owner.soundSlots[_id].play()
  694. #print('now editing sound ', _id)
  695. _id += 1
  696. def ReceiveMessage(self, message):
  697. _id = 0
  698. o = self.FSM.owner
  699. o.draw.rectangle((0, 0, o.width, o.height), outline=0, fill=(0, 0, 0))
  700. o.draw.text((0, 0), "SelSou", font=o.h1, fill="#FFFFFF")
  701. o.draw.text((0, 20), str(o.eSound), font=o.h2, fill="#FFFFFF")
  702. o.update_display(0)
  703. play_seq(o, message)
  704. def draw_square(self):
  705. o = self.FSM.owner
  706. size = 32
  707. og_x = 110
  708. o_x = og_x
  709. o_y = 0
  710. _id = 0
  711. for n in o.soundSlots[o.eSound].notes[o.ePattern]:
  712. if _id == o.eSound:
  713. o.draw.rectangle((o_x, o_y, o_x + size, o_y + size), outline=0, fill=o.color_b)
  714. else:
  715. o.draw.rectangle((o_x, o_y, o_x + size, o_y + size), outline=0, fill=o.dark_grey)
  716. o_x = o_x + size
  717. _id += 1
  718. if _id % 4 == 0:
  719. o_y += size
  720. o_x = og_x
  721. def Exit(self):
  722. self.FSM.owner.pub.unregister("beat", self)
  723. #====================================
  724. class SelectPattern(State):
  725. def __init__(self,FSM):
  726. super(SelectPattern, self).__init__(FSM)
  727. def Enter(self):
  728. self.FSM.stateLife = 1
  729. o = self.FSM.owner
  730. o.header_text = "Select Pattern"
  731. o.pub.register("beat", self)
  732. self.cur_playing = 0
  733. self.active_patterns = []
  734. self.get_active_patterns(o)
  735. if o.keyState[16] > 0 or o.keyState[17] > 0:
  736. pass
  737. else:
  738. draw_header(o)
  739. self.draw_square()
  740. text_box1(o, "Pat", str(o.ePattern))
  741. text_box4(o, "Bank", str(o.pat_bank))
  742. o.update_display(0)
  743. super(SelectPattern, self).Enter()
  744. def Execute(self):
  745. o = self.FSM.owner
  746. menu1_actions(self, o)
  747. menu2_actions_pattern(self, o)
  748. if o.keyState[16] == 1:
  749. draw_menu1(o)
  750. o.update_display(0)
  751. elif o.keyState[16] == 4:
  752. draw_header(o)
  753. self.draw_square()
  754. text_box1(o, "Pat", str(o.ePattern))
  755. text_box4(o, "Bank", str(o.pat_bank))
  756. o.update_display(0)
  757. if o.keyState[17] == 1:
  758. draw_menu2_pattern(o)
  759. o.update_display(0)
  760. elif o.keyState[17] == 4:
  761. draw_header(o)
  762. self.draw_square()
  763. #text_box1(o, "SVol", str(o.soundSlots[o.eSound].volume))
  764. #text_box2(o, "BPM", str(o.sconf['bpm']))
  765. text_box1(o, "Pat", str(o.ePattern))
  766. text_box4(o, "Bank", str(o.pat_bank))
  767. #text_box3(o, "Pitch", str(o.soundSlots[o.eSound].pitch))
  768. self.get_active_patterns(o)
  769. o.update_display(0)
  770. if o.keyState[16] > 0 or o.keyState[17] > 0:
  771. pass
  772. else:
  773. _id = 0
  774. for k in o.keyState:
  775. if _id > 15:
  776. pass
  777. else:
  778. if k == 1:
  779. o.ePattern = (o.pat_bank * 16) +_id
  780. draw_header(o)
  781. self.draw_square()
  782. text_box1(o, "Pat", str(o.ePattern))
  783. text_box4(o, "Bank", str(o.pat_bank))
  784. o.update_display(0)
  785. _id += 1
  786. def ReceiveMessage(self, message):
  787. o = self.FSM.owner
  788. play_seq(o, message)
  789. if message[0] != self.cur_playing:
  790. #print('playing ', message)
  791. self.cur_playing = message[0]
  792. if o.keyState[16] > 0 or o.keyState[17] > 0:
  793. pass
  794. else:
  795. draw_header(o)
  796. self.draw_square()
  797. text_box1(o, "Pat", str(o.ePattern))
  798. text_box4(o, "PBank", str(o.pat_bank))
  799. o.update_display(0)
  800. def draw_square(self):
  801. o = self.FSM.owner
  802. size = 22
  803. og_x = (o.width / 2) - (size * 2)
  804. o_x = og_x
  805. o_y = 127
  806. _id = 0
  807. for n in o.soundSlots[o.eSound].notes[o.ePattern]:
  808. if (_id + (o.pat_bank * 16)) == o.ePattern:
  809. o.draw.rectangle((o_x, o_y, o_x + size, o_y - size), outline=o.olive, fill=o.olive, width=1)
  810. elif (_id + (o.pat_bank * 16)) == self.cur_playing:
  811. o.draw.rectangle((o_x, o_y, o_x + size, o_y - size), outline=o.olive, fill=o.light_grey, width=1)
  812. elif self.active_patterns[_id] == 1:
  813. o.draw.rectangle((o_x, o_y, o_x + size, o_y - size), outline=o.light_grey, fill=o.blue, width=1)
  814. else:
  815. o.draw.rectangle((o_x, o_y, o_x + size, o_y - size), outline=o.light_grey, fill=o.dark_blue, width=1)
  816. o_x = o_x + size
  817. _id += 1
  818. if _id % 4 == 0:
  819. o_y -= size
  820. o_x = og_x
  821. def get_active_patterns(self, o):
  822. #for n in o.soundSlots[o.eSound]
  823. self.active_patterns = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
  824. pat_iter = o.pat_bank * 16
  825. _iter = 0
  826. print('is this note on? ', o.soundSlots[0].notes[0][0][0])
  827. while pat_iter < ((o.pat_bank * 16) + 16):
  828. for s in o.soundSlots:
  829. print('checking soundSlot ', s.id)
  830. pat_used = 0
  831. for p in s.notes[pat_iter]:
  832. if self.active_patterns[_iter] == 1:
  833. break
  834. if p[0] == 1:
  835. #pat_used = 1
  836. self.active_patterns[_iter] = 1
  837. print('found ', pat_iter, ' -- ', p[0])
  838. break
  839. #self.active_patterns.append(pat_used)
  840. pat_iter += 1
  841. _iter += 1
  842. print('active patterns ', self.active_patterns)
  843. def Exit(self):
  844. self.FSM.owner.pub.unregister("beat", self)
  845. #====================================
  846. class EditSoundSequence(State):
  847. def __init__(self,FSM):
  848. super(EditSoundSequence, self).__init__(FSM)
  849. def Enter(self):
  850. self.FSM.stateLife = 1
  851. o = self.FSM.owner
  852. o.header_text = "Note Edit"
  853. o.pub.register("beat", self)
  854. if o.keyState[16] > 0 or o.keyState[17] > 0:
  855. pass
  856. else:
  857. draw_header(o)
  858. self.draw_square()
  859. text_box1(o, "Note Vol", str(o.note_vol))
  860. o.update_display(0)
  861. super(EditSoundSequence, self).Enter()
  862. def Execute(self):
  863. o = self.FSM.owner
  864. menu1_actions(self, o)
  865. menu2_actions_note(self, o)
  866. if o.keyState[16] == 1:
  867. draw_menu1(o)
  868. o.update_display(0)
  869. elif o.keyState[16] == 4:
  870. draw_header(o)
  871. self.draw_square()
  872. text_box1(o, "Note Vol", str(o.note_vol))
  873. o.update_display(0)
  874. if o.keyState[17] == 1:
  875. draw_menu2_note(o)
  876. o.update_display(0)
  877. elif o.keyState[17] == 4:
  878. draw_header(o)
  879. self.draw_square()
  880. text_box1(o, "Note Vol", str(o.note_vol))
  881. o.update_display(0)
  882. if o.keyState[16] == 2:
  883. pass
  884. else:
  885. _id = 0
  886. for k in self.FSM.owner.keyState:
  887. if _id < 16 and k == 1:
  888. if o.soundSlots[o.eSound].notes[o.ePattern][_id][0] == 1:
  889. print('turn note off')
  890. o.soundSlots[o.eSound].notes[o.ePattern][_id][0] = 0
  891. o.soundSlots[o.eSound].notes[o.ePattern][_id][1] = 0
  892. else:
  893. print('turn note on')
  894. o.soundSlots[o.eSound].notes[o.ePattern][_id][0] = 1
  895. o.soundSlots[o.eSound].notes[o.ePattern][_id][1] = o.note_vol
  896. _id += 1
  897. def draw_square(self):
  898. o = self.FSM.owner
  899. size = 22
  900. og_x = (o.width / 2) - (size * 2)
  901. o_x = og_x
  902. o_y = 127
  903. _id = 0
  904. for n in o.soundSlots[o.eSound].notes[o.ePattern]:
  905. if _id == o.playhead:
  906. if o.curPattern == o.ePattern:
  907. o.draw.rectangle((o_x, o_y, o_x + size, o_y - size), outline=o.olive, fill=o.olive, width=1)
  908. else:
  909. o.draw.rectangle((o_x, o_y, o_x + size, o_y - size), outline=o.olive, fill=o.pink, width=1)
  910. elif n[0] == 1:
  911. o.draw.rectangle((o_x, o_y, o_x + size, o_y - size), outline=o.light_grey, fill=o.light_grey, width=1)
  912. else:
  913. o.draw.rectangle((o_x, o_y, o_x + size, o_y - size), outline=o.light_grey, fill=o.blue, width=1)
  914. o_x = o_x + size
  915. _id += 1
  916. if _id % 4 == 0:
  917. o_y -= size
  918. o_x = og_x
  919. def ReceiveMessage(self, message):
  920. _id = 0
  921. o = self.FSM.owner
  922. play_seq(o, message)
  923. if o.keyState[16] > 0 or o.keyState[17] > 0:
  924. pass
  925. else:
  926. draw_header(o)
  927. self.draw_square()
  928. text_box1(o, "Note Vol", str(o.note_vol))
  929. o.update_display(0)
  930. def Exit(self):
  931. self.FSM.owner.pub.unregister("beat", self)
  932. #====================================
  933. class EditSong(State):
  934. def __init__(self,FSM):
  935. super(EditSong, self).__init__(FSM)
  936. def Enter(self):
  937. o = self.FSM.owner
  938. o.header_text = "Edit Song"
  939. o.pub.register("beat", self)
  940. if o.keyState[16] > 0 or o.keyState[17] > 0:
  941. pass
  942. else:
  943. draw_header(o)
  944. o.draw.text((20, 30), "Current Song", font=o.h2, fill=o.light_grey)
  945. o.draw.text((20, 50), str(o.song), font=o.h2, fill=o.light_grey)
  946. o.update_display(0)
  947. self.new_song = []
  948. super(EditSong, self).Enter()
  949. def Execute(self):
  950. o = self.FSM.owner
  951. menu1_actions(self, o)
  952. if o.keyState[16] == 1:
  953. draw_menu1(o)
  954. o.update_display(0)
  955. elif o.keyState[16] == 4:
  956. draw_header(o)
  957. if self.new_song == []:
  958. o.draw.text((20, 30), "Current Song", font=o.h2, fill=o.light_grey)
  959. o.draw.text((20, 50), str(o.song), font=o.h2, fill=o.light_grey)
  960. else:
  961. o.draw.text((20, 30), "New Song", font=o.h2, fill=o.pink)
  962. o.draw.text((20, 50), str(self.new_song), font=o.h2, fill=o.pink)
  963. o.update_display(0)
  964. if o.keyState[16] > 0:
  965. pass
  966. else:
  967. _id = 0
  968. for k in self.FSM.owner.keyState:
  969. if k == 1 and _id < 16:
  970. self.new_song.append((_id + (o.pat_bank * 16)))
  971. draw_header(o)
  972. o.draw.text((20, 30), "New Song", font=o.h2, fill=o.pink)
  973. o.draw.text((20, 50), str(self.new_song), font=o.h2, fill=o.pink)
  974. o.update_display(0)
  975. _id += 1
  976. def ReceiveMessage(self, message):
  977. _id = 0
  978. o = self.FSM.owner
  979. play_seq(o, message)
  980. def Exit(self):
  981. o = self.FSM.owner
  982. self.FSM.owner.pub.unregister("beat", self)
  983. if self.new_song != []:
  984. o.song = self.new_song
  985. o.songCycle = cycle(self.new_song)
  986. self.curPattern = next(o.songCycle)
  987. o.songStart = o.curPattern
  988. #====================================
  989. class File(State):
  990. def __init__(self,FSM):
  991. super(File, self).__init__(FSM)
  992. def Enter(self):
  993. self.FSM.stateLife = 1
  994. o = self.FSM.owner
  995. o.header_text = "File"
  996. o.pub.register("beat", self)
  997. self.cur_el = 0
  998. if o.keyState[16] > 0 or o.keyState[17] > 0:
  999. pass
  1000. else:
  1001. draw_header(o)
  1002. self.draw_footer(o)
  1003. self.draw_main(o)
  1004. o.update_display(0)
  1005. self.menu = ["Save", "Save As", "Open", "New"]
  1006. super(File, self).Enter()
  1007. def Execute(self):
  1008. o = self.FSM.owner
  1009. menu1_actions(self, o)
  1010. if o.keyState[16] == 1:
  1011. draw_menu1(o)
  1012. #self.draw_footer(o)
  1013. o.update_display(0)
  1014. elif o.keyState[16] == 4:
  1015. draw_header(o)
  1016. self.draw_footer(o)
  1017. self.draw_main(o)
  1018. o.update_display(0)
  1019. 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:
  1020. draw_header(o)
  1021. self.draw_footer(o)
  1022. self.draw_main(o)
  1023. o.update_display(0)
  1024. print('draw butt')
  1025. if o.keyState[16] > 0 or o.keyState[17] > 0:
  1026. pass
  1027. else:
  1028. if o.keyState[0] == 1:
  1029. if self.cur_el > 0:
  1030. self.cur_el -= 1
  1031. print('cur el down is ', self.cur_el)
  1032. if o.keyState[1] == 1:
  1033. if self.cur_el < (len(self.menu) - 1):
  1034. self.cur_el += 1
  1035. print('cur el up is ', self.cur_el)
  1036. if o.keyState[3] == 1:
  1037. file_command(self, o, self.menu[self.cur_el])
  1038. pass
  1039. def draw_footer(self, o):
  1040. _iter = 0
  1041. width = o.width / 4
  1042. xpos = 0
  1043. yposa = 135 - 25
  1044. yposb = 135
  1045. opts = ["Up", "Down", "Esc", "OK"]
  1046. while _iter < 4:
  1047. o.draw.rectangle((xpos, yposa, xpos + width, yposb), outline=0, fill=o.blue)
  1048. if o.keyState[_iter] == 1:
  1049. o.draw.rectangle((xpos, yposa, xpos + width, yposb), outline=0, fill=o.light_grey)
  1050. o.center_block(opts[_iter], o.h2, [xpos,yposa,xpos + width,yposb], o.blue)
  1051. else:
  1052. o.center_block(opts[_iter], o.h2, [xpos,yposa,xpos + width,yposb], o.light_grey)
  1053. xpos += width
  1054. _iter += 1
  1055. def draw_main(self, o):
  1056. al = []
  1057. ypos = 30
  1058. if self.cur_el == 0:
  1059. _iter = 0
  1060. while len(al) < 4:
  1061. al.append(self.menu[_iter])
  1062. _iter += 1
  1063. elif self.cur_el == 1:
  1064. al.append(self.menu[self.cur_el - 1])
  1065. al.append(self.menu[self.cur_el])
  1066. al.append(self.menu[self.cur_el + 1])
  1067. al.append(self.menu[self.cur_el + 2])
  1068. elif self.cur_el == (len(self.menu) - 2):
  1069. al.append(self.menu[self.cur_el - 2])
  1070. al.append(self.menu[self.cur_el - 1])
  1071. al.append(self.menu[self.cur_el])
  1072. al.append(self.menu[self.cur_el + 1])
  1073. elif self.cur_el == (len(self.menu) - 1):
  1074. al.append(self.menu[self.cur_el - 3])
  1075. al.append(self.menu[self.cur_el - 2])
  1076. al.append(self.menu[self.cur_el - 1])
  1077. al.append(self.menu[self.cur_el])
  1078. else:
  1079. al.append(self.menu[self.cur_el - 1])
  1080. al.append(self.menu[self.cur_el])
  1081. al.append(self.menu[self.cur_el + 1])
  1082. al.append(self.menu[self.cur_el + 2])
  1083. _iter = 0
  1084. for i in al:
  1085. if i == self.menu[self.cur_el]:
  1086. #if _iter == self.cur_el:
  1087. o.draw.rectangle((0, ypos, o.width, ypos + 20), outline=0, fill=o.light_grey)
  1088. o.center_block(i, o.h2, [0, ypos, o.width,ypos + 20], o.blue)
  1089. else:
  1090. o.draw.rectangle((0, ypos, o.width, ypos + 20), outline=0, fill=o.blue)
  1091. o.center_block(i, o.h2, [0, ypos, o.width,ypos + 20], o.light_grey)
  1092. ypos += 20
  1093. _iter += 1
  1094. def ReceiveMessage(self, message):
  1095. o = self.FSM.owner
  1096. play_seq(o, message)
  1097. def Exit(self):
  1098. self.FSM.owner.pub.unregister("beat", self)
  1099. class Util(State):
  1100. def __init__(self,FSM):
  1101. super(Util, self).__init__(FSM)
  1102. def Enter(self):
  1103. o = self.FSM.owner
  1104. o.header_text = "Utilities"
  1105. o.pub.register("beat", self)
  1106. self.menu = ["Clock", "Ball", "Exit", "Shutdown", "Reboot", "Theme", "Update"]
  1107. self.cur_el = 0
  1108. if o.keyState[16] > 0 or o.keyState[17] > 0:
  1109. pass
  1110. else:
  1111. draw_header(o)
  1112. self.draw_footer(o)
  1113. self.draw_main(o)
  1114. o.update_display(0)
  1115. super(Util, self).Enter()
  1116. def Execute(self):
  1117. o = self.FSM.owner
  1118. menu1_actions(self, o)
  1119. if o.keyState[16] == 1:
  1120. draw_menu1(o)
  1121. o.update_display(0)
  1122. elif o.keyState[16] == 4:
  1123. draw_header(o)
  1124. self.draw_footer(o)
  1125. self.draw_main(o)
  1126. o.update_display(0)
  1127. 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:
  1128. draw_header(o)
  1129. self.draw_footer(o)
  1130. self.draw_main(o)
  1131. o.update_display(0)
  1132. print('draw butt')
  1133. if o.keyState[16] > 0 or o.keyState[17] > 0:
  1134. pass
  1135. else:
  1136. if o.keyState[0] == 1:
  1137. if self.cur_el > 0:
  1138. self.cur_el -= 1
  1139. print('cur el down is ', self.cur_el)
  1140. if o.keyState[1] == 1:
  1141. if self.cur_el < (len(self.menu) - 1):
  1142. self.cur_el += 1
  1143. print('cur el up is ', self.cur_el)
  1144. if o.keyState[3] == 1:
  1145. util_command(self, o, self.menu[self.cur_el])
  1146. def ReceiveMessage(self, message):
  1147. _id = 0
  1148. o = self.FSM.owner
  1149. play_seq(o, message)
  1150. # if o.keyState[16] > 0 or o.keyState[17] > 0:
  1151. # pass
  1152. # else:
  1153. # draw_header(o)
  1154. # o.update_display(0)
  1155. def draw_main(self, o):
  1156. al = []
  1157. ypos = 30
  1158. if self.cur_el == 0:
  1159. _iter = 0
  1160. while len(al) < 4:
  1161. al.append(self.menu[_iter])
  1162. _iter += 1
  1163. elif self.cur_el == 1:
  1164. al.append(self.menu[self.cur_el - 1])
  1165. al.append(self.menu[self.cur_el])
  1166. al.append(self.menu[self.cur_el + 1])
  1167. al.append(self.menu[self.cur_el + 2])
  1168. elif self.cur_el == (len(self.menu) - 2):
  1169. al.append(self.menu[self.cur_el - 2])
  1170. al.append(self.menu[self.cur_el - 1])
  1171. al.append(self.menu[self.cur_el])
  1172. al.append(self.menu[self.cur_el + 1])
  1173. elif self.cur_el == (len(self.menu) - 1):
  1174. al.append(self.menu[self.cur_el - 3])
  1175. al.append(self.menu[self.cur_el - 2])
  1176. al.append(self.menu[self.cur_el - 1])
  1177. al.append(self.menu[self.cur_el])
  1178. else:
  1179. al.append(self.menu[self.cur_el - 1])
  1180. al.append(self.menu[self.cur_el])
  1181. al.append(self.menu[self.cur_el + 1])
  1182. al.append(self.menu[self.cur_el + 2])
  1183. _iter = 0
  1184. for i in al:
  1185. if i == self.menu[self.cur_el]:
  1186. #if _iter == self.cur_el:
  1187. o.draw.rectangle((0, ypos, o.width, ypos + 20), outline=0, fill=o.light_grey)
  1188. o.center_block(i, o.h2, [0, ypos, o.width,ypos + 20], o.blue)
  1189. else:
  1190. o.draw.rectangle((0, ypos, o.width, ypos + 20), outline=0, fill=o.blue)
  1191. o.center_block(i, o.h2, [0, ypos, o.width,ypos + 20], o.light_grey)
  1192. ypos += 20
  1193. _iter += 1
  1194. def draw_footer(self, o):
  1195. _iter = 0
  1196. width = o.width / 4
  1197. xpos = 0
  1198. yposa = 135 - 25
  1199. yposb = 135
  1200. opts = ["Up", "Down", "Esc", "OK"]
  1201. while _iter < 4:
  1202. o.draw.rectangle((xpos, yposa, xpos + width, yposb), outline=0, fill=o.blue)
  1203. if o.keyState[_iter] == 1:
  1204. o.draw.rectangle((xpos, yposa, xpos + width, yposb), outline=0, fill=o.light_grey)
  1205. o.center_block(opts[_iter], o.h2, [xpos,yposa,xpos + width,yposb], o.blue)
  1206. else:
  1207. o.center_block(opts[_iter], o.h2, [xpos,yposa,xpos + width,yposb], o.light_grey)
  1208. xpos += width
  1209. _iter += 1
  1210. def Exit(self):
  1211. self.FSM.owner.pub.unregister("beat", self)
  1212. class Ball(State):
  1213. def __init__(self,FSM):
  1214. super(Ball, self).__init__(FSM)
  1215. def Enter(self):
  1216. self.ball_size = 10
  1217. self.ballx = self.ball_size
  1218. self.bally = self.ball_size
  1219. self.ball_speed = 15
  1220. self.ball_x_dir = 1
  1221. self.ball_y_dir = 1
  1222. self.ball2x = self.ball_size + 180
  1223. self.ball2y = self.ball_size + 70
  1224. self.ball2_x_dir = -1
  1225. self.ball2_y_dir = -1
  1226. self.xmult = 1.0
  1227. o = self.FSM.owner
  1228. o.header_text = "Ball"
  1229. o.pub.register("beat", self)
  1230. if o.keyState[16] > 0 or o.keyState[17] > 0:
  1231. pass
  1232. else:
  1233. draw_header(o)
  1234. o.update_display(0)
  1235. super(Ball, self).Enter()
  1236. def Execute(self):
  1237. o = self.FSM.owner
  1238. menu1_actions(self, o)
  1239. if o.keyState[16] == 1:
  1240. draw_menu1(o)
  1241. o.update_display(0)
  1242. if o.keyState[16] > 0 or o.keyState[17] > 0:
  1243. pass
  1244. else:
  1245. now = datetime.now()
  1246. current_time = time.strftime("%-I:%M %p")
  1247. if self.ballx < (0 + self.ball_size):
  1248. self.ball_x_dir = 1
  1249. if self.ballx > (o.width - self.ball_size):
  1250. self.ball_x_dir = -1
  1251. if self.bally < (0 + self.ball_size):
  1252. self.ball_y_dir = 1
  1253. if self.bally > (o.height - self.ball_size):
  1254. self.ball_y_dir = -1
  1255. # if self.ballx > (o.width - self.ball_size) or self.ballx < (0 + self.ball_size):
  1256. # self.ball_x_dir *= -1
  1257. # if self.bally > (o.height - self.ball_size) or self.bally < (0 + self.ball_size):
  1258. # self.ball_y_dir *= -1
  1259. self.ballx += self.ball_speed * self.ball_x_dir
  1260. self.bally += self.ball_speed * self.ball_y_dir
  1261. if self.ball2x < (0 + self.ball_size):
  1262. self.ball2_x_dir = 1
  1263. self.xmult = random.uniform(.6, 1.2)
  1264. if self.ball2x > (o.width - self.ball_size):
  1265. self.ball2_x_dir = -1
  1266. self.xmult = random.uniform(.6, 1.2)
  1267. if self.ball2y < (0 + self.ball_size):
  1268. self.ball2_y_dir = 1
  1269. if self.ball2y > (o.height - self.ball_size):
  1270. self.ball2_y_dir = -1
  1271. # if self.ball2x > (o.width - self.ball_size) or self.ball2x < (0 + self.ball_size):
  1272. # self.ball2_x_dir *= -1
  1273. # self.xmult = random.uniform(.8, 1.2)
  1274. # if self.ball2y > (o.height - self.ball_size) or self.ball2y < (0 + self.ball_size):
  1275. # self.ball2_y_dir *= -1
  1276. self.ball2x += (self.ball_speed * self.ball2_x_dir) * self.xmult
  1277. self.ball2y += (self.ball_speed * self.ball2_y_dir)
  1278. o.draw.rectangle((0, 0, o.width, o.height), outline=0, fill=o.blue)
  1279. o.draw.ellipse((self.ballx - self.ball_size, self.bally - self.ball_size, self.ballx + self.ball_size, self.bally + self.ball_size), fill = o.light_grey, outline =o.light_grey)
  1280. o.draw.ellipse((self.ball2x - self.ball_size, self.ball2y - self.ball_size, self.ball2x + self.ball_size, self.ball2y + self.ball_size), fill = o.light_grey, outline =o.light_grey)
  1281. #o.center_block(current_time, o.h3, [0,0,o.width,o.height], o.pink)
  1282. o.update_display(0)
  1283. def ReceiveMessage(self, message):
  1284. o = self.FSM.owner
  1285. play_seq(o, message)
  1286. def Exit(self):
  1287. self.FSM.owner.pub.unregister("beat", self)
  1288. #====================================
  1289. class Clock(State):
  1290. def __init__(self,FSM):
  1291. super(Clock, self).__init__(FSM)
  1292. def Enter(self):
  1293. o = self.FSM.owner
  1294. current_time = time.strftime("%-I:%M:%S")
  1295. self.ball_sizex, self.ball_sizey = o.h3.getsize("18:88:88")
  1296. self.ball_sizex /= 2
  1297. self.ball_sizey /= 2
  1298. self.ballx = o.width / 2
  1299. self.bally = o.height / 2
  1300. self.ball_speed = 1
  1301. self.ball_x_dir = 1
  1302. self.ball_y_dir = 1
  1303. o.header_text = "Clock"
  1304. o.pub.register("beat", self)
  1305. if o.keyState[16] > 0 or o.keyState[17] > 0:
  1306. pass
  1307. else:
  1308. pass
  1309. super(Clock, self).Enter()
  1310. def Execute(self):
  1311. o = self.FSM.owner
  1312. menu1_actions(self, o)
  1313. if o.keyState[16] == 1:
  1314. draw_menu1(o)
  1315. o.update_display(0)
  1316. if o.keyState[16] > 0 or o.keyState[17] > 0:
  1317. pass
  1318. else:
  1319. current_time = time.strftime("%-I:%M:%S")
  1320. if self.ballx > (o.width - self.ball_sizex) or self.ballx < (0 + self.ball_sizex):
  1321. self.ball_x_dir *= -1
  1322. if (self.bally > o.height - self.ball_sizey) or (self.bally < 0 + self.ball_sizey):
  1323. self.ball_y_dir *= -1
  1324. self.ballx += self.ball_speed * self.ball_x_dir
  1325. self.bally += self.ball_speed * self.ball_y_dir
  1326. o.draw.rectangle((0, 0, o.width, o.height), outline=0, fill=o.bg_color)
  1327. o.draw.text((self.ballx - self.ball_sizex, self.bally - self.ball_sizey), current_time, align='center', font=o.h3, fill=o.light_grey)
  1328. o.update_display(0)
  1329. def ReceiveMessage(self, message):
  1330. o = self.FSM.owner
  1331. play_seq(o, message)
  1332. def Exit(self):
  1333. self.FSM.owner.pub.unregister("beat", self)
  1334. #====================================
  1335. class Exit(State):
  1336. def __init__(self,FSM):
  1337. super(Exit, self).__init__(FSM)
  1338. def Enter(self):
  1339. o = self.FSM.owner
  1340. o.draw.rectangle((0, 0, o.width, o.height), outline=0, fill=o.blue)
  1341. o.center_block("goodbye", o.h2, [0, 0, o.width,o.height], o.light_grey)
  1342. o.update_display(0)
  1343. pygame.quit()
  1344. sys.exit()
  1345. super(Exit, self).Enter()
  1346. def Execute(self):
  1347. self.FSM.stateLife += 1
  1348. def Exit(self):
  1349. pass
  1350. #====================================
  1351. class SelTheme(State):
  1352. def __init__(self,FSM):
  1353. super(SelTheme, self).__init__(FSM)
  1354. def Enter(self):
  1355. o = self.FSM.owner
  1356. o.header_text = "Select Theme"
  1357. o.pub.register("beat", self)
  1358. self.cur_el = 0
  1359. self.globbed = glob.glob('/home/pi/zpc_ct/themes/*.thm')
  1360. self.globbed.sort()
  1361. self.names = [os.path.basename(x) for x in self.globbed]
  1362. self.names = [os.path.splitext(x)[0] for x in self.names]
  1363. if o.mconf['theme'] in self.names:
  1364. _id = self.names.index(o.mconf['theme'])
  1365. self.cur_el = _id
  1366. #print('found theme ', _id)
  1367. else:
  1368. print('nope ', o.mconf['theme'], ' - ', self.globbed)
  1369. #self.theme = ConfigObj("/home/pi/zpc_ct/themes/" + self.mconf['theme'])
  1370. #print(os.path.splitext(x)[0])
  1371. self.menu = self.names
  1372. #self.cur_el = 0
  1373. print('theme menu is ', self.menu)
  1374. if o.keyState[16] > 0 or o.keyState[17] > 0:
  1375. pass
  1376. else:
  1377. draw_header(o)
  1378. self.draw_main(o)
  1379. o.update_display(0)
  1380. super(SelTheme, self).Enter()
  1381. def Execute(self):
  1382. o = self.FSM.owner
  1383. menu1_actions(self, o)
  1384. if o.keyState[16] == 1:
  1385. draw_menu1(o)
  1386. o.update_display(0)
  1387. elif o.keyState[16] == 4:
  1388. draw_header(o)
  1389. self.draw_footer(o)
  1390. self.draw_main(o)
  1391. o.update_display(0)
  1392. 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:
  1393. draw_header(o)
  1394. self.draw_footer(o)
  1395. self.draw_main(o)
  1396. o.update_display(0)
  1397. print('draw butt')
  1398. if o.keyState[16] > 0 or o.keyState[17] > 0:
  1399. pass
  1400. else:
  1401. if o.keyState[0] == 1:
  1402. if self.cur_el > 0:
  1403. self.cur_el -= 1
  1404. o.mconf['theme'] = self.menu[self.cur_el]
  1405. o.save_config()
  1406. o.load_config()
  1407. o.apply_theme()
  1408. print('cur el down is ', self.cur_el)
  1409. #o.applyTheme
  1410. if o.keyState[1] == 1:
  1411. if self.cur_el < (len(self.menu) - 1):
  1412. self.cur_el += 1
  1413. o.mconf['theme'] = self.menu[self.cur_el]
  1414. o.save_config()
  1415. o.load_config()
  1416. o.apply_theme()
  1417. print('cur el up is ', self.cur_el)
  1418. print('loading theme ', self.menu[self.cur_el], '-- ', o.mconf['theme'])
  1419. if o.keyState[3] == 1:
  1420. util_command(self, o, self.menu[self.cur_el])
  1421. def draw_main(self, o):
  1422. al = []
  1423. ypos = 30
  1424. if self.cur_el == 0:
  1425. _iter = 0
  1426. while len(al) < 4:
  1427. al.append(self.menu[_iter])
  1428. _iter += 1
  1429. elif self.cur_el == 1:
  1430. al.append(self.menu[self.cur_el - 1])
  1431. al.append(self.menu[self.cur_el])
  1432. al.append(self.menu[self.cur_el + 1])
  1433. al.append(self.menu[self.cur_el + 2])
  1434. elif self.cur_el == (len(self.menu) - 2):
  1435. al.append(self.menu[self.cur_el - 2])
  1436. al.append(self.menu[self.cur_el - 1])
  1437. al.append(self.menu[self.cur_el])
  1438. al.append(self.menu[self.cur_el + 1])
  1439. elif self.cur_el == (len(self.menu) - 1):
  1440. al.append(self.menu[self.cur_el - 3])
  1441. al.append(self.menu[self.cur_el - 2])
  1442. al.append(self.menu[self.cur_el - 1])
  1443. al.append(self.menu[self.cur_el])
  1444. else:
  1445. al.append(self.menu[self.cur_el - 1])
  1446. al.append(self.menu[self.cur_el])
  1447. al.append(self.menu[self.cur_el + 1])
  1448. al.append(self.menu[self.cur_el + 2])
  1449. _iter = 0
  1450. for i in al:
  1451. if i == self.menu[self.cur_el]:
  1452. #if _iter == self.cur_el:
  1453. o.draw.rectangle((0, ypos, o.width, ypos + 20), outline=0, fill=o.light_grey)
  1454. o.center_block(i, o.h2, [0, ypos, o.width,ypos + 20], o.blue)
  1455. else:
  1456. o.draw.rectangle((0, ypos, o.width, ypos + 20), outline=0, fill=o.blue)
  1457. o.center_block(i, o.h2, [0, ypos, o.width,ypos + 20], o.light_grey)
  1458. ypos += 20
  1459. _iter += 1
  1460. def draw_footer(self, o):
  1461. _iter = 0
  1462. width = o.width / 4
  1463. xpos = 0
  1464. yposa = 135 - 25
  1465. yposb = 135
  1466. opts = ["Up", "Down", "", ""]
  1467. while _iter < 4:
  1468. o.draw.rectangle((xpos, yposa, xpos + width, yposb), outline=0, fill=o.blue)
  1469. if o.keyState[_iter] == 1:
  1470. o.draw.rectangle((xpos, yposa, xpos + width, yposb), outline=0, fill=o.light_grey)
  1471. o.center_block(opts[_iter], o.h2, [xpos,yposa,xpos + width,yposb], o.blue)
  1472. else:
  1473. o.center_block(opts[_iter], o.h2, [xpos,yposa,xpos + width,yposb], o.light_grey)
  1474. xpos += width
  1475. _iter += 1
  1476. def ReceiveMessage(self, message):
  1477. o = self.FSM.owner
  1478. play_seq(o, message)
  1479. def Exit(self):
  1480. self.FSM.owner.pub.unregister("beat", self)
  1481. #====================================
  1482. class OpenSong(State):
  1483. def __init__(self,FSM):
  1484. super(OpenSong, self).__init__(FSM)
  1485. def Enter(self):
  1486. o = self.FSM.owner
  1487. o.header_text = "Open Song"
  1488. o.pub.register("beat", self)
  1489. self.cur_el = 0
  1490. self.globbed = glob.glob('/home/pi/zpc_ct/user/songs/*.sng')
  1491. self.globbed.sort()
  1492. if '/home/pi/zpc_ct/user/songs/_blank.sng' in self.globbed:
  1493. self.globbed.remove('/home/pi/zpc_ct/user/songs/_blank.sng')
  1494. #del self.globbed[0]
  1495. self.names = [os.path.basename(x) for x in self.globbed]
  1496. self.names = [os.path.splitext(x)[0] for x in self.names]
  1497. print('we got these songs ', self.names)
  1498. print('in these paths ', self.globbed)
  1499. if o.mconf['default_song'] in self.names:
  1500. _id = self.names.index(o.mconf['default_song'])
  1501. self.cur_el = _id
  1502. else:
  1503. print('nope ', o.mconf['default_song'], ' - ', self.globbed)
  1504. self.menu = self.names
  1505. #self.cur_el = 0
  1506. #print('theme menu is ', self.menu)
  1507. if o.keyState[16] > 0 or o.keyState[17] > 0:
  1508. pass
  1509. else:
  1510. draw_header(o)
  1511. self.draw_main(o)
  1512. o.update_display(0)
  1513. super(OpenSong, self).Enter()
  1514. def Execute(self):
  1515. o = self.FSM.owner
  1516. menu1_actions(self, o)
  1517. if o.keyState[16] == 1:
  1518. draw_menu1(o)
  1519. o.update_display(0)
  1520. elif o.keyState[16] == 4:
  1521. draw_header(o)
  1522. self.draw_footer(o)
  1523. self.draw_main(o)
  1524. o.update_display(0)
  1525. 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:
  1526. draw_header(o)
  1527. self.draw_footer(o)
  1528. self.draw_main(o)
  1529. o.update_display(0)
  1530. print('draw butt')
  1531. if o.keyState[16] > 0 or o.keyState[17] > 0:
  1532. pass
  1533. else:
  1534. if o.keyState[0] == 1:
  1535. if self.cur_el > 0:
  1536. self.cur_el -= 1
  1537. # o.mconf['theme'] = self.menu[self.cur_el]
  1538. # o.save_config()
  1539. # o.load_config()
  1540. # o.apply_theme()
  1541. print('cur el down is ', self.cur_el)
  1542. #o.applyTheme
  1543. if o.keyState[1] == 1:
  1544. if self.cur_el < (len(self.menu) - 1):
  1545. self.cur_el += 1
  1546. # o.mconf['theme'] = self.menu[self.cur_el]
  1547. # o.save_config()
  1548. # o.load_config()
  1549. # o.apply_theme()
  1550. print('cur el up is ', self.cur_el)
  1551. #print('loading theme ', self.menu[self.cur_el], '-- ', o.mconf['theme'])
  1552. if o.keyState[3] == 1:
  1553. #open_command(self, o, self.menu[self.cur_el])
  1554. o.mconf['default_song'] = self.menu[self.cur_el]
  1555. o.load_song()
  1556. #pass
  1557. def draw_main(self, o):
  1558. al = []
  1559. ypos = 30
  1560. if self.cur_el == 0:
  1561. _iter = 0
  1562. while len(al) < 4:
  1563. al.append(self.menu[_iter])
  1564. _iter += 1
  1565. elif self.cur_el == 1:
  1566. al.append(self.menu[self.cur_el - 1])
  1567. al.append(self.menu[self.cur_el])
  1568. al.append(self.menu[self.cur_el + 1])
  1569. al.append(self.menu[self.cur_el + 2])
  1570. elif self.cur_el == (len(self.menu) - 2):
  1571. al.append(self.menu[self.cur_el - 2])
  1572. al.append(self.menu[self.cur_el - 1])
  1573. al.append(self.menu[self.cur_el])
  1574. al.append(self.menu[self.cur_el + 1])
  1575. elif self.cur_el == (len(self.menu) - 1):
  1576. al.append(self.menu[self.cur_el - 3])
  1577. al.append(self.menu[self.cur_el - 2])
  1578. al.append(self.menu[self.cur_el - 1])
  1579. al.append(self.menu[self.cur_el])
  1580. else:
  1581. al.append(self.menu[self.cur_el - 1])
  1582. al.append(self.menu[self.cur_el])
  1583. al.append(self.menu[self.cur_el + 1])
  1584. al.append(self.menu[self.cur_el + 2])
  1585. _iter = 0
  1586. for i in al:
  1587. if i == self.menu[self.cur_el]:
  1588. #if _iter == self.cur_el:
  1589. o.draw.rectangle((0, ypos, o.width, ypos + 20), outline=0, fill=o.light_grey)
  1590. o.center_block(i, o.h2, [0, ypos, o.width,ypos + 20], o.blue)
  1591. else:
  1592. o.draw.rectangle((0, ypos, o.width, ypos + 20), outline=0, fill=o.blue)
  1593. o.center_block(i, o.h2, [0, ypos, o.width,ypos + 20], o.light_grey)
  1594. ypos += 20
  1595. _iter += 1
  1596. def draw_footer(self, o):
  1597. _iter = 0
  1598. width = o.width / 4
  1599. xpos = 0
  1600. yposa = 135 - 25
  1601. yposb = 135
  1602. opts = ["Up", "Down", "Esc", "Open"]
  1603. while _iter < 4:
  1604. o.draw.rectangle((xpos, yposa, xpos + width, yposb), outline=0, fill=o.blue)
  1605. if o.keyState[_iter] == 1:
  1606. o.draw.rectangle((xpos, yposa, xpos + width, yposb), outline=0, fill=o.light_grey)
  1607. o.center_block(opts[_iter], o.h2, [xpos,yposa,xpos + width,yposb], o.blue)
  1608. else:
  1609. o.center_block(opts[_iter], o.h2, [xpos,yposa,xpos + width,yposb], o.light_grey)
  1610. xpos += width
  1611. _iter += 1
  1612. def ReceiveMessage(self, message):
  1613. o = self.FSM.owner
  1614. play_seq(o, message)
  1615. def Exit(self):
  1616. self.FSM.owner.pub.unregister("beat", self)
  1617. class EnterText(State):
  1618. def __init__(self,FSM):
  1619. super(EnterText, self).__init__(FSM)
  1620. def Enter(self):
  1621. o = self.FSM.owner
  1622. self.input_string = ""
  1623. #if
  1624. o.header_text = "EnterText"
  1625. o.pub.register("beat", self)
  1626. self.last_action = pygame.time.get_ticks()
  1627. self.caps = False
  1628. self.cur_but = 18
  1629. self.cur_letter = ""
  1630. if o.keyState[16] > 0 or o.keyState[17] > 0:
  1631. pass
  1632. else:
  1633. pass
  1634. self.skips = [7, 3, 11, 12, 13, 14, 15]
  1635. self.lc_labels = [["p", "q", "r", "s"], ["t", "u", "v"], ["w", "x", "y", "z"], ["C", "A", "P", "S"],
  1636. ["g", "h", "i"], ["j", "k", "l"], ["m", "n", "o"], ["D", "E", "L"],
  1637. ["_", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-"], ["a", "b", "c"], ["d", "e", "f"], ["O", "K"],
  1638. [], [], [], []]
  1639. self.uc_labels = [["P", "Q", "R", "S"], ["T", "U", "V"], ["W", "X", "Y", "Z"], ["C", "A", "P", "S"],
  1640. ["G", "H", "I"], ["J", "K", "L"], ["M", "N", "O"], ["D", "E", "L"],
  1641. ["_", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-"], ["A", "B", "C"], ["D", "E", "F"], ["O", "K"],
  1642. [], [], [], []]
  1643. self.labels = self.lc_labels
  1644. #self.songCycle = cycle(self.song)
  1645. #self.curPattern = next(self.songCycle)
  1646. self.cycles = []
  1647. for l in self.labels:
  1648. self.cycles.append(cycle(l))
  1649. self.draw(o)
  1650. o.update_display(0)
  1651. super(EnterText, self).Enter()
  1652. def Execute(self):
  1653. o = self.FSM.owner
  1654. y_size = o.height / 4
  1655. if o.keyState[11] == 1:
  1656. self.FSM.ToTransition('toFile')
  1657. if o.keyState[16] > 0 and o.keyState[17] > 0:
  1658. pass
  1659. else:
  1660. _iter = 0
  1661. for k in o.keyState:
  1662. if k == 1:
  1663. cur_time = pygame.time.get_ticks()
  1664. print(cur_time - self.last_action)
  1665. #print('cur letter ', next(self.cycles[_iter]))
  1666. if _iter not in self.skips:
  1667. self.last_action = cur_time
  1668. self.cur_letter = next(self.cycles[_iter])
  1669. self.cur_but = _iter
  1670. self.draw(o)
  1671. o.center_block(self.input_string + self.cur_letter, o.h2, [0, 0, o.width, y_size], o.light_grey)
  1672. if _iter == 7:
  1673. self.input_string = self.input_string[:-1]
  1674. #self.input_string = self.input_string.rstrip(self.input_string[-1])
  1675. print('deleting')
  1676. self.last_action = 100000000
  1677. self.draw(o)
  1678. o.center_block(self.input_string, o.h2, [0, 0, o.width, y_size], o.light_grey)
  1679. elif _iter == 3:
  1680. print('caps')
  1681. if self.caps:
  1682. self.caps = False
  1683. self.labels = self.lc_labels
  1684. else:
  1685. self.caps = True
  1686. self.labels = self.uc_labels
  1687. self.draw(o)
  1688. o.center_block(self.input_string, o.h2, [0, 0, o.width, y_size], o.light_grey)
  1689. self.cycles = []
  1690. for l in self.labels:
  1691. self.cycles.append(cycle(l))
  1692. o.update_display(0)
  1693. _iter += 1
  1694. cur_time = pygame.time.get_ticks()
  1695. delay = cur_time - self.last_action
  1696. if delay > 1000 and delay < 100000:
  1697. self.input_string += self.cur_letter
  1698. self.last_action = 100000000
  1699. self.reset_cycles()
  1700. self.draw(o)
  1701. o.center_block(self.input_string, o.h2, [0, 0, o.width, y_size], o.light_grey)
  1702. o.update_display(0)
  1703. def ReceiveMessage(self, message):
  1704. o = self.FSM.owner
  1705. play_seq(o, message)
  1706. def draw(self, o):
  1707. x_size = o.width / 4
  1708. y_size = o.height / 4
  1709. og_x = 0
  1710. o_x = og_x
  1711. o_y = o.height
  1712. text_padding = 6
  1713. _id = 0
  1714. while _id < 16:
  1715. lab = ""
  1716. for i in self.labels[_id]:
  1717. lab += i
  1718. if _id == 8:
  1719. lab = "_12"
  1720. o.draw.rectangle((o_x, o_y, o_x + x_size, o_y - y_size), outline=0, fill=o.blue)
  1721. o.center_block(lab, o.h2, [o_x, o_y, o_x + x_size, o_y - y_size], o.light_grey)
  1722. o_x = o_x + x_size
  1723. _id += 1
  1724. if _id % 4 == 0:
  1725. o_y -= y_size
  1726. o_x = og_x
  1727. o.draw.rectangle((0, 0, o.width, y_size), outline=0, fill=o.blue)
  1728. def reset_cycles(self):
  1729. self.cycles = []
  1730. for l in self.labels:
  1731. self.cycles.append(cycle(l))
  1732. def Exit(self):
  1733. self.FSM.owner.pub.unregister("beat", self)
  1734. #====================================
  1735. class LoadSound(State):
  1736. def __init__(self,FSM):
  1737. super(LoadSound, self).__init__(FSM)
  1738. def Enter(self):
  1739. o = self.FSM.owner
  1740. o.header_text = "Load Sound"
  1741. o.pub.register("beat", self)
  1742. self.cur_el = 0
  1743. self.menu = []
  1744. self.menu.append("..")
  1745. self.menu += self.get_file_list(o)
  1746. self.last_preview = None
  1747. if o.keyState[16] > 0 or o.keyState[17] > 0:
  1748. pass
  1749. else:
  1750. draw_header(o)
  1751. self.draw_main(o)
  1752. o.update_display(0)
  1753. super(LoadSound, self).Enter()
  1754. def Execute(self):
  1755. o = self.FSM.owner
  1756. menu1_actions(self, o)
  1757. if o.keyState[16] == 1:
  1758. draw_menu1(o)
  1759. o.update_display(0)
  1760. elif o.keyState[16] == 4:
  1761. draw_header(o)
  1762. self.draw_footer(o)
  1763. self.draw_main(o)
  1764. o.update_display(0)
  1765. 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:
  1766. draw_header(o)
  1767. self.draw_footer(o)
  1768. self.draw_main(o)
  1769. o.update_display(0)
  1770. #print('draw butt')
  1771. if o.keyState[16] > 0 or o.keyState[17] > 0:
  1772. pass
  1773. else:
  1774. if o.keyState[0] == 1:
  1775. if self.cur_el > 0:
  1776. self.cur_el -= 1
  1777. # o.mconf['theme'] = self.menu[self.cur_el]
  1778. # o.save_config()
  1779. # o.load_config()
  1780. # o.apply_theme()
  1781. #print('cur el down is ', self.cur_el)
  1782. #o.applyTheme
  1783. if o.keyState[1] == 1:
  1784. if self.cur_el < (len(self.menu) - 1):
  1785. self.cur_el += 1
  1786. # o.mconf['theme'] = self.menu[self.cur_el]
  1787. # o.save_config()
  1788. # o.load_config()
  1789. # o.apply_theme()
  1790. #print('cur el up is ', self.cur_el)
  1791. #print('loading theme ', self.menu[self.cur_el], '-- ', o.mconf['theme'])
  1792. if o.keyState[2] == 1:
  1793. if self.is_wav():
  1794. if self.last_preview == self.menu[self.cur_el]:
  1795. pass
  1796. else:
  1797. snd = pygame.mixer.Sound(o.load_song_dir + self.menu[self.cur_el])
  1798. pygame.mixer.Sound.play(snd)
  1799. if o.keyState[3] == 1:
  1800. if self.menu[self.cur_el] == '..':
  1801. print('directory up')
  1802. if o.load_song_dir == '/home/pi/zpc_ct/sounds/':
  1803. print('root dir - cant go up')
  1804. else:
  1805. o.load_song_dir = os.path.dirname(os.path.dirname(o.load_song_dir))
  1806. o.load_song_dir += '/'
  1807. print('new up dir is ', o.load_song_dir)
  1808. self.menu = []
  1809. self.menu.append("..")
  1810. self.menu += self.get_file_list(o)
  1811. self.cur_el = 0
  1812. elif os.path.isdir(o.load_song_dir + self.menu[self.cur_el]):
  1813. print('this is a directory ', o.load_song_dir + self.menu[self.cur_el])
  1814. o.load_song_dir = o.load_song_dir + self.menu[self.cur_el] + '/'
  1815. self.menu = []
  1816. self.menu.append("..")
  1817. self.menu += self.get_file_list(o)
  1818. self.cur_el = 0
  1819. else:
  1820. print('this is not a directory ', o.load_song_dir + self.menu[self.cur_el])
  1821. o.soundSlots[o.eSound].file = o.load_song_dir + self.menu[self.cur_el]
  1822. o.soundSlots[o.eSound].volume = 16
  1823. o.soundSlots[o.eSound].pitch = 0
  1824. o.soundSlots[o.eSound].create_sound()
  1825. o.soundSlots[o.eSound].set_pitch()
  1826. #pass
  1827. def get_file_list(self, o):
  1828. #root, dirs, files = os.walk(o.load_song_dir).next()
  1829. dirs = os.listdir(o.load_song_dir)
  1830. #print('got file list')
  1831. #print(root)
  1832. #print(dirs)
  1833. dirs.sort()
  1834. #print(files)
  1835. return dirs
  1836. def draw_main(self, o):
  1837. al = []
  1838. ypos = 30
  1839. #print(self.menu)
  1840. if self.cur_el == 0:
  1841. _iter = 0
  1842. while len(al) < 4:
  1843. if _iter < len(self.menu):
  1844. #print('appending to al', _iter, ' ', len(self.menu))
  1845. al.append(self.menu[_iter])
  1846. else:
  1847. break
  1848. _iter += 1
  1849. elif self.cur_el == 1:
  1850. al.append(self.menu[self.cur_el - 1])
  1851. al.append(self.menu[self.cur_el])
  1852. if (self.cur_el + 1) < len(self.menu):
  1853. al.append(self.menu[self.cur_el + 1])
  1854. if (self.cur_el + 2) < len(self.menu):
  1855. al.append(self.menu[self.cur_el + 2])
  1856. elif self.cur_el == (len(self.menu) - 2):
  1857. al.append(self.menu[self.cur_el - 2])
  1858. al.append(self.menu[self.cur_el - 1])
  1859. al.append(self.menu[self.cur_el])
  1860. al.append(self.menu[self.cur_el + 1])
  1861. elif self.cur_el == (len(self.menu) - 1):
  1862. al.append(self.menu[self.cur_el - 3])
  1863. al.append(self.menu[self.cur_el - 2])
  1864. al.append(self.menu[self.cur_el - 1])
  1865. al.append(self.menu[self.cur_el])
  1866. else:
  1867. al.append(self.menu[self.cur_el - 1])
  1868. al.append(self.menu[self.cur_el])
  1869. al.append(self.menu[self.cur_el + 1])
  1870. al.append(self.menu[self.cur_el + 2])
  1871. _iter = 0
  1872. #print(al)
  1873. for i in al:
  1874. if i == self.menu[self.cur_el]:
  1875. #if _iter == self.cur_el:
  1876. o.draw.rectangle((0, ypos, o.width, ypos + 20), outline=0, fill=o.light_grey)
  1877. o.center_block(i, o.h2, [0, ypos, o.width,ypos + 20], o.blue)
  1878. else:
  1879. o.draw.rectangle((0, ypos, o.width, ypos + 20), outline=0, fill=o.blue)
  1880. o.center_block(i, o.h2, [0, ypos, o.width,ypos + 20], o.light_grey)
  1881. ypos += 20
  1882. _iter += 1
  1883. def draw_footer(self, o):
  1884. _iter = 0
  1885. width = o.width / 4
  1886. xpos = 0
  1887. yposa = 135 - 25
  1888. yposb = 135
  1889. opts = ["Up", "Down", "Play", "Open"]
  1890. while _iter < 4:
  1891. o.draw.rectangle((xpos, yposa, xpos + width, yposb), outline=0, fill=o.blue)
  1892. if o.keyState[_iter] == 1:
  1893. o.draw.rectangle((xpos, yposa, xpos + width, yposb), outline=0, fill=o.light_grey)
  1894. o.center_block(opts[_iter], o.h2, [xpos,yposa,xpos + width,yposb], o.blue)
  1895. else:
  1896. if _iter == 2:
  1897. if self.is_wav():
  1898. o.center_block(opts[_iter], o.h2, [xpos,yposa,xpos + width,yposb], o.light_grey)
  1899. else:
  1900. o.center_block(opts[_iter], o.h2, [xpos,yposa,xpos + width,yposb], o.dark_grey)
  1901. else:
  1902. o.center_block(opts[_iter], o.h2, [xpos,yposa,xpos + width,yposb], o.light_grey)
  1903. xpos += width
  1904. _iter += 1
  1905. def is_wav(self):
  1906. if len(self.menu[self.cur_el]) > 3 and (self.menu[self.cur_el][-1] == 'V' or self.menu[self.cur_el][-1] == 'v') and (self.menu[self.cur_el][-2] == 'A' or self.menu[self.cur_el][-2] == 'a'):
  1907. return True
  1908. else:
  1909. return False
  1910. def ReceiveMessage(self, message):
  1911. o = self.FSM.owner
  1912. play_seq(o, message)
  1913. def Exit(self):
  1914. self.FSM.owner.pub.unregister("beat", self)