3#include "main-header.h"
6#include <platform-header.h>
20#define SECS_AFTER_BOOT_BEFORE_POWERSAFE 120
21#define SECS_AFTER_WAKE_BEFORE_POWERSAFE 10
24#define SECS_AFTER_LASTCOM_BEFORE_POWERSAFE 3
27static long throttle_after_fast = 0;
28static long powersave_afterwake = 0;
29static long powersave_aftercom = 0;
30static byte powermode = 0;
31static byte booted = 0;
33static void power_mcusleep();
34static void trigger_send_powering_down();
44int power_set_connectivity(
struct power_state *ps,
int turnon) {
46 printf(
"[power] Turning connectivity on...\r\n");
52 start_nic_by_type(SOURCE_RADIO);
55 start_nic_by_type(SOURCE_LORA);
58 send_routing_update_now();
60 trigger_send_powering_down();
61 printf(
"[power] Turning connectivity off...\r\n");
62 ps->wifi_on = esp8266_is_enabled();
64 ps->ti1101_on = stop_nic_by_type(SOURCE_RADIO);
65 ps->lora_on = stop_nic_by_type(SOURCE_LORA);
73int set_mcu_power_mode(
int state) {
75 struct cnw_config *cfg = config_get_ram_struct();
79 led_set_machine_managed();
80 led_indicate(LED_POWERSAVE);
82 led_set_user_managed();
85 printf(
"[power] Going to low power mode for %i secs...\r\n\r\n", cfg->sleep_duration);
86 mculib_sleep(0, cfg->sleep_duration);
103void calc_power_mode() {
107void power_main_loop() {
109 if (!config_get_flag(CONFIG_FLAGS_POWER_SAVE)) {
113 if (!powerup_delivered()) {
119 if (mculib_has_time_passed(SECS_AFTER_BOOT_BEFORE_POWERSAFE, &powersave_afterwake) == 0) {
124 struct cnw_config *cfg = config_get_ram_struct();
130 if (config_get_powermode_flag(CONFIG_POWERFLAG_THROTTLE)) {
131 if (mculib_has_time_passed(10, &throttle_after_fast)) {
140void power_mcusleep() {
141 struct cnw_config *cfg = config_get_ram_struct();
143 if ((cfg->sleep_duration == 0) || (cfg->nonsleep_duration == 0)) {
152 if (powermode == 0) {
153 if (mculib_has_time_passed(cfg->nonsleep_duration, &powersave_afterwake) == 0) {
157 mculib_has_time_passed(0, &powersave_aftercom);
164 if (powermode == 1) {
167 mculib_has_time_passed(0, &powersave_aftercom);
170 if (mculib_has_time_passed(SECS_AFTER_LASTCOM_BEFORE_POWERSAFE, &powersave_aftercom) == 0) {
178 set_mcu_power_mode(0);
182 mculib_has_time_passed(0, &powersave_aftercom);
183 mculib_has_time_passed(0, &powersave_afterwake);
187static void trigger_send_powering_down() {
196 com->target = CLOUD_SERVER;
198 for (z = 0; z < 10; z++) {
206 printf(
"[power] Result of sending powerinfo to server: %i\r\n", r);
212 com->target = BROADCAST;
214 for (z = 0; z < 10; z++) {
222 printf(
"[power] Result of sending powerinfo broadcast: %i\r\n", r);
225void power_set_speed(
int speed) {
227 mculib_has_time_passed(0, &throttle_after_fast);
233 if (!config_get_flag(CONFIG_FLAGS_POWER_SAVE)) {
237 if (mculib_get_speed() == speed) {
240 printf(
"[power] setting mculib speed to %i\r\n", speed);
241 mculib_set_speed(speed);
void esp8266_enable()
enable the esp8266 (switch it on)
int send_command(struct command *com)
send a command to another module (or broadcast)
void free_command(struct command *com)
free a command
void remove_stale_routes()
"old" routes are being removed this is the route garbage collector
void command_add_arg(struct command *com, const char *format,...)
adds an arg to a partially initialised command structure
int get_outbound_command_count_important()
return number of commands to be delivered (apart from announce/noop)
int count_open_streams()
count number of open streams
void esp8266_disable()
disable the esp8266 (switch it off)
struct command * alloc_command()
allocate a free command