Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
PicoModel Class Reference

#include <PicoModel.h>

+ Inheritance diagram for PicoModel:
+ Collaboration diagram for PicoModel:

Public Member Functions

 PicoModel ()
 
void onStart ()
 
float getTemperature ()
 
void setLedState (bool state)
 
bool getLedState ()
 
bool getGpioState (int pin)
 
void setGpioState (int pin, bool state)
 
void saveState ()
 
void restoreState ()
 
void onNetworkReady ()
 
- Public Member Functions inherited from FrameworkModel
 FrameworkModel (const std::string &path)
 Constructor.
 
bool load ()
 Loads the JSON collection from storage.
 
bool save ()
 Saves the current collection to storage.
 
std::vector< nlohmann::json > all () const
 Returns all items in the collection.
 
std::optional< nlohmann::json > find (const std::string &id) const
 Finds an item by ID.
 
bool create (const nlohmann::json &item)
 Adds a new item to the collection.
 
bool update (const std::string &id, const nlohmann::json &updatedItem)
 Updates an item by ID.
 
bool remove (const std::string &id)
 Removes an item by ID.
 
nlohmann::json toJson () const
 Returns the full collection as a JSON array object.
 
nlohmann::json findAsJson (const std::string &id) const
 Finds a single item by ID and returns it as JSON or null.
 
bool save (const std::string &id, const json &data)
 Saves a single item by ID and JSON object.
 
bool createFromJson (const nlohmann::json &obj)
 
bool updateFromJson (const std::string &id, const nlohmann::json &updates)
 
nlohmann::json deleteAsJson (const std::string &id)
 
bool saveAll ()
 
template<typename T >
getValue (const std::string &key, const T &defaultValue=T())
 Reads a single top-level key from the model file.
 
template<typename T >
void setValue (const std::string &key, const T &value)
 Sets a single top-level key in the model file.
 

Private Attributes

std::vector< int > activePins = { 2, 3, 4, 5, 6, 7, 8, 9, 16, 17, 18, 19 }
 
bool suppressSave = false
 

Additional Inherited Members

- Protected Member Functions inherited from FrameworkModel
virtual std::string getIdField () const
 Returns the JSON key used as the record ID.
 
- Protected Attributes inherited from FrameworkModel
nlohmann::json collection = nlohmann::json::array()
 In-memory array of records.
 

Detailed Description

Definition at line 5 of file PicoModel.h.

Constructor & Destructor Documentation

◆ PicoModel()

PicoModel::PicoModel ( )

Definition at line 13 of file PicoModel.cpp.

14 : FrameworkModel("pico_model.json") // Initialize with a storage manager and path
15{
16}
Provides a basic JSON-backed record model.

Member Function Documentation

◆ getGpioState()

bool PicoModel::getGpioState ( int  pin)

Definition at line 53 of file PicoModel.cpp.

54{
55 bool state = gpio_get(pin);
56 return state; // Return the state of the specified GPIO pin
57}

Referenced by GpioController::getState(), GpioController::handleGetMultipleGpios(), and saveState().

+ Here is the caller graph for this function:

◆ getLedState()

bool PicoModel::getLedState ( )

Definition at line 43 of file PicoModel.cpp.

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}

Referenced by DashboardController::getLedState(), saveState(), and setLedState().

+ Here is the caller graph for this function:

◆ getTemperature()

float PicoModel::getTemperature ( )

Definition at line 24 of file PicoModel.cpp.

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}

Referenced by DashboardController::getTemperature().

+ Here is the caller graph for this function:

◆ onNetworkReady()

void PicoModel::onNetworkReady ( )

Definition at line 86 of file PicoModel.cpp.

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}
T getValue(const std::string &key, const T &defaultValue=T())
Reads a single top-level key from the model file.
void setLedState(bool state)
Definition PicoModel.cpp:34
bool suppressSave
Definition PicoModel.h:33
Temporarily sets a boolean flag and restores it automatically.
Definition WithFlag.h:21

References FrameworkModel::getValue(), setLedState(), and suppressSave.

Referenced by App::onEvent().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ onStart()

void PicoModel::onStart ( )

Definition at line 18 of file PicoModel.cpp.

19{
20 // Initialize GPIO for LED
21 restoreState(); // Restore the state of the model
22}
void restoreState()
Definition PicoModel.cpp:95

References restoreState().

Referenced by App::onStart().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ restoreState()

void PicoModel::restoreState ( )

Definition at line 95 of file PicoModel.cpp.

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}
bool load()
Loads the JSON collection from storage.
static bool isConnected()
Check whether the device is connected to Wi-Fi.
Definition Network.cpp:144
void setGpioState(int pin, bool state)
Definition PicoModel.cpp:59

References FrameworkModel::getValue(), Network::isConnected(), FrameworkModel::load(), setGpioState(), setLedState(), and suppressSave.

Referenced by onStart().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ saveState()

void PicoModel::saveState ( )

Definition at line 70 of file PicoModel.cpp.

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}
nlohmann::json json
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.
bool getGpioState(int pin)
Definition PicoModel.cpp:53
bool getLedState()
Definition PicoModel.cpp:43
std::vector< int > activePins
Definition PicoModel.h:31

References activePins, getGpioState(), getLedState(), FrameworkModel::save(), and FrameworkModel::setValue().

Referenced by setGpioState(), and setLedState().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setGpioState()

void PicoModel::setGpioState ( int  pin,
bool  state 
)

Definition at line 59 of file PicoModel.cpp.

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}
void saveState()
Definition PicoModel.cpp:70

References saveState(), and suppressSave.

Referenced by restoreState(), and GpioController::setState().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setLedState()

void PicoModel::setLedState ( bool  state)

Definition at line 34 of file PicoModel.cpp.

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}

References getLedState(), saveState(), FrameworkModel::setValue(), and suppressSave.

Referenced by onNetworkReady(), restoreState(), and DashboardController::setLedState().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ activePins

std::vector<int> PicoModel::activePins = { 2, 3, 4, 5, 6, 7, 8, 9, 16, 17, 18, 19 }
private

Definition at line 31 of file PicoModel.h.

31{ 2, 3, 4, 5, 6, 7, 8, 9, 16, 17, 18, 19 }; // Pins in use - staying away from sd card pins

Referenced by saveState().

◆ suppressSave

bool PicoModel::suppressSave = false
private

Definition at line 33 of file PicoModel.h.

Referenced by onNetworkReady(), restoreState(), setGpioState(), and setLedState().


The documentation for this class was generated from the following files: