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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. from datetime import date
  12. import ast
  13. import random
  14. from pathlib import Path
  15. #import socket
  16. import FSM
  17. import utils as u
  18. import menus as m
  19. #states
  20. from sMain import Main
  21. from sSelectSound import SelectSound
  22. from sSelectPattern import SelectPattern
  23. from sEditSong import EditSong
  24. from sEditSoundSequence import EditSoundSequence
  25. from sFile import File
  26. from sUtil import Util
  27. from sBall import Ball
  28. from sExit import Exit
  29. from sReboot import Reboot
  30. from sClock import Clock
  31. from sShutdown import Shutdown
  32. from sSelTheme import SelTheme
  33. from sOpenSong import OpenSong
  34. from sEnterText import EnterText
  35. from sLoadSound import LoadSound
  36. from sPing import Ping
  37. from sWifi import Wifi
  38. from sDefender import Defender
  39. from sCalculator import Calculator
  40. from sAbout import About
  41. from sUpdate import Update
  42. from sSelectFolder import SelectFolder
  43. from sNewDir import NewDir
  44. from sWifiSSID import WifiSSID
  45. from sDeleteSound import DeleteSound
  46. #====================================
  47. State = type("State", (object,), {})
  48. #====================================
  49. class State(object):
  50. def __init__(self, FSM):
  51. self.FSM = FSM
  52. self.timer = 0
  53. self.startTime = 0
  54. def Enter(self):
  55. self.timer = 0
  56. self.startTime = 0
  57. def Execute(self):
  58. print('Executing')
  59. def Exit(self):
  60. print('Exiting')
  61. #====================================
  62. class Example(State):
  63. def __init__(self,FSM):
  64. super(Example, self).__init__(FSM)
  65. def Enter(self):
  66. self.FSM.stateLife = 1
  67. super(Example, self).Enter()
  68. def Execute(self):
  69. self.FSM.stateLife += 1
  70. def Exit(self):
  71. pass
  72. #====================================
  73. class Startup(State):
  74. def __init__(self,FSM):
  75. super(Startup, self).__init__(FSM)
  76. def Enter(self):
  77. self.FSM.stateLife = 1
  78. self.birth = time.perf_counter()
  79. super(Startup, self).Enter()
  80. def Execute(self):
  81. self.FSM.stateLife += 1
  82. print('startup state')
  83. if hasattr(self, 'birth'):
  84. pass
  85. else:
  86. self.birth = time.perf_counter()
  87. self.FSM.ToTransition('toIntro')
  88. def Exit(self):
  89. pass
  90. #====================================
  91. class Intro(State):
  92. def __init__(self,FSM):
  93. super(Intro, self).__init__(FSM)
  94. def Enter(self):
  95. self.FSM.stateLife = 1
  96. o = self.FSM.owner
  97. o.header_text = "Starting up..."
  98. o.draw.rectangle((0, 0, self.FSM.owner.width, self.FSM.owner.height), outline=0, fill=o.blue)
  99. #u.draw_header(o)
  100. #o.draw.rectangle((0, 0, o.width, o.height), outline=0, fill=o.blue)
  101. o.center_block("zpc_ct", o.h4, [0, 0, o.width, o.height-40], o.light_grey)
  102. o.center_block("Starting up...", o.h2, [0, 0, o.width, o.height+60], o.light_grey)
  103. #o.display_thread.start()
  104. o.update_display(0)
  105. #o.update_display(0)
  106. super(Intro, self).Enter()
  107. def Execute(self):
  108. self.FSM.stateLife += 1
  109. o = self.FSM.owner
  110. self.FSM.ToTransition('toLoadDefault')
  111. def Exit(self):
  112. pass
  113. #====================================
  114. class LoadDefault(State):
  115. def __init__(self,FSM):
  116. super(LoadDefault, self).__init__(FSM)
  117. def Enter(self):
  118. self.FSM.stateLife = 1
  119. o = self.FSM.owner
  120. o.init_slots()
  121. o.load_song()
  122. self.bank = 0
  123. self.sounds = []
  124. self.globbed = glob.glob('/home/pi/skull/*.wav') + glob.glob('/home/pi/skull/*.mp3')
  125. self.globbed.sort()
  126. obj_id = 0
  127. # for s in o.sconf['sounds']:
  128. # p1 = self.FSM.owner.SoundSlot(s, obj_id, o)
  129. # self.FSM.owner.soundSlots.append(p1)
  130. # obj_id += 1
  131. # _iter = 0
  132. # lst = ast.literal_eval(o.sconf['notes'][0])
  133. # print('length ', len(lst))
  134. # for n in o.soundSlots:
  135. # m = []
  136. # if _iter < (len(o.sconf.as_list('notes')) - 1):
  137. # lst = ast.literal_eval(o.sconf.as_list('notes')[_iter])
  138. # n.notes = lst
  139. # _iter += 1
  140. super(LoadDefault, self).Enter()
  141. def Execute(self):
  142. self.FSM.stateLife += 1
  143. self.FSM.ToTransition('toMain')
  144. def Exit(self):
  145. pass
  146. #====================================
  147. #====================================
  148. class PadPlayer(State):
  149. def __init__(self,FSM):
  150. super(PadPlayer, self).__init__(FSM)
  151. def Enter(self):
  152. self.FSM.stateLife = 1
  153. print('entering pad player')
  154. super(PadPlayer, self).Enter()
  155. def Execute(self):
  156. self.FSM.stateLife += 1
  157. _id = 0
  158. for k in self.FSM.owner.keyState:
  159. if _id < 16 and k == 1:
  160. self.FSM.owner.soundSlots[_id].play()
  161. _id += 1
  162. if self.FSM.owner.keys:
  163. print("Pressed: ", self.FSM.owner.keys)
  164. #self.FSM.ToTransition('toLand')
  165. def Exit(self):
  166. pass
  167. #====================================
  168. class SeqPlayer(State):
  169. def __init__(self,FSM):
  170. super(SeqPlayer, self).__init__(FSM)
  171. def Enter(self):
  172. self.FSM.stateLife = 1
  173. self.beat = 0
  174. self.FSM.owner.pub.register("beat", self)
  175. print('entering seq player')
  176. o = self.FSM.owner
  177. o.draw.rectangle((0, 0, o.width, o.height), outline=0, fill=o.blue)
  178. o.draw.text((20, 0), 'SeqPlayer', font=o.h1, fill="#FFFFFF")
  179. o.update_display(0)
  180. super(SeqPlayer, self).Enter()
  181. def Execute(self):
  182. self.FSM.stateLife += 1
  183. o = self.FSM.owner
  184. if o.keyState[12] == 1:
  185. o.start_playback()
  186. u.play_seq(o, [o.curPattern, 0])
  187. if o.keyState[13] == 1:
  188. o.stop_playback()
  189. #self.beat = o.playhead
  190. #print('key0 ', o.keyState[16])
  191. if o.keyState[16] == 2:
  192. #print('key0 ', o.keyState[16])
  193. if o.keyState[0] == 1:
  194. print('to main')
  195. self.FSM.ToTransition('toMain')
  196. def ReceiveMessage(self, message):
  197. #print(self.name, 'got message', message)
  198. o = self.FSM.owner
  199. o.draw.rectangle((0, 0, o.width, o.height), outline=0, fill=o.blue)
  200. o.draw.text((20, 0), str(message[0]) + ' - ' + str(message[1]), font=o.h1, fill="#FFFFFF")
  201. o.update_display(0)
  202. u.play_seq(o, message)
  203. def Exit(self):
  204. self.FSM.owner.pub.unregister("beat", self)
  205. #====================================