|
| 1 | +#include <Arduino.h> |
| 2 | +#include <M5Cardputer.h> |
| 3 | +#include <FastLED.h> |
| 4 | + |
| 5 | +#define PIN_PIR 1 // Movement Detector |
| 6 | +#define PIN_LED 21 // Builtin |
| 7 | +#define NUM_LEDS 1 |
| 8 | + |
| 9 | +CRGB leds[NUM_LEDS]; // init RGB LED builtin |
| 10 | +int lastState = -1; // Save last detection state |
| 11 | + |
| 12 | +void displayMainView() { |
| 13 | + |
| 14 | + // Config |
| 15 | + M5Cardputer.Display.setRotation(1); |
| 16 | + M5Cardputer.Display.setTextColor(GREEN); |
| 17 | + M5Cardputer.Display.setFont(&fonts::Orbitron_Light_24); |
| 18 | + M5Cardputer.Display.setTextSize(1); |
| 19 | + |
| 20 | + // Main Title |
| 21 | + M5Cardputer.Display.drawRoundRect(0, 0, 240, 30, 5, TFT_DARKGREY); // frame around titlte |
| 22 | + M5Cardputer.Display.setTextColor(TFT_LIGHTGREY); |
| 23 | + M5Cardputer.Display.setTextSize(0.8); |
| 24 | + M5Cardputer.Display.setCursor(16, 3); |
| 25 | + M5Cardputer.Display.printf("Movement Detector"); |
| 26 | + |
| 27 | + // Menu |
| 28 | + M5Cardputer.Display.drawRoundRect(0, 108, 240, 30, 4, TFT_DARKGREY); // frame around controls |
| 29 | + M5Cardputer.Display.setTextSize(0.65); |
| 30 | + M5Cardputer.Display.setCursor(14, 112); |
| 31 | + M5Cardputer.Display.printf("Brightness : < > Vol : v "); |
| 32 | + |
| 33 | + // Volume area |
| 34 | + M5Cardputer.Display.drawRoundRect(153, 40, 82, 58, 5, TFT_LIGHTGREY); |
| 35 | + M5Cardputer.Display.setCursor(162, 45); |
| 36 | + M5Cardputer.Display.setTextSize(0.5); |
| 37 | + M5Cardputer.Display.setTextColor(TFT_DARKGREY); |
| 38 | + M5Cardputer.Display.println("VOLUME"); |
| 39 | + |
| 40 | + // Volume value |
| 41 | + M5Cardputer.Display.setCursor(165, 60); |
| 42 | + M5Cardputer.Display.setTextSize(1); |
| 43 | + M5Cardputer.Display.setTextColor(TFT_LIGHTGREY); |
| 44 | + M5Cardputer.Speaker.setVolume(40); |
| 45 | + M5Cardputer.Display.print(M5Cardputer.Speaker.getVolume()); |
| 46 | + |
| 47 | + // Dessiner la lettre "V" à l'envers pour volume controls |
| 48 | + int startX = 218; // Position de départ X |
| 49 | + int startY = 119; // Position de départ Y |
| 50 | + int height = 8; // Hauteur de la lettre |
| 51 | + int width = 10; // Largeur de la lettre |
| 52 | + M5.Lcd.drawLine(startX, startY + height, startX + width / 2, startY, TFT_LIGHTGREY); // Ligne gauche |
| 53 | + M5.Lcd.drawLine(startX + width / 2, startY, startX + width, startY + height, TFT_LIGHTGREY); // Ligne droite |
| 54 | +} |
| 55 | + |
| 56 | +void displayVolume(uint8_t volume) { |
| 57 | + M5Cardputer.Display.fillRect(155, 65, 68, 25, BLACK); // reset area |
| 58 | + M5Cardputer.Display.setTextSize(1); |
| 59 | + M5Cardputer.Display.setTextColor(TFT_LIGHTGREY); |
| 60 | + M5Cardputer.Display.setCursor(165, 60); |
| 61 | + M5Cardputer.Display.print(volume); |
| 62 | +} |
| 63 | + |
| 64 | +void displayDetection(int state) { |
| 65 | + if (state == LOW) { // No move |
| 66 | + M5Cardputer.Display.fillRect(0, 40, 145, 58, BLACK); |
| 67 | + M5Cardputer.Display.drawRoundRect(5, 40, 140, 58, 5, GREEN); |
| 68 | + M5Cardputer.Display.setCursor(22, 55); |
| 69 | + M5Cardputer.Display.setTextColor(GREEN); |
| 70 | + M5Cardputer.Display.setTextSize(0.9); |
| 71 | + M5Cardputer.Display.println("No move"); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + // Movement detected |
| 76 | + M5Cardputer.Display.fillRect(0, 40, 145, 58, BLACK); // reset area |
| 77 | + M5Cardputer.Display.fillRoundRect(5, 40, 140, 58, 5, RED); // frame around |
| 78 | + M5Cardputer.Display.setTextColor(TFT_LIGHTGREY); |
| 79 | + M5Cardputer.Display.setCursor(23, 48); |
| 80 | + M5Cardputer.Display.setTextSize(0.75); |
| 81 | + M5Cardputer.Display.println("Movement"); |
| 82 | + M5Cardputer.Display.setCursor(30, 70); |
| 83 | + M5Cardputer.Display.println("detected"); |
| 84 | +} |
| 85 | + |
| 86 | +void toggleLED() { |
| 87 | + if (M5Cardputer.Display.getBrightness() != 0) { |
| 88 | + FastLED.show(); // If screen is on, led is on |
| 89 | + } else { |
| 90 | + FastLED.clear(true); // led off |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +void handleKeyboard() { |
| 95 | + if (M5Cardputer.Keyboard.isChange()) { |
| 96 | + if (M5Cardputer.Keyboard.isPressed()) { |
| 97 | + // VOLUME UP |
| 98 | + if (M5Cardputer.Keyboard.isKeyPressed(';')) { |
| 99 | + uint8_t volume = M5Cardputer.Speaker.getVolume(); |
| 100 | + if (volume+20 < 255) { |
| 101 | + volume += 20; |
| 102 | + } else { |
| 103 | + volume = 255; |
| 104 | + } |
| 105 | + displayVolume(volume); |
| 106 | + M5Cardputer.Speaker.setVolume(volume); |
| 107 | + } |
| 108 | + |
| 109 | + // VOLUME DOWN |
| 110 | + else if (M5Cardputer.Keyboard.isKeyPressed('.')) { |
| 111 | + uint8_t volume = M5Cardputer.Speaker.getVolume(); |
| 112 | + if (volume-20 > 0) { |
| 113 | + volume -= 20; |
| 114 | + } else { |
| 115 | + volume = 0; |
| 116 | + } |
| 117 | + displayVolume(volume); |
| 118 | + M5Cardputer.Speaker.setVolume(volume); |
| 119 | + } |
| 120 | + |
| 121 | + // BRIGHTNESS UP |
| 122 | + else if (M5Cardputer.Keyboard.isKeyPressed('/')) { |
| 123 | + uint8_t brightness = M5Cardputer.Display.getBrightness(); |
| 124 | + if (brightness+20 < 255) { |
| 125 | + brightness += 20; |
| 126 | + } else { |
| 127 | + brightness = 255; |
| 128 | + } |
| 129 | + M5Cardputer.Display.setBrightness(brightness); |
| 130 | + } |
| 131 | + |
| 132 | + // BRIGHTNESS DOWN |
| 133 | + else if (M5Cardputer.Keyboard.isKeyPressed(',')) { |
| 134 | + uint8_t brightness = M5Cardputer.Display.getBrightness(); |
| 135 | + if (brightness-20 > 0) { |
| 136 | + brightness -= 20; |
| 137 | + } else { |
| 138 | + brightness = 0; |
| 139 | + } |
| 140 | + M5Cardputer.Display.setBrightness(brightness); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +/* |
| 147 | +############################################################################################## |
| 148 | +# # |
| 149 | +# SETUP & LOOP # |
| 150 | +# # |
| 151 | +############################################################################################## |
| 152 | +*/ |
| 153 | + |
| 154 | +void setup() { |
| 155 | + auto cfg = M5.config(); // M5 default config |
| 156 | + M5Cardputer.begin(cfg, true); |
| 157 | + M5Cardputer.Speaker.setVolume(10); |
| 158 | + |
| 159 | + // Initialiser mvmnt detector |
| 160 | + pinMode(PIN_PIR, INPUT); // Configurer la broche du détecteur PIR comme entrée |
| 161 | + |
| 162 | + // Initialiser la LED BUILTIN à l'état éteint |
| 163 | + FastLED.addLeds<WS2812, PIN_LED, GRB>(leds, 1); |
| 164 | + |
| 165 | + // Display the main frame |
| 166 | + displayMainView(); |
| 167 | +} |
| 168 | + |
| 169 | +void loop() { |
| 170 | + |
| 171 | + // Update state |
| 172 | + M5Cardputer.update(); |
| 173 | + |
| 174 | + // Lire l'état du détecteur de mouvement |
| 175 | + int motionState = digitalRead(PIN_PIR); |
| 176 | + |
| 177 | + // Handle brightness/volume controls |
| 178 | + handleKeyboard(); |
| 179 | + |
| 180 | + // Si une présence est détectée |
| 181 | + if (motionState == HIGH) { |
| 182 | + // Red LED |
| 183 | + leds[0] = CRGB::Red; |
| 184 | + toggleLED(); |
| 185 | + |
| 186 | + // Set alarm |
| 187 | + M5Cardputer.Speaker.tone(4000, 20); |
| 188 | + M5Cardputer.Speaker.tone(3000, 10); |
| 189 | + |
| 190 | + } else { |
| 191 | + // Green LED |
| 192 | + leds[0] = CRGB::Green; |
| 193 | + toggleLED(); |
| 194 | + } |
| 195 | + |
| 196 | + // Render only if motionState has changed |
| 197 | + if (lastState != motionState) { |
| 198 | + displayDetection(motionState); |
| 199 | + lastState = motionState; |
| 200 | + } |
| 201 | + |
| 202 | + delay(20); |
| 203 | +} |
0 commit comments