ENTS-tock
Environmental NeTworked Sensor firmware on Tock OS
Loading...
Searching...
No Matches
power.cpp
Go to the documentation of this file.
1#include "modules/power.hpp"
2
3#include <ArduinoLog.h>
5
6#include "driver/gpio.h"
7#include "esp_sleep.h"
8
9namespace ModuleHandler {
10
12RTC_DATA_ATTR static unsigned int boot_count = 0;
13
15 // set module type
16 type = Esp32Command_power_command_tag;
17
18 ++boot_count;
19}
20
22
23void ModulePower::OnReceive(const Esp32Command& cmd) {
24 Log.traceln("ModulePower::OnReceive");
25
26 // check if power command
27 if (cmd.which_command != Esp32Command_power_command_tag) {
28 return;
29 }
30
31 Log.traceln("PowerCommand type: %d", cmd.command.power_command.type);
32
33 // switch for command types
34 switch (cmd.command.power_command.type) {
35 case PowerCommand_Type_SLEEP:
36 Log.traceln("Calling SLEEP");
37 Sleep();
38 break;
39
40 case PowerCommand_Type_WAKEUP:
41 Log.traceln("Calling REASON");
42 Wakeup();
43 break;
44
45 default:
46 Log.warningln("Power command type not found!");
47 break;
48 }
49}
50
52 Log.traceln("ModulePower::OnRequest");
53 memcpy(buffer, request_buffer, request_buffer_len);
54 return request_buffer_len;
55}
56
57void ModulePower::Sleep() {
58 Log.noticeln("Setting sleep flag");
59 sleep_flag = true;
60
61 PowerCommand resp = PowerCommand_init_zero;
62 resp.type = PowerCommand_Type_SLEEP;
63
64 request_buffer_len =
65 EncodePowerCommand(&resp, request_buffer, sizeof(request_buffer));
66}
67
68void ModulePower::Wakeup() {
69 Log.noticeln("Wakeup called");
70
71 esp_sleep_wakeup_cause_t wakeup_reason = esp_sleep_get_wakeup_cause();
72
73 PowerCommand resp = PowerCommand_init_zero;
74 resp.type = PowerCommand_Type_WAKEUP;
75 resp.reason = static_cast<PowerCommand_WakeupReason>(wakeup_reason);
76 resp.boot_count = boot_count;
77
78 request_buffer_len =
79 EncodePowerCommand(&resp, request_buffer, sizeof(request_buffer));
80}
81
83 // only enter sleep if flag is set
84 if (!sleep_flag) {
85 return;
86 }
87
88 Log.noticeln("Entering deep sleep mode");
89
90 // configure gpio pin
91 const gpio_config_t config = {
92 .pin_bit_mask = BIT(GPIO_NUM_3),
93 .mode = GPIO_MODE_INPUT,
94 .pull_up_en = GPIO_PULLUP_DISABLE,
95 .pull_down_en = GPIO_PULLDOWN_DISABLE,
96 };
97 gpio_config(&config);
98 esp_deep_sleep_enable_gpio_wakeup(BIT(wakeup_pin), ESP_GPIO_WAKEUP_GPIO_HIGH);
99
100 // enter deep sleep
101 esp_deep_sleep_start();
102}
103
104} // namespace ModuleHandler
void OnReceive(const Esp32Command &cmd)
Definition power.cpp:23
void EnterSleep()
Put the esp32 into a deep sleep state if stated.
Definition power.cpp:82
size_t OnRequest(uint8_t *buffer)
Definition power.cpp:51
uint8_t buffer[32]
size_t EncodePowerCommand(const PowerCommand *power_cmd, uint8_t *buffer, size_t size)
Encodes a PowerCommand.
Definition transcoder.c:335
Power module for the esp32.
Module for controlling power on esp32.
Library for encoding/decoding protobuf messages.