M5Unit-FLASHLIGHT 0.1.0 git rev:ba3d197
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
70constexpr uint16_t FLASH_MAX_DURATION_MS{220};
71
76constexpr uint32_t TORCH_DEFAULT_DURATION_MS{1300};
79[[deprecated("Use TORCH_DEFAULT_DURATION_MS instead")]] constexpr uint16_t TORCH_MAX_DURATION_MS{1300};
80
89constexpr uint8_t to_pulse_count(const Brightness b)
90{
91 return static_cast<uint8_t>(static_cast<uint8_t>(b) + 1u);
92}
93
94} // namespace aw3641e
95
116class UnitAW3641E : public Component {
117 M5_UNIT_COMPONENT_HPP_BUILDER(UnitAW3641E, 0x00);
118
119public:
124 struct config_t {
128 aw3641e::SwitchPosition switch_position{aw3641e::SwitchPosition::Flash};
129 };
130
133 explicit UnitAW3641E(const uint8_t addr = DEFAULT_ADDRESS) : Component(addr)
134 {
135 }
137 virtual ~UnitAW3641E()
138 {
139 }
140
143 virtual bool begin() override;
146 virtual void update(const bool force = false) override;
147
152 inline config_t config() const
153 {
154 return _cfg;
155 }
158 inline void config(const config_t& cfg)
159 {
160 _cfg = cfg;
161 }
163
166
170 bool stop();
171
192 bool flash(const aw3641e::Brightness brightness = aw3641e::Brightness::Pct100,
193 const uint16_t duration_ms = aw3641e::FLASH_MAX_DURATION_MS);
194
214 bool torch(const uint32_t duration_ms = aw3641e::TORCH_DEFAULT_DURATION_MS);
215
228 bool torchOn();
230
236 inline uint32_t lastDurationMs() const
237 {
238 return _duration_ms;
239 }
243 inline bool active() const
244 {
245 return _active;
246 }
248
249protected:
250 // @brief Send a 1-wire EN pulse train of the given count (1..8 for Brightness Pct100..Pct30)
251 // @param pulse_count Number of rising edges (1..8). Final state is EN = HIGH.
252 // @return True if the GPIO writes succeeded.
253 bool send_pulse_train(const uint8_t pulse_count);
254 // Cancel any in-flight operation (EN LOW for T_OFF), then drive EN HIGH in Torch mode.
255 bool start_torch();
256
257protected:
258 config_t _cfg{};
259 // Flash/torch timing tracker for non-blocking auto-shutdown in update().
260 bool _active{false};
261 bool _continuous{false}; // torchOn(): update() does not turn the LED off
262 types::elapsed_time_t _start_ms{0};
263 uint32_t _duration_ms{0};
264};
265
266} // namespace unit
267} // namespace m5
268
269#endif
virtual ~UnitAW3641E()
Destructor.
Definition unit_AW3641E.hpp:137
bool active() const
Is a flash or torch currently in progress?
Definition unit_AW3641E.hpp:243
void config(const config_t &cfg)
Set the configuration.
Definition unit_AW3641E.hpp:158
config_t config() const
Gets the configuration.
Definition unit_AW3641E.hpp:152
virtual bool begin() override
Begin the unit (configures EN as OUTPUT and drives EN LOW)
Definition unit_AW3641E.cpp:63
virtual void update(const bool force=false) override
Drive EN LOW automatically when the flash/torch duration has elapsed (not for torchOn())
Definition unit_AW3641E.cpp:89
UnitAW3641E(const uint8_t addr=DEFAULT_ADDRESS)
Constructor.
Definition unit_AW3641E.hpp:133
uint32_t lastDurationMs() const
Last applied on-duration in milliseconds (value used for flash() or torch())
Definition unit_AW3641E.hpp:236
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:111
bool torchOn()
Turn the LED on in Torch mode until stop() is called.
Definition unit_AW3641E.cpp:167
bool stop()
Stop any in-flight flash or torch, including torchOn() (drives EN LOW immediately)
Definition unit_AW3641E.cpp:101
bool torch(const uint32_t duration_ms=aw3641e::TORCH_DEFAULT_DURATION_MS)
Drive the LED in Torch mode for the given duration.
Definition unit_AW3641E.cpp:147
For AW3641E.
Top level namespace of M5Stack.
Unit-related namespace.
Settings for begin.
Definition unit_AW3641E.hpp:124
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:128
Brightness
Flash brightness level (percentage of I_FLASH; Flash mode only)
Definition unit_AW3641E.hpp:48
constexpr uint32_t PULSE_LOW_US
Logic-low pulse width in microseconds (T_LO; datasheet range 0.75-10 µs)
Definition unit_AW3641E.hpp:62
constexpr uint32_t PULSE_HIGH_US
Logic-high pulse width in microseconds (T_HI; datasheet range 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 uint32_t TORCH_DEFAULT_DURATION_MS
Default torch() duration in milliseconds (used when torch() is called with no argument)
Definition unit_AW3641E.hpp:76
constexpr uint8_t to_pulse_count(const Brightness b)
Translate a Brightness level to the AW3641E rising-edge pulse count.
Definition unit_AW3641E.hpp:89
constexpr uint16_t TORCH_MAX_DURATION_MS
Alias for TORCH_DEFAULT_DURATION_MS (torch() does not clamp the duration; this is only the default)
Definition unit_AW3641E.hpp:79
constexpr uint32_t PULSE_OFF_US
EN-low time before a pulse sequence in microseconds (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:70