2023-05-14 12:19:04 +02:00
|
|
|
from microbit import uart, sleep
|
2023-05-14 06:17:15 +02:00
|
|
|
from microbit import *
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# 5-labyrinthe-microbit.py
|
|
|
|
# @title: Programme pour la carte micro:bit de gestion de la centrale inertielle
|
|
|
|
# @project: Blender-EduTech - Tutoriel 5 : Labyrinthe à bille - Interfacer avec une carte micro:bit
|
|
|
|
# @lang: fr
|
|
|
|
# @authors: Philippe Roy <philippe.roy@ac-grenoble.fr>
|
|
|
|
# @copyright: Copyright (C) 2023 Philippe Roy
|
|
|
|
# @license: GNU GPL
|
|
|
|
#
|
2023-05-17 15:10:27 +02:00
|
|
|
# Commandes déclenchées par UPBGE pour le scène du labyrinthe
|
2023-05-14 06:17:15 +02:00
|
|
|
#
|
|
|
|
###############################################################################
|
|
|
|
|
2023-05-14 12:19:04 +02:00
|
|
|
###############################################################################
|
|
|
|
# Initialisation
|
|
|
|
###############################################################################
|
|
|
|
|
2023-05-17 15:51:34 +02:00
|
|
|
attente_image = Image("00000:00000:00300:00000:00000")
|
2023-05-14 12:19:04 +02:00
|
|
|
display.show(attente_image) # Témoin de fonctionnement
|
|
|
|
|
|
|
|
uart.init(baudrate= 115200) # Initialisation du port série
|
|
|
|
|
2023-05-17 15:10:27 +02:00
|
|
|
###############################################################################
|
2023-05-14 12:19:04 +02:00
|
|
|
# Boucle principale
|
2023-05-17 15:10:27 +02:00
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
msg_str=''
|
2023-05-14 12:19:04 +02:00
|
|
|
|
2023-05-14 06:17:15 +02:00
|
|
|
while True:
|
2023-05-17 15:10:27 +02:00
|
|
|
|
2023-05-17 15:51:34 +02:00
|
|
|
# A propos
|
|
|
|
if button_a.is_pressed() or button_b.is_pressed():
|
|
|
|
display.scroll("Labyrinte")
|
|
|
|
display.show(attente_image) # Témoin de fonctionnement
|
|
|
|
|
2023-05-17 15:10:27 +02:00
|
|
|
# Inclinaison de la carte -> UBGE
|
|
|
|
accel_x=accelerometer.get_x() # Roulis
|
|
|
|
accel_y=accelerometer.get_y() # Tangage
|
2023-05-14 12:19:04 +02:00
|
|
|
uart.write(str(accel_x)+","+str(accel_y)+"\n")
|
2023-05-17 15:10:27 +02:00
|
|
|
|
|
|
|
# UBGE -> micro:bit (lecture du message)
|
2023-05-17 15:51:34 +02:00
|
|
|
msg_byte = uart.readline()
|
|
|
|
if msg_byte:
|
2023-05-17 15:10:27 +02:00
|
|
|
display.clear()
|
2023-05-17 15:51:34 +02:00
|
|
|
msg_str = str(msg_byte, 'ascii')
|
2023-05-17 15:10:27 +02:00
|
|
|
if "91" in msg_str: # Chute
|
2023-05-17 15:51:34 +02:00
|
|
|
display.show(Image.SAD)
|
|
|
|
sleep(500);
|
|
|
|
uart.write("start\n")
|
2023-05-17 15:10:27 +02:00
|
|
|
elif "92" in msg_str: # Victoire
|
|
|
|
display.show(Image.HAPPY)
|
|
|
|
else: # Position de la bille
|
|
|
|
display.set_pixel(int(msg_str[0]), int(msg_str[1]), 9)
|
2023-05-14 12:19:04 +02:00
|
|
|
|
2023-05-14 06:17:15 +02:00
|
|
|
sleep(100)
|