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.

sWifiSSID.py 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import FSM
  2. import utils as u
  3. import menus as m
  4. import pygame
  5. from itertools import cycle
  6. import os
  7. #====================================
  8. State = type("State", (object,), {})
  9. #====================================
  10. class State(object):
  11. def __init__(self, FSM):
  12. self.FSM = FSM
  13. def Enter(self):
  14. pass
  15. def Execute(self):
  16. pass
  17. def Exit(self):
  18. pass
  19. class WifiSSID(State):
  20. def __init__(self,FSM):
  21. super(WifiSSID, self).__init__(FSM)
  22. def Enter(self):
  23. o = self.FSM.owner
  24. m.menu1_actions(self, o)
  25. if o.keyState[16] == 1:
  26. m.draw_menu1(o)
  27. o.update_display(0)
  28. elif o.keyState[16] == 4:
  29. self.draw(o)
  30. o.update_display(0)
  31. self.input_string = ""
  32. o.header_text = "EnterText"
  33. o.pub.register("beat", self)
  34. self.last_action = pygame.time.get_ticks()
  35. self.caps = False
  36. self.cur_but = 18
  37. self.cur_letter = ""
  38. if o.keyState[16] > 0 or o.keyState[17] > 0:
  39. pass
  40. else:
  41. pass
  42. self.skips = [7, 3, 11, 12, 13, 14, 15]
  43. self.lc_labels = [["p", "q", "r", "s"], ["t", "u", "v"], ["w", "x", "y", "z"], ["C", "A", "P", "S"],
  44. ["g", "h", "i"], ["j", "k", "l"], ["m", "n", "o"], ["D", "E", "L"],
  45. ["_", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "[", "]", ";", ",", ".", "/", " "], ["a", "b", "c"], ["d", "e", "f"], ["O", "K"],
  46. [], [], [], []]
  47. self.uc_labels = [["P", "Q", "R", "S"], ["T", "U", "V"], ["W", "X", "Y", "Z"], ["C", "A", "P", "S"],
  48. ["G", "H", "I"], ["J", "K", "L"], ["M", "N", "O"], ["D", "E", "L"],
  49. ["!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "+", "=", "{", "}", "|", ":", "<", ">", "?"], ["A", "B", "C"], ["D", "E", "F"], ["O", "K"],
  50. [], [], [], []]
  51. self.labels = self.lc_labels
  52. self.cycles = []
  53. for l in self.labels:
  54. self.cycles.append(cycle(l))
  55. self.draw(o)
  56. o.center_block('SSID: ' + self.input_string, o.h2, [0, 0, o.width, o.height / 4], o.light_grey)
  57. o.update_display(0)
  58. super(WifiSSID, self).Enter()
  59. def Execute(self):
  60. o = self.FSM.owner
  61. if o.wifi_ssid == '':
  62. in_type = 'SSID: '
  63. else:
  64. in_type = 'PASS: '
  65. y_size = o.height / 4
  66. if o.keyState[11] == 1:
  67. if self.input_string != 'blank':
  68. if self.input_string != '':
  69. if o.wifi_ssid == '':
  70. o.wifi_ssid = self.input_string
  71. self.input_string = ''
  72. in_type = 'PASS: '
  73. u.draw_header(o)
  74. self.draw(o)
  75. o.center_block(in_type + self.input_string, o.h2, [0, 0, o.width, y_size], o.light_grey)
  76. o.update_display(0)
  77. else:
  78. if len(self.input_string) > 7:
  79. o.wifi_pass = self.input_string
  80. self.setup_new(o)
  81. self.FSM.ToTransition('toWifi')
  82. m.menu1_actions(self, o)
  83. m.menu2_actions_saveas(self, o)
  84. if o.keyState[16] == 1:
  85. m.draw_menu1(o)
  86. o.update_display(0)
  87. elif o.keyState[16] == 4:
  88. u.draw_header(o)
  89. self.draw(o)
  90. o.center_block(in_type + self.input_string, o.h2, [0, 0, o.width, y_size], o.light_grey)
  91. o.update_display(0)
  92. if o.keyState[17] == 1:
  93. m.draw_menu_saveas(o)
  94. o.update_display(0)
  95. elif o.keyState[17] == 4:
  96. u.draw_header(o)
  97. self.draw(o)
  98. o.center_block(in_type + self.input_string, o.h2, [0, 0, o.width, y_size], o.light_grey)
  99. o.update_display(0)
  100. # 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:
  101. # u.draw_header(o)
  102. # self.draw(o)
  103. # o.center_block(in_type + self.input_string, o.h2, [0, 0, o.width, y_size], o.light_grey)
  104. # o.update_display(0)
  105. if o.keyState[16] > 0 or o.keyState[17] > 0:
  106. pass
  107. else:
  108. _iter = 0
  109. for k in o.keyState:
  110. if k == 1:
  111. cur_time = pygame.time.get_ticks()
  112. if _iter not in self.skips:
  113. self.last_action = cur_time
  114. if _iter < len(self.cycles):
  115. self.cur_letter = next(self.cycles[_iter])
  116. self.cur_but = _iter
  117. self.draw(o)
  118. o.center_block(in_type + self.input_string + self.cur_letter, o.h2, [0, 0, o.width, y_size], o.light_grey)
  119. if _iter == 7:
  120. self.input_string = self.input_string[:-1]
  121. self.last_action = 100000000
  122. self.draw(o)
  123. o.center_block(in_type + self.input_string, o.h2, [0, 0, o.width, y_size], o.light_grey)
  124. elif _iter == 3:
  125. if self.caps:
  126. self.caps = False
  127. self.labels = self.lc_labels
  128. else:
  129. self.caps = True
  130. self.labels = self.uc_labels
  131. self.draw(o)
  132. o.center_block(in_type + self.input_string, o.h2, [0, 0, o.width, y_size], o.light_grey)
  133. self.cycles = []
  134. for l in self.labels:
  135. self.cycles.append(cycle(l))
  136. o.update_display(0)
  137. _iter += 1
  138. cur_time = pygame.time.get_ticks()
  139. delay = cur_time - self.last_action
  140. if delay > 1000 and delay < 100000:
  141. self.input_string += self.cur_letter
  142. self.last_action = 100000000
  143. self.reset_cycles()
  144. self.draw(o)
  145. o.center_block(in_type + self.input_string, o.h2, [0, 0, o.width, y_size], o.light_grey)
  146. o.update_display(0)
  147. def ReceiveMessage(self, message):
  148. o = self.FSM.owner
  149. u.play_seq(o, message)
  150. def draw(self, o):
  151. x_size = o.width / 4
  152. y_size = o.height / 4
  153. og_x = 0
  154. o_x = og_x
  155. o_y = o.height
  156. text_padding = 6
  157. _id = 0
  158. while _id < 16:
  159. lab = ""
  160. for i in self.labels[_id]:
  161. lab += i
  162. if _id == 8:
  163. if self.caps:
  164. lab = "!@#"
  165. else:
  166. lab = "_12"
  167. o.draw.rectangle((o_x, o_y, o_x + x_size, o_y - y_size), outline=0, fill=o.blue)
  168. o.center_block(lab, o.h2, [o_x, o_y, o_x + x_size, o_y - y_size], o.light_grey)
  169. o_x = o_x + x_size
  170. _id += 1
  171. if _id % 4 == 0:
  172. o_y -= y_size
  173. o_x = og_x
  174. o.draw.rectangle((0, 0, o.width, y_size), outline=0, fill=o.blue)
  175. def setup_new(self, o):
  176. o.draw.rectangle((0, 0, o.width, o.height), outline=0, fill=o.blue)
  177. o.center_block("puters be putin..", o.h2, [0, 0, o.width, o.height], o.light_grey)
  178. o.update_display(0)
  179. with open('wifi.txt', 'w') as f:
  180. f.write(o.wifi_ssid + "\n" + o.wifi_pass + "\n")
  181. command = 'sleep 3'
  182. os.system(command)
  183. command = 'sudo ./set_wifi.sh'
  184. os.system(command)
  185. command = 'sleep 2'
  186. command = 'sudo wpa_cli -i wlan0 reconfigure'
  187. os.system(command)
  188. command = 'sleep 8'
  189. def reset_cycles(self):
  190. self.cycles = []
  191. for l in self.labels:
  192. self.cycles.append(cycle(l))
  193. def Exit(self):
  194. self.FSM.owner.pub.unregister("beat", self)