Add scan hotspot function (WIP 4)

This commit is contained in:
pangduckwai
2019-05-15 02:26:21 +08:00
parent 7cb3af72cb
commit 12d97d2ea6
+78 -18
View File
@@ -47,6 +47,10 @@ x4 = x1+9
x5 = x2+9 x5 = x2+9
x6 = x3+9 x6 = x3+9
choice1 = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
choice2 = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
choice3 = ["0","1","2","3","4","5","6","7","8","9","!","@","#","$","%","^","&","*","(",")",",",".","?",":",";","'"]
# init GPIO # init GPIO
GPIO.setmode(GPIO.BCM) GPIO.setmode(GPIO.BCM)
GPIO.setup(JS_U_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Input with pull-up GPIO.setup(JS_U_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Input with pull-up
@@ -71,40 +75,50 @@ stamp = time.time() #Current timestamp
start = time.time() #Start screen saver count down start = time.time() #Start screen saver count down
iface = "" iface = ""
aplist = [] aplist = []
apIndx = -1
idxWin = 0 idxWin = 0
idxLen = 0 idxLen = 0
pwdLst = ["a", "b", "c"] #TEMP!!!
pwdLen = 0
test = 0 # For testing!@1!!!!!!!!!!!!!!!!!!!! test = 0 # For testing!@1!!!!!!!!!!!!!!!!!!!!
def take_act(channel): def click_b3(channel):
global start global start
global vert global vert
global apIndx
if state == BTN3_PIN: if state == BTN3_PIN:
apIndx = -1
if vert == 1: if vert == 1:
if horz == 1: if horz == 1:
show_stt(995) main_fun(995)
os.system("sudo shutdown -h now") os.system("sudo shutdown -h now")
else: else:
show_stt(996) main_fun(996)
os.system("sudo shutdown -r now") os.system("sudo shutdown -r now")
elif vert == 2: elif vert == 2:
show_stt(997) main_fun(997)
os.system("sudo usb_drive.sh refresh") os.system("sudo usb_drive.sh refresh")
start = stamp - SCREEN_SAVER - 10 start = stamp - SCREEN_SAVER - 10
else: else:
show_stt(BTN1_PIN) main_fun(BTN1_PIN)
else: else:
vert = 3 # Default vert = 3 # Default
show_stt(BTN3_PIN) main_fun(BTN3_PIN)
def scan_wifi(channel): def click_b2(channel):
global aplist global aplist
global idxWin global idxWin
global idxLen global idxLen
global start global start
global vert global vert
global test global test
start = time.time() start = time.time()
if apIndx >= 0:
main_fun(channel)
else:
# result = os.popen("iwlist {0} scan 2>/dev/null | grep '^..*ESSID:\"..*\"$' | sed 's/^.*ESSID:\"\\(..*\\)\".*$/\\1/'".format(iface)).read() # result = os.popen("iwlist {0} scan 2>/dev/null | grep '^..*ESSID:\"..*\"$' | sed 's/^.*ESSID:\"\\(..*\\)\".*$/\\1/'".format(iface)).read()
# aplist = result.splitlines() # aplist = result.splitlines()
if test == 0: if test == 0:
@@ -132,14 +146,38 @@ def scan_wifi(channel):
if vert > idxLen: if vert > idxLen:
vert = idxLen vert = idxLen
show_stt(channel) main_fun(channel)
def select_h(channel): def select_h(channel):
global apIndx
global start global start
global pwdLen
global horz global horz
global vert
if state == BTN2_PIN: if state == BTN2_PIN:
if apIndx < 0: #select ssid mode
if channel == JS_L_PIN or channel == JS_R_PIN: if channel == JS_L_PIN or channel == JS_R_PIN:
print "Selected: ", aplist[idxWin + vert - 1] #TEMP!!! start = time.time()
apIndx = idxWin + vert - 1
pwdLen = len(pwdLst)
if (pwdLen > 0):
horz = pwdLen - 1
else:
horz = 0
print "Selected: ", aplist[apIndx] #TEMP!!!
else: #input mode
if channel == JS_L_PIN:
start = time.time()
if horz > 0:
horz = horz - 1
else:
apIndx = -1 #Exit input mode
horz = 0
vert = 1
elif channel == JS_R_PIN:
start = time.time()
if horz < pwdLen:
horz = horz + 1
else: else:
if channel == JS_L_PIN: if channel == JS_L_PIN:
start = time.time() start = time.time()
@@ -157,6 +195,7 @@ def select_v(channel):
global start global start
global vert global vert
if state == BTN2_PIN: if state == BTN2_PIN:
if apIndx < 0: #select ssid mode
if channel == JS_U_PIN: if channel == JS_U_PIN:
start = time.time() start = time.time()
if vert > 1: if vert > 1:
@@ -171,6 +210,19 @@ def select_v(channel):
idxWin = idxWin + 1 idxWin = idxWin + 1
else: else:
pass pass
else: #input mode
if channel == JS_U_PIN:
start = time.time()
if vert > 0:
vert = vert - 1
else:
vert = 25
elif channel == JS_D_PIN:
start = time.time()
if vert < 25:
vert = vert + 1
else:
vert = 0
else: else:
if vert > 3: if vert > 3:
vert = 3 vert = 3
@@ -237,6 +289,7 @@ def draw_scn(channel):
draw.polygon([(x1,y2+6),(x2,y2-1),(x2,y2+4),(x3,y2+4),(x3,y2+8),(x2,y2+8),(x2,y2+13)], outline=255, fill=1) draw.polygon([(x1,y2+6),(x2,y2-1),(x2,y2+4),(x3,y2+4),(x3,y2+8),(x2,y2+8),(x2,y2+13)], outline=255, fill=1)
elif channel == BTN2_PIN: elif channel == BTN2_PIN:
if apIndx < 0: #select ssid mode
y2 = y1*vert y2 = y1*vert
if (idxWin >= 0) and (idxWin < idxLen): if (idxWin >= 0) and (idxWin < idxLen):
LINE1 = aplist[idxWin] LINE1 = aplist[idxWin]
@@ -257,6 +310,13 @@ def draw_scn(channel):
if thumb_1 > 60: if thumb_1 > 60:
thumb_1 = 60 thumb_1 = 60
draw.rectangle((125, thumb_0, 127, thumb_1), outline=255, fill=1) draw.rectangle((125, thumb_0, 127, thumb_1), outline=255, fill=1)
else: #input mode
LINE1 = "abc"
cursorx = 6 * horz
draw.text((122, y1) , choice1[vert], font=font, fill=255)
draw.text((122, y1*2), choice2[vert], font=font, fill=255)
draw.text((122, y1*3), choice3[vert], font=font, fill=255)
draw.rectangle((cursorx,y1+10,cursorx+4,y1+11), outline=255, fill=1)
elif channel == 995: elif channel == 995:
LINE2 = " Shutting down..." LINE2 = " Shutting down..."
@@ -288,7 +348,7 @@ def draw_scn(channel):
else: else:
draw.text((x0, y1*4), LINE4, font=font, fill=255) draw.text((x0, y1*4), LINE4, font=font, fill=255)
def show_stt(channel): def main_fun(channel):
global serial global serial
global device global device
global draw global draw
@@ -305,6 +365,7 @@ def show_stt(channel):
state = channel state = channel
start = time.time() start = time.time()
apIndx = -1
draw_scn(channel) draw_scn(channel)
else: else:
pass pass
@@ -312,6 +373,7 @@ def show_stt(channel):
if ((channel > 0) and (channel == state) and (channel != BTN2_PIN)) or ((stamp - start) > SCREEN_SAVER): # A button is pressed or timed out, turn off display if ((channel > 0) and (channel == state) and (channel != BTN2_PIN)) or ((stamp - start) > SCREEN_SAVER): # A button is pressed or timed out, turn off display
GPIO.output(RST_PIN,GPIO.LOW) GPIO.output(RST_PIN,GPIO.LOW)
state = 0 state = 0
apIndx = -1
elif (channel > 0) and (channel != state): elif (channel > 0) and (channel != state):
state = channel state = channel
start = time.time() start = time.time()
@@ -319,9 +381,9 @@ def show_stt(channel):
else: # state > 0 && channel == 0 && not-yet-timeout, refresh screen else: # state > 0 && channel == 0 && not-yet-timeout, refresh screen
draw_scn(state) draw_scn(state)
GPIO.add_event_detect(BTN1_PIN, GPIO.RISING, callback=show_stt, bouncetime=200) GPIO.add_event_detect(BTN1_PIN, GPIO.RISING, callback=main_fun, bouncetime=200)
GPIO.add_event_detect(BTN2_PIN, GPIO.RISING, callback=scan_wifi, bouncetime=200) GPIO.add_event_detect(BTN2_PIN, GPIO.RISING, callback=click_b2, bouncetime=200)
GPIO.add_event_detect(BTN3_PIN, GPIO.RISING, callback=take_act, bouncetime=200) GPIO.add_event_detect(BTN3_PIN, GPIO.RISING, callback=click_b3, bouncetime=200)
GPIO.add_event_detect(JS_L_PIN, GPIO.RISING, callback=select_h, bouncetime=200) GPIO.add_event_detect(JS_L_PIN, GPIO.RISING, callback=select_h, bouncetime=200)
GPIO.add_event_detect(JS_R_PIN, GPIO.RISING, callback=select_h, bouncetime=200) GPIO.add_event_detect(JS_R_PIN, GPIO.RISING, callback=select_h, bouncetime=200)
GPIO.add_event_detect(JS_U_PIN, GPIO.RISING, callback=select_v, bouncetime=200) GPIO.add_event_detect(JS_U_PIN, GPIO.RISING, callback=select_v, bouncetime=200)
@@ -331,13 +393,11 @@ iface = subprocess.check_output("iwgetid | awk '{print $1}'", shell = True).rstr
# Main Loop # Main Loop
try: try:
show_stt(BTN1_PIN) main_fun(BTN1_PIN)
while True: while True:
stamp = time.time() stamp = time.time()
show_stt(0) main_fun(0)
time.sleep(1) time.sleep(1)
except: except:
print "Stopped", sys.exc_info()[0] print "Stopped", sys.exc_i
raise
GPIO.cleanup()