summaryrefslogtreecommitdiffstats
path: root/Digicode/Digicode.pde
blob: fbf8be65dc4afcee325724a54fca3fdd9d160783 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Synthesizer;
import javax.sound.midi.MidiChannel;
import javax.sound.midi.MidiUnavailableException;


int[] notes = { 72, 72, 72, 74, 76, 76, 74, 72, 76, 74, 74, 72, 72, 72, 72};
int timer = 0;
int numnote = 0;

int nChannelNumber = 0;
int nNoteNumber = 61;  // MIDI key number
int nVelocity = 127;
int nDuration = 32000;
MidiChannel channel;

int nbclics=0;                              // Compteur de clic
int[] code = {1, 2, 3, 4};                  // Code à trouver
int[] codeAffiche = new int[code.length];   // Tableau des touches cliquées avec la même taille que le code
boolean toucheValide = false;               // Si la touche Valider est cliquée

int rayon = 25;
int tailleTexte1 = 12;
int tailleTexte2 = 24;

void setup() {  
 frameRate(30);
 Synthesizer synth = null;
    try
    {
      synth = MidiSystem.getSynthesizer();
    }
    catch (MidiUnavailableException e)
    {
      e.printStackTrace();
      System.exit(1);
    }

    try
    {
      synth.open();
    }
    catch (MidiUnavailableException e)
    {
      e.printStackTrace();
      System.exit(1);
    }
    
    MidiChannel[]  channels = synth.getChannels();
    channel = channels[nChannelNumber];
  
    size(250, 400); // Fenêtre de 250x400 px
    for (int i=0; i<(codeAffiche.length); i++) // Initialize le tableau des touches cliquées à -1
      codeAffiche[i] = -1;
      
  background(0);
  fill(128);
  rect(25, 345, 200, 35, rayon); // Rectangle de la touche Valider
  fill(0);
  fill(128);
  textSize(tailleTexte2);
  for (int i=0; i<3 ; i++) // Pour chaque ligne
  {
    for (int j=0; j<3 ; j++) // Pour chaque colonne
    {
      fill(128);
      ellipse(25+rayon+(j*75), 125+rayon+(i*75), rayon*2, rayon*2); // Affiche le rectangle de la touche 
      fill(0);
      text((j+1)+3*(i), 25+(j*75)+18, 125+(i*75)+33); // Affiche le numéro de la touche
    }
  }
  textSize(tailleTexte2);
  text("Valider", 90, 370);
  textSize(tailleTexte1);
}

void draw() {
  timer++;

  if(timer != 0 && timer%10 == 0 && numnote < notes.length)
  {
      channel.noteOn(notes[numnote], nVelocity);
      numnote++;
      
  }
 
  boolean valide = true;
  
  fill(255);
  rect(25, 25 , 200 , 75, rayon); // Rectangle de l'affichage
  fill(0);

  if(codeAffiche[0] == -1) // Si aucune touche n'a été cliquée
  {
    text("Entrez un code à 4 chiffres.", 35, 45);
  }
  else
  {
    for (int i=0; i<(codeAffiche.length); i++) // Pour chaque touche cliquée
    {
      if(codeAffiche[i] != -1)
        text(codeAffiche[i], 35+(i*7), 45); // Affiche la touche cliquée
      if(codeAffiche[i] != code[i])
        valide = false; // Vérifie si elle correspond au code
    }
  }

  textSize(tailleTexte1);
  
  if(toucheValide) // Si la touche Valider a été cliquée
  {
    if(valide) // Et si le code est valide
      text("Code valide !", 35, 65);
    else
      text("Code invalide !", 35, 65);
  }
  
  text(nbclics, 190, 75); // Affiche le compteur de clic
}

void mousePressed() {
  for (int i=0; i<3 ; i++)
  {
    for (int j=0; j<3 ; j++)
    {
        if(sqrt( pow((50+(j*75) - (float)mouseX), 2) + pow((150+(i*75) - (float)mouseY), 2)) <= rayon) // Si une touche d'un chiffre est cliquée (distance entre le centre du cercle et l'endroit cliqué inférieur au rayon)
        {
          if(toucheValide) // Si la touche Valider a été cliquée précédemment
          {
              toucheValide = false;
              for (int k=0; k<(codeAffiche.length); k++) // Remet le tableau des touches cliquées à zéro
                codeAffiche[k] = -1;
          }

          int num = (j+1)+3*(i); // Chiffre cliqué
          channel.noteOn((num*2)+67, nVelocity);

          for (int k=0; k<(codeAffiche.length); k++)
          {
            if(codeAffiche[k] == -1) // Si le chiffre à la position k du tableau n'est pas définit
            {
              codeAffiche[k] = num; // Le définit avec le chiffre cliqué
              break;
            }
          }
        }
    }
  }
  
  if((mouseX >= 25) && (mouseY >= 350) &&
        (mouseX <= 225) && (mouseY <= 375)) // Si la touche Valider est cliquée
  {
    toucheValide = true;
  }

  nbclics++; // Augmente le compteur de clics
}