M5Unit-LIGHT 0.1.0 git rev:1ddbe92
Loading...
Searching...
No Matches
unit_Light_data.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
3 *
4 * SPDX-License-Identifier: MIT
5 */
12#ifndef M5_UNIT_LIGHT_UNIT_LIGHT_DATA_HPP
13#define M5_UNIT_LIGHT_UNIT_LIGHT_DATA_HPP
14
15#include <cstdint>
16#include <limits>
17
18#include <m5_utility/stl/extension.hpp>
19
20namespace m5 {
21namespace unit {
22
27namespace light {
28
31constexpr uint32_t DEFAULT_INTERVAL_MS{50};
32constexpr uint16_t DEFAULT_DARK{0};
33constexpr uint16_t DEFAULT_BRIGHT{4095};
35
40struct Data {
41 uint16_t analog_raw{};
42 bool digital{};
43 uint16_t dark{DEFAULT_DARK};
45
54 inline float normalized() const
55 {
56 if (dark == bright) {
57 return std::numeric_limits<float>::quiet_NaN();
58 }
59 const float span{static_cast<float>(bright) - static_cast<float>(dark)};
60 const float v{(static_cast<float>(analog_raw) - static_cast<float>(dark)) * 100.0f / span};
61 return m5::stl::clamp(v, 0.0f, 100.0f);
62 }
63};
64
65} // namespace light
66} // namespace unit
67} // namespace m5
68
69#endif
For UnitLight (U021)
Top level namespace of M5Stack.
Unit-related namespace.
Measurement data group.
Definition unit_Light_data.hpp:40
bool digital
Comparator threshold output (active level is board-specific; check it on the board)
Definition unit_Light_data.hpp:42
float normalized() const
Normalized brightness in 0..100 (%), where 0% = dark reference and 100% = bright reference.
Definition unit_Light_data.hpp:54
uint16_t bright
ADC reading captured when the sensor was lit (bright reference)
Definition unit_Light_data.hpp:44
uint16_t dark
ADC reading captured when the sensor was covered (dark reference)
Definition unit_Light_data.hpp:43
uint16_t analog_raw
Raw ADC value from the photoresistor path.
Definition unit_Light_data.hpp:41
constexpr uint32_t DEFAULT_INTERVAL_MS
Default sampling interval (ms)
Definition unit_Light_data.hpp:31
constexpr uint16_t DEFAULT_BRIGHT
Default bright reference (ADC value, 12-bit full scale)
Definition unit_Light_data.hpp:33
constexpr uint16_t DEFAULT_DARK
Default dark reference (ADC value)
Definition unit_Light_data.hpp:32