Skip to content

Commit 09e81a4

Browse files
committed
Simplify API
1 parent 63b86a0 commit 09e81a4

File tree

2 files changed

+34
-81
lines changed

2 files changed

+34
-81
lines changed

src/Board.cpp

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,10 @@ void Board::enableSleepWhenIdle(){
128128
#endif
129129

130130

131-
void Board::enableWakeupFromRTC(){
132-
#if defined(ARDUINO_PORTENTA_C33)
133-
lowPower->enableWakeupFromRTC();
134-
#elif defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) || defined(ARDUINO_NICLA_VISION)
135-
standbyType |= StandbyType::untilTimeElapsed;
136-
#endif
137-
}
138-
139131
#if defined(ARDUINO_PORTENTA_C33)
140-
bool Board::sleepFor(int hours, int minutes, int seconds, void (* const callbackFunction)(), RTClock * rtc){
132+
void Board::enableWakeupFromRTC(uint32_t hours, uint32_t minutes, uint32_t seconds, void (* const callbackFunction)() = nullptr, RTClock * rtc = &RTC){
133+
lowPower->enableWakeupFromRTC();
134+
141135
RTCTime currentTime;
142136
if (!rtc -> getTime(currentTime)) {
143137
return false;
@@ -162,30 +156,19 @@ bool Board::sleepFor(int hours, int minutes, int seconds, void (* const callback
162156
return false; // Failed to set the alarm
163157
}
164158
delay(1);
165-
return true;
166-
}
167-
168-
bool Board::sleepFor(int hours, int minutes, int seconds, void (* const callbackFunction)()){
169-
return this -> sleepFor(hours, minutes, seconds, callbackFunction, &RTC);
159+
return true;
170160
}
171161
#endif
172162

173-
bool Board::sleepFor(int hours, int minutes, int seconds){
174-
#if defined(ARDUINO_PORTENTA_C33)
175-
return this -> sleepFor(hours, minutes, seconds, NULL, &RTC);
176-
#elif defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) || defined(ARDUINO_NICLA_VISION)
177-
this -> rtcWakeupDelay = RTCWakeupDelay(hours, minutes, seconds);
178-
return true;
179-
#endif
180-
}
181-
182-
#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4)
183-
bool Board::sleepFor(RTCWakeupDelay delay){
184-
this -> rtcWakeupDelay = delay;
185-
return true;
163+
#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) || defined(ARDUINO_NICLA_VISION)
164+
void Board::enableWakeupFromRTC(uint32_t hours, uint32_t minutes, uint32_t seconds){
165+
standbyType |= StandbyType::untilTimeElapsed;
166+
wakeupDelayHours = hours;
167+
wakeupDelayMinutes = minutes;
168+
wakeupDelaySeconds = seconds;
186169
}
187170
#endif
188-
171+
189172
#if defined(ARDUINO_PORTENTA_C33)
190173
void Board::sleepUntilWakeupEvent(){
191174
lowPower -> sleep();
@@ -196,6 +179,7 @@ void Board::standByUntilWakeupEvent(){
196179
#if defined(ARDUINO_PORTENTA_C33)
197180
lowPower -> deepSleep();
198181
#elif defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) || defined(ARDUINO_NICLA_VISION)
182+
RTCWakeupDelay rtcWakeupDelay = RTCWakeupDelay(wakeupDelayHours, wakeupDelayMinutes, wakeupDelaySeconds);
199183
if(standbyType == StandbyType::untilEither)
200184
LowPower.standbyM7(LowPowerStandbyType::untilPinActivity | LowPowerStandbyType::untilTimeElapsed, rtcWakeupDelay);
201185
else if (standbyType == StandbyType::untilPinActivity)
@@ -214,6 +198,7 @@ void Board::setAllPeripheralsPower(bool on){
214198
// to communicate with the MCU.
215199
Wire3.end();
216200
#else if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4)
201+
// TODO Can we extract this into functions?
217202
if(on){
218203
PMIC.getControl() -> turnLDO2On(Ldo2Mode::Normal);
219204
PMIC.getControl() -> turnLDO2On(Ldo2Mode::Sleep);
@@ -248,8 +233,9 @@ void Board::setAllPeripheralsPower(bool on){
248233
#endif
249234
}
250235

251-
#if defined(ARDUINO_PORTENTA_C33)
252236
void Board::setAnalogDigitalConverterPower(bool on){
237+
// TODO Not available on the Portenta H7?
238+
#if defined(ARDUINO_PORTENTA_C33)
253239
if(on){
254240
PMIC.getControl()->turnLDO1On(Ldo1Mode::Normal);
255241
PMIC.getControl()->turnLDO1On(Ldo1Mode::Sleep);
@@ -259,8 +245,8 @@ void Board::setAnalogDigitalConverterPower(bool on){
259245
PMIC.getControl()->turnLDO1Off(Ldo1Mode::Sleep);
260246
PMIC.getControl()->turnLDO1Off(Ldo1Mode::Standby);
261247
}
262-
}
263248
#endif
249+
}
264250

265251
void Board::setCommunicationPeripheralsPower(bool on){
266252
// TODO: Why do we only use the normal mode here?

src/Board.h

Lines changed: 18 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ class Board {
100100
*/
101101
void enableWakeupFromPin();
102102

103-
104103
/**
105104
* Enables sleep mode when the board is idle.
106105
*/
@@ -117,62 +116,29 @@ class Board {
117116
#endif
118117

119118

120-
/**
121-
* Enables wake-up of the device from the RTC.
122-
*/
123-
void enableWakeupFromRTC();
124-
125119
#if defined(ARDUINO_PORTENTA_C33)
126-
// TODO Do I needs to call enableWakeupFromRTC() before calling this function?
127120
/**
128-
* @brief Put the device in sleep mode for a specified amount of time. Restarts after waking up.
121+
* @brief Enables wake-up of the device from the RTC.
129122
* This function allows to use a custom RTC instance to put the device in sleep mode.
130123
* @param hours The number of hours to sleep.
131124
* @param minutes The number of minutes to sleep.
132125
* @param seconds The number of seconds to sleep.
133126
* @param callbackFunction The function to call when the device wakes up.
127+
* If no callback function is provided, the device will wake up without calling any function.
134128
* @param rtc The RTC instance to use for the sleep function.
129+
* If no RTC instance is provided, the default RTC instance is used.
135130
* @return True if successful, false otherwise.
136131
*/
137-
bool sleepFor(int hours, int minutes, int seconds, void (* const callbackFunction)(), RTClock * rtc);
138-
139-
/**
140-
* @brief Put the device in sleep mode for a specified amount of time.
141-
* This function uses the default RTC instance to put the device in sleep mode.
142-
* @param hours The number of hours to sleep.
143-
* @param minutes The number of minutes to sleep.
144-
* @param seconds The number of seconds to sleep.
145-
* @param callbackFunction The function to call when the device wakes up.
146-
* @return True if successful, false otherwise.
147-
*/
148-
bool sleepFor(int hours, int minutes, int seconds, void (* const callbackFunction)());
132+
void enableWakeupFromRTC(uint32_t hours, uint32_t minutes, uint32_t seconds, void (* const callbackFunction)() = nullptr, RTClock * rtc = &RTC);
149133
#endif
150134

151-
/**
152-
* @brief Put the device in sleep mode for a specified amount of time. It restarts after waking up.
153-
* This function uses the default RTC instance to put the device in sleep mode and
154-
* does not call a function when the device wakes up.
155-
* @param hours The number of hours to sleep.
156-
* @param minutes The number of minutes to sleep.
157-
* @param seconds The number of seconds to sleep.
158-
* @return True if successful, false otherwise.
159-
*/
160-
bool sleepFor(int hours, int minutes, int seconds);
161-
162-
#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4)
163-
164-
/**
165-
* Sleeps the board for a specified delay.
166-
*
167-
* @param delay The delay as an RTCWakeupDelay object.
168-
* @return True if the board successfully sleeps, false otherwise.
169-
*/
170-
bool sleepFor(RTCWakeupDelay delay);
135+
#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4)
136+
void enableWakeupFromRTC(uint32_t hours, uint32_t minutes, uint32_t seconds);
171137
#endif
172-
138+
173139

174140
#if defined(ARDUINO_PORTENTA_C33)
175-
// TODO How to use this with the RTC? enableWakeupFromRTC(), then sleepFor, then this?
141+
// TODO Restarts after waking up.?
176142
/**
177143
* Put the device into sleep mode until a wakeup event occurs
178144
* This sleep mode is ideal for applications requiring periodic wake-ups or
@@ -182,13 +148,13 @@ class Board {
182148
* A wakeup event can be an interrupt on a pin or the RTC,
183149
* depending on what you set with enableWakeupFromPin() and enableWakeupFromRTC().
184150
*/
185-
void standByUntilWakeupEvent();
151+
void sleepUntilWakeupEvent();
186152
#endif
187153

188-
// TODO Same as above
154+
// TODO measurements are board specific. Need adjustments for Portenta H7.
189155
/**
190-
* Put the device into deep sleep mode until a wakeup event occurs.
191-
* For scenarios demanding drastic power conservation, the Deep Sleep Mode significantly reduces
156+
* Put the device into standby mode until a wakeup event occurs.
157+
* For scenarios demanding drastic power conservation, the standby Mode significantly reduces
192158
* the board's power usage to range between 90uA and 11mA depending on the state of the peripherals.
193159
* This mode restarts the board on wake-up, effectively running the setup() function again.
194160
* A wakeup event can be an interrupt on a pin or the RTC, depending on what
@@ -208,14 +174,13 @@ class Board {
208174
*/
209175
void setCommunicationPeripheralsPower(bool on);
210176

211-
#if defined(ARDUINO_PORTENTA_C33)
212177
/**
213178
* @brief Toggles the power of the analog digital converter on Portenta C33.
179+
* This is not available on the Portenta H7.
214180
* @param on True to turn on the power, false to turn it off.
215181
*/
216182
void setAnalogDigitalConverterPower(bool on);
217183

218-
#endif
219184
/**
220185
* @brief Set the reference voltage. This value is used by the ADC to convert analog values to digital values.
221186
* This can be particularly useful to increase the accuracy of the ADC when working with low voltages
@@ -235,10 +200,12 @@ class Board {
235200

236201
#if defined(ARDUINO_PORTENTA_C33)
237202
LowPower * lowPower;
238-
#elif defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) || defined(ARDUINO_NICLA_VISION)
239-
StandbyType standbyType = StandbyType::none;
240-
RTCWakeupDelay rtcWakeupDelay = RTCWakeupDelay(0, 0, 0);
241203
#endif
204+
205+
StandbyType standbyType = StandbyType::none;
206+
uint32_t wakeupDelayHours;
207+
uint32_t wakeupDelayMinutes;
208+
uint32_t wakeupDelaySeconds;
242209
};
243210

244211
#endif

0 commit comments

Comments
 (0)