M5Unit-FLASHLIGHT 0.0.1 git rev:9a14869
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
75constexpr uint16_t TORCH_MAX_DURATION_MS{1300};
76
85constexpr uint8_t to_pulse_count(const Brightness b)
86{
87 return static_cast<uint8_t>(static_cast<uint8_t>(b) + 1u);
88}
89
90} // namespace aw3641e
91
111class UnitAW3641E : public Component {
112 M5_UNIT_COMPONENT_HPP_BUILDER(UnitAW3641E, 0x00);
113
114public:
119 struct config_t {
123 aw3641e::SwitchPosition switch_position{aw3641e::SwitchPosition::Flash};
124 };
125
128 explicit UnitAW3641E(const uint8_t addr = DEFAULT_ADDRESS) : Component(addr)
129 {
130 }
132 virtual ~UnitAW3641E()
133 {
134 }
135
138 virtual bool begin() override;
141 virtual void update(const bool force = false) override;
142
148 {
149 return _cfg;
150 }
153 inline void config(const config_t& cfg)
154 {
155 _cfg = cfg;
156 }
158
161
165 bool stop();
166
182 bool flash(const aw3641e::Brightness brightness = aw3641e::Brightness::Pct100,
183 const uint16_t duration_ms = aw3641e::FLASH_MAX_DURATION_MS);
184
201 bool torch(const uint16_t duration_ms = aw3641e::TORCH_MAX_DURATION_MS);
203
208 inline uint16_t lastDurationMs() const
209 {
210 return _flash_duration_ms;
211 }
214 inline bool active() const
215 {
216 return _flash_active;
217 }
219
220protected:
224 bool send_pulse_train(const uint8_t pulse_count);
225
226protected:
227 config_t _cfg{};
228 // Flash timing tracker for non-blocking auto-shutdown in update().
229 bool _flash_active{false};
230 uint32_t _flash_start_ms{0};
231 uint16_t _flash_duration_ms{0};
232};
233
234} // namespace unit
235} // namespace m5
236
237#endif
virtual ~UnitAW3641E()
Destructor.
Definition unit_AW3641E.hpp:132
bool active() const
Is a flash or torch currently in progress?
Definition unit_AW3641E.hpp:214
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:173
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:138
void config(const config_t &cfg)
Set the configuration.
Definition unit_AW3641E.hpp:153
virtual bool begin() override
Begin the unit (configures EN as OUTPUT and drives EN LOW)
Definition unit_AW3641E.cpp:56
virtual void update(const bool force=false) override
Drive EN LOW automatically when the flash/torch duration has elapsed.
Definition unit_AW3641E.cpp:81
UnitAW3641E(const uint8_t addr=DEFAULT_ADDRESS)
Constructor.
Definition unit_AW3641E.hpp:128
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:103
bool stop()
Stop any in-flight flash or torch (drives EN LOW immediately)
Definition unit_AW3641E.cpp:94
config_t config()
Gets the configuration.
Definition unit_AW3641E.hpp:147
uint16_t lastDurationMs() const
Last applied on-duration in milliseconds (clamped value used for flash() or torch())
Definition unit_AW3641E.hpp:208
For AW3641E.
Top level namespace of M5Stack.
Unit-related namespace.
Settings for begin.
Definition unit_AW3641E.hpp:119
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:123
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:85
constexpr uint16_t TORCH_MAX_DURATION_MS
Maximum duration for a single torch() call in milliseconds.
Definition unit_AW3641E.hpp:75
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