Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
PicoModel.cpp
Go to the documentation of this file.
1#include <pico/cyw43_arch.h>
2#include <hardware/adc.h>
3
4#include "network/Network.h"
5#include "PicoModel.h"
6#include "utility/WithFlag.h"
7
8// Define your model properties and methods here
9// For example, you might have properties for temperature, LED state, etc.
10
11// Add methods to manipulate these properties
12
14 : FrameworkModel("pico_model.json") // Initialize with a storage manager and path
15{
16}
17
19{
20 // Initialize GPIO for LED
21 restoreState(); // Restore the state of the model
22}
23
25{
26 adc_init();
27 adc_set_temp_sensor_enabled(true);
28 adc_select_input(4);
29 float voltage = adc_read() * (3.3f / (1 << 12));
30 return 27.0f - (voltage - 0.706f) / 0.001721f; // Convert voltage to temperature
31}
32// LED state management
33
34void PicoModel::setLedState(bool state)
35{
36 cyw43_arch_gpio_put(0, state ? 1 : 0); // Set GPIO 0 for LED state
37 setValue("led", getLedState());
38 if (!suppressSave)
39 {
40 saveState(); // Only save if not restoring
41 }
42}
44{
45 if (cyw43_is_initialized){
46 return cyw43_arch_gpio_get(0); // Get GPIO 0 state for LED
47 }
48 else{
49 return false; // Return false if Wi-Fi is not initialized
50 }
51}
52
54{
55 bool state = gpio_get(pin);
56 return state; // Return the state of the specified GPIO pin
57}
58
59void PicoModel::setGpioState(int pin, bool state)
60{
61 gpio_init(pin);
62 gpio_set_dir(pin, GPIO_OUT);
63 gpio_put(pin, state);
64 if (!suppressSave)
65 {
66 saveState(); // Only save if not restoring
67 }
68}
69
71{
72 // Update the model with the new state
73 // Save the LED state
74 setValue("led", getLedState());
75 // Save the state of all active GPIO pins
76 json gpios;
77 for (int pin : activePins)
78 {
79 gpios[std::to_string(pin)] = getGpioState(pin);
80 }
81 setValue("gpio_states", gpios);
82 printf("[PicoModel] Saving state to storage...\n");
83 save();
84}
85
87{
88 // This method is called when the network is ready
89 // Initialize the LED state based on the stored value
90 bool ledState = getValue("led", false);
91 WithFlag _(suppressSave); // Suppress saving during initialization
92 setLedState(ledState);
93}
94
96{
97 if (!load())
98 return;
99 printf("[PicoModel] Restoring state from storage...\n");
100 // Restore LED state
101 suppressSave = true; // Suppress saving during restore
103 {
104 bool ledState = getValue("led", false);
105 setLedState(ledState);
106 }
107 else
108 {
109 printf("[PicoModel] Network not connected, deferring LED initialization\n");
110 }
111
112 // Restore GPIO pin states
113 auto gpioStates = getValue<json>("gpio_states", {});
114 for (auto &[pinStr, stateJson] : gpioStates.items())
115 {
116 int pin = std::stoi(pinStr);
117 bool state = stateJson.get<bool>();
118 setGpioState(pin, state);
119 }
120 suppressSave = false; // Re-enable saving after restore
121}
nlohmann::json json
Manages Wi-Fi initialization, connection status, and power management for Pico W.
RAII-style scoped flag setter. Temporarily sets a boolean flag and restores it on destruction.
Provides a basic JSON-backed record model.
bool load()
Loads the JSON collection from storage.
T getValue(const std::string &key, const T &defaultValue=T())
Reads a single top-level key from the model file.
bool save()
Saves the current collection to storage.
void setValue(const std::string &key, const T &value)
Sets a single top-level key in the model file.
static bool isConnected()
Check whether the device is connected to Wi-Fi.
Definition Network.cpp:144
void setLedState(bool state)
Definition PicoModel.cpp:34
float getTemperature()
Definition PicoModel.cpp:24
void setGpioState(int pin, bool state)
Definition PicoModel.cpp:59
bool getGpioState(int pin)
Definition PicoModel.cpp:53
void saveState()
Definition PicoModel.cpp:70
bool suppressSave
Definition PicoModel.h:33
bool getLedState()
Definition PicoModel.cpp:43
void onStart()
Definition PicoModel.cpp:18
void restoreState()
Definition PicoModel.cpp:95
void onNetworkReady()
Definition PicoModel.cpp:86
std::vector< int > activePins
Definition PicoModel.h:31
Temporarily sets a boolean flag and restores it automatically.
Definition WithFlag.h:21