|
| 1 | +/* |
| 2 | +******************************************************************************* |
| 3 | +* Copyright (c) 2022 by M5Stack |
| 4 | +* Equipped with M5Core sample source code |
| 5 | +* 配套 M5Core 示例源代码 |
| 6 | +* Visit the website for more information: |
| 7 | +* 获取更多资料请访问: |
| 8 | +* |
| 9 | +* Please follow the steps below to add FastLED library: |
| 10 | +* - Arduino menu --> Manage Libraries... --> FastLED --> install |
| 11 | +* 在烧录前请按以下步骤添加 FastLED 库: |
| 12 | +* - Arduino menu --> Manage Libraries... --> FastLED --> install |
| 13 | +* |
| 14 | +* describe: KEY. 按键. |
| 15 | +* date: 2022/6/1 |
| 16 | +******************************************************************************* |
| 17 | +*/ |
| 18 | + |
| 19 | +#include <M5Stack.h> |
| 20 | +#include <FastLED.h> |
| 21 | + |
| 22 | +uint8_t ledColor = 0; |
| 23 | + |
| 24 | +#define KEY_PIN 22 // Define Key Pin. 定义Key引脚 |
| 25 | +#define DATA_PIN 21 // Define LED pin. 定义LED引脚. |
| 26 | +CRGB leds[1]; // Define the array of leds. 定义LED阵列. |
| 27 | + |
| 28 | +void LED(void *parameter); |
| 29 | +void changeLedColor(); |
| 30 | + |
| 31 | +void setup() { |
| 32 | + M5.begin(); // Init M5Stack 初始化M5Stack |
| 33 | + M5.Lcd.setTextSize(3); |
| 34 | + M5.Lcd.print(("\n UNIT-KEY Example\n\n Key State:")); |
| 35 | + |
| 36 | + pinMode(KEY_PIN, INPUT_PULLUP); // Init Key pin. 初始化Key引脚. |
| 37 | + |
| 38 | + FastLED.addLeds<SK6812, DATA_PIN, GRB>(leds, 1); // Init FastLED. 初始化FastLED. |
| 39 | + |
| 40 | + xTaskCreate( |
| 41 | + LED, "led", 1000, NULL, 0, |
| 42 | + NULL); // Create a thread for breathing LED. 创建一个线程用于LED呼吸灯. |
| 43 | +} |
| 44 | + |
| 45 | +void loop() { |
| 46 | + if (!digitalRead(KEY_PIN)) { // If Key was pressed. 如果按键按下. |
| 47 | + M5.Lcd.setCursor(75, 130); |
| 48 | + M5.Lcd.print(("Pressed ")); |
| 49 | + changeLedColor(); // Change LED color. 更换LED呼吸灯颜色. |
| 50 | + while (!digitalRead(KEY_PIN)) // Hold until the key released. 在松开按键前保持状态. |
| 51 | + ; |
| 52 | + } else { |
| 53 | + M5.Lcd.setCursor(75, 130); |
| 54 | + M5.Lcd.println(("Released")); |
| 55 | + } |
| 56 | + delay(100); |
| 57 | +} |
| 58 | + |
| 59 | +void LED(void *parameter) { |
| 60 | + leds[0] = CRGB::Red; |
| 61 | + for (;;) { |
| 62 | + for (int i = 0; i < 255; i++) { // Set LED brightness from 0 to 255. 设置LED亮度从0到255. |
| 63 | + FastLED.setBrightness(i); |
| 64 | + FastLED.show(); |
| 65 | + delay(5); |
| 66 | + } |
| 67 | + for (int i = 255; i > 0; i--) { // Set LED brightness from 255 to 0. 设置LED亮度从255到0. |
| 68 | + FastLED.setBrightness(i); |
| 69 | + FastLED.show(); |
| 70 | + delay(5); |
| 71 | + } |
| 72 | + } |
| 73 | + vTaskDelete(NULL); |
| 74 | +} |
| 75 | + |
| 76 | +void changeLedColor() { |
| 77 | + ledColor++; |
| 78 | + if (ledColor > 2) ledColor = 0; |
| 79 | + switch (ledColor) { // Change LED colors between R,G,B. 在红绿蓝中切换LED颜色. |
| 80 | + case 0: |
| 81 | + leds[0] = CRGB::Red; |
| 82 | + break; |
| 83 | + case 1: |
| 84 | + leds[0] = CRGB::Green; |
| 85 | + break; |
| 86 | + case 2: |
| 87 | + leds[0] = CRGB::Blue; |
| 88 | + break; |
| 89 | + default: |
| 90 | + break; |
| 91 | + } |
| 92 | +} |
0 commit comments