PAL_AMB-usenap
This act includes the following:
- Wireless packet transmission and reception
- Configuration via interactive mode -
<STG_STD>
- State transition control via state machine -
<SM_SIMPLE>
- Board operation via
<PAL_AMB>
board behavior
Act Features
- Uses the Environmental Sensor Pal AMBIENT SENSE PAL to acquire sensor values.
- Uses sleep function to operate with coin battery.
- Uses sleep function even during sensor data acquisition.
Act Explanation
begin()
The begin()
function is called just before the very first loop()
after the setup()
function finishes (and then TWENET initialization is performed).
void begin() {
sleepNow(); // the first time is just sleeping.
}
After setup()
, the initial sleep is executed. Sensor data acquisition is started during setup()
, but the result is not evaluated. This is done to activate the sensor once beforehand and is not necessarily required.
wakeup()
Procedures after wake-up. The following processing is performed:
- If sensor data acquisition has not been started yet, start sensor data acquisition and enter a short sleep.
- Since sensor data acquisition was started immediately before, check the data and send it wirelessly.
void wakeup() {
if (!b_senser_started) {
// delete/make shorter this message if power requirement is harder.
Serial << mwx::crlf
<< "--- PAL_AMB:" << FOURCHARS << " wake up ---"
<< mwx::crlf
<< "..start sensor capture again."
<< mwx::crlf;
startSensorCapture();
b_senser_started = true;
napNow(); // short period sleep.
} else {
Serial << "..wake up from short nap.." << mwx::crlf;
auto&& brd = the_twelite.board.use<PAL_AMB>();
b_senser_started = false;
// tell sensors waking up.
brd.sns_LTR308ALS.process_ev(E_EVENT_START_UP);
brd.sns_SHTC3.process_ev(E_EVENT_START_UP);
}
}
The above branch is controlled by the global variable b_sensor_started
. If !b_sensor_started
, sensor acquisition start (startSensorCapture()
) is performed, and a short sleep is entered by napNow()
. The time is 100ms.
After waking up from the sleep by napNow()
, the section where b_sensor_started==true
is executed. Here, the E_EVENT_START_UP
event is notified to the two sensors. This event means that enough time has passed for the sensor acquisition to be completed. Based on this notification, sns_LTR308ALS
and sns_SHTC3
become available. Then it proceeds to loop()
, and the wireless packet is sent.
napNow()
. If waking up in a short time, it is expected that errors such as failure to obtain sensor data will occur in the subsequent process because the required elapsed time is insufficient.napNow()
Executes a very short sleep.
void napNow() {
uint32_t u32ct = 100;
Serial << "..nap " << int(u32ct) << "ms." << mwx::crlf;
the_twelite.sleep(u32ct, false, false, TWENET::SLEEP_WAKETIMER_SECONDARY);
}
If the second parameter of sleep is true, the next wake-up time is adjusted based on the previous sleep wake-up time. This is set when you want to wake up every 5 seconds regularly.
If the third parameter is true, the sleep does not retain memory. After waking up, wakeup()
is not called, and the process is the same as power-on reset.
The fourth parameter specifies using the second wake-up timer. Here, the first timer is used for normal sleep, and the second is used for short sleep. This act has no strong reason to use the second timer, but for example, if you want to wake up every 5 seconds as mentioned above, using the first timer for short sleep resets the counter value, making elapsed time correction calculations complicated, so the second timer is used.