Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
Open-Cube FW
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Open Cube
Open-Cube FW
Commits
094f7c25
Commit
094f7c25
authored
5 months ago
by
Václav Jelínek
Browse files
Options
Downloads
Patches
Plain Diff
Rework menu, add bt and test pages
parent
4aa1376d
No related branches found
Branches containing commit
No related tags found
1 merge request
!12
Major update of menu visuals and example programs
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
main/main.py
+97
-72
97 additions, 72 deletions
main/main.py
with
97 additions
and
72 deletions
main/main.py
+
97
−
72
View file @
094f7c25
...
...
@@ -7,14 +7,22 @@ import gc
from
lib.robot
import
Robot
from
lib.robot_consts
import
Button
esp_name
=
"
Open-Cube01
"
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
micropython
.
alloc_emergency_exception_buf
(
100
)
...
...
@@ -23,33 +31,27 @@ robot = Robot()
def
main
():
#
Show menu on display
def
display_
show_menu
(
robot
,
bat_voltage
,
program_list
,
current_program
):
#
Fill framebuffer with programs menu
def
display_
fill_programs
(
robot
,
bat_voltage
,
program_list
,
current_program
):
robot
.
display
.
fill
(
0
)
robot
.
display
.
text
(
'
{:.2f}V
'
.
format
(
bat_voltage
),
87
,
0
,
1
)
robot
.
display
.
text
(
'
Press OK
'
,
0
,
0
,
1
)
robot
.
display
.
text
(
'
to start
'
,
0
,
10
,
1
)
robot
.
display
.
text
(
'
Press UP or DOWN
'
,
0
,
25
,
1
)
robot
.
display
.
text
(
'
to list programs
'
,
0
,
35
,
1
)
robot
.
display
.
centered_text
(
'
< Programs >
'
,
0
,
1
)
if
len
(
program_list
)
>
0
:
robot
.
display
.
text
(
program_list
[
current_program
][
0
][:
-
3
],
0
,
55
,
1
)
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
)
else
:
robot
.
display
.
text
(
"
<brick
is empty
>
"
,
0
,
55
,
1
)
robot
.
display
.
text
(
"
cube
is empty
"
,
0
,
30
,
1
)
#
Show program error on display
def
display_
show_error
(
robot
):
#
Fill framebuffer with bt menu
def
display_
fill_bt_menu
(
robot
,
esp_default_name
):
robot
.
display
.
fill
(
0
)
robot
.
display
.
text
(
'
Program
'
,
35
,
20
,
1
)
robot
.
display
.
text
(
'
error!
'
,
40
,
30
,
1
)
robot
.
display
.
show
()
robot
.
display
.
text
(
'
BT pair
'
,
35
,
20
,
1
)
robot
.
display
.
text
(
esp_default_name
,
40
,
0
,
1
)
def
display_show_startup
(
robot
):
robot
.
display
.
fill
(
0
)
robot
.
display
.
text
(
'
Turning
'
,
35
,
20
,
1
)
robot
.
display
.
text
(
'
on
'
,
40
,
30
,
1
)
robot
.
display
.
show
()
def
display_show_bt_pin
(
robot
,
pin
,
connecting
):
# Add bt connecting window to framebuffer
def
display_fill_bt_pin
(
robot
,
pin
,
connecting
):
robot
.
display
.
rect
(
6
,
6
,
116
,
52
,
1
)
robot
.
display
.
rect
(
7
,
7
,
114
,
50
,
1
)
robot
.
display
.
fill_rect
(
8
,
8
,
112
,
48
,
0
)
...
...
@@ -63,6 +65,19 @@ def main():
robot
.
display
.
text
(
'
no
'
,
10
,
44
,
1
)
robot
.
display
.
text
(
'
yes
'
,
100
,
44
,
1
)
# Show program error on display
def
display_show_error
(
robot
):
robot
.
display
.
fill
(
0
)
robot
.
display
.
text
(
'
Program
'
,
35
,
20
,
1
)
robot
.
display
.
text
(
'
error!
'
,
40
,
30
,
1
)
robot
.
display
.
show
()
def
display_show_startup
(
robot
):
robot
.
display
.
fill
(
0
)
robot
.
display
.
text
(
'
Turning
'
,
35
,
20
,
1
)
robot
.
display
.
text
(
'
on
'
,
40
,
30
,
1
)
robot
.
display
.
show
()
def
display_show_program
(
robot
):
robot
.
display
.
fill
(
0
)
robot
.
display
.
text
(
'
Program
'
,
35
,
20
,
1
)
...
...
@@ -70,27 +85,28 @@ def main():
robot
.
display
.
show
()
# Analyze pressed buttons for menu movement
def
buttons
(
robot
,
button_values
,
robot
S
tate
,
menu_move
,
menu_debounce
):
def
buttons
(
robot
,
button_values
,
robot
_s
tate
,
menu_move
_v
,
menu_move_h
,
menu_debounce
):
ok_pressed
=
0
if
button_values
[
Button
.
OK
]:
ok_pressed
=
1
elif
button_values
[
Button
.
DOWN
]:
if
robotState
==
0
:
menu_move
=
1
elif
button_values
[
Button
.
UP
]:
if
robotState
==
0
:
menu_move
=-
1
elif
button_values
[
Button
.
RIGHT
]:
pass
elif
button_values
[
Button
.
LEFT
]:
pass
elif
robot_state
==
0
:
if
button_values
[
Button
.
DOWN
]:
menu_move_v
=
1
elif
button_values
[
Button
.
UP
]:
menu_move_v
=-
1
elif
button_values
[
Button
.
RIGHT
]:
menu_move_h
=
1
elif
button_values
[
Button
.
LEFT
]:
menu_move_h
=-
1
# Buttons debounce
if
not
button_values
[
Button
.
UP
]
and
not
button_values
[
Button
.
DOWN
]:
if
robotState
==
0
and
menu_debounce
==
1
:
menu_move
=
0
if
(
not
button_values
[
Button
.
UP
]
and
not
button_values
[
Button
.
DOWN
]
and
not
button_values
[
Button
.
RIGHT
]
and
not
button_values
[
Button
.
LEFT
]):
if
robot_state
==
0
and
menu_debounce
==
1
:
menu_move_v
=
0
menu_move_h
=
0
menu_debounce
=
0
return
menu_move
,
menu_debounce
,
ok_pressed
return
menu_move
_v
,
menu_move_h
,
menu_debounce
,
ok_pressed
# Request ESP32 for BT pair pin
def
send_esp_req
(
robot
):
...
...
@@ -108,8 +124,8 @@ def main():
utime
.
sleep_ms
(
200
)
# wait for all components of the Cube to start
# Initialize variables
robot
S
tate
=
0
menu_move
=
0
robot
_s
tate
=
0
menu_move
_v
,
menu_move_h
=
0
,
0
menu_debounce
=
0
current_program
=
0
counter
=
1
...
...
@@ -144,48 +160,56 @@ def main():
if
not
robot
.
esp
.
running
()
and
counter
%
20
==
0
:
reset
=
robot
.
esp
.
reset
(
100
)
if
reset
:
robot
.
esp
.
set_name
(
esp_name
,
100
)
print
(
"
ESP BT name:
"
,
esp_name
)
robot
.
esp
.
set_name
(
esp_
default_
name
,
100
)
print
(
"
ESP BT name:
"
,
esp_
default_
name
)
bat_voltage
=
robot
.
battery
.
voltage
()
# Get current battery voltage
button_values
=
robot
.
buttons
.
pressed
()
# Get buttons state
# Request BT pair pin every 20 cycles
if
robot
.
esp
.
running
()
and
counter
%
20
==
0
:
bt_pair
,
pin
=
send_esp_req
(
robot
)
if
pin
!=
None
and
pin
==
rejected_pin
:
bt_pair
=
False
if
not
bt_pair
:
bt_connecting
=
False
# Move menu accordingly to pressed buttons
if
not
bt_pair
:
menu_move
,
menu_debounce
,
ok_pressed
=
buttons
(
robot
,
button_values
,
robotState
,
menu_move
,
menu_debounce
)
display_show_menu
(
robot
,
bat_voltage
,
program_list
,
current_program
)
# Show BT pair pin request on display and send confirm or cancel
if
bt_pair
:
display_show_bt_pin
(
robot
,
pin
,
bt_connecting
)
if
button_values
[
Button
.
RIGHT
]:
robot
.
esp
.
send_pair
()
bt_connecting
=
True
elif
button_values
[
Button
.
LEFT
]:
rejected_pin
=
pin
robot
.
esp
.
send_cancel
()
else
:
menu_move_v
,
menu_move_h
,
menu_debounce
,
ok_pressed
=
buttons
(
robot
,
button_values
,
robot_state
,
menu_move_v
,
menu_move_h
,
menu_debounce
)
# Check if buttons were pressed and move menu accordingly
if
menu_move
!=
0
and
menu_debounce
==
0
and
program_count
>
0
:
current_program
+=
menu_move
current_program
%=
program_count
menu_debounce
=
1
if
menu_debounce
==
0
:
if
menu_move_v
!=
0
and
program_count
>
0
:
current_program
+=
menu_move_v
current_program
%=
program_count
menu_debounce
=
1
elif
menu_move_h
!=
0
:
current_menu
+=
menu_move_h
current_menu
%=
Menu
.
COUNT
bt_pair
=
False
menu_debounce
=
1
if
current_menu
==
Menu
.
BT
:
display_fill_bt_menu
(
robot
,
esp_default_name
)
# Request BT pair pin every 10 cycles
if
robot
.
esp
.
running
()
and
counter
%
10
==
0
:
bt_pair
,
pin
=
send_esp_req
(
robot
)
if
pin
!=
None
and
pin
==
rejected_pin
:
bt_pair
=
False
if
not
bt_pair
:
bt_connecting
=
False
# Show BT pair pin request on display and send confirm or cancel
if
bt_pair
:
display_fill_bt_pin
(
robot
,
pin
,
bt_connecting
)
if
button_values
[
Button
.
RIGHT
]:
robot
.
esp
.
send_pair
()
bt_connecting
=
True
elif
button_values
[
Button
.
LEFT
]:
rejected_pin
=
pin
robot
.
esp
.
send_cancel
()
elif
current_menu
==
Menu
.
MOT_SEN_TEST
:
elif
current_menu
==
Menu
.
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
S
tate
=
1
robot
_s
tate
=
1
try
:
# Open user program and execute it
program_name
=
program_list
[
current_program
][
0
]
...
...
@@ -208,9 +232,10 @@ def main():
robot
.
deinit_all
()
# Collect unreachable objects (this deinitializes EV3 motors & sensors)
gc
.
collect
()
robot
S
tate
=
0
robot
_s
tate
=
0
robot
.
esp
.
esp_running
=
False
print
(
"
Program finished
\n
"
)
display_fill_programs
(
robot
,
bat_voltage
,
program_list
,
current_program
)
robot
.
display
.
show
()
counter
+=
1
utime
.
sleep
(
0.05
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment