Loading [MathJax]/extensions/tex2jax.js
SensESP 3.1.0
Universal Signal K sensor toolkit ESP32
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
sensesp_base_app.h
Go to the documentation of this file.
1#ifndef SENSESP_BASE_APP_H_
2#define SENSESP_BASE_APP_H_
3
4#ifndef BUTTON_BUILTIN
5// Default button pin on ESP32-DevKitC devices is 0 (GPIO0),
6// normally connected to the BOOT button.
7#define BUTTON_BUILTIN 0
8#endif
9
10#include "sensesp.h"
11
12#include <memory>
13
14#include "esp_log.h"
17
18namespace sensesp {
19
20constexpr auto kDefaultHostname = "SensESP";
21
22inline void SetupLogging(esp_log_level_t default_level = ESP_LOG_DEBUG) {
23 esp_log_level_set("*", default_level);
24}
25
26inline void SetupSerialDebug(uint32_t baudrate) {
28
29 if (baudrate != 115200) {
30 ESP_LOGW(__FILENAME__, "SetupSerialDebug baudrate parameter is ignored.");
31 }
32}
33
44 protected:
45 std::shared_ptr<reactesp::EventLoop> event_loop_;
46
47 public:
51 static const std::shared_ptr<SensESPBaseApp>& get() { return instance_; }
52
56 virtual bool destroy() {
57 bool outside_users = instance_.use_count() > 1;
58
59 if (outside_users) {
60 ESP_LOGW(__FILENAME__,
61 "SensESPBaseApp instance has active references and won't be "
62 "properly destroyed.");
63 }
64 instance_ = nullptr;
65 return !outside_users;
66 }
67
72 virtual void start() {
73 ESP_LOGW(__FILENAME__, "start() call is deprecated and can be removed.");
74 }
75
80 virtual void reset() {
81 ESP_LOGW(__FILENAME__,
82 "Resetting the device configuration to system defaults.");
84
85 this->event_loop_->onDelay(1000, []() {
86 ESP.restart();
87 delay(1000);
88 });
89 }
90
96 std::shared_ptr<ObservableValue<String>> get_hostname_observable() {
97 return std::static_pointer_cast<ObservableValue<String>>(hostname_);
98 }
99
105 static String get_hostname() {
106 return SensESPBaseApp::get()->get_hostname_observable().get()->get();
107 }
108
114 static std::shared_ptr<reactesp::EventLoop> get_event_loop() {
115 return SensESPBaseApp::get()->event_loop_;
116 }
117
118 protected:
127 : filesystem_{std::make_shared<Filesystem>()},
128 event_loop_{std::make_shared<reactesp::EventLoop>()} {
129 // Instance is now set by the builder
130 }
131
132 ~SensESPBaseApp() { instance_ = nullptr; }
133
135 hostname_ = std::make_shared<PersistingObservableValue<String>>(
136 kDefaultHostname, "/system/hostname");
137 ConfigItem(hostname_); // Make hostname configurable
138 }
139
147 virtual void setup() {
148 if (!hostname_) {
150 }
151 }
152
153 static std::shared_ptr<SensESPBaseApp> instance_;
154
155 void set_instance(const std::shared_ptr<SensESPBaseApp>& instance) {
156 instance_ = instance;
157 }
158
159 std::shared_ptr<PersistingObservableValue<String>> hostname_;
160
161 std::shared_ptr<Filesystem> filesystem_;
162
163 const SensESPBaseApp* set_hostname(String hostname) {
164 if (!hostname_) {
166 }
167 hostname_->set(hostname);
168 return this;
169 }
170};
171
172} // namespace sensesp
173
174#endif
static void reset_all()
The base class for SensESP applications.
static String get_hostname()
Get the current hostname.
static std::shared_ptr< reactesp::EventLoop > get_event_loop()
Get the event loop object from the singleton SensESPBaseApp instance.
static std::shared_ptr< SensESPBaseApp > instance_
std::shared_ptr< reactesp::EventLoop > event_loop_
void set_instance(const std::shared_ptr< SensESPBaseApp > &instance)
const SensESPBaseApp * set_hostname(String hostname)
std::shared_ptr< PersistingObservableValue< String > > hostname_
virtual void start()
Start the app (activate all the subcomponents)
std::shared_ptr< ObservableValue< String > > get_hostname_observable()
Get the hostname observable object.
virtual void setup()
Perform initialization of SensESPBaseApp once builder configuration is done.
SensESPBaseApp()
Construct a new SensESP Base App object.
virtual void reset()
Reset the device to factory defaults.
virtual bool destroy()
Destroy the SensESPBaseApp instance.
std::shared_ptr< Filesystem > filesystem_
static const std::shared_ptr< SensESPBaseApp > & get()
Get the singleton instance of the SensESPBaseApp.
constexpr auto kDefaultHostname
void SetupSerialDebug(uint32_t baudrate)
std::shared_ptr< ConfigItemT< T > > ConfigItem(std::shared_ptr< T >)
Register a ConfigItemT with the ConfigItemBase.
void SetupLogging(esp_log_level_t default_level=ESP_LOG_DEBUG)