Tutoriel 4 : position de la bille sur la matrice de leds

This commit is contained in:
Philippe Roy 2023-05-07 04:37:49 +02:00
parent b4762f276e
commit 1d18f53be5
3 changed files with 60 additions and 38 deletions

View File

@ -42,8 +42,6 @@ Matrix_8x8 matrix;
******************************************************************************/
String serial_msg = ""; // Message
uint64_t serial_msg_int;
String serial_msg_old = ""; // Message
bool serial_msg_complet = false; // Flag de message complet
/******************************************************************************
@ -67,6 +65,8 @@ void setup() {
matrix.setBlinkRate(BLINK_OFF);
matrix.clear();
matrix.display();
/* matrix.writeOnePicture(0xff90b2a2a6a4ae82); // Labyrinthe */
/* matrix.display(); */
// Ok
// Serial.println("Initialisation des composants I2C.");
@ -113,19 +113,30 @@ void loop() {
if (serial_msg_complet) {
/* matrix.writeString(serial_msg, 1000, ACTION_SCROLLING); */
/* matrix.display(); */
if (serial_msg.length() ==2) {
if (serial_msg[0] == 'X') matrix.clear();
if (serial_msg[0] == 'N') matrix.writeOnePicture(0x00000000000000ff);
if (serial_msg[0] == 'E') matrix.writeOnePicture(0x8080808080808080);
if (serial_msg[0] == 'S') matrix.writeOnePicture(0xff00000000000000);
if (serial_msg[0] == 'O') matrix.writeOnePicture(0x0101010101010101);
}
if (serial_msg.length() ==3) {
if ((serial_msg[0] == 'N') and (serial_msg[1] == 'E')) matrix.writeOnePicture(0x00000000808080f0);
if ((serial_msg[0] == 'S') and (serial_msg[1] == 'E')) matrix.writeOnePicture(0xf080808000000000);
if ((serial_msg[0] == 'S') and (serial_msg[1] == 'O')) matrix.writeOnePicture(0x0f01010100000000);
if ((serial_msg[0] == 'N') and (serial_msg[1] == 'O')) matrix.writeOnePicture(0x000000000101010f);
}
/* matrix.clear(); */
/* matrix.display(); */
/* matrix.writeOnePicture(0xff90b2a2a6a4ae82); // Labyrinthe */
int xy= serial_msg.toInt();
int x= xy/10;
int y= xy-(x*10);
matrix.clear();
matrix.writePixel(x, y, true);
/* Orientation du plateau (N, NE, SE, S, SO, O, NO) */
/* if (serial_msg.length() ==2) { */
/* if (serial_msg[0] == 'X') matrix.clear(); */
/* if (serial_msg[0] == 'N') matrix.writeOnePicture(0x00000000000000ff); */
/* if (serial_msg[0] == 'E') matrix.writeOnePicture(0x8080808080808080); */
/* if (serial_msg[0] == 'S') matrix.writeOnePicture(0xff00000000000000); */
/* if (serial_msg[0] == 'O') matrix.writeOnePicture(0x0101010101010101); */
/* } */
/* if (serial_msg.length() ==3) { */
/* if ((serial_msg[0] == 'N') and (serial_msg[1] == 'E')) matrix.writeOnePicture(0x00000000808080f0); */
/* if ((serial_msg[0] == 'S') and (serial_msg[1] == 'E')) matrix.writeOnePicture(0xf080808000000000); */
/* if ((serial_msg[0] == 'S') and (serial_msg[1] == 'O')) matrix.writeOnePicture(0x0f01010100000000); */
/* if ((serial_msg[0] == 'N') and (serial_msg[1] == 'O')) matrix.writeOnePicture(0x000000000101010f); */
/* } */
matrix.display();
serial_msg = "";
serial_msg_complet = false;
@ -134,7 +145,7 @@ void loop() {
/******************************************************************************
* Évenement provoqué UPBGE
* Évenement provoqué UPBGE (via la liaison série)
******************************************************************************/
void serialEvent() {

View File

@ -32,6 +32,9 @@ ACTIVATE = bge.logic.KX_INPUT_ACTIVE
serial_baud=115200
# serial_comm = serial.Serial('COM4',serial_baud, timeout=0.016) # Windows
serial_comm = serial.Serial('/dev/ttyACM1',serial_baud, timeout=0.016) # GNU/Linux
serial_msg_init = "99\n" # Vider la matrice de led
serial_comm.write(serial_msg_init.encode())
print (serial_comm)
###############################################################################
@ -107,32 +110,40 @@ def capteur(cont):
y=-(float(y_txt)/57.3) * resolution # 1/ 360 / (2 * pi)
applyRotationTo(scene.objects['Plateau'], x,y, 0)
# Ecriture sur la liaison série : programme Arduino : 3-labyrinthe-imu.ino
# Ecriture de l'orientation du plateau sur la liaison série : programme Arduino : 3-labyrinthe-imu.ino
obj['Rx']=obj.worldOrientation.to_euler().x*57.3
obj['Ry']=obj.worldOrientation.to_euler().y*57.3
obj['Rz']=obj.worldOrientation.to_euler().z*57.3
# if obj['Rx']<-2 and obj['Ry'] >-2 and obj['Ry'] <2:
# serial_msg_out = "N\n"
# if obj['Rx']>2 and obj['Ry'] >-2 and obj['Ry'] <2:
# serial_msg_out = "S\n"
# if obj['Rx']>-2 and obj['Rx']<2 and obj['Ry'] <-2 :
# serial_msg_out = "O\n"
# if obj['Rx']>-2 and obj['Rx']<2 and obj['Ry'] > 2 :
# serial_msg_out = "E\n"
# if obj['Rx']<-2 and obj['Ry'] <-2 :
# serial_msg_out = "NO\n"
# if obj['Rx']<-2 and obj['Ry'] > 2 :
# serial_msg_out = "NE\n"
# if obj['Rx']>2 and obj['Ry'] <-2 :
# serial_msg_out = "SO\n"
# if obj['Rx']>2 and obj['Ry'] > 2 :
# serial_msg_out = "SE\n"
# if obj['Rx']>-2 and obj['Rx']<2 and obj['Ry'] >-2 and obj['Ry'] <2:
# serial_msg_out = "X\n"
if obj['Rx']<-2 and obj['Ry'] >-2 and obj['Ry'] <2:
serial_msg_out = "N\n"
if obj['Rx']>2 and obj['Ry'] >-2 and obj['Ry'] <2:
serial_msg_out = "S\n"
if obj['Rx']>-2 and obj['Rx']<2 and obj['Ry'] <-2 :
serial_msg_out = "O\n"
if obj['Rx']>-2 and obj['Rx']<2 and obj['Ry'] > 2 :
serial_msg_out = "E\n"
if obj['Rx']<-2 and obj['Ry'] <-2 :
serial_msg_out = "NO\n"
if obj['Rx']<-2 and obj['Ry'] > 2 :
serial_msg_out = "NE\n"
if obj['Rx']>2 and obj['Ry'] <-2 :
serial_msg_out = "SO\n"
if obj['Rx']>2 and obj['Ry'] > 2 :
serial_msg_out = "SE\n"
if obj['Rx']>-2 and obj['Rx']<2 and obj['Ry'] >-2 and obj['Ry'] <2:
serial_msg_out = "X\n"
# Ecriture de la position de la bille sur la liaison série : programme Arduino : 3-labyrinthe-imu.ino
obj_bille = scene.objects['Bille']
obj_bille['x'] = obj_bille.worldPosition.x # de -3.5 à 3.5
obj_bille['y'] = obj_bille.worldPosition.y # de 3.5 à -3.5
obj_bille['Lx']=-1*round(obj_bille.worldPosition.x-3.5) # de 7 à 0
if obj_bille['Lx']<0: obj_bille['Lx']=0
if obj_bille['Lx']>7: obj_bille['Lx']=7
obj_bille['Ly']=-1*round(obj_bille.worldPosition.y-3.5) # de 0 à 7
if obj_bille['Ly']<0: obj_bille['Ly']=0
if obj_bille['Ly']>7: obj_bille['Ly']=7
serial_msg_out = str(obj_bille['Lx'])+str(obj_bille['Ly'])+"\n"
serial_comm.write(serial_msg_out.encode())
###############################################################################