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 54KB

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