Visualisateur Temps Parole/Code Source

De Wiki de Projets IMA
// Nicolas Lefebvre
// Polytech Lille 2011
// Permet d'afficher sur une matrice de 9*9 sous matrice de 8*8 led le pourcentage de paroles de 2 personnes


//a decommenter pour le code pour 3 matrice inférieur, et commenter pour les 6 supérieures
//à mettro à 0 pour les 6  matrice et à 2 pour les 3
//#define __MATRICE3__ 0 //pour les 6
#define __MATRICE3__ 2 //pour les 3


//largeur et hauteur des caracteres
//enlever 1 à la valeur voulue (si 12-6 désiré, mettre 11-5)
#define HCHAR 11
#define LCHAR 6

//Define the "Normal" Colors
#define BLACK  0
#define RED  0xE0
#define GREEN  0x1C
#define BLUE  0x03
#define ORANGE  RED|GREEN
#define MAGENTA  RED|BLUE
#define TEAL  BLUE|GREEN
#define WHITE (RED|GREEN|BLUE)-0xA0

#define COULEUR_FOND 0x03

//Define the SPI Pin Numbers
#define DATAOUT 11//MOSI
#define DATAIN  12//MISO
#define SPICLOCK  13//sck
#define SLAVESELECT 10//ss

//Pin des micros
#define ENTREE01 2
#define ENTREE02 3

//interrupteur de selection de mode
#define INT01 4
#define INT02 5
#define INT03 6

#define NOMBRE_ENTREES 2

//Matrice globale
#define MATRIX_WIDTH       24
#define MATRIX_HEIGHT      24

#define SUBMATRIX_WIDTH    8
#define SUBMATRIX_HEIGHT   8

//Define the variables we'll need later in the program
char frame_buffer [MATRIX_WIDTH][MATRIX_HEIGHT];


int parole1 = 1,parole1_prec = 1,pourc1=50,pourc1_prec=0;
int parole2 = 1,parole2_prec = 1,pourc2=50,pourc2_prec=0;
int parole3 = 1,parole3_prec = 1,pourc3=0,pourc3_prec=0;
int parole4 = 1,parole4_prec = 1,pourc4=0,pourc4_prec=0;
int someChar;
int modeselect=1,modeselect_prec=0;
int compteur=0;// permet d'eviter que les informations soient transmise en série à chaque boucle
int modif=0;//permet d'indiquer qu'il faut mettre à jour les données séries
int rayon1 = 2*MATRIX_WIDTH / NOMBRE_ENTREES;//utilise en temps que nombre de LED de la matrice globale
int rayon2 = 2*MATRIX_WIDTH / NOMBRE_ENTREES;

void setup()
{
Serial.begin(9600);  // used for debug
//SPI Bus setup

SPCR=(1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0);
   SPSR = SPSR & B11111110;

   // Set the pin modes for the RGB matrix
   pinMode(SPICLOCK,OUTPUT);
   pinMode(DATAOUT,OUTPUT);
   pinMode(DATAIN,INPUT);
   pinMode(SLAVESELECT,OUTPUT);
   pinMode(ENTREE01,INPUT);
   pinMode(ENTREE02,INPUT);
   pinMode(INT01,INPUT);
   pinMode(INT02,INPUT);
   pinMode(INT03,INPUT);

   // Make sure the RGB matrix is deactivated
   digitalWrite(SPICLOCK,LOW);
   digitalWrite(SLAVESELECT,HIGH);
   delayMicroseconds(500); // delay as the LED Matrix datasheet's recommends


initialiseMatrice(frame_buffer,COULEUR_FOND);
displayMatrix(frame_buffer);
}


void loop()
{

#if (__MATRICE3__ ==0)
   // Detection parole des micros
   if (digitalRead (ENTREE01) == LOW){ //Micro 1
      parole1=parole1+1;
      modif=1;
   }
   if (digitalRead (ENTREE02) == LOW){ //Micro 2
      parole2=parole2+1;
      modif=1;
   }
   //bornage des compteurs
   if ((parole1>200)||(parole2>200)){
      parole1/=2;
      parole2/=2;
   }

   // Mise à jour des pourcentage pour l'affichage
   if((modif==1)&&(compteur>10)){

// Calcul du pourcentage
      pourc1=(100*parole1)/(parole1+parole2);
      if(pourc1==37) pourc1++;// permet de zapper 37 qui fait '%' en ASCII et deconfigure les matrices
      pourc2=100-pourc1;
      if(pourc2==37){
      pourc2++;
      pourc1=100-pourc2;
      }

//Envoi en série
      if(pourc1!=pourc1_prec){
      Serial.print((char)251);
      //pourc1_prec = pourc1;
      Serial.print((char)pourc1);
      }
      if(pourc2!=pourc2_prec){
      Serial.print((char)252);
      //pourc2_prec = pourc2;
      Serial.print((char)pourc2);
      }
      if(pourc3!=pourc3_prec){
      Serial.print((char)253);
      //pourc3_prec = pourc3;
      Serial.print((char)pourc3);
      }
      if(pourc4!=pourc4_prec){
      Serial.print((char)254);
      //pourc4_prec = pourc4;
      Serial.print((char)pourc4);
      }

      compteur=0;
   }

#else
//reception série
      //if(Serial.available() > 0){
      char someChar = Serial.read();
      if (someChar==(char)251){
	    //affichageNombre(frame_buffer,6,16, RED,1);
	    pourc1 = (char)Serial.read();
	    modif=1;
      } else if (someChar==(char)252){
	    pourc2 = (char)Serial.read();
	    modif=1;
      } else if (someChar==(char)253){
	    pourc3 = (char)Serial.read();
	    modif=1;
      } else if (someChar==(char)254){
	    pourc4 = (char)Serial.read();
	    modif=1;
      }

#endif

// changement de mode
if ((digitalRead (INT01) == HIGH)&&(modeselect!= 1)){
   modeselect = 1;
} else if ((digitalRead (INT02) == HIGH)&&(modeselect!= 2)){
   modeselect = 2;
} else if ((digitalRead (INT03) == HIGH)&&(modeselect!= 3)){
   modeselect = 3;
}
if (modeselect!= modeselect_prec){ // si on change de mode on initialise les matrices
   initialiseMatrice(frame_buffer,COULEUR_FOND);
   modeselect_prec=modeselect;
   compteur=11;
   modif=1;
}
//affichage
if(modif==1){
   switch (modeselect) {
      case 1:
      affichagePourcentage2();
      break;
      case 2:
      cercles2();
      break;
      case 3:
      carreAleatoire2();
      break;
      default:
      affichagePourcentage2();
      break;
   }

   displayMatrix(frame_buffer);
   pourc1_prec=pourc1;
   pourc2_prec=pourc2;
   modif=0;
}

delay(10); // allow some time for the Serial data to be sent
compteur++;
}

//Use this command to send a single color value to the RGB matrix.
//NOTE: You must send 64 color values to the RGB matrix before it displays an image!
char spi_transfer(volatile char data)
{
   SPDR = data;                    // Start the transmission
   while (!(SPSR & (1<<SPIF))){};  // Wait for the end of the transmission
   return SPDR;                    // return the received byte
}

void affichageParole1(){ // affichage du 1er compteur de parole sur la premiere ligne
affichageNombre(frame_buffer,0,0, GREEN,(parole1/100)%10);
   affichageNombre(frame_buffer,8,0, GREEN,(parole1/10)%10);
   affichageNombre(frame_buffer,16,0, GREEN,parole1%10);
}

   //permet d'afficher une matrice du programme sur la matrice de led
void displayMatrix(char matrix[MATRIX_WIDTH][MATRIX_HEIGHT])
{
   int index=0;
   #if (__MATRICE3__ ==2)
   int height = 8 / SUBMATRIX_HEIGHT; // 1
   int width = 24 / SUBMATRIX_WIDTH;    // 3
   #else
   int height = 16 / SUBMATRIX_HEIGHT; // 2
   int width = 24 / SUBMATRIX_WIDTH;    // 3
   #endif
   for(int i=height-1;i>=0;i--)     // 0 a 0
   for(int j=width-1;j>=0;j--){   // 3 a 0
      digitalWrite(SLAVESELECT, LOW);
      delayMicroseconds(500);
      for(int k=0;k<SUBMATRIX_HEIGHT;k++)
	 for(int l=SUBMATRIX_WIDTH-1;l>=0;l--){
	 int y=(i+__MATRICE3__)*SUBMATRIX_HEIGHT+k;
	 int x=j*SUBMATRIX_WIDTH+l;
	 char color=matrix[x][y];
	 spi_transfer(color);
	 }
      digitalWrite(SLAVESELECT, HIGH);
      delayMicroseconds(10);
      }
}

//afficha
void carreAleatoire2(){
   int i,j;
   if (pourc1>pourc1_prec){
   do{
      i=random(24);
      j=random(24);
   } while (frame_buffer[i][j]==RED);
   frame_buffer[i][j]=RED;

   } else if (pourc1<pourc1_prec){
   do{
      i=random(24);
      j=random(24);
   } while (frame_buffer[i][j]==GREEN);
   frame_buffer[i][j]=GREEN;

   }
}

//mode affichage arc de cercle
void cercles2(){

   initialiseMatrice(frame_buffer,COULEUR_FOND);

   rayon1=1.4*(MATRIX_WIDTH*pourc1)/100;
   rayon2=1.4*(MATRIX_WIDTH*pourc2)/100;
      for (int i=0;i<MATRIX_WIDTH;i++){
      for (int j=0; j<MATRIX_HEIGHT; j++){
	 if(((i*i)+(j*j))<=((rayon1)*(rayon1))){
	    frame_buffer [i][j]=RED;
	 } else if((((23-i)*(23-i))+((23-j)*(23-j)))<=((rayon2)*(rayon2))){
	    frame_buffer [i][j]=GREEN;
	 }
      }
      }
}

//affichage des pourcentages
void affichagePourcentage2(){

if(pourc1!=pourc1_prec){
   affichageNombre(frame_buffer,0,0, RED,(pourc1/10)%10);
   affichageNombre(frame_buffer,8,0, RED,pourc1%10);

}
if(pourc2!=pourc2_prec){
   affichageNombre(frame_buffer,8,12, GREEN,(pourc2/10)%10);
   affichageNombre(frame_buffer,16,12, GREEN,pourc2%10);

}

}

void affichagePourcentage4(){
   pourc1=100*parole1/(parole1+parole2+parole3+parole4);
pourc2=100*parole2/(parole1+parole2+parole3+parole4);
pourc3=100*parole3/(parole1+parole2+parole3+parole4);
pourc4=100*parole4/(parole1+parole2+parole3+parole4);

if(pourc1!=pourc1_prec){
   affichageNombre(frame_buffer,0,0, RED,pourc1/10);
   affichageNombre(frame_buffer,6,0, RED,pourc1%10);
   pourc1_prec=pourc1;
}
if(pourc2!=pourc2_prec){
   affichageNombre(frame_buffer,12,0, GREEN,pourc2/10);
   affichageNombre(frame_buffer,18,0, GREEN,pourc2%10);
   pourc2_prec=pourc2;
}
if(pourc3!=pourc3_prec){
   affichageNombre(frame_buffer,0,12, ORANGE,pourc3/10);
   affichageNombre(frame_buffer,6,12, ORANGE,pourc3%10);
   pourc3_prec=pourc3;
}
if(pourc4!=pourc4_prec){
   affichageNombre(frame_buffer,12,12, TEAL,pourc4/10);
   affichageNombre(frame_buffer,18,12, TEAL,pourc4%10);
   pourc4_prec=pourc4;
}
}

void initialiseMatrice(char matrix[MATRIX_WIDTH][MATRIX_HEIGHT],int couleur){
for (int i=0;i<MATRIX_WIDTH;i++){
   for (int j=0; j<MATRIX_HEIGHT; j++){
      matrix[i][j]=couleur;
   }

}
}

//permet d'ecrire un nombre (pour affichage) à des coordonnées précisées dans une matrice
void affichageNombre(char matrix[MATRIX_WIDTH][MATRIX_HEIGHT],int x,int y,int couleur,int nombre){
for (int i=x;i<x+LCHAR+1;i++){
   for (int j=y; j<y+HCHAR+1; j++){
      matrix[i][j]=COULEUR_FOND;
   }

}
   switch (nombre) {
   case 1:
      un(frame_buffer,x,y,couleur);
      break;
   case 2:
      deux(frame_buffer,x,y,couleur);
      break;
   case 3:
      trois(frame_buffer,x,y,couleur);
      break;
   case 4:
      quatre(frame_buffer,x,y,couleur);
      break;
   case 5:
      cinq(frame_buffer,x,y,couleur);
      break;
   case 6:
      six(frame_buffer,x,y,couleur);
      break;
   case 7:
      sept(frame_buffer,x,y,couleur);
      break;
   case 8:
      huit(frame_buffer,x,y,couleur);
      break;
   case 9:
      neuf(frame_buffer,x,y,couleur);
      break;
   case 0:
      zero(frame_buffer,x,y,couleur);
      break;
   default:
      erreur(frame_buffer,x,y,couleur);
      break;

}


}

//permet de dessiner une ligne
void ligne(char matrix[MATRIX_WIDTH][MATRIX_HEIGHT],int x1,int y1, int x2, int y2,int couleur){
   int i=0;
   if(x1==x2){
   for(i=y1;i<y2;i++){
      matrix[x1][i]=couleur;
   }
   } else if (y1==y2){
   for(i=x1;i<x2;i++){
      matrix[i][y1]=couleur;
   }
   }

}

void erreur(char matrix[MATRIX_WIDTH][MATRIX_HEIGHT],int x,int y,int couleur){
   a(frame_buffer,x,y,couleur);
   d(frame_buffer,x,y,couleur);
   g(frame_buffer,x,y,couleur);
}
void un(char matrix[MATRIX_WIDTH][MATRIX_HEIGHT],int x,int y,int couleur){
   b(frame_buffer,x,y,couleur);
   c(frame_buffer,x,y,couleur);
}
void deux(char matrix[MATRIX_WIDTH][MATRIX_HEIGHT],int x,int y,int couleur){
   a(frame_buffer,x,y,couleur);
   b(frame_buffer,x,y,couleur);
   d(frame_buffer,x,y,couleur);
   e(frame_buffer,x,y,couleur);
   g(frame_buffer,x,y,couleur);
}
void trois(char matrix[MATRIX_WIDTH][MATRIX_HEIGHT],int x,int y,int couleur){
   a(frame_buffer,x,y,couleur);
   b(frame_buffer,x,y,couleur);
   c(frame_buffer,x,y,couleur);
   d(frame_buffer,x,y,couleur);
   g(frame_buffer,x,y,couleur);
}
void quatre(char matrix[MATRIX_WIDTH][MATRIX_HEIGHT],int x,int y,int couleur){
   b(frame_buffer,x,y,couleur);
   c(frame_buffer,x,y,couleur);
   f(frame_buffer,x,y,couleur);
   g(frame_buffer,x,y,couleur);
}
void cinq(char matrix[MATRIX_WIDTH][MATRIX_HEIGHT],int x,int y,int couleur){
   a(frame_buffer,x,y,couleur);
   c(frame_buffer,x,y,couleur);
   d(frame_buffer,x,y,couleur);
   f(frame_buffer,x,y,couleur);
   g(frame_buffer,x,y,couleur);
}
void six(char matrix[MATRIX_WIDTH][MATRIX_HEIGHT],int x,int y,int couleur){
   a(frame_buffer,x,y,couleur);
   c(frame_buffer,x,y,couleur);
   d(frame_buffer,x,y,couleur);
   e(frame_buffer,x,y,couleur);
   f(frame_buffer,x,y,couleur);
   g(frame_buffer,x,y,couleur);
}
void sept(char matrix[MATRIX_WIDTH][MATRIX_HEIGHT],int x,int y,int couleur){
   a(frame_buffer,x,y,couleur);
   b(frame_buffer,x,y,couleur);
   c(frame_buffer,x,y,couleur);
}
void huit(char matrix[MATRIX_WIDTH][MATRIX_HEIGHT],int x,int y,int couleur){
   a(frame_buffer,x,y,couleur);
   b(frame_buffer,x,y,couleur);
   c(frame_buffer,x,y,couleur);
   d(frame_buffer,x,y,couleur);
   e(frame_buffer,x,y,couleur);
   f(frame_buffer,x,y,couleur);
   g(frame_buffer,x,y,couleur);
}
void neuf(char matrix[MATRIX_WIDTH][MATRIX_HEIGHT],int x,int y,int couleur){
   a(frame_buffer,x,y,couleur);
   b(frame_buffer,x,y,couleur);
   c(frame_buffer,x,y,couleur);
   d(frame_buffer,x,y,couleur);
   f(frame_buffer,x,y,couleur);
   g(frame_buffer,x,y,couleur);
}
void zero(char matrix[MATRIX_WIDTH][MATRIX_HEIGHT],int x,int y,int couleur){
   a(frame_buffer,x,y,couleur);
   b(frame_buffer,x,y,couleur);
   c(frame_buffer,x,y,couleur);
   d(frame_buffer,x,y,couleur);
   e(frame_buffer,x,y,couleur);
   f(frame_buffer,x,y,couleur);
}

//barres des systèmes d'affichage 7 segments
void a(char matrix[MATRIX_WIDTH][MATRIX_HEIGHT],int x,int y,int couleur){
   ligne(matrix,x,y,x+LCHAR+1,y,couleur);//haut
}
void b(char matrix[MATRIX_WIDTH][MATRIX_HEIGHT],int x,int y,int couleur){
   ligne(matrix,x+LCHAR,y,x+LCHAR,y+(HCHAR/2),couleur);//haut
}
void c(char matrix[MATRIX_WIDTH][MATRIX_HEIGHT],int x,int y,int couleur){
   ligne(matrix,x+LCHAR,y+(HCHAR/2),x+LCHAR,y+HCHAR+1,couleur);//haut
}
void d(char matrix[MATRIX_WIDTH][MATRIX_HEIGHT],int x,int y,int couleur){
   ligne(matrix,x,y+HCHAR,x+LCHAR+1,y+HCHAR,couleur);//haut
}
void e(char matrix[MATRIX_WIDTH][MATRIX_HEIGHT],int x,int y,int couleur){
   ligne(matrix,x,y+(HCHAR/2),x,y+HCHAR+1,couleur);//haut
}
void f(char matrix[MATRIX_WIDTH][MATRIX_HEIGHT],int x,int y,int couleur){
   ligne(matrix,x,y,x,y+(HCHAR/2),couleur);//haut
}

void g(char matrix[MATRIX_WIDTH][MATRIX_HEIGHT],int x,int y,int couleur){
   ligne(matrix,x,y+(HCHAR/2),x+LCHAR+1,y+(HCHAR/2),couleur);//haut
}