M5Unit-FLASHLIGHT 0.0.2 git rev:4071dde
Loading...
Searching...
No Matches
unit_AW3641E.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
3 *
4 * SPDX-License-Identifier: MIT
5 */
13#ifndef M5_UNIT_FLASHLIGHT_UNIT_AW3641E_HPP
14#define M5_UNIT_FLASHLIGHT_UNIT_AW3641E_HPP
15
16#include <M5UnitComponent.hpp>
17
18namespace m5 {
19namespace unit {
20
25namespace aw3641e {
26
36enum class SwitchPosition : uint8_t {
37 Flash,
38 Torch,
39};
40
48enum class Brightness : uint8_t {
49 Pct100,
50 Pct90,
51 Pct80,
52 Pct70,
53 Pct60,
54 Pct50,
55 Pct40,
56 Pct30,
57};
58
60constexpr uint32_t PULSE_HIGH_US{4};
62constexpr uint32_t PULSE_LOW_US{4};
64constexpr uint32_t PULSE_OFF_US{600};
65
69constexpr uint16_t FLASH_MAX_DURATION_MS{220};
70
77constexpr uint16_t TORCH_MAX_DURATION_MS{1300};
78
87constexpr uint8_t to_pulse_count(const Brightness b)
88{
89 return static_cast<uint8_t>(static_cast<uint8_t>(b) + 1u);
90}
91
92} // namespace aw3641e
93
113class UnitAW3641E : public Component {
114 M5_UNIT_COMPONENT_HPP_BUILDER(UnitAW3641E, 0x00);
115
116public:
121 struct config_t {
125 aw3641e::SwitchPosition switch_position{aw3641e::SwitchPosition::Flash};
126 };
127
130 explicit UnitAW3641E(const uint8_t addr = DEFAULT_ADDRESS) : Component(addr)
131 {
132 }
134 virtual ~UnitAW3641E()
135 {
136 }
137
140 virtual bool begin() override;
143 virtual void update(const bool force = false) override;
144
150 {
151 return _cfg;
152 }
155 inline void config(const config_t& cfg)
156 {
157 _cfg = cfg;
158 }
160
163
167 bool stop();
168
187 bool flash(const aw3641e::Brightness brightness = aw3641e::Brightness::Pct100,
188 const uint16_t duration_ms = aw3641e::FLASH_MAX_DURATION_MS);
189
210 bool torch(const uint16_t duration_ms = aw3641e::TORCH_MAX_DURATION_MS);
212
217 inline uint16_t lastDurationMs() const
218 {
219 return _duration_ms;
220 }
223 inline bool active() const
224 {
225 return _active;
226 }
228
229protected:
233 bool send_pulse_train(const uint8_t pulse_count);
234
235protected:
236 config_t _cfg{};
237 // Flash/torch timing tracker for non-blocking auto-shutdown in update().
238 bool _active{false};
239 uint32_t _start_ms{0};
240 uint16_t _duration_ms{0};
241};
242
243} // namespace unit
244} // namespace m5
245
246#endif
virtual ~UnitAW3641E()
Destructor.
Definition unit_AW3641E.hpp:134
bool active() const
Is a flash or torch currently in progress?
Definition unit_AW3641E.hpp:223
bool send_pulse_train(const uint8_t pulse_count)
Send a 1-wire EN pulse train of the given count (1..8 for Brightness Pct100..Pct30)
Definition unit_AW3641E.cpp:179
bool torch(const uint16_t duration_ms=aw3641e::TORCH_MAX_DURATION_MS)
Drive the LED continuously in Torch mode for up to TORCH_MAX_DURATION_MS.
Definition unit_AW3641E.cpp:144
void config(const config_t &cfg)
Set the configuration.
Definition unit_AW3641E.hpp:155
virtual bool begin() override
Begin the unit (configures EN as OUTPUT and drives EN LOW)
Definition unit_AW3641E.cpp:62
virtual void update(const bool force=false) override
Drive EN LOW automatically when the flash/torch duration has elapsed.
Definition unit_AW3641E.cpp:87
UnitAW3641E(const uint8_t addr=DEFAULT_ADDRESS)
Constructor.
Definition unit_AW3641E.hpp:130
bool flash(const aw3641e::Brightness brightness=aw3641e::Brightness::Pct100, const uint16_t duration_ms=aw3641e::FLASH_MAX_DURATION_MS)
Fire a flash with the given brightness for up to FLASH_MAX_DURATION_MS.
Definition unit_AW3641E.cpp:109
bool stop()
Stop any in-flight flash or torch (drives EN LOW immediately)
Definition unit_AW3641E.cpp:100
config_t config()
Gets the configuration.
Definition unit_AW3641E.hpp:149
uint16_t lastDurationMs() const
Last applied on-duration in milliseconds (clamped value used for flash() or torch())
Definition unit_AW3641E.hpp:217
For AW3641E.
Top level namespace of M5Stack.
Unit-related namespace.
Settings for begin.
Definition unit_AW3641E.hpp:121
aw3641e::SwitchPosition switch_position
Declaration of the physical S1 switch position. The driver cannot detect S1 at runtime; it trusts thi...
Definition unit_AW3641E.hpp:125
Brightness
Flash brightness level (percentage of I_FLASH; Flash mode only)
Definition unit_AW3641E.hpp:48
constexpr uint32_t PULSE_LOW_US
Minimum logic-low pulse width in microseconds (T_LO; datasheet 0.75-10 µs)
Definition unit_AW3641E.hpp:62
constexpr uint32_t PULSE_HIGH_US
Minimum logic-high pulse width in microseconds (T_HI; datasheet 0.75-10 µs)
Definition unit_AW3641E.hpp:60
SwitchPosition
Position of the on-board mode selection switch (silkscreen S1, SPDT type)
Definition unit_AW3641E.hpp:36
@ Torch
S1 = Torch side. FLASH pin = GND. Use torch() to drive continuous illumination.
@ Flash
S1 = Flash side. FLASH pin = +5V. Use flash() to fire pulse-controlled flashes.
constexpr uint8_t to_pulse_count(const Brightness b)
Translate a Brightness level to the AW3641E rising-edge pulse count.
Definition unit_AW3641E.hpp:87
constexpr uint16_t TORCH_MAX_DURATION_MS
Maximum duration for a single torch() call in milliseconds.
Definition unit_AW3641E.hpp:77
constexpr uint32_t PULSE_OFF_US
Minimum EN-low delay required before a pulse sequence (T_OFF; datasheet > 500 µs)
Definition unit_AW3641E.hpp:64
constexpr uint16_t FLASH_MAX_DURATION_MS
Maximum duration for a single flash() call in milliseconds.
Definition unit_AW3641E.hpp:69