Polytech Tours - Projet C++ embarqué sur cible mbed
MatchColors.cpp@0:21e183c9ef81, 2016-04-24 (annotated)
- Committer:
- LecomteDelys
- Date:
- Sun Apr 24 15:43:51 2016 +0000
- Revision:
- 0:21e183c9ef81
Programme final
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
LecomteDelys | 0:21e183c9ef81 | 1 | #include "MatchColors.h" |
LecomteDelys | 0:21e183c9ef81 | 2 | #define TIME_TO_WAIT 10 |
LecomteDelys | 0:21e183c9ef81 | 3 | |
LecomteDelys | 0:21e183c9ef81 | 4 | /*Constructeur*/ |
LecomteDelys | 0:21e183c9ef81 | 5 | MatchColors::MatchColors() |
LecomteDelys | 0:21e183c9ef81 | 6 | { |
LecomteDelys | 0:21e183c9ef81 | 7 | uint8_t nbColors = 0; |
LecomteDelys | 0:21e183c9ef81 | 8 | configSPI spi; |
LecomteDelys | 0:21e183c9ef81 | 9 | configI2C i2c; |
LecomteDelys | 0:21e183c9ef81 | 10 | PinName latchPin; |
LecomteDelys | 0:21e183c9ef81 | 11 | |
LecomteDelys | 0:21e183c9ef81 | 12 | /*Lecture des paramètres de jeu et de configuration des pins*/ |
LecomteDelys | 0:21e183c9ef81 | 13 | ConfigFile("/local/Config.txt", m_difficulty, m_nbleds, spi, i2c, latchPin); |
LecomteDelys | 0:21e183c9ef81 | 14 | |
LecomteDelys | 0:21e183c9ef81 | 15 | /*Instanciation du capteur de couleur*/ |
LecomteDelys | 0:21e183c9ef81 | 16 | m_colourSensor = new GroveColourSensor(i2c.sda, i2c.scl); |
LecomteDelys | 0:21e183c9ef81 | 17 | m_colourSensor->powerUp(); |
LecomteDelys | 0:21e183c9ef81 | 18 | |
LecomteDelys | 0:21e183c9ef81 | 19 | /*Définition du nombre de couleur max affichées en fonction de la difficulté sélectionnée*/ |
LecomteDelys | 0:21e183c9ef81 | 20 | switch(m_difficulty){ |
LecomteDelys | 0:21e183c9ef81 | 21 | case 0 : |
LecomteDelys | 0:21e183c9ef81 | 22 | nbColors = 3 ; |
LecomteDelys | 0:21e183c9ef81 | 23 | break; |
LecomteDelys | 0:21e183c9ef81 | 24 | case 1 : |
LecomteDelys | 0:21e183c9ef81 | 25 | nbColors = 4 ; |
LecomteDelys | 0:21e183c9ef81 | 26 | break; |
LecomteDelys | 0:21e183c9ef81 | 27 | case 2 : |
LecomteDelys | 0:21e183c9ef81 | 28 | nbColors = 5 ; |
LecomteDelys | 0:21e183c9ef81 | 29 | break; |
LecomteDelys | 0:21e183c9ef81 | 30 | default : |
LecomteDelys | 0:21e183c9ef81 | 31 | nbColors = 4 ; |
LecomteDelys | 0:21e183c9ef81 | 32 | break; |
LecomteDelys | 0:21e183c9ef81 | 33 | } |
LecomteDelys | 0:21e183c9ef81 | 34 | |
LecomteDelys | 0:21e183c9ef81 | 35 | /*Instanciation du bandeau de LEDs RGB*/ |
LecomteDelys | 0:21e183c9ef81 | 36 | m_stripe = new HL1606Stripe(spi.mosi, spi.miso, spi.sck, latchPin, m_nbleds, nbColors); |
LecomteDelys | 0:21e183c9ef81 | 37 | |
LecomteDelys | 0:21e183c9ef81 | 38 | /*Instanciation de la gestion de couleurs*/ |
LecomteDelys | 0:21e183c9ef81 | 39 | m_colormManager = new ColorManager(); |
LecomteDelys | 0:21e183c9ef81 | 40 | |
LecomteDelys | 0:21e183c9ef81 | 41 | m_randomColors = new uint8_t[m_nbleds]; |
LecomteDelys | 0:21e183c9ef81 | 42 | } |
LecomteDelys | 0:21e183c9ef81 | 43 | |
LecomteDelys | 0:21e183c9ef81 | 44 | /*Destructeur*/ |
LecomteDelys | 0:21e183c9ef81 | 45 | MatchColors::~MatchColors() |
LecomteDelys | 0:21e183c9ef81 | 46 | { |
LecomteDelys | 0:21e183c9ef81 | 47 | m_timeoutInit.detach(); |
LecomteDelys | 0:21e183c9ef81 | 48 | m_timeoutPlay.detach(); |
LecomteDelys | 0:21e183c9ef81 | 49 | m_tickerResultat.detach(); |
LecomteDelys | 0:21e183c9ef81 | 50 | |
LecomteDelys | 0:21e183c9ef81 | 51 | if(m_colourSensor != NULL) |
LecomteDelys | 0:21e183c9ef81 | 52 | delete m_colourSensor; |
LecomteDelys | 0:21e183c9ef81 | 53 | |
LecomteDelys | 0:21e183c9ef81 | 54 | if(m_stripe != NULL) |
LecomteDelys | 0:21e183c9ef81 | 55 | delete m_stripe; |
LecomteDelys | 0:21e183c9ef81 | 56 | } |
LecomteDelys | 0:21e183c9ef81 | 57 | |
LecomteDelys | 0:21e183c9ef81 | 58 | /*Initialisation d'une partie*/ |
LecomteDelys | 0:21e183c9ef81 | 59 | void MatchColors::InitGame() |
LecomteDelys | 0:21e183c9ef81 | 60 | { |
LecomteDelys | 0:21e183c9ef81 | 61 | static bool stripeIsON = false; |
LecomteDelys | 0:21e183c9ef81 | 62 | |
LecomteDelys | 0:21e183c9ef81 | 63 | m_timeoutInit.detach(); |
LecomteDelys | 0:21e183c9ef81 | 64 | |
LecomteDelys | 0:21e183c9ef81 | 65 | if(stripeIsON) { |
LecomteDelys | 0:21e183c9ef81 | 66 | /*Masquer les couleurs au joueur*/ |
LecomteDelys | 0:21e183c9ef81 | 67 | stripeIsON = false; |
LecomteDelys | 0:21e183c9ef81 | 68 | m_stripe->SwitchOffRGB(); |
LecomteDelys | 0:21e183c9ef81 | 69 | |
LecomteDelys | 0:21e183c9ef81 | 70 | /*Lancer la série de match*/ |
LecomteDelys | 0:21e183c9ef81 | 71 | m_timeoutPlay.attach(this, &MatchColors::Play, 3); |
LecomteDelys | 0:21e183c9ef81 | 72 | } else { |
LecomteDelys | 0:21e183c9ef81 | 73 | /*Remplir le bandeau de leds avec des couleurs aléatoires*/ |
LecomteDelys | 0:21e183c9ef81 | 74 | m_stripe->FillRandomlyRGB(m_randomColors); |
LecomteDelys | 0:21e183c9ef81 | 75 | stripeIsON = true; |
LecomteDelys | 0:21e183c9ef81 | 76 | m_result = false; |
LecomteDelys | 0:21e183c9ef81 | 77 | |
LecomteDelys | 0:21e183c9ef81 | 78 | /*Lancer la seconde phase de l'initialisation pour masquer les couleurs*/ |
LecomteDelys | 0:21e183c9ef81 | 79 | m_timeoutInit.attach(this, &MatchColors::InitGame, TIME_TO_WAIT-2*m_difficulty); |
LecomteDelys | 0:21e183c9ef81 | 80 | } |
LecomteDelys | 0:21e183c9ef81 | 81 | } |
LecomteDelys | 0:21e183c9ef81 | 82 | |
LecomteDelys | 0:21e183c9ef81 | 83 | void MatchColors::Play() |
LecomteDelys | 0:21e183c9ef81 | 84 | { |
LecomteDelys | 0:21e183c9ef81 | 85 | HSV hsv; |
LecomteDelys | 0:21e183c9ef81 | 86 | RGB rgb; |
LecomteDelys | 0:21e183c9ef81 | 87 | static uint8_t tour = 0; |
LecomteDelys | 0:21e183c9ef81 | 88 | Colour_t color; |
LecomteDelys | 0:21e183c9ef81 | 89 | |
LecomteDelys | 0:21e183c9ef81 | 90 | /*Lire la couleur sélectionnée par le joueur*/ |
LecomteDelys | 0:21e183c9ef81 | 91 | m_colourSensor->readColours(rgb); |
LecomteDelys | 0:21e183c9ef81 | 92 | m_colormManager->RGBtoHSV(rgb, hsv); |
LecomteDelys | 0:21e183c9ef81 | 93 | color=m_colormManager->getColorFromHue(hsv.hue); |
LecomteDelys | 0:21e183c9ef81 | 94 | |
LecomteDelys | 0:21e183c9ef81 | 95 | /*Matcher la couleur sélectionnée avec la solution*/ |
LecomteDelys | 0:21e183c9ef81 | 96 | if(color == m_randomColors[tour]) { |
LecomteDelys | 0:21e183c9ef81 | 97 | /*La couleur sélectionnée est bonne*/ |
LecomteDelys | 0:21e183c9ef81 | 98 | m_timeoutPlay.detach(); |
LecomteDelys | 0:21e183c9ef81 | 99 | m_stripe->setLED(color,tour); |
LecomteDelys | 0:21e183c9ef81 | 100 | tour++; |
LecomteDelys | 0:21e183c9ef81 | 101 | |
LecomteDelys | 0:21e183c9ef81 | 102 | /*Est ce la fin de la manche? (tour = 8)*/ |
LecomteDelys | 0:21e183c9ef81 | 103 | if(tour == m_nbleds) { |
LecomteDelys | 0:21e183c9ef81 | 104 | m_result = true; |
LecomteDelys | 0:21e183c9ef81 | 105 | tour = 0; |
LecomteDelys | 0:21e183c9ef81 | 106 | |
LecomteDelys | 0:21e183c9ef81 | 107 | /*Afficher animation de victoire*/ |
LecomteDelys | 0:21e183c9ef81 | 108 | m_tickerResultat.attach(this, &MatchColors::ShowResult, 0.125); |
LecomteDelys | 0:21e183c9ef81 | 109 | } else { |
LecomteDelys | 0:21e183c9ef81 | 110 | /*Tour suivant*/ |
LecomteDelys | 0:21e183c9ef81 | 111 | m_timeoutPlay.attach(this, &MatchColors::Play, 4); |
LecomteDelys | 0:21e183c9ef81 | 112 | } |
LecomteDelys | 0:21e183c9ef81 | 113 | } else { |
LecomteDelys | 0:21e183c9ef81 | 114 | /*La couleur sélectionnée est fausse*/ |
LecomteDelys | 0:21e183c9ef81 | 115 | m_result = false; |
LecomteDelys | 0:21e183c9ef81 | 116 | tour = 0; |
LecomteDelys | 0:21e183c9ef81 | 117 | |
LecomteDelys | 0:21e183c9ef81 | 118 | /*Afficher animation de défaite*/ |
LecomteDelys | 0:21e183c9ef81 | 119 | m_tickerResultat.attach(this, &MatchColors::ShowResult, 0.125); |
LecomteDelys | 0:21e183c9ef81 | 120 | } |
LecomteDelys | 0:21e183c9ef81 | 121 | } |
LecomteDelys | 0:21e183c9ef81 | 122 | |
LecomteDelys | 0:21e183c9ef81 | 123 | void MatchColors::ShowResult() |
LecomteDelys | 0:21e183c9ef81 | 124 | { |
LecomteDelys | 0:21e183c9ef81 | 125 | static uint8_t counter = 0; |
LecomteDelys | 0:21e183c9ef81 | 126 | static bool status = false; |
LecomteDelys | 0:21e183c9ef81 | 127 | |
LecomteDelys | 0:21e183c9ef81 | 128 | if(counter < 20) { |
LecomteDelys | 0:21e183c9ef81 | 129 | if(status) { |
LecomteDelys | 0:21e183c9ef81 | 130 | /*Animation de victoire ou de défaite*/ |
LecomteDelys | 0:21e183c9ef81 | 131 | if(m_result) { |
LecomteDelys | 0:21e183c9ef81 | 132 | m_stripe->FillRGB(GREEN); |
LecomteDelys | 0:21e183c9ef81 | 133 | } else { |
LecomteDelys | 0:21e183c9ef81 | 134 | m_stripe->FillRGB(m_randomColors); |
LecomteDelys | 0:21e183c9ef81 | 135 | } |
LecomteDelys | 0:21e183c9ef81 | 136 | } else { |
LecomteDelys | 0:21e183c9ef81 | 137 | m_stripe->SwitchOffRGB(); |
LecomteDelys | 0:21e183c9ef81 | 138 | } |
LecomteDelys | 0:21e183c9ef81 | 139 | |
LecomteDelys | 0:21e183c9ef81 | 140 | counter++; |
LecomteDelys | 0:21e183c9ef81 | 141 | status = !status; |
LecomteDelys | 0:21e183c9ef81 | 142 | |
LecomteDelys | 0:21e183c9ef81 | 143 | } else { |
LecomteDelys | 0:21e183c9ef81 | 144 | m_tickerResultat.detach(); |
LecomteDelys | 0:21e183c9ef81 | 145 | counter = 0; |
LecomteDelys | 0:21e183c9ef81 | 146 | status = false; |
LecomteDelys | 0:21e183c9ef81 | 147 | m_stripe->SwitchOffRGB(); |
LecomteDelys | 0:21e183c9ef81 | 148 | |
LecomteDelys | 0:21e183c9ef81 | 149 | /*Lancer l'initialisation d'une nouvelle partie*/ |
LecomteDelys | 0:21e183c9ef81 | 150 | m_timeoutInit.attach(this, &MatchColors::InitGame, 2); |
LecomteDelys | 0:21e183c9ef81 | 151 | } |
LecomteDelys | 0:21e183c9ef81 | 152 | } |