Louise Kraffe
/
test_led_rgb
just a test to export
main.cpp@0:b098b6e14465, 2021-11-17 (annotated)
- Committer:
- arkhadium
- Date:
- Wed Nov 17 13:22:26 2021 +0000
- Revision:
- 0:b098b6e14465
test for rgb led
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
arkhadium | 0:b098b6e14465 | 1 | #include "mbed.h" |
arkhadium | 0:b098b6e14465 | 2 | #include "neopixel.h" // Bibliothèque pour les LED's neopixel WS2812 |
arkhadium | 0:b098b6e14465 | 3 | |
arkhadium | 0:b098b6e14465 | 4 | #define nb_led 3 // Nombre de LED utiliser |
arkhadium | 0:b098b6e14465 | 5 | #define tension_min 0.265f // Tension min de la pile égale à 0.9V |
arkhadium | 0:b098b6e14465 | 6 | #define tension_max 0.455f // Tension max de la pile égale à 1.55V |
arkhadium | 0:b098b6e14465 | 7 | #define tension_non_alcaline 0.6f // Tension d'une pile qui dépasse les 2V donc ce n'est pas alcaline |
arkhadium | 0:b098b6e14465 | 8 | #define mode_max 5 // Nombre de mode |
arkhadium | 0:b098b6e14465 | 9 | #define cycle_nb 10 // Le nombre de charge et décharge au meme temps |
arkhadium | 0:b098b6e14465 | 10 | |
arkhadium | 0:b098b6e14465 | 11 | uint32_t rouge = 0xFF0000; |
arkhadium | 0:b098b6e14465 | 12 | uint32_t jaune = 0xFFFF00; |
arkhadium | 0:b098b6e14465 | 13 | uint32_t vert = 0x00FF00; // Pour allumer les LED's |
arkhadium | 0:b098b6e14465 | 14 | uint32_t bleu = 0x0000FF; |
arkhadium | 0:b098b6e14465 | 15 | uint32_t violet = 0xFF00FF; |
arkhadium | 0:b098b6e14465 | 16 | uint32_t non_couleur = 0x000000; |
arkhadium | 0:b098b6e14465 | 17 | |
arkhadium | 0:b098b6e14465 | 18 | float adj_lum = 0.05f; // Adjustement de l'intensité lumineuse |
arkhadium | 0:b098b6e14465 | 19 | float mes_pile = 0; // Mesurer la tension de la pile |
arkhadium | 0:b098b6e14465 | 20 | int mode = 0; // Initialiser le mode |
arkhadium | 0:b098b6e14465 | 21 | |
arkhadium | 0:b098b6e14465 | 22 | PwmOut charge(D9); // Pin PWM charge |
arkhadium | 0:b098b6e14465 | 23 | NeoPixelOut neoPixel(D11); // Pin DIN LED |
arkhadium | 0:b098b6e14465 | 24 | Pixel pixels[nb_led]; // Fonction pour allumer les LED's |
arkhadium | 0:b098b6e14465 | 25 | DigitalIn BP(D5, PullDown); // Bouton poussoir |
arkhadium | 0:b098b6e14465 | 26 | AnalogIn mes(A0); // Mesure la tension de la pile |
arkhadium | 0:b098b6e14465 | 27 | int etat_BP = 0; |
arkhadium | 0:b098b6e14465 | 28 | //Fonctions |
arkhadium | 0:b098b6e14465 | 29 | void init(); // Fonction pour Initialiser |
arkhadium | 0:b098b6e14465 | 30 | void changement_mode(); // Fonction pour changer le mode |
arkhadium | 0:b098b6e14465 | 31 | void led_init(float adj_lum); // Fonction pour initialiser les LED's |
arkhadium | 0:b098b6e14465 | 32 | void led_reset(); |
arkhadium | 0:b098b6e14465 | 33 | void eteindre_led(int led); // Fonction pour étaindre les LED's |
arkhadium | 0:b098b6e14465 | 34 | void led_controlle(int led, uint32_t color); // Fonction pour controller les LED's |
arkhadium | 0:b098b6e14465 | 35 | |
arkhadium | 0:b098b6e14465 | 36 | int main() |
arkhadium | 0:b098b6e14465 | 37 | { |
arkhadium | 0:b098b6e14465 | 38 | init(); |
arkhadium | 0:b098b6e14465 | 39 | //led_reset(); |
arkhadium | 0:b098b6e14465 | 40 | |
arkhadium | 0:b098b6e14465 | 41 | while (1) { |
arkhadium | 0:b098b6e14465 | 42 | |
arkhadium | 0:b098b6e14465 | 43 | switch(etat_BP) { |
arkhadium | 0:b098b6e14465 | 44 | |
arkhadium | 0:b098b6e14465 | 45 | case 0 : |
arkhadium | 0:b098b6e14465 | 46 | if (BP) { |
arkhadium | 0:b098b6e14465 | 47 | wait_ms(50); |
arkhadium | 0:b098b6e14465 | 48 | changement_mode(); |
arkhadium | 0:b098b6e14465 | 49 | etat_BP ++; |
arkhadium | 0:b098b6e14465 | 50 | |
arkhadium | 0:b098b6e14465 | 51 | } |
arkhadium | 0:b098b6e14465 | 52 | break; |
arkhadium | 0:b098b6e14465 | 53 | |
arkhadium | 0:b098b6e14465 | 54 | case 1 : |
arkhadium | 0:b098b6e14465 | 55 | if (!BP) { |
arkhadium | 0:b098b6e14465 | 56 | |
arkhadium | 0:b098b6e14465 | 57 | wait_ms(50); |
arkhadium | 0:b098b6e14465 | 58 | etat_BP ++; |
arkhadium | 0:b098b6e14465 | 59 | } |
arkhadium | 0:b098b6e14465 | 60 | break; |
arkhadium | 0:b098b6e14465 | 61 | |
arkhadium | 0:b098b6e14465 | 62 | case 2 : |
arkhadium | 0:b098b6e14465 | 63 | etat_BP = 0; |
arkhadium | 0:b098b6e14465 | 64 | break; |
arkhadium | 0:b098b6e14465 | 65 | |
arkhadium | 0:b098b6e14465 | 66 | default : |
arkhadium | 0:b098b6e14465 | 67 | etat_BP = 0; |
arkhadium | 0:b098b6e14465 | 68 | break; |
arkhadium | 0:b098b6e14465 | 69 | } |
arkhadium | 0:b098b6e14465 | 70 | } |
arkhadium | 0:b098b6e14465 | 71 | } |
arkhadium | 0:b098b6e14465 | 72 | |
arkhadium | 0:b098b6e14465 | 73 | |
arkhadium | 0:b098b6e14465 | 74 | void init() |
arkhadium | 0:b098b6e14465 | 75 | { |
arkhadium | 0:b098b6e14465 | 76 | led_init(adj_lum); |
arkhadium | 0:b098b6e14465 | 77 | } |
arkhadium | 0:b098b6e14465 | 78 | void changement_mode() |
arkhadium | 0:b098b6e14465 | 79 | { |
arkhadium | 0:b098b6e14465 | 80 | mode++; |
arkhadium | 0:b098b6e14465 | 81 | |
arkhadium | 0:b098b6e14465 | 82 | switch(mode) { |
arkhadium | 0:b098b6e14465 | 83 | case 0: |
arkhadium | 0:b098b6e14465 | 84 | //led_reset(); |
arkhadium | 0:b098b6e14465 | 85 | break; |
arkhadium | 0:b098b6e14465 | 86 | case 1: |
arkhadium | 0:b098b6e14465 | 87 | led_controlle(1, jaune); |
arkhadium | 0:b098b6e14465 | 88 | break; |
arkhadium | 0:b098b6e14465 | 89 | case 2: |
arkhadium | 0:b098b6e14465 | 90 | led_controlle(2, bleu); |
arkhadium | 0:b098b6e14465 | 91 | break; |
arkhadium | 0:b098b6e14465 | 92 | case 3: |
arkhadium | 0:b098b6e14465 | 93 | led_controlle(3, rouge); |
arkhadium | 0:b098b6e14465 | 94 | break; |
arkhadium | 0:b098b6e14465 | 95 | case 4: |
arkhadium | 0:b098b6e14465 | 96 | led_controlle(3, non_couleur); |
arkhadium | 0:b098b6e14465 | 97 | break; |
arkhadium | 0:b098b6e14465 | 98 | default: |
arkhadium | 0:b098b6e14465 | 99 | //led_reset(); |
arkhadium | 0:b098b6e14465 | 100 | mode = 0; |
arkhadium | 0:b098b6e14465 | 101 | break; |
arkhadium | 0:b098b6e14465 | 102 | } |
arkhadium | 0:b098b6e14465 | 103 | wait_ms(10); |
arkhadium | 0:b098b6e14465 | 104 | } |
arkhadium | 0:b098b6e14465 | 105 | |
arkhadium | 0:b098b6e14465 | 106 | //////////////////////////////////// LED //////////////////////////////////// |
arkhadium | 0:b098b6e14465 | 107 | void led_init(float adj_lum) |
arkhadium | 0:b098b6e14465 | 108 | { |
arkhadium | 0:b098b6e14465 | 109 | wait_ms(20); // wait for HSE to stabilize |
arkhadium | 0:b098b6e14465 | 110 | neoPixel.global_scale = adj_lum; // Adjust brightness |
arkhadium | 0:b098b6e14465 | 111 | neoPixel.normalize = false; // Equalize brightness to make r + g + b = 255 |
arkhadium | 0:b098b6e14465 | 112 | } |
arkhadium | 0:b098b6e14465 | 113 | |
arkhadium | 0:b098b6e14465 | 114 | void led_reset() |
arkhadium | 0:b098b6e14465 | 115 | { |
arkhadium | 0:b098b6e14465 | 116 | for(int i=1; i==4; i++) { |
arkhadium | 0:b098b6e14465 | 117 | //pixels[i].hex = non_couleur; |
arkhadium | 0:b098b6e14465 | 118 | led_controlle(i,jaune); |
arkhadium | 0:b098b6e14465 | 119 | } |
arkhadium | 0:b098b6e14465 | 120 | } |
arkhadium | 0:b098b6e14465 | 121 | |
arkhadium | 0:b098b6e14465 | 122 | void led_controlle(int led, uint32_t color) |
arkhadium | 0:b098b6e14465 | 123 | { |
arkhadium | 0:b098b6e14465 | 124 | |
arkhadium | 0:b098b6e14465 | 125 | pixels[led-1].hex = color; |
arkhadium | 0:b098b6e14465 | 126 | neoPixel.send(pixels, nb_led); |
arkhadium | 0:b098b6e14465 | 127 | } |
arkhadium | 0:b098b6e14465 | 128 | |
arkhadium | 0:b098b6e14465 | 129 | void eteindre_led(int led) |
arkhadium | 0:b098b6e14465 | 130 | { |
arkhadium | 0:b098b6e14465 | 131 | |
arkhadium | 0:b098b6e14465 | 132 | |
arkhadium | 0:b098b6e14465 | 133 | } |