 // Au MIPS-lab, il faut envoyer en UDP à l'arduino "lamex" où x est le numéro de la
 // lame du Cray à activer.
 
-#include <SPI.h> 
+#include <SPI.h>
 #include <Ethernet.h>
 #include <EthernetUdp.h>
 

IPAddress ip(192, 168, 42, 250); // Défini l'adresse IP

int localPort = 9694; // Défini le port
  
int pin_LAME1 = 5;
int pin_LAME2 = 6;
int pin_LAME3 = 2;
int pin_LAME4 = 3;
 
int lame = 0;
char packetBuffer[32];	// Où sont stockés les données reçues.x
char ReplyBuffer[] = " OK "; // Réponse de l'arduino
int demande = 0;

EthernetUDP Udp;

	pin_LAME2 = LOW;
	pin_LAME3 = LOW;
	pin_LAME4 = LOW;

	pinMode(pin_STATUS1, INPUT);
	pinMode(pin_STATUS2, INPUT);
	pinMode(pin_STATUS3, INPUT);
	pinMode(pin_STATUS4, INPUT);
}

void sendStatus(lame, lameNumber){
  int status = digitalRead(lame);
  Serial.println("status de la lame %d : %d", lameNumber, status);
  Udp.beginPacket(Udp.remoteIP(), 9694);
  if (status)
    {
      Udp.write(ReplyOn);
    }
  else
    {
      Udp.write(ReplyOff);
    }
  Udp.endPacket();
}

void loop() {
	int packetSize = Udp.parsePacket();
	if(packetSize) { // Si il y a un paquet
		Udp.read(packetBuffer,32); // le lire
		op = packetBuffer.substrings(4);
		lame = packetBuffer[4]-'0'; // et convertir le 5e caractére en chiffre (char -> int)
		switch(op) {
		case "lame":
		  switch (lame) {
		  case 1:
		    pin_LAME1 = HIGH;
		    Serial.println("Active la lame 1");
		    delay(1000);
		    pin_LAME1 = LOW;
		    break;
		  case 2:
		    pin_LAME2 = HIGH;
		    Serial.println("Active la lame 2");
		    delay(1000);
		    pin_LAME2 = LOW;
		    break;
		  case 3:
		    pin_LAME3 = HIGH;
		    Serial.println("Active la lame 3");
		    delay(1000);
		    pin_LAME3 = LOW;
		    break;
		  case 4:
		    pin_LAME4 = HIGH;
		    Serial.println("Active la lame 4");
		    delay(1000);
		    pin_LAME4 = LOW;
		    break;
		  }

		  Udp.beginPacket(Udp.remoteIP(), 9694);
		  Udp.write(ReplyBuffer);
		  Udp.endPacket();
		  break;

		case "stat":
		  switch(lame) {
 		  case 1:
		    sendStatus(pin_STATUS1, 1)
		    break;
		  case 2:
		    sendStatus(pin_STATUS2, 2)
		    break;
		  case 3:
		    sendStatus(pin_STATUS3, 3)
		    break;
		  case 4:
		    sendStatus(pin_STATUS4, 4)
		    break;
		  }
	}
	delay(10);
}
