working on menus

This commit is contained in:
dekunukem
2021-12-19 22:32:38 +00:00
parent bb7b532c6b
commit c4954474c6
5 changed files with 252 additions and 129 deletions

View File

@@ -1,3 +1,37 @@
class usb4vc_level(object):
def __init__(self):
super(usb4vc_level, self).__init__()
self.max_pages = 3
self.current_page = 0
# while 1:
# with oled_canvas as ocan:
# ocan.rectangle((0, 0, device.width-1, device.height-1), outline=0, fill=0)
# oled_print_centered(f"{int(time.time())}", font_large, 0, ocan)
# time.sleep(1)
print(self.current_level, self.current_page)
# while 1:
# with canvas(device) as draw:
# # draw.rectangle(device.bounding_box, outline="white", fill="black")
# # regular font test
# draw.text((0, 0), "123456789012345678901234567890", font=font_regular, fill="white")
# draw.text((0, 10), "ABCDEFGHIJ", font=font_regular, fill="white")
# draw.text((0, 20), "abcdefghij", font=font_regular, fill="white")
# # medium font test
# # draw.text((0, 0), "123456789012345678901234567890", font=font_medium, fill="white")
# # draw.text((0, 15), "123456789012345678901234567890", font=font_medium, fill="white")
# # large font test
# # draw.text((0, 0), "123456789012345678901234567890", font=font_large, fill="white")
# # draw.text((0, 20), "123456789012345678901234567890", font=font_regular, fill="white")
# time.sleep(1)
# ------------
# print(gp_status_dict[gp_id], axes_info)
# for key in gp_status_dict[gp_id]:

View File

@@ -6,7 +6,7 @@ import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
import usb4vc_usb_scan
import usb4vc_oled
import usb4vc_ui
RPI_APP_VERSION_TUPLE = (0, 0, 1)
PBOARD_RESET_PIN = 25
@@ -25,16 +25,16 @@ GPIO.setup(PBOARD_RESET_PIN, GPIO.IN)
GPIO.setup(PBOARD_DFU_PIN, GPIO.OUT)
GPIO.output(PBOARD_DFU_PIN, GPIO.LOW)
usb4vc_oled.print_welcome_screen(RPI_APP_VERSION_TUPLE)
# usb4vc_ui.print_welcome_screen(RPI_APP_VERSION_TUPLE)
reset_pboard()
# usb4vc_usb_scan.set_protocol()
usb4vc_usb_scan.get_pboard_info()
# time.sleep(5)
# usb4vc_oled.oled_clear()
# usb4vc_ui.oled_clear()
# usb4vc_oled.oled_queue_worker.start()
usb4vc_ui.ui_worker.start()
usb4vc_usb_scan.usb_device_scan_thread.start()
usb4vc_usb_scan.raw_input_event_parser_thread.start()

View File

@@ -1,113 +0,0 @@
# https://luma-oled.readthedocs.io/en/latest/software.html
import sys
import time
import queue
import threading
from demo_opts import get_device
from luma.core.render import canvas
from PIL import ImageFont
oled_display_queue = queue.PriorityQueue()
"""
oled display object:
text
size
font
x, y
duration
afterwards
"""
OLED_WIDTH = 128
OLED_HEIGHT = 32
my_arg = ['--display', 'ssd1306', '--interface', 'spi', '--spi-port', '0', '--spi-device', '1', '--gpio-reset', '6', '--gpio-data-command', '5', '--width', str(OLED_WIDTH), '--height', str(OLED_HEIGHT), '--spi-bus-speed', '2000000']
device = get_device(my_arg)
"""
OLED for USB4VC
128*32
3 font sizes available, regular/large: ProggyTiny.ttf, medium: ChiKareGo2.ttf.
regular ProggyTiny:
16 point, 3 lines, y=0, 10, 20
large type ProggyTiny:
32 point, 2 lines (1 large + 1 regular), y=0, 20
medium type ChiKareGo2:
16 point, 2 lines, y=0, 15
characters per line:
regular font: 21
medium font: 16
large font: 11
int(sys.argv[1])
$ sh sync.sh; ssh -t pi@192.168.1.56 "pkill python3;cd ~/usb4vc;python3 usb4vc_oled.py"
"""
font_regular = ImageFont.truetype("ProggyTiny.ttf", 16)
font_medium = ImageFont.truetype("ChiKareGo2.ttf", 16)
font_large = ImageFont.truetype("ProggyTiny.ttf", 32)
max_char_per_line = {font_regular:21, font_medium:16, font_large:11}
width_per_char = {font_regular:6, font_medium:8, font_large:12}
def oled_print_centered(text, font, y, this_canvas):
text = text.strip()[:max_char_per_line[font]]
start_x = int((OLED_WIDTH - (len(text) * width_per_char[font]))/2)
if start_x < 0:
start_x = 0
this_canvas.text((start_x, y), text, font=font, fill="white")
# persistent canvas, will need to clear areas before drawing new stuff
oled_canvas = canvas(device)
def print_welcome_screen(version_tuple):
with oled_canvas as ocan:
oled_print_centered("USB4VC", font_large, 0, ocan)
oled_print_centered(f"V{version_tuple[0]}.{version_tuple[1]}.{version_tuple[2]} dekuNukem", font_regular, 20, ocan)
device.contrast(1)
def oled_clear():
device.clear()
def oled_queue_worker():
print("oled_queue_worker started")
while 1:
# print(oled_display_queue.qsize())
time.sleep(0.75)
oled_queue_worker = threading.Thread(target=oled_queue_worker, daemon=True)
# while 1:
# with oled_canvas as ocan:
# ocan.rectangle((0, 0, device.width-1, device.height-1), outline=0, fill=0)
# oled_print_centered(f"{int(time.time())}", font_large, 0, ocan)
# time.sleep(1)
# while 1:
# with canvas(device) as draw:
# # draw.rectangle(device.bounding_box, outline="white", fill="black")
# # regular font test
# draw.text((0, 0), "123456789012345678901234567890", font=font_regular, fill="white")
# draw.text((0, 10), "ABCDEFGHIJ", font=font_regular, fill="white")
# draw.text((0, 20), "abcdefghij", font=font_regular, fill="white")
# # medium font test
# # draw.text((0, 0), "123456789012345678901234567890", font=font_medium, fill="white")
# # draw.text((0, 15), "123456789012345678901234567890", font=font_medium, fill="white")
# # large font test
# # draw.text((0, 0), "123456789012345678901234567890", font=font_large, fill="white")
# # draw.text((0, 20), "123456789012345678901234567890", font=font_regular, fill="white")
# time.sleep(1)

View File

@@ -1,16 +1,220 @@
import time
# https://luma-oled.readthedocs.io/en/latest/software.html
import sys
import time
import queue
import threading
from demo_opts import get_device
from luma.core.render import canvas
from PIL import ImageFont
import RPi.GPIO as GPIO
import usb4vc_usb_scan
BUTTON_GPIO = 16
"""
oled display object:
def button_pressed_callback(channel):
print(time.time(), "Button pressed!")
text
size
font
x, y
duration
afterwards
"""
oled_display_queue = queue.PriorityQueue()
PLUS_BUTTON_PIN = 27
MINUS_BUTTON_PIN = 19
ENTER_BUTTON_PIN = 22
SHUTDOWN_BUTTON_PIN = 21
GPIO.setmode(GPIO.BCM)
GPIO.setup(BUTTON_GPIO, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# https://sourceforge.net/p/raspberry-gpio-python/wiki/Inputs/
GPIO.add_event_detect(BUTTON_GPIO, GPIO.FALLING, callback=button_pressed_callback, bouncetime=100)
while 1:
time.sleep(1)
GPIO.setup(PLUS_BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(PLUS_BUTTON_PIN, GPIO.FALLING, bouncetime=200)
GPIO.setup(MINUS_BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(MINUS_BUTTON_PIN, GPIO.FALLING, bouncetime=200)
GPIO.setup(ENTER_BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(ENTER_BUTTON_PIN, GPIO.FALLING, bouncetime=200)
GPIO.setup(SHUTDOWN_BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(SHUTDOWN_BUTTON_PIN, GPIO.FALLING, bouncetime=200)
OLED_WIDTH = 128
OLED_HEIGHT = 32
my_arg = ['--display', 'ssd1306', '--interface', 'spi', '--spi-port', '0', '--spi-device', '1', '--gpio-reset', '6', '--gpio-data-command', '5', '--width', str(OLED_WIDTH), '--height', str(OLED_HEIGHT), '--spi-bus-speed', '2000000']
device = get_device(my_arg)
"""
OLED for USB4VC
128*32
3 font sizes available, regular/large: ProggyTiny.ttf, medium: ChiKareGo2.ttf.
regular ProggyTiny:
16 point, 3 lines, y=0, 10, 20
large type ProggyTiny:
32 point, 2 lines (1 large + 1 regular), y=0, 20
medium type ChiKareGo2:
16 point, 2 lines, y=0, 15
characters per line:
regular font: 21
medium font: 16
large font: 11
int(sys.argv[1])
$ sh sync.sh; ssh -t pi@192.168.1.56 "pkill python3;cd ~/usb4vc;python3 usb4vc_ui.py"
"""
font_regular = ImageFont.truetype("ProggyTiny.ttf", 16)
font_medium = ImageFont.truetype("ChiKareGo2.ttf", 16)
font_large = ImageFont.truetype("ProggyTiny.ttf", 32)
max_char_per_line = {font_regular:21, font_medium:17, font_large:11}
width_per_char = {font_regular:6, font_medium:8, font_large:12}
def oled_print_centered(text, font, y, this_canvas):
text = text.strip()[:max_char_per_line[font]]
start_x = int((OLED_WIDTH - (len(text) * width_per_char[font]))/2)
if start_x < 0:
start_x = 0
this_canvas.text((start_x, y), text, font=font, fill="white")
# persistent canvas, will need to clear areas before drawing new stuff
# oled_canvas = canvas(device)
# def print_welcome_screen(version_tuple):
# with oled_canvas as ocan:
# oled_print_centered("USB4VC", font_large, 0, ocan)
# oled_print_centered(f"V{version_tuple[0]}.{version_tuple[1]}.{version_tuple[2]} dekuNukem", font_regular, 20, ocan)
# device.contrast(1)
def oled_clear():
device.clear()
"""
(level, page)
(0, 0)
(0, 1)
(0, 2)
"""
class usb4vc_menu(object):
def __init__(self):
super(usb4vc_menu, self).__init__()
self.current_level = 0
self.current_page = 0
self.level_size = 2
self.page_size = [3, 6]
self.last_refresh = 0
def switch_page(self, amount):
self.current_page = (self.current_page + amount) % self.page_size[self.current_level]
def switch_level(self, amount):
new_level = self.current_level + amount
if new_level < self.level_size:
self.current_level = new_level
self.current_page = 0
def display_page(self, level, page):
if level == 0:
if page == 0:
with canvas(device) as draw:
draw.text((0, 0), "Keyboard: PS/2", font=font_regular, fill="white")
draw.text((0, 10), "Mouse: Serial", font=font_regular, fill="white")
draw.text((0, 20), "Gamepad: Generic", font=font_regular, fill="white")
if page == 1:
with canvas(device) as draw:
draw.text((0, 0), f"USB Keyboard: {len(usb4vc_usb_scan.keyboard_opened_device_dict)}", font=font_regular, fill="white")
draw.text((0, 10), f"USB Mouse: {len(usb4vc_usb_scan.mouse_opened_device_dict)}", font=font_regular, fill="white")
draw.text((0, 20), f"USB Gamepad: {len(usb4vc_usb_scan.gamepad_opened_device_dict)}", font=font_regular, fill="white")
if page == 2:
with canvas(device) as draw:
draw.text((0, 0), "PBoard: IBM PC Compat", font=font_regular, fill="white")
draw.text((0, 10), "PBoard FW: 0.1.1", font=font_regular, fill="white")
draw.text((0, 20), "IP: 192.168.231.12", font=font_regular, fill="white")
if level == 1:
if page == 0:
with canvas(device) as draw:
oled_print_centered("Keyboard Protocol", font_medium, 0, draw)
oled_print_centered("PS/2", font_medium, 15, draw)
if page == 1:
with canvas(device) as draw:
oled_print_centered("Mouse Protocol", font_medium, 0, draw)
oled_print_centered("Serial", font_medium, 15, draw)
if page == 2:
with canvas(device) as draw:
oled_print_centered("Mouse Sensitivity", font_medium, 0, draw)
oled_print_centered("1.0", font_medium, 15, draw)
if page == 3:
with canvas(device) as draw:
oled_print_centered("Gamepad Protocol", font_medium, 0, draw)
oled_print_centered("MS Sidewinder", font_medium, 15, draw)
if page == 4:
with canvas(device) as draw:
oled_print_centered("Pair Bluetooth", font_medium, 10, draw)
if page == 5:
with canvas(device) as draw:
oled_print_centered("Back", font_medium, 10, draw)
def action(self, level, page):
if level == 0:
self.switch_level(1)
self.display_curent_page()
if level == 1:
if page == 5:
self.switch_level(-1)
self.display_curent_page()
def action_current_page(self):
self.action(self.current_level, self.current_page);
def display_curent_page(self):
self.display_page(self.current_level, self.current_page)
def update_usb_status(self):
if self.current_level == 0 and self.current_page == 1 and time.time() - self.last_refresh > 0.2:
self.display_page(0, 1)
self.last_refresh = time.time()
my_menu = usb4vc_menu()
def ui_worker():
print("ui_worker started")
my_menu.display_page(0, 0)
while 1:
time.sleep(0.1)
my_menu.update_usb_status();
if GPIO.event_detected(PLUS_BUTTON_PIN):
print(time.time(), "PLUS_BUTTON pressed!")
my_menu.switch_page(1)
my_menu.display_curent_page()
if GPIO.event_detected(MINUS_BUTTON_PIN):
print(time.time(), "MINUS_BUTTON pressed!")
my_menu.switch_page(-1)
my_menu.display_curent_page()
if GPIO.event_detected(ENTER_BUTTON_PIN):
print(time.time(), "ENTER_BUTTON pressed!")
my_menu.action_current_page()
if GPIO.event_detected(SHUTDOWN_BUTTON_PIN):
print(time.time(), "SHUTDOWN_BUTTON pressed!")
ui_worker = threading.Thread(target=ui_worker, daemon=True)

View File

@@ -5,8 +5,7 @@ import spidev
import threading
import subprocess
import RPi.GPIO as GPIO
from usb4vc_oled import oled_display_queue
import usb4vc_ui
"""
sudo apt install stm32flash
@@ -325,7 +324,6 @@ def usb_device_scan_worker():
device_file_list = os.listdir(input_device_path)
except FileNotFoundError:
print("No input devices found")
oled_display_queue.put((0, 'no input'))
continue
except Exception as e:
print('list input device exception:', e)