Outils pour utilisateurs

Outils du site


interface_lego_dacta_70909

Ceci est une ancienne révision du document !


Table des matières

INterface LEGO dacta 70909

Matériel

  • Interface série 70909
  • Adaptateur USB/Série
  • Câble série Male/femelle

Processing

import processing.serial.*;
 
Serial portSerie;
Bouton[] boutons;
 
int val;
boolean connexion;
String init = "";
 
void setup() {
  size(400, 400);
  noStroke();
  portSerie = new Serial(this, "/dev/cu.usbserial-FT1MGSG5", 9600);
  String message = "p\0###Do you byte, when I knock?$$$";
  portSerie.write(message);
  connexion = false;
  color c = color(255, 0, 0);
  boutons = new Bouton[2];
  boutons[0] = new Bouton(new PVector(50, 100), new PVector(100, 100), color(255, 0, 0));
  boutons[1] = new Bouton(new PVector(250, 100), new PVector(100, 100), color(255, 0, 0));
 
  int valeur = 0b00100001;
  thread("maintenirConnexion");
}
 
void draw() {
  background(255);
  if (!connexion) {
    if  (init.length() < 31) {
      while (portSerie.available() > 0) {
        String inBuffer = portSerie.readString();   
        if (inBuffer != null) {
          init += inBuffer;
        }
      }
    } else {
      if (init.equals("###Just a bit off the block!$$$")) {
        connexion = true;
        println(init);
      } else {
        init = "";
        String message = "p\0###Do you byte, when I knock?$$$";
        portSerie.write(message);
      }
    }
  }
for (Bouton b : boutons) {
    b.dessiner();
  }
}
 
void mousePressed() {
  for (Bouton b : boutons) {
    b.cliquer();
  }
  if (boutons[0].actif) {
    portSerie.write(0x10);
  }
  if (boutons[1].actif) {
    portSerie.write(0x18);
  }
}
 
void mouseReleased() {
  for (Bouton b : boutons) {
    b.relacher();
  }
  portSerie.write(0x90);
  portSerie.write(0xFF);
 
}
 
void keyPressed() {
  if (key == 'a') {
    for (int i = 0; i < 8; i++) {
      portSerie.write(0x10 + i);
      delay(500);
      portSerie.write(0x30 + i);
    }
  }
  if (key == 'z') {
    for (int i = 0; i < 8; i++) {
      portSerie.write(0x18 + i);
      delay(500);
      portSerie.write(0x30 + i);
    }
  }
}
 
void maintenirConnexion() {
  int tempsInit = millis();
  while (true) {
    if ((millis() - tempsInit) > 2000) {
      tempsInit = millis();
      portSerie.write(0x02);
    }
  }
}
 
class Bouton {
  PVector position, taille;
  color couleur;
  boolean actif;
 
  Bouton(PVector p, PVector t, color c) {
    position = p;
    taille = t;
    couleur = c;
  }
 
  void dessiner() {
    noStroke();
    if (actif) {
      fill(color(150, 50, 50));
    } else {
      fill(couleur);
    }
    rect(position.x, position.y, taille.x, taille.y, 10);
  }
 
  void cliquer() {
    if ((mouseX > position.x) && (mouseX < position.x + taille.x) && (mouseY > position.y) && (mouseY < position.y + taille.y)) {
      actif = true;
    }
  }
 
  void relacher() {
    actif = false;
  }
}

Ressources

interface_lego_dacta_70909.1586873106.txt.gz · Dernière modification : 2020/04/14 14:05 de Mushussu