Last active
May 2, 2020 19:06
-
-
Save vitouXY/194a25ea5a768bacc736df4ba2a64ff0 to your computer and use it in GitHub Desktop.
0.0.1-04.27.2020-raw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding:utf-8 -*- | |
# vim: set fileencoding=utf-8 : | |
""" | |
piMenu | |
1.3" OLED Display HAT for Raspberry Pi by Waveshare | |
128x64 | |
Driver: SH1106 | |
Interface: SPI | |
NOTES: | |
* Error with : * | |
* and more unknown errors... | |
(C) 2020 All Rights Not Reserved - This program comes with ABSOLUTELY NO | |
WARRANTY. This is free software, and you are welcome to redistribute | |
it under the GNU GPL Version 3 (or later) License. | |
""" | |
from __future__ import print_function, unicode_literals | |
__author__ = "vitouXY" | |
__copyright__ = "Copyright 2020, All Rights Not Reserved" | |
__credits__ = ["vitouXY"] | |
__license__ = "GPL" | |
__version__ = "0.0.1" | |
__maintainer__ = "vitouXY" | |
__email__ = "nothave@mail." | |
__status__ = "--" | |
__rev__="05.02.2020-raw" | |
#| Imports | |
import sys | |
import os | |
import RPi.GPIO as GPIO | |
from luma.core.interface.serial import spi, i2c | |
from luma.core.render import canvas | |
from luma.core import lib | |
from luma.oled.device import sh1106 | |
from PIL import Image | |
from PIL import ImageDraw | |
from PIL import ImageFont | |
from subprocess import * | |
import subprocess | |
from collections import OrderedDict | |
import signal | |
import threading | |
import logging | |
import time | |
from datetime import datetime | |
import platform | |
import os.path | |
import usb.core | |
#try: | |
# from monicommon import myFunc | |
# from monicommon import myEntry | |
#except ImportError: | |
# sys.exit("The monicommon library was not found.") | |
try: | |
import psutil | |
except ImportError: | |
sys.exit("The psutil library was not found. Run 'sudo -H pip install psutil' to install it.") | |
if sys.version_info < (3, 0): | |
sys.exit("{}: Currently, this program runs only under python3.".format(sys.version_info[0])) | |
if os.geteuid() != 0: | |
sys.exit('{}: Operation not permitted'.format(os.geteuid())) | |
if os.name != 'posix': | |
sys.exit('{}: Platform not supported'.format(os.name)) | |
#| Global variables | |
VENDOR = 0x12d1 | |
PRODUCT = 0x14db | |
WIDTH = 128 | |
HEIGTH = 64 | |
FONT = ImageFont.load_default() # 4x6 | |
LINE_MENU_MAX = 5 | |
# 0 1 2 3 4 5 6 | |
# 4x6 05 16 27 38 49 -- -- -- 4L | |
# 4x6 00 12 22 32 42 52 -- -- 5L | |
# 00 08 16 24 32 40 48 56 7L +8 | |
LINE_MENU_POS_X = 2 | |
LINE_MENU_POS_Y = {0:11,1:21,2:31,3:41,4:51} # 5 | |
try: # ./fonts/FreePixel.ttf ## ), "fonts", "FreePixel.ttf" )), 9) | |
FONT_ = ImageFont.truetype(os.path.abspath(os.path.join(os.path.dirname(__file__), | |
"FreePixel.ttf" )), 9) | |
except: | |
FONT_ = ImageFont.load_default() | |
THREAD_Timeout = 30 | |
global mainTHREAD_Kill | |
CURR_SELECTED = 0 | |
CURR_TOP = 0 | |
STATE = 1 | |
mainTHREAD_Kill = 0 | |
MENU_STATE = 0 | |
ENTRIES = {} | |
ENTRIES_backMenu = {} | |
ENTRIES_menuLevel = 0 | |
ENTRIES_Lst = {} | |
TEXT_Lst = [] | |
ENTRIES_backMenu[ENTRIES_menuLevel] = 0 | |
# GPIO define | |
RST_PIN = 25 | |
CS_PIN = 8 | |
DC_PIN = 24 | |
# Buttons | |
key = {} | |
key['key1'] = 21 | |
key['key2'] = 20 | |
key['key3'] = 16 | |
key['left'] = 5 | |
key['up'] = 6 | |
key['down'] = 19 | |
key['right'] = 26 | |
key['press'] = 13 | |
# init GPIO | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(key['key1'], GPIO.IN, pull_up_down=GPIO.PUD_UP) | |
GPIO.setup(key['key2'], GPIO.IN, pull_up_down=GPIO.PUD_UP) | |
GPIO.setup(key['key3'], GPIO.IN, pull_up_down=GPIO.PUD_UP) | |
GPIO.setup(key['left'], GPIO.IN, pull_up_down=GPIO.PUD_UP) | |
GPIO.setup(key['up'], GPIO.IN, pull_up_down=GPIO.PUD_UP) | |
GPIO.setup(key['press'], GPIO.IN, pull_up_down=GPIO.PUD_UP) | |
GPIO.setup(key['down'], GPIO.IN, pull_up_down=GPIO.PUD_UP) | |
GPIO.setup(key['right'], GPIO.IN, pull_up_down=GPIO.PUD_UP) | |
serial = spi(device = 0, | |
port = 0, | |
bus_speed_hz = 8000000, | |
transfer_size = 4096, | |
gpio_DC = DC_PIN, | |
gpio_RST = RST_PIN | |
) | |
device = sh1106(serial, rotate = 2) | |
# Initialize the display... | |
# draw = ImageDraw.Draw(Image.new('1', (WIDTH,HEIGTH))) | |
# draw.rectangle((0,0,WIDTH,HEIGTH), outline=0, fill=0) | |
# device.contrast(255) # device.hide() | |
# device.show() | |
# device.clear() | |
#| Class declarations | |
#| Function declarations | |
#' '' | |
def keyboardInterruptHandler(signal, frame): | |
global mainTHREAD_Kill | |
mainTHREAD_Kill = 1 | |
time.sleep(2) | |
#GPIO.output(RST_PIN,GPIO.LOW) | |
GPIO.cleanup() | |
#logging.warning('--KeyboardInterrupt! 0') | |
sys.exit(" KeyboardInterrupt (ID: {}) has been caught. Cleaning up...".format(signal)) | |
signal.signal(signal.SIGINT, keyboardInterruptHandler) | |
#' '' | |
def Test(arg): | |
print(' < ! > Im Test!!! < ! > ', arg) | |
ENTRIES = { | |
0:{ | |
'Menu 1':{ | |
1:{ | |
'Sub 1 Menu 1':{ | |
2:{ | |
'Sub 2 Menu 1':{ | |
3:{ | |
Test | |
} | |
}, | |
'Sub 2 Mwnu 2':{ | |
3:{ | |
'Sub 3 Menu 1':{ | |
4:{ | |
Test | |
} | |
}, | |
'Sub 3 Menu 2':{ | |
4:{ | |
Test | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
}, | |
'Menu 2':{ | |
1:{ | |
Test | |
} | |
} | |
} | |
} | |
ENTRIES[0]['Menu 3'] = {1:{Test}} | |
######################################################################## | |
logging.info(os.getpid(), platform.system()) | |
def drawText(text=[]): | |
x = 8 | |
count = 0 | |
max_count = 7 | |
if len(text) < max_count: | |
max_count = len(text) | |
#device.clear() | |
time.sleep(0.01) | |
with canvas(device) as draw: | |
while count < max_count: | |
draw.text((1,x*count), str(text[count]), 'white') | |
count += 1 | |
def showSplash(): | |
device.contrast(255) | |
logging.debug('--showSplash!') | |
drawText([ | |
'', | |
'', | |
'PiMenu', | |
__version__+'-'+__rev__ | |
]) | |
time.sleep(3) | |
device.contrast(100) | |
drawText([ | |
'Raspberry Pi Zero W', | |
'', | |
'128x64 OLED Hat', | |
'Drive: sh1106', | |
'Interface: SPI', | |
'', | |
'GNU/Linux & Python3' | |
]) | |
time.sleep(1) | |
device.contrast(50) | |
def ScreenSaver(): | |
global STATE | |
logging.debug('--ScreenSaver!') | |
time.sleep(0.01) | |
if STATE == 1: | |
STATE = 0 | |
device.hide() | |
#GPIO.output(RST_PIN,GPIO.LOW) | |
else: | |
STATE = 1 | |
device.show() | |
def showMenu(): | |
logging.debug('--showMenu!') | |
countS = 0.00 | |
while True: | |
time.sleep(0.01) | |
if MENU_STATE == 1: | |
getMenu(1) | |
with canvas(device) as draw: | |
draw.rectangle((0,0,127,63), outline='white', fill='black') | |
draw.rectangle((0,0,128,10), outline='white', fill='white') | |
draw.text((LINE_MENU_POS_X,0), clock(), 'black') | |
# StatusBox (from Right to Left) | |
# 1 | |
if IsConnected() == 1: draw.rectangle((123,2,126,8), outline='black', fill='black') | |
else: draw.rectangle((123,2,126,8), outline='black', fill='white') | |
# 2 | |
#if 0 == 1: draw.rectangle((118,2,121,8), outline='black', fill='black') | |
#else: draw.rectangle((118,2,121,8), outline='black', fill='white') | |
# 3 | |
#if 0 == 1: draw.rectangle((113,2,116,8), outline='black', fill='black') | |
#else: draw.rectangle((113,2,116,8), outline='black', fill='white') | |
draw.rectangle((LINE_MENU_POS_X, (CURR_SELECTED - CURR_TOP)*10+12, | |
128-(LINE_MENU_POS_X+1),((CURR_SELECTED - CURR_TOP)*10)+10+12), | |
outline='white', fill='white') | |
for ind, y in LINE_MENU_POS_Y.items(): | |
try: | |
numb = CURR_TOP + ind | |
text = ENTRIES_menuName[numb] | |
if text.lower().find('_separator_'.lower()) >= 0: | |
text = ' ' | |
elif (CURR_SELECTED == numb) and (draw.textsize(text, font=FONT)[0] > 125): | |
lm = int(125/draw.textsize('M', font=FONT)[0]) | |
countSS = int(countS) | |
try: | |
text = text+' ' | |
li = 0+countSS | |
le = lm+countSS | |
if (len(text)-li-1 < lm) or (le+1 > len(text)): | |
countS = 0.00 | |
elif countSS == 0: countS += 0.02 | |
else: countS += 0.05 | |
text = text[li:le] | |
except: | |
countS = 0.00 | |
text = text | |
else: countS = 0.00 | |
#if CURR_SELECTED == numb: text = '>'+text | |
textColor = ['white']*len(ENTRIES_menuName) | |
textColor[CURR_SELECTED] = 'black' | |
draw.text((LINE_MENU_POS_X+1,y), text, textColor[ind+CURR_TOP]) | |
# .__1__2__ x | |
# | | |
# 1 #### | |
# | # # | |
# 2 #### | |
# | | |
# y | |
# x1 y1 x2 y2 | |
draw.rectangle((126,(ind+1)*10+7, 128,((ind+1)*10)+9), outline='white', fill='white') | |
draw.rectangle((0,(ind+1)*10+7, 1,((ind+1)*10)+9), outline='white', fill='white') | |
except: | |
pass | |
elif (MENU_STATE == 2) or (MENU_STATE == 3): | |
with canvas(device) as draw: | |
x = 8 | |
max_count = 7 | |
ttlist = TEXT_Lst | |
if len(ttlist) < max_count: | |
max_count = len(ttlist) | |
#device.clear() | |
time.sleep(0.01) | |
for count in range(0,max_count): | |
draw.text((0,x*count), str(ttlist[count]), font=FONT_, fill='white') | |
if mainTHREAD_Kill == 1: break | |
def getMenu(menu=1, data=TEXT_Lst): | |
global ENTRIES_menuLevel | |
global ENTRIES_backMenu | |
global ENTRIES_Lst | |
global ENTRIES_menuName | |
global MENU_STATE | |
global TEXT_Lst | |
TEXT_Lst = [] | |
if (menu == 2) and (str(data.__class__.__name__).lower() == 'list'.lower()): | |
TEXT_Lst = data | |
MENU_STATE = 2 | |
else: | |
ENTRIES_menuName = {} | |
count = 0 | |
for menun, menud in ENTRIES_Lst[ENTRIES_menuLevel].items(): | |
if str(menud.__class__.__name__).lower() == 'dict'.lower(): | |
ENTRIES_menuName[count] = menun | |
count += 1 | |
MENU_STATE = 1 | |
def detectDown(any=''): | |
global CURR_SELECTED | |
global CURR_TOP | |
global ENTRIES_Lst | |
global ENTRIES_menuLevel | |
global ENTRIES_backMenu | |
global MENU_STATE | |
if (STATE == 1) and (MENU_STATE == 1): | |
if CURR_SELECTED + 1 >= len(ENTRIES_menuName): | |
CURR_SELECTED = 0 | |
CURR_TOP = 0 | |
else: | |
CURR_SELECTED = CURR_SELECTED + 1 | |
if CURR_SELECTED > CURR_TOP + LINE_MENU_MAX-1: | |
CURR_TOP = CURR_TOP + 1 | |
getMenu(1) | |
elif (STATE == 1) and (MENU_STATE == 2): | |
True | |
def detectUp(any=''): | |
global CURR_SELECTED | |
global CURR_TOP | |
global ENTRIES_Lst | |
global ENTRIES_menuLevel | |
global ENTRIES_backMenu | |
global MENU_STATE | |
if (STATE == 1) and (MENU_STATE == 1): | |
if CURR_SELECTED - 1 < 0: | |
CURR_SELECTED = len(ENTRIES_menuName) - 1 | |
if len(ENTRIES_menuName) >= LINE_MENU_MAX+1: | |
CURR_TOP = len(ENTRIES_menuName) - LINE_MENU_MAX | |
else: | |
CURR_SELECTED = CURR_SELECTED - 1 | |
if CURR_SELECTED < CURR_TOP: | |
CURR_TOP = CURR_TOP - 1 | |
getMenu(1) | |
elif (STATE == 1) and (MENU_STATE == 2): | |
True | |
def detectRight(any=''): | |
global CURR_SELECTED | |
global CURR_TOP | |
global ENTRIES_Lst | |
global ENTRIES_menuLevel | |
global ENTRIES_backMenu | |
global MENU_STATE | |
global THREAD_Lock | |
global THREAD_Kill | |
if (STATE == 1) and (MENU_STATE == 1): | |
for menun, menud in ENTRIES_Lst[ENTRIES_menuLevel].items(): | |
if menun.lower().find('_separator_'.lower()) >= 0: | |
getMenu(1) | |
elif menun == ENTRIES_menuName[CURR_SELECTED]: | |
for ind, new_value in menud.items(): | |
if str(new_value).lower() == '{}': | |
getMenu(1) | |
elif str(new_value.__class__.__name__).lower() == 'dict'.lower(): | |
ENTRIES_backMenu[ENTRIES_menuLevel] = CURR_SELECTED | |
ENTRIES_menuLevel = ENTRIES_menuLevel + 1 | |
CURR_SELECTED = 0 | |
CURR_TOP = 0 | |
ENTRIES_Lst[ENTRIES_menuLevel] = new_value | |
getMenu(1) | |
elif str(new_value.__class__.__name__).lower() == 'set'.lower(): | |
THREAD_Lock = 1 | |
getMenu(2,['Wait...']) | |
for func in new_value: | |
if str(func.__class__.__name__).lower() == 'function'.lower(): | |
device.contrast(250) | |
THREAD_Lock = 1 | |
THREAD_Kill = False | |
#funcThread = threading.Thread(target=func, args=(lambda:THREAD_Kill,), daemon=True) | |
funcThread = threading.Thread(target=func, args=(str(menun),), daemon=True) | |
funcThread.start() | |
count_kill = 0 | |
while funcThread.isAlive(): | |
THREAD_Lock = 1 | |
time.sleep(1) | |
if count_kill > THREAD_Timeout: | |
THREAD_Kill = True | |
count_kill += 1 | |
device.contrast(5) | |
THREAD_Lock = 0 | |
myFuncEntry.get() | |
elif (STATE == 1) and (MENU_STATE == 2): | |
True | |
def detectLeft(any=''): | |
global CURR_SELECTED | |
global CURR_TOP | |
global ENTRIES_Lst | |
global ENTRIES_menuLevel | |
global ENTRIES_backMenu | |
global MENU_STATE | |
global THREAD_Kill | |
if (STATE == 1) and (MENU_STATE == 1): | |
if ENTRIES_menuLevel > 0: | |
ENTRIES_menuLevel = ENTRIES_menuLevel - 1 | |
CURR_SELECTED = ENTRIES_backMenu[ENTRIES_menuLevel] | |
CURR_TOP = ENTRIES_backMenu[ENTRIES_menuLevel] | |
if (CURR_SELECTED == len(ENTRIES_Lst[ENTRIES_menuLevel].keys()) - 1): | |
#print('a1') | |
if (len(ENTRIES_Lst[ENTRIES_menuLevel].keys()) >= LINE_MENU_MAX+1): | |
#print('a2') | |
CURR_TOP = len(ENTRIES_Lst[ENTRIES_menuLevel].keys()) - LINE_MENU_MAX | |
elif (len(ENTRIES_Lst[ENTRIES_menuLevel].keys()) < LINE_MENU_MAX+1): | |
#print('a3') | |
CURR_TOP = 0 | |
elif (CURR_SELECTED <= len(ENTRIES_Lst[ENTRIES_menuLevel].keys()) - 1): | |
#print('b1') | |
if (CURR_SELECTED < LINE_MENU_MAX) or (len(ENTRIES_Lst[ENTRIES_menuLevel].keys()) < LINE_MENU_MAX+1): | |
#print('b2') | |
CURR_TOP = 0 | |
elif (len(ENTRIES_Lst[ENTRIES_menuLevel].keys()) >= LINE_MENU_MAX+1): | |
#print('b3') | |
CURR_TOP = len(ENTRIES_Lst[ENTRIES_menuLevel].keys()) - LINE_MENU_MAX | |
myFuncEntry.get() | |
getMenu(1) | |
elif (STATE == 1) and (MENU_STATE == 2): | |
THREAD_Kill = True | |
if THREAD_Lock == 0: | |
MENU_STATE = 1 | |
getMenu(1) | |
def detectPress(any=''): | |
global CURR_SELECTED | |
global CURR_TOP | |
global ENTRIES_Lst | |
global ENTRIES_menuLevel | |
global ENTRIES_backMenu | |
global MENU_STATE | |
if (STATE == 1) and (MENU_STATE == 1): | |
getMenu(1) | |
elif (STATE == 1) and (MENU_STATE == 2): | |
True | |
def detectKey1(any=''): | |
global CURR_SELECTED | |
global CURR_TOP | |
global ENTRIES_Lst | |
global ENTRIES_menuLevel | |
global ENTRIES_backMenu | |
global MENU_STATE | |
device.contrast(255) | |
if (STATE == 1) and (MENU_STATE == 1): | |
getMenu(1) | |
elif (STATE == 1) and (MENU_STATE == 2): | |
True | |
def detectKey2(any=''): | |
global CURR_SELECTED | |
global CURR_TOP | |
global ENTRIES_Lst | |
global ENTRIES_menuLevel | |
global ENTRIES_backMenu | |
global MENU_STATE | |
device.contrast(5) | |
if (STATE == 1) and (MENU_STATE == 1): | |
getMenu(1) | |
elif (STATE == 1) and (MENU_STATE == 2): | |
True | |
def detectKey3(any=''): | |
global CURR_SELECTED | |
global CURR_TOP | |
global ENTRIES_Lst | |
global ENTRIES_menuLevel | |
global ENTRIES_backMenu | |
global MENU_STATE | |
ScreenSaver() | |
if MENU_STATE == 1: | |
getMenu(1) | |
elif (STATE == 1) and (MENU_STATE == 2): | |
True | |
######################################################################## | |
######################################################################## | |
########## USER CONFIG | |
######################################################################## | |
######################################################################## | |
# > /dev/null 2>&1 < /dev/null | |
def RunCmdAu(cmd): | |
p = Popen(cmd, shell=True, stdout=PIPE) | |
output = p.communicate()[0] | |
output = str(output, 'utf-8').split('\n')[0] | |
return output | |
def RunCmdA(cmd): | |
p = Popen(cmd, shell=True, stdout=PIPE) | |
output = p.communicate()[0] | |
output = str(output).split('\n')[0] | |
return output | |
def RunCmdBu(cmd): | |
output = subprocess.check_output(cmd, shell= True) | |
output = str(output, 'utf-8') | |
return output | |
def RunCmdB(cmd): | |
output = subprocess.check_output(cmd, shell= True) | |
output = str(output) | |
return output | |
def IsConnected(): | |
return usb.core.find(idVendor=VENDOR, idProduct=PRODUCT) is not None | |
def entry_Test(): | |
global ENTRIES | |
for x in range(0,3): | |
ENTRIES[0]['Run Test '+ x ] = {1:{myFunc.Test2}} | |
ENTRIES[0]['Menu 6'] = {1:{myFunc.Test2}} | |
# overwrite | |
ENTRIES[0]['Menu 6'][1] = {myFunc.Test} | |
ENTRIES[0]['Menu 6'][2] = {myFunc.Test2} | |
def clock(): | |
#"date +\"%Y-%m-%d %H:%M:%S\"" "date +%R" | |
now = datetime.now() | |
today_date = now.strftime("%d-%b-%Y") | |
today_time = now.strftime("%H:%M:%S") | |
return "%s %s" % (today_date, today_time) | |
#return "%s %s" % (today_time) | |
def Services_Update(): | |
# ENTRIES[0]['System'] = {1:{}} # add, clear all | |
#ENTRIES[0]['System'][1]['_separator_01'] ={999:{}} | |
ENTRIES[0]['System'][1]['Services'] = { | |
2:{ | |
'Start':{3:{}}, | |
'Stop':{3:{}}, | |
'Enable':{3:{}}, | |
'Disable':{3:{}} | |
} | |
} | |
services = [] | |
services = ['wpa_supplicant','wpa_supplicant','psiphon@a','psiphon@b','psiphon@c','haproxy','dhcpcd','bluetooth'] | |
services = list(dict.fromkeys(services)) | |
if len(services) > 0: | |
for ind in range(0,len(services)): | |
service = str(services[ind]).replace('.service','') | |
ENTRIES[0]['System'][1]['Services'][2]['Start'][3][service] = { | |
4:{myFunc.Service_Start_} | |
} | |
ENTRIES[0]['System'][1]['Services'][2]['Stop'][3][service] = { | |
4:{myFunc.Service_Stop_} | |
} | |
ENTRIES[0]['System'][1]['Services'][2]['Enable'][3][service] = { | |
4:{myFunc.Service_Enable_} | |
} | |
ENTRIES[0]['System'][1]['Services'][2]['Disable'][3][service] = { | |
4:{myFunc.Service_Disable_} | |
} | |
def Network_Update(): | |
global ENTRIES | |
ENTRIES[0]['Network'] = {1:{}} # add, clear all | |
#ENTRIES[0]['Network'][1] = {} | |
ENTRIES[0]['Network'][1]['Bring Up'] = {2:{}} | |
ENTRIES[0]['Network'][1]['Bring Down'] = {2:{}} | |
stats = psutil.net_if_stats() | |
available_networks = [] | |
up_available_networks = [] | |
down_available_networks = [] | |
for intface, addr_list in stats.items(): | |
available_networks.append(intface) | |
down_available_networks.append(intface) | |
if intface in stats and getattr(stats[intface], "isup"): | |
up_available_networks.append(intface) | |
down_available_networks.remove(intface) | |
if len(up_available_networks) > 0: | |
for ind in range(0,len(up_available_networks)): | |
iface = str(up_available_networks[ind]) | |
ENTRIES[0]['Network'][1]['Bring Down'][2][iface] = { | |
3:{myFunc.Iface_Down_} | |
} | |
if len(down_available_networks) > 0: | |
for ind in range(0,len(down_available_networks)): | |
iface = str(down_available_networks[ind]) | |
ENTRIES[0]['Network'][1]['Bring Up'][2][iface] = { | |
3:{myFunc.Iface_Up_} | |
} | |
if len(available_networks) > 0: | |
for ind in range(0,len(available_networks)): | |
iface = str(available_networks[ind]) | |
if iface.find('wlan') < 0: | |
continue | |
ENTRIES[0]['System'][1]['Services'][2]['Start'][3]['wpa_supplicant@'+iface] = { | |
4:{myFunc.Service_Start_} | |
} | |
ENTRIES[0]['System'][1]['Services'][2]['Stop'][3]['wpa_supplicant@'+iface] = { | |
4:{myFunc.Service_Stop_} | |
} | |
ENTRIES[0]['System'][1]['Services'][2]['Enable'][3]['wpa_supplicant@'+iface] = { | |
4:{myFunc.Service_Enable_} | |
} | |
ENTRIES[0]['System'][1]['Services'][2]['Disable'][3]['wpa_supplicant@'+iface] = { | |
4:{myFunc.Service_Disable_} | |
} | |
########## USER FUNCTIONS FOR ENTRIES | |
class myFunc: | |
def Test(arg): | |
while True: | |
time.sleep(0.01) | |
if THREAD_Kill:break | |
getMenu(2,['Line Text 01/07']) | |
time.sleep(0.2) | |
getMenu(2,['','Line Text 02/07']) | |
time.sleep(0.2) | |
getMenu(2,['','','Line Text 03/07']) | |
time.sleep(0.2) | |
getMenu(2,['','','','Line Text 04/07']) | |
time.sleep(0.2) | |
getMenu(2,['','','','','Line Text 05/07']) | |
time.sleep(0.2) | |
getMenu(2,['','','','','','Line Text 06/07']) | |
time.sleep(0.2) | |
getMenu(2,['','','','','','','Line Text 07/07']) | |
time.sleep(0.2) | |
def Test2(arg): | |
while True: | |
time.sleep(0.01) | |
if THREAD_Kill:break | |
getMenu(2,[arg,RunCmdAu('date +%r')]) | |
def Halt(arg): | |
getMenu(2,['','Halt...']) | |
RunCmdA('halt --halt') | |
def Halt_Force(arg): | |
getMenu(2,['','Halt... (Force)']) | |
RunCmdA('halt --halt --force') | |
def Reboot(arg): | |
getMenu(2,['','Reboot...']) | |
RunCmdA('halt --reboot') | |
def Reboot_Force(arg): | |
getMenu(2,['','Reboot... (Force)']) | |
RunCmdA('halt --reboot --force') | |
def Service_Start_(arg): | |
cmd = 'start' | |
RunCmdA('systemctl '+ cmd +' '+arg+'.service') | |
getMenu(2,['['+arg+']('+cmd+')',' Done!']) | |
def Service_Stop_(arg): | |
cmd = 'stop' | |
RunCmdA('systemctl '+ cmd +' '+arg+'.service') | |
getMenu(2,['['+arg+']('+cmd+')',' Done!']) | |
def Service_Enable_(arg): | |
cmd = 'enable' | |
RunCmdA('systemctl '+ cmd +' '+arg+'.service') | |
getMenu(2,['['+arg+']('+cmd+')',' Done!']) | |
def Service_Disable_(arg): | |
cmd = 'disable' | |
RunCmdA('systemctl '+ cmd +' '+arg+'.service') | |
getMenu(2,['['+arg+']('+cmd+')',' Done!']) | |
def Iface_Up_(arg): | |
cmd = 'up' | |
RunCmdA('ip link set dev '+ arg +' '+cmd) | |
getMenu(2,['['+arg+']('+cmd+')',' Done!']) | |
def Iface_Down_(arg): | |
cmd = 'down' | |
RunCmdA('ip link set dev '+ arg +' '+cmd) | |
getMenu(2,['['+arg+']('+cmd+')',' Done!']) | |
########## FUNCTION THAT GENERATES ENTRIES - POST LOAD '_ENTRIES' | |
class myFuncEntry: | |
def get(): | |
global ENTRIES | |
#ENTRIES[0]['Test X'] = {1:{myFunc.Test2}} | |
try: | |
#entry_Test() | |
Services_Update() | |
Network_Update() | |
except: pass | |
########## USER ENTRIES | |
class myEntry: | |
_ENTRIES = {} | |
def get(): | |
#global _ENTRIES | |
# Don't repeat Name at the same Level. | |
# Funtions without (). Example: 'myFunc.Test' for 'myFunc.Test()' | |
# level name -> level name/function -> level name/function -> ... | |
_ENTRIES = { | |
0: | |
{ | |
'System': | |
{ | |
1: | |
{ | |
'Halt':{2:{'Normal':{3:{myFunc.Halt}},'Force':{3:{myFunc.Halt_Force}}}}, | |
'Reboot':{2:{'Normal':{3:{myFunc.Reboot}},'Force':{3:{myFunc.Reboot_Force}}}} | |
} | |
} | |
} | |
} | |
# | |
#_ENTRIES[0]['TestX'] = {1:{myFunc.Test2}} | |
# | |
return _ENTRIES | |
######################################################################## | |
######################################################################## | |
######################################################################## | |
# def detectKey1(event): | |
GPIO.add_event_detect(key['key1'], GPIO.RISING, callback=detectKey1, bouncetime=200) | |
GPIO.add_event_detect(key['key2'], GPIO.RISING, callback=detectKey2, bouncetime=200) | |
GPIO.add_event_detect(key['key3'], GPIO.RISING, callback=detectKey3, bouncetime=200) | |
GPIO.add_event_detect(key['left'], GPIO.RISING, callback=detectLeft, bouncetime=200) | |
GPIO.add_event_detect(key['up'], GPIO.RISING, callback=detectUp, bouncetime=200) | |
GPIO.add_event_detect(key['press'], GPIO.RISING, callback=detectPress, bouncetime=200) | |
GPIO.add_event_detect(key['down'], GPIO.RISING, callback=detectDown, bouncetime=200) | |
GPIO.add_event_detect(key['right'], GPIO.RISING, callback=detectRight, bouncetime=200) | |
# if GPIO.event_detected(key['key1']): | |
#GPIO.add_event_detect(key['key1'], GPIO.FALLING, bouncetime=300) | |
#GPIO.add_event_detect(key['key2'], GPIO.FALLING, bouncetime=300) | |
#GPIO.add_event_detect(key['key3'], GPIO.FALLING, bouncetime=300) | |
#GPIO.add_event_detect(key['left'], GPIO.FALLING, bouncetime=300) | |
#GPIO.add_event_detect(key['up'], GPIO.FALLING, bouncetime=300) | |
#GPIO.add_event_detect(key['press'], GPIO.FALLING, bouncetime=300) | |
#GPIO.add_event_detect(key['down'], GPIO.FALLING, bouncetime=300) | |
#GPIO.add_event_detect(key['right'], GPIO.FALLING, bouncetime=300) | |
# GPIO.wait_for_edge(key['key1'], GPIO.FALLING) | |
threadN02 = '' | |
def main(): | |
global MENU_STATE | |
global ENTRIES | |
global ENTRIES_Lst | |
global mainTHREAD_Kill | |
#args = sys.argv[1:] | |
#if not args: | |
# print('usage: [--flags options] [inputs] ') | |
# sys.exit(1) | |
showSplash() | |
######################################################################## | |
try: | |
ENTRIES = myEntry.get() | |
myFuncEntry.get() | |
except: | |
logging.error('--Err! Entries!') | |
pass | |
######################################################################## | |
ENTRIES_Lst[ENTRIES_menuLevel] = ENTRIES[ENTRIES_menuLevel] | |
MENU_STATE = 1 | |
try: | |
threadN02 = threading.Thread(name='showMenu', target=showMenu) | |
threadN02.start() | |
#threadN03 = threading.Thread(name='xMenu', target=xMenu, daemon=True) | |
#threadN03.start() | |
signal.pause() | |
except (KeyboardInterrupt, SystemExit): | |
#logging.warning('--KeyboardInterrupt!') | |
mainTHREAD_Kill = 1 | |
#time.sleep(2) | |
sys.exit(' Bye!') | |
#except: # comment for output | |
# logging.error('--Uups!') | |
# sys.exit(' Uups!') | |
#| Main body | |
if __name__ == '__main__': | |
try: | |
main() | |
except (KeyboardInterrupt, SystemExit): | |
mainTHREAD_Kill = 1 | |
logging.warning('--KeyboardInterrupt!') | |
#time.sleep(2) | |
sys.exit(' Bye!') | |
except: # comment for output | |
mainTHREAD_Kill = 1 | |
logging.error('--Oops!') | |
time.sleep(2) | |
sys.exit(' Oops!') | |
mainTHREAD_Kill = 1 | |
time.sleep(2) | |
GPIO.cleanup() | |
sys.exit(2) | |
os.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment