import FSM import utils as u import menus as m import pygame from itertools import cycle import os #==================================== State = type("State", (object,), {}) #==================================== class State(object): def __init__(self, FSM): self.FSM = FSM def Enter(self): pass def Execute(self): pass def Exit(self): pass class WifiSSID(State): def __init__(self,FSM): super(WifiSSID, self).__init__(FSM) def Enter(self): o = self.FSM.owner m.menu1_actions(self, o) if o.keyState[16] == 1: m.draw_menu1(o) o.update_display(0) elif o.keyState[16] == 4: self.draw(o) o.update_display(0) self.input_string = "" o.header_text = "EnterText" o.pub.register("beat", self) self.last_action = pygame.time.get_ticks() self.caps = False self.cur_but = 18 self.cur_letter = "" if o.keyState[16] > 0 or o.keyState[17] > 0: pass else: pass self.skips = [7, 3, 11, 12, 13, 14, 15] self.lc_labels = [["p", "q", "r", "s"], ["t", "u", "v"], ["w", "x", "y", "z"], ["C", "A", "P", "S"], ["g", "h", "i"], ["j", "k", "l"], ["m", "n", "o"], ["D", "E", "L"], ["_", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "[", "]", ";", ",", ".", "/", " "], ["a", "b", "c"], ["d", "e", "f"], ["O", "K"], [], [], [], []] self.uc_labels = [["P", "Q", "R", "S"], ["T", "U", "V"], ["W", "X", "Y", "Z"], ["C", "A", "P", "S"], ["G", "H", "I"], ["J", "K", "L"], ["M", "N", "O"], ["D", "E", "L"], ["!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "+", "=", "{", "}", "|", ":", "<", ">", "?"], ["A", "B", "C"], ["D", "E", "F"], ["O", "K"], [], [], [], []] self.labels = self.lc_labels self.cycles = [] for l in self.labels: self.cycles.append(cycle(l)) self.draw(o) o.center_block('SSID: ' + self.input_string, o.h2, [0, 0, o.width, o.height / 4], o.light_grey) o.update_display(0) super(WifiSSID, self).Enter() def Execute(self): o = self.FSM.owner if o.wifi_ssid == '': in_type = 'SSID: ' else: in_type = 'PASS: ' y_size = o.height / 4 if o.keyState[11] == 1: if self.input_string != 'blank': if self.input_string != '': if o.wifi_ssid == '': o.wifi_ssid = self.input_string self.input_string = '' in_type = 'PASS: ' u.draw_header(o) self.draw(o) o.center_block(in_type + self.input_string, o.h2, [0, 0, o.width, y_size], o.light_grey) o.update_display(0) else: if len(self.input_string) > 7: o.wifi_pass = self.input_string self.setup_new(o) self.FSM.ToTransition('toWifi') m.menu1_actions(self, o) m.menu2_actions_saveas(self, o) if o.keyState[16] == 1: m.draw_menu1(o) o.update_display(0) elif o.keyState[16] == 4: u.draw_header(o) self.draw(o) o.center_block(in_type + self.input_string, o.h2, [0, 0, o.width, y_size], o.light_grey) o.update_display(0) if o.keyState[17] == 1: m.draw_menu_saveas(o) o.update_display(0) elif o.keyState[17] == 4: u.draw_header(o) self.draw(o) o.center_block(in_type + self.input_string, o.h2, [0, 0, o.width, y_size], o.light_grey) o.update_display(0) # 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: # u.draw_header(o) # self.draw(o) # o.center_block(in_type + self.input_string, o.h2, [0, 0, o.width, y_size], o.light_grey) # o.update_display(0) if o.keyState[16] > 0 or o.keyState[17] > 0: pass else: _iter = 0 for k in o.keyState: if k == 1: cur_time = pygame.time.get_ticks() if _iter not in self.skips: self.last_action = cur_time if _iter < len(self.cycles): self.cur_letter = next(self.cycles[_iter]) self.cur_but = _iter self.draw(o) o.center_block(in_type + self.input_string + self.cur_letter, o.h2, [0, 0, o.width, y_size], o.light_grey) if _iter == 7: self.input_string = self.input_string[:-1] self.last_action = 100000000 self.draw(o) o.center_block(in_type + self.input_string, o.h2, [0, 0, o.width, y_size], o.light_grey) elif _iter == 3: if self.caps: self.caps = False self.labels = self.lc_labels else: self.caps = True self.labels = self.uc_labels self.draw(o) o.center_block(in_type + self.input_string, o.h2, [0, 0, o.width, y_size], o.light_grey) self.cycles = [] for l in self.labels: self.cycles.append(cycle(l)) o.update_display(0) _iter += 1 cur_time = pygame.time.get_ticks() delay = cur_time - self.last_action if delay > 1000 and delay < 100000: self.input_string += self.cur_letter self.last_action = 100000000 self.reset_cycles() self.draw(o) o.center_block(in_type + self.input_string, o.h2, [0, 0, o.width, y_size], o.light_grey) o.update_display(0) def ReceiveMessage(self, message): o = self.FSM.owner u.play_seq(o, message) def draw(self, o): x_size = o.width / 4 y_size = o.height / 4 og_x = 0 o_x = og_x o_y = o.height text_padding = 6 _id = 0 while _id < 16: lab = "" for i in self.labels[_id]: lab += i if _id == 8: if self.caps: lab = "!@#" else: lab = "_12" o.draw.rectangle((o_x, o_y, o_x + x_size, o_y - y_size), outline=0, fill=o.blue) o.center_block(lab, o.h2, [o_x, o_y, o_x + x_size, o_y - y_size], o.light_grey) o_x = o_x + x_size _id += 1 if _id % 4 == 0: o_y -= y_size o_x = og_x o.draw.rectangle((0, 0, o.width, y_size), outline=0, fill=o.blue) def setup_new(self, o): o.draw.rectangle((0, 0, o.width, o.height), outline=0, fill=o.blue) o.center_block("puters be putin..", o.h2, [0, 0, o.width, o.height], o.light_grey) o.update_display(0) with open('wifi.txt', 'w') as f: f.write(o.wifi_ssid + "\n" + o.wifi_pass + "\n") command = 'sleep 3' os.system(command) command = 'sudo ./set_wifi.sh' os.system(command) command = 'sleep 2' command = 'sudo wpa_cli -i wlan0 reconfigure' os.system(command) command = 'sleep 8' def reset_cycles(self): self.cycles = [] for l in self.labels: self.cycles.append(cycle(l)) def Exit(self): self.FSM.owner.pub.unregister("beat", self)