Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@7:232b7abac027, 2021-03-31 (annotated)
- Committer:
- lauric
- Date:
- Wed Mar 31 13:35:37 2021 +0000
- Revision:
- 7:232b7abac027
- Parent:
- 6:f8cb706c480c
Salut les gros;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| lauric | 7:232b7abac027 | 1 | #include "mbed.h" //Définition Mbde |
| lauric | 7:232b7abac027 | 2 | #include "C12832.h" //Librairie du LCD |
| lauric | 7:232b7abac027 | 3 | |
| lauric | 7:232b7abac027 | 4 | #define RED 0b100 |
| lauric | 7:232b7abac027 | 5 | #define GREEN 0b010 |
| lauric | 7:232b7abac027 | 6 | #define BLUE 0b001 |
| lauric | 7:232b7abac027 | 7 | #define BLACK 0b000 |
| dreschpe | 0:f6a57b843f79 | 8 | |
| lauric | 7:232b7abac027 | 9 | DigitalIn fire(D4); //Joystick fire |
| lauric | 7:232b7abac027 | 10 | DigitalOut LED_rouge(D5); //Led rouge |
| lauric | 7:232b7abac027 | 11 | DigitalOut LED_bleue(D8); //Led Bleue |
| lauric | 7:232b7abac027 | 12 | DigitalOut LED_verte(D9); //Led Verte |
| lauric | 7:232b7abac027 | 13 | Timer timer; |
| lauric | 7:232b7abac027 | 14 | Ticker ticker; |
| lauric | 7:232b7abac027 | 15 | C12832 lcd(D11, D13, D12, D7, D10); //Broches pour LCD définies par la librairie C12832 |
| lauric | 7:232b7abac027 | 16 | |
| lauric | 7:232b7abac027 | 17 | int last_state; |
| lauric | 7:232b7abac027 | 18 | int begin; // Temps |
| lauric | 7:232b7abac027 | 19 | |
| lauric | 7:232b7abac027 | 20 | int etat_ledVerte = 0, etat = 1; |
| lauric | 7:232b7abac027 | 21 | |
| lauric | 7:232b7abac027 | 22 | int getFireState(); |
| lauric | 7:232b7abac027 | 23 | bool isElapsedTime(int time); |
| lauric | 7:232b7abac027 | 24 | void setColor(int rgb); |
| lauric | 7:232b7abac027 | 25 | |
| lauric | 7:232b7abac027 | 26 | void func_ticker() |
| lauric | 7:232b7abac027 | 27 | { |
| lauric | 7:232b7abac027 | 28 | etat_ledVerte = !etat_ledVerte; |
| lauric | 7:232b7abac027 | 29 | } |
| lauric | 7:232b7abac027 | 30 | |
| dreschpe | 1:1c6a9eaf55b5 | 31 | |
| dreschpe | 0:f6a57b843f79 | 32 | int main() |
| dreschpe | 0:f6a57b843f79 | 33 | { |
| chris | 2:a87e255a8f3a | 34 | lcd.cls(); |
| lauric | 7:232b7abac027 | 35 | timer.start(); |
| lauric | 7:232b7abac027 | 36 | |
| lauric | 7:232b7abac027 | 37 | setColor(BLACK); |
| lauric | 7:232b7abac027 | 38 | |
| lauric | 7:232b7abac027 | 39 | while(true) { |
| lauric | 7:232b7abac027 | 40 | |
| lauric | 7:232b7abac027 | 41 | lcd.locate(20, 20); |
| lauric | 7:232b7abac027 | 42 | lcd.printf("Etat %d", etat); |
| lauric | 7:232b7abac027 | 43 | |
| lauric | 7:232b7abac027 | 44 | switch(etat) { |
| lauric | 7:232b7abac027 | 45 | case 1: |
| lauric | 7:232b7abac027 | 46 | |
| lauric | 7:232b7abac027 | 47 | if(getFireState() == 2) { |
| lauric | 7:232b7abac027 | 48 | setColor(RED); |
| lauric | 7:232b7abac027 | 49 | etat = 2; |
| lauric | 7:232b7abac027 | 50 | begin = timer.read_ms(); |
| lauric | 7:232b7abac027 | 51 | } |
| lauric | 7:232b7abac027 | 52 | |
| lauric | 7:232b7abac027 | 53 | break; |
| lauric | 7:232b7abac027 | 54 | case 2: |
| dreschpe | 0:f6a57b843f79 | 55 | |
| lauric | 7:232b7abac027 | 56 | if(isElapsedTime(5000)) { |
| lauric | 7:232b7abac027 | 57 | etat = 1; |
| lauric | 7:232b7abac027 | 58 | setColor(BLACK); |
| lauric | 7:232b7abac027 | 59 | } else if(getFireState() == 2) { |
| lauric | 7:232b7abac027 | 60 | begin = timer.read_ms(); |
| lauric | 7:232b7abac027 | 61 | setColor(BLACK); |
| lauric | 7:232b7abac027 | 62 | etat = 3; |
| lauric | 7:232b7abac027 | 63 | ticker.attach(&func_ticker, 0.5); |
| lauric | 7:232b7abac027 | 64 | } |
| lauric | 7:232b7abac027 | 65 | |
| lauric | 7:232b7abac027 | 66 | break; |
| lauric | 7:232b7abac027 | 67 | case 3: |
| lauric | 7:232b7abac027 | 68 | |
| lauric | 7:232b7abac027 | 69 | if(etat_ledVerte) |
| lauric | 7:232b7abac027 | 70 | setColor(RED); |
| lauric | 7:232b7abac027 | 71 | else |
| lauric | 7:232b7abac027 | 72 | setColor(BLACK); |
| lauric | 7:232b7abac027 | 73 | |
| lauric | 7:232b7abac027 | 74 | if(isElapsedTime(5000)) { |
| lauric | 7:232b7abac027 | 75 | etat = 1; |
| lauric | 7:232b7abac027 | 76 | setColor(BLACK); |
| lauric | 7:232b7abac027 | 77 | ticker.detach(); |
| lauric | 7:232b7abac027 | 78 | } else if(getFireState() == 2) { |
| lauric | 7:232b7abac027 | 79 | begin = timer.read_ms(); |
| lauric | 7:232b7abac027 | 80 | etat = 2; |
| lauric | 7:232b7abac027 | 81 | ticker.detach(); |
| lauric | 7:232b7abac027 | 82 | setColor(RED); |
| lauric | 7:232b7abac027 | 83 | } |
| lauric | 7:232b7abac027 | 84 | break; |
| lauric | 7:232b7abac027 | 85 | } |
| lauric | 7:232b7abac027 | 86 | wait(0.1); |
| lauric | 7:232b7abac027 | 87 | lcd.cls(); |
| dreschpe | 0:f6a57b843f79 | 88 | } |
| dreschpe | 0:f6a57b843f79 | 89 | } |
| lauric | 7:232b7abac027 | 90 | |
| lauric | 7:232b7abac027 | 91 | void setColor(int rgb) // RGB |
| lauric | 7:232b7abac027 | 92 | { |
| lauric | 7:232b7abac027 | 93 | LED_rouge = !(0b100 & rgb); |
| lauric | 7:232b7abac027 | 94 | LED_verte = !(0b010 & rgb); |
| lauric | 7:232b7abac027 | 95 | LED_bleue = !(0b001 & rgb); |
| lauric | 7:232b7abac027 | 96 | } |
| lauric | 7:232b7abac027 | 97 | |
| lauric | 7:232b7abac027 | 98 | bool isElapsedTime(int time) // retourne vrai si x temps c'est écoulé, update begin time |
| lauric | 7:232b7abac027 | 99 | { |
| lauric | 7:232b7abac027 | 100 | int t = timer.read_ms(); |
| lauric | 7:232b7abac027 | 101 | if(t - begin >= 5000) { |
| lauric | 7:232b7abac027 | 102 | begin = t; |
| lauric | 7:232b7abac027 | 103 | return 1; |
| lauric | 7:232b7abac027 | 104 | } else |
| lauric | 7:232b7abac027 | 105 | return 0; |
| lauric | 7:232b7abac027 | 106 | } |
| lauric | 7:232b7abac027 | 107 | |
| lauric | 7:232b7abac027 | 108 | int getFireState() // Retourne si front montant ou descendant ou rien. |
| lauric | 7:232b7abac027 | 109 | { |
| lauric | 7:232b7abac027 | 110 | int etat_fire = fire.read(); |
| lauric | 7:232b7abac027 | 111 | int etat = 0; |
| lauric | 7:232b7abac027 | 112 | if(!last_state && etat_fire) { // Front montant |
| lauric | 7:232b7abac027 | 113 | etat = 1; |
| lauric | 7:232b7abac027 | 114 | } else if(last_state && !etat_fire) { // Front descendant |
| lauric | 7:232b7abac027 | 115 | etat = 2; |
| lauric | 7:232b7abac027 | 116 | } else { |
| lauric | 7:232b7abac027 | 117 | etat = 0; |
| lauric | 7:232b7abac027 | 118 | } |
| lauric | 7:232b7abac027 | 119 | last_state = etat_fire; |
| lauric | 7:232b7abac027 | 120 | return etat; |
| lauric | 7:232b7abac027 | 121 | } |