Skip to content
Snippets Groups Projects
Commit 707f93e8 authored by Václav Jelínek's avatar Václav Jelínek
Browse files

Add menu programs to frozen manifest, update menu

parent 77bf383b
No related branches found
No related tags found
1 merge request!12Major update of menu visuals and example programs
Pipeline #106393 canceled
......@@ -30,12 +30,14 @@ class Port:
M2: Port = const(1)
M3: Port = const(2)
M4: Port = const(3)
M_COUNT: Port = const(4)
S1: Port = const(0)
S2: Port = const(1)
S3: Port = const(2)
S4: Port = const(3)
S_COUNT: Port = const(4)
class Button:
POWER: Button = const(0)
LEFT: Button = const(1)
......
......@@ -5,22 +5,14 @@ import micropython
import gc
from lib.robot import Robot
from lib.robot_consts import Button
from lib.robot_consts import Button, Port
try
try:
from config import *
except:
from lib.default_config import *
# File and directory constants for browsing user programs
FILE = 0x8000
DIRECTORY = 0x4000
class Menu:
PROGRAM: Menu = micropython.const(0)
MOT_SEN_TEST: Menu = micropython.const(1)
BT: Menu = micropython.const(2)
COUNT: Menu = micropython.const(3)
# Allocate exception buffer for interrupt handlers
......@@ -30,25 +22,43 @@ micropython.alloc_emergency_exception_buf(100)
robot = Robot()
def main():
print([i for i in os.ilistdir()])
class Menu:
PROGRAM: Menu = micropython.const(0)
MOT_TEST: Menu = micropython.const(1)
SEN_TEST: Menu = micropython.const(2)
CUBE_TEST: Menu = micropython.const(3)
BT: Menu = micropython.const(4)
COUNT: Menu = micropython.const(5)
# File and directory constants for browsing user programs
FILE = 0x8000
DIRECTORY = 0x4000
DISPLAY_TITLE_POS = 18
DISPLAY_PROG_POS = 45
DISPLAY_ARROW_GAP = 10
menu_list = [0, 0, 0, 0, 0]
menu_list_size = [0, Port.M_COUNT, Port.S_COUNT, 2, 1]
# Fill framebuffer with battery voltage
def display_fill_battery(robot, bat_voltage):
robot.display.text('{:.2f}V'.format(bat_voltage), 87, 0, 1)
# Fill framebuffer with programs menu
def display_fill_programs(robot, bat_voltage, program_list, current_program):
def display_fill_programs(robot, program_list, current_program):
robot.display.fill(0)
robot.display.text('{:.2f}V'.format(bat_voltage), 87, 0, 1)
robot.display.centered_text('< Programs >', 0, 1)
robot.display.centered_text('< Programs >', DISPLAY_TITLE_POS, 1)
if len(program_list) > 0:
robot.display.centered_text('^', 15, 1)
robot.display.text(program_list[current_program][0][:-3], 0, 30, 1)
robot.display.centered_text('v', 45, 1)
robot.display.centered_text(program_list[current_program][0][:-3], DISPLAY_PROG_POS, 1)
display_fill_arrows(robot)
else:
robot.display.text("cube is empty", 0, 30, 1)
# Fill framebuffer with bt menu
def display_fill_bt_menu(robot, esp_default_name):
robot.display.fill(0)
robot.display.text('BT pair', 35, 20, 1)
robot.display.text(esp_default_name, 40, 0, 1)
robot.display.centered_text('< BT pair >', DISPLAY_TITLE_POS, 1)
robot.display.centered_text(esp_default_name, DISPLAY_PROG_POS, 1)
# Add bt connecting window to framebuffer
def display_fill_bt_pin(robot, pin, connecting):
......@@ -65,6 +75,34 @@ def main():
robot.display.text('no', 10, 44, 1)
robot.display.text('yes', 100,44, 1)
# Fill framebuffer with motor test menu
def display_fill_mot_test(robot, current_motor_test):
robot.display.fill(0)
robot.display.centered_text('< Motor test >', DISPLAY_TITLE_POS, 1)
robot.display.centered_text('M{}'.format(current_motor_test+1), DISPLAY_PROG_POS, 1)
display_fill_arrows(robot)
# Fill framebuffer with sensor test menu
def display_fill_sen_test(robot, current_sen_test):
robot.display.fill(0)
robot.display.centered_text('< Sensor test >', DISPLAY_TITLE_POS, 1)
robot.display.centered_text('S{}'.format(current_sen_test+1), DISPLAY_PROG_POS, 1)
display_fill_arrows(robot)
# Fill framebuffer with cube test menu
def display_fill_cube_test(robot, current_oth_test):
robot.display.fill(0)
robot.display.centered_text('< Cube test >', DISPLAY_TITLE_POS, 1)
if current_oth_test == 0:
test_name = "Gyro Acc"
elif current_oth_test == 1:
test_name = "Utility"
robot.display.centered_text(test_name, DISPLAY_PROG_POS, 1)
display_fill_arrows(robot)
def display_fill_arrows(robot):
robot.display.centered_text('^', DISPLAY_PROG_POS - DISPLAY_ARROW_GAP, 1)
robot.display.centered_text('v', DISPLAY_PROG_POS + DISPLAY_ARROW_GAP, 1)
# Show program error on display
def display_show_error(robot):
robot.display.fill(0)
......@@ -121,13 +159,35 @@ def main():
robot.esp.timeout()
return bt_pair, pin
def run_program(robot, program_name, program_path, globals):
print("Running program", program_name)
display_show_program(robot)
try:
# Open user program and execute it
with open(program_path) as f:
code = compile(f.read(), program_name, 'exec')
exec(code, globals)
except Exception as e:
# Print exception for debugging and show error message on display
# if error occurs in user program
sys.print_exception(e)
display_show_error(robot)
utime.sleep(1.0)
finally:
# Deinit all sensors, motors and uart if initialized by user program
# after user program ends
robot.deinit_all()
# Collect unreachable objects (this deinitializes EV3 motors & sensors)
gc.collect()
robot.esp.esp_running = False
print("Program finished\n")
utime.sleep_ms(200) # wait for all components of the Cube to start
# Initialize variables
robot_state = 0
menu_move_v, menu_move_h = 0, 0
menu_debounce = 0
current_program = 0
current_menu = 0
counter = 1
rejected_pin = 0
bt_pair = False
......@@ -152,7 +212,7 @@ def main():
except OSError:
# Create program directory if this is a new installation
os.mkdir("./programs")
program_count = len(program_list)
menu_list_size[Menu.PROGRAM] = len(program_list)
# Loop that shows display, controls menu and starts user programs
while(True):
......@@ -174,9 +234,10 @@ def main():
menu_debounce)
# Check if buttons were pressed and move menu accordingly
if menu_debounce == 0:
if menu_move_v != 0 and program_count > 0:
current_program += menu_move_v
current_program %= program_count
if menu_move_v != 0:
menu_list[current_menu] += menu_move_v
menu_list[current_menu] %= menu_list_size[current_menu]
menu_debounce = 1
elif menu_move_h != 0:
current_menu += menu_move_h
......@@ -203,39 +264,45 @@ def main():
rejected_pin = pin
robot.esp.send_cancel()
elif current_menu == Menu.MOT_SEN_TEST:
elif current_menu == Menu.MOT_TEST:
display_fill_mot_test(robot, menu_list[current_menu])
if ok_pressed:
robot_state = 1
program_name = f"Motor M{menu_list[current_menu]+1}"
program_path = "./menu_programs/motor.py"
run_program(robot, program_name, program_path, {'robot': robot, 'chosen_motor_port': menu_list[current_menu]})
robot_state = 0
menu_debounce = 1
elif current_menu == Menu.SEN_TEST:
display_fill_sen_test(robot, menu_list[current_menu])
elif current_menu == Menu.CUBE_TEST:
display_fill_cube_test(robot, menu_list[current_menu])
if ok_pressed:
robot_state = 1
if menu_list[current_menu] == 0:
program_name = "Gyro Acc"
program_path = "./menu_programs/gyro_acc.py"
elif menu_list[current_menu] == 1:
program_name = "Utility"
program_path = "./menu_programs/utility.py"
run_program(robot, program_name, program_path, {'robot': robot})
robot_state = 0
menu_debounce = 1
elif current_menu == Menu.PROGRAM:
current_program = menu_list[current_menu]
display_fill_programs(robot, program_list, current_program)
# Check if OK button was pressed and start user program
if ok_pressed:
print("Running program", program_list[current_program][0])
display_show_program(robot)
robot_state = 1
try:
# Open user program and execute it
program_name = program_list[current_program][0]
program_path = "./programs/" + program_name
if program_list[current_program][1] == DIRECTORY:
program_name = "/main.py"
program_path += program_name
with open(program_path) as f:
code = compile(f.read(), program_name, 'exec')
exec(code, globals())
except Exception as e:
# Print exception for debugging and show error message on display
# if error occurs in user program
sys.print_exception(e)
display_show_error(robot)
utime.sleep(1.0)
finally:
# Deinit all sensors, motors and uart if initialized by user program
# after user program ends
robot.deinit_all()
# Collect unreachable objects (this deinitializes EV3 motors & sensors)
gc.collect()
robot_state = 0
robot.esp.esp_running = False
print("Program finished\n")
display_fill_programs(robot, bat_voltage, program_list, current_program)
program_name = program_list[current_program][0]
program_path = "./programs/" + program_name
if program_list[current_program][1] == DIRECTORY:
program_path += "/main.py"
run_program(robot, program_name, program_path, {'robot': robot})
robot_state = 0
menu_debounce = 1
display_fill_battery(robot, bat_voltage)
robot.display.show()
counter += 1
utime.sleep(0.05)
......
include("$(PORT_DIR)/boards/manifest.py")
package("lib", base_path="$(MPY_DIR)/../..")
package("menu_programs", base_path="$(MPY_DIR)/../..")
module("main.py", base_path="$(MPY_DIR)/../../main")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment