简体中文
English
简体中文
日本語

Arduino 上手教程

2. 设备开发 & 案例程序

5. 扩展模块

6. 应用案例

Unit FlashLight Arduino 使用教程

1. 准备工作

2. 注意事项

引脚兼容性
由于每款主机的引脚配置不同,为了让用户更方便地使用,M5Stack 官方提供了引脚兼容性表,方便用户查看,请根据实际引脚连接情况修改案例程序。

3. 案例程序

  • 本教程中使用的主控设备为 CoreS3,搭配 Unit FlashLight。Unit FlashLight 连接到 CoreS3 的 Port B,灯光控制信号使用 G9。
cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
#include <M5Unified.h>
#include <M5UnitUnified.h>
#include <M5UnitUnifiedFLASHLIGHT.h>

using namespace m5::unit::aw3641e;

constexpr uint16_t UI_BG     = 0x1082;
constexpr uint16_t UI_PANEL  = 0x2104;
constexpr uint16_t UI_HEADER = 0x0349;
constexpr uint16_t UI_MUTED  = 0xBDF7;
constexpr uint8_t TORCH_BRIGHTNESS = 10;
constexpr uint32_t PWM_FREQUENCY = 1000;
constexpr uint8_t PWM_RESOLUTION = 8;
constexpr uint8_t PWM_DUTY = (256u * TORCH_BRIGHTNESS + 50u) / 100u;
constexpr uint8_t PWM_CHANNEL = 7;

m5::unit::UnitUnified units;
m5::unit::UnitFlashLight unit;
M5Canvas canvas(&M5.Display);
int torch_pin = -1;

bool configureTorchPwm()
{
#if defined(ESP_ARDUINO_VERSION_MAJOR) && ESP_ARDUINO_VERSION_MAJOR >= 3
    if (!ledcAttach(torch_pin, PWM_FREQUENCY, PWM_RESOLUTION)) {
        return false;
    }
    ledcWrite(torch_pin, PWM_DUTY);
#else
    if (ledcSetup(PWM_CHANNEL, PWM_FREQUENCY, PWM_RESOLUTION) == 0) {
        return false;
    }
    ledcAttachPin(torch_pin, PWM_CHANNEL);
    ledcWrite(PWM_CHANNEL, PWM_DUTY);
#endif
    return true;
}

void drawStatus(const char* state)
{
    const unsigned percent = TORCH_BRIGHTNESS;
    char brightness[8] = {};
    snprintf(brightness, sizeof(brightness), "%u%%", percent);

    canvas.fillScreen(UI_BG);
    canvas.fillRect(0, 0, 320, 42, UI_HEADER);
    canvas.setTextDatum(textdatum_t::middle_center);
    canvas.setFont(&fonts::Font4);
    canvas.setTextSize(1);
    canvas.setTextColor(TFT_WHITE, UI_HEADER);
    canvas.drawString("UNIT FLASHLIGHT", 160, 16);
    canvas.setFont(&fonts::Font2);
    canvas.setTextColor(UI_MUTED, UI_HEADER);
    canvas.drawString("FLASHLIGHT  /  STATUS", 160, 33);

    canvas.fillRoundRect(12, 54, 296, 42, 6, UI_PANEL);
    canvas.drawRoundRect(12, 54, 296, 42, 6, TFT_ORANGE);
    canvas.setTextDatum(textdatum_t::middle_left);
    canvas.setTextColor(UI_MUTED, UI_PANEL);
    canvas.drawString("MODE", 24, 75);
    canvas.setTextColor(TFT_ORANGE, UI_PANEL);
    canvas.drawString("TORCH", 88, 75);
    canvas.setTextColor(TFT_WHITE, UI_PANEL);
    canvas.drawString("ON", 205, 75);

    canvas.fillRoundRect(12, 108, 296, 88, 6, UI_PANEL);
    canvas.drawRoundRect(12, 108, 296, 88, 6, UI_MUTED);
    canvas.setFont(&fonts::Font2);
    canvas.setTextDatum(textdatum_t::middle_left);
    canvas.setTextColor(UI_MUTED, UI_PANEL);
    canvas.drawString("BRIGHTNESS", 24, 124);
    canvas.setFont(&fonts::Font4);
    canvas.setTextDatum(textdatum_t::middle_center);
    canvas.setTextColor(TFT_YELLOW, UI_PANEL);
    canvas.drawString(brightness, 160, 140);
    canvas.fillRoundRect(54, 160, 212, 14, 4, UI_BG);
    canvas.fillRoundRect(54, 160, 212 * percent / 100, 14, 4, TFT_YELLOW);
    canvas.setFont(&fonts::Font2);
    canvas.setTextDatum(textdatum_t::middle_center);
    canvas.setTextColor(UI_MUTED, UI_PANEL);
    canvas.drawString("1000 Hz PWM", 160, 185);

    canvas.fillRect(0, 204, 320, 36, UI_HEADER);
    canvas.setTextDatum(textdatum_t::middle_left);
    canvas.setTextColor(TFT_WHITE, UI_HEADER);
    canvas.drawString("STATE", 16, 222);
    canvas.setTextColor(TFT_CYAN, UI_HEADER);
    canvas.drawString(state, 82, 222);
    canvas.setTextColor(UI_MUTED, UI_HEADER);
    canvas.drawString("CONTINUOUS", 206, 222);
    canvas.pushSprite(0, 0);
}

void setup()
{
    M5.begin();
    Serial.begin(115200);
    canvas.setColorDepth(16);
    canvas.createSprite(320, 240);
    auto cfg = unit.config();
    cfg.switch_position = SwitchPosition::Torch;
    unit.config(cfg);

    torch_pin = M5.getPin(m5::pin_name_t::port_b_pin2);
    if (!units.add(unit, -1, torch_pin) || !units.begin() ||
        !configureTorchPwm()) {
        drawStatus("ERROR");
        while (true) {
            delay(1000);
        }
    }
    drawStatus("Torch 10%");
}

void loop()
{
    M5.update();
    units.update();
}

4. 编译上传

  • 复制粘贴上述例程代码到项目代码区,选中设备端口(详情请参考 程序编译与烧录),点击 Arduino IDE 左上角编译上传按钮,等待程序完成编译并上传至设备。

5. 闪光灯手电筒模式

  • 程序启动后,Unit FlashLight 会在 Torch 模式下持续点亮,控制信号使用 1kHz、10% 占空比 PWM,主控屏幕显示当前状态。Torch 模式的实际 LED 恒流由驱动芯片固定,10% 是控制信号的平均占空比;请避免直视灯光,并保证 Unit 有稳定的 5V 供电。
Page Tools
PDF
On This Page