Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
alerts.h
Go to the documentation of this file.
1
5#ifndef ALERTS_H
6#define ALERTS_H
7
8#include <atomic>
9#include <cstdint>
10
12
13namespace lampda {
14namespace logic {
16namespace alerts {
17
19enum class Type : uint32_t
20{
21 // 0 means no errors
22
23 // always sort them by importance
24 MAIN_LOOP_FREEZE = 1 << 0, // main loop does not respond
25 BATTERY_READINGS_INCOHERENT = 1 << 1, // the pin that reads the battery value is not coherent with
26 // it's givent min and max
27 BATTERY_CRITICAL = 1 << 2, // battery is too low, shutdown immediatly
28 BATTERY_LOW = 1 << 3, // battery is dangerously low
29 LONG_LOOP_UPDATE = 1 << 4, // the main loop is taking too long to execute
30 // (bugs when reading button inputs)
31 TEMP_TOO_HIGH = 1 << 5, // Processor temperature is too high
32 TEMP_CRITICAL = 1 << 6, // Processor temperature is critical
33
34 BLUETOOTH_ADVERT = 1 << 7, // bluetooth is advertising
35
36 HARDWARE_ALERT = 1 << 8, // any hardware alert
37
38 FAVORITE_SET = 1 << 9, // user favorite mode is set
39
40 OTG_FAILED = 1 << 10, // OTG activation failed
41
42 SYSTEM_OFF_FAILED = 1 << 11, // system failed to go to sleep, big trouble here
43 SYSTEM_IN_ERROR_STATE = 1 << 12, // system is locked in an error state
44
45 SYSTEM_IN_LOCKOUT = 1 << 13, // system lockout, the lamp should not output any light
46
47 SUNSET_TIMER_ENABLED = 1 << 14, // active sunset timer, system will auto turn off
48
49 SYSTEM_SLEEP_SKIPPED = 1 << 15, // the system skipped the sleep clean phase (crash ? new flash ?)
50
51 USB_PORT_SHORT = 1 << 16, // the usb port is dirty, or wet
52
53 BATTERY_MISSING = 1 << 17, // no battery plugged in the system
54
55 CHARGER_ERROR = 1 << 18, // the charger signaled an error
56
57 OTG_OVER_LIMITS = 1 << 19,
58};
59
65{
66public:
68 void raise(const Type type);
69
71 void clear(const Type type);
72
80 bool clear_unless_refreshed(const Type type,
81 const std::atomic<uint32_t>& refreshCount,
82 const uint32_t refreshCountAtDecision);
83
85 bool is_raised(const Type type) const { return (_current.load() & static_cast<uint32_t>(type)) != 0x00; }
86
88 bool is_clear() const { return _current.load() == 0x00; }
89
92 uint32_t get_time_since_raised(const Type type) const;
93
95 bool can_use_output_power() const;
96
98 bool can_charge_battery() const;
99
101 bool can_use_usb_port() const;
102
103 AlertManager_t() = default;
104 AlertManager_t(const AlertManager_t&) = delete;
105 AlertManager_t& operator=(AlertManager_t const&) = delete;
106
107private:
110 std::atomic<uint32_t> _current {0};
111};
112
114extern AlertManager_t manager;
115
118
124
127
129void handle_all(const bool shouldIgnoreAlerts);
130
133
135void show_all();
136
139
140} // namespace alerts
141} // namespace logic
142} // namespace lampda
143
144#endif
Handle the whole alerts logic. Use this to raise and clear alerts, and make decisions based on those ...
Definition: alerts.h:65
uint32_t get_time_since_raised(const Type type) const
the time in milliseconds since this alert was raised, or zero if it's not. This raise time is updated...
Definition: alerts.cpp:1101
bool is_clear() const
Return true if no alerts are raised.
Definition: alerts.h:88
bool can_charge_battery() const
Return true if a raised alert prevents battery charging.
Definition: alerts.cpp:1132
bool can_use_usb_port() const
Return true if no raised alerts block the use of power through USB port.
Definition: alerts.cpp:1147
bool can_use_output_power() const
Return true if a raised alert prevents power set to output.
Definition: alerts.cpp:1117
void clear(const Type type)
Clear an alert.
Definition: alerts.cpp:1063
void raise(const Type type)
Raise an alert.
Definition: alerts.cpp:1031
bool clear_unless_refreshed(const Type type, const std::atomic< uint32_t > &refreshCount, const uint32_t refreshCountAtDecision)
Clear an alert, unless it was refreshed by a raise() since the clear decision was taken.
Definition: alerts.cpp:1072
bool is_raised(const Type type) const
Return true if the target alert is raised.
Definition: alerts.h:85
Type
Alert types : 31 errors max.
Definition: alerts.h:20
@ OTG_OVER_LIMITS
OTG has gone over the limits (current or voltage)
brightness_t get_alerts_max_brightness()
Return the max output brightness allowed by the raised alerts (the lowest of their limits)
Definition: alerts.cpp:902
void signal_wake_up_from_charger()
Signal slot that the system just powered up from charger.
Definition: alerts.cpp:900
AlertManager_t manager
Instanciation of the AlertManager.
Definition: alerts.cpp:30
bool is_request_shutdown()
Return true if an alert requested an immediate shutdown. Handling this is urgent or dammages can happ...
Definition: alerts.cpp:1029
void process_interrupt_signals()
Raise the alerts signaled from interrupts since the last call. Called by the alerts loop and the powe...
Definition: alerts.cpp:839
void handle_all(const bool shouldIgnoreAlerts)
handle the behavior for all alerts. Must be called often
Definition: alerts.cpp:913
void signal_usb_port_fault()
Signal a fault of the USB port protection. Safe to call from an interrupt: it only sets a flag....
Definition: alerts.cpp:833
void show_all()
show all the raised alerts in Serial debug
Definition: alerts.cpp:1010
Program scope.
Definition: control_fixed_modes.hpp:14
uint16_t brightness_t
Define the type of the brightness parameters.
Definition: constants.h:151
Define the system hardware constants.