Skip to content

Commit

Permalink
powermon WIP (for meshtastic#4136 )
Browse files Browse the repository at this point in the history
  • Loading branch information
geeksville committed Jun 19, 2024
1 parent 03033ec commit f435851
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 1 deletion.
2 changes: 1 addition & 1 deletion protobufs
33 changes: 33 additions & 0 deletions src/PowerMon.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "PowerMon.h"
#include "configuration.h"

void PowerMon::setState(_meshtastic_PowerMon_State state, const char *reason)
{
auto oldstates = states;
states |= state;
if (oldstates != states) {
emitLog(reason);
}
}

void PowerMon::clearState(_meshtastic_PowerMon_State state, const char *reason)
{
auto oldstates = states;
states &= ~state;
if (oldstates != states) {
emitLog(reason);
}
}

void PowerMon::emitLog(const char *reason)
{
// The nrf52 printf doesn't understand 64 bit ints, so if we ever reach that point this function will need to change.
LOG_INFO("PowerMon:C,0x%08lx,%s\n", (uint32_t)states, reason);
}

PowerMon *powerMon;

void powerMonInit()
{
powerMon = new PowerMon();
}
30 changes: 30 additions & 0 deletions src/PowerMon.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once
#include "configuration.h"

#include "meshtastic/powermon.pb.h"

/**
* The singleton class for monitoring power consumption of device
* subsystems/modes.
*
* For more information see the PowerMon docs.
*/
class PowerMon
{
uint64_t states = 0UL;

public:
PowerMon() {}

// Mark entry/exit of a power consuming state
void setState(_meshtastic_PowerMon_State state, const char *reason = "");
void clearState(_meshtastic_PowerMon_State state, const char *reason = "");

private:
// Emit the coded log message
void emitLog(const char *reason);
};

extern PowerMon *powerMon;

void powerMonInit();
2 changes: 2 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "MeshService.h"
#include "NodeDB.h"
#include "PowerFSM.h"
#include "PowerMon.h"
#include "ReliableRouter.h"
#include "airtime.h"
#include "buzz.h"
Expand Down Expand Up @@ -234,6 +235,7 @@ void setup()
#ifdef DEBUG_PORT
consoleInit(); // Set serial baud rate and init our mesh console
#endif
powerMonInit();

serialSinceMsec = millis();

Expand Down
13 changes: 13 additions & 0 deletions src/mesh/generated/meshtastic/powermon.pb.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* Automatically generated nanopb constant definitions */
/* Generated by nanopb-0.4.8 */

#include "meshtastic/powermon.pb.h"
#if PB_PROTO_HEADER_VERSION != 40
#error Regenerate this file with the current version of nanopb generator.
#endif

PB_BIND(meshtastic_PowerMon_Event, meshtastic_PowerMon_Event, AUTO)




81 changes: 81 additions & 0 deletions src/mesh/generated/meshtastic/powermon.pb.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/* Automatically generated nanopb header */
/* Generated by nanopb-0.4.8 */

#ifndef PB_MESHTASTIC_POWERMON_MESHTASTIC_POWERMON_PB_H_INCLUDED
#define PB_MESHTASTIC_POWERMON_MESHTASTIC_POWERMON_PB_H_INCLUDED
#include <pb.h>

#if PB_PROTO_HEADER_VERSION != 40
#error Regenerate this file with the current version of nanopb generator.
#endif

/* Enum definitions */
/* Any significant power changing event in meshtastic should be tagged with a powermon state transition.
If you are making new meshtastic features feel free to add new entries at the end of this definition. */
typedef enum _meshtastic_PowerMon_State {
meshtastic_PowerMon_State_None = 0,
meshtastic_PowerMon_State_CPU_DeepSleep = 1,
meshtastic_PowerMon_State_CPU_Sleep = 2,
meshtastic_PowerMon_State_CPU_Awake = 4,
meshtastic_PowerMon_State_Lora_RXOn = 8,
meshtastic_PowerMon_State_Lora_TXOn = 16,
meshtastic_PowerMon_State_Lora_RXActive = 32,
meshtastic_PowerMon_State_BT_On = 64,
meshtastic_PowerMon_State_LED_On = 128,
meshtastic_PowerMon_State_Screen_On = 256,
meshtastic_PowerMon_State_Screen_Drawing = 512,
meshtastic_PowerMon_State_Wifi_On = 1024,
meshtastic_PowerMon_State_GPS_LowPower = 2048,
meshtastic_PowerMon_State_GPS_MediumPower = 4096,
meshtastic_PowerMon_State_GPS_HighPower = 8192
} meshtastic_PowerMon_State;

/* Struct definitions */
/* the log messages will be short and complete (see PowerMon.Event in the protobufs for details).
something like "PwrMon,C,0x00001234,REASON" where the hex number is the bitmask of all current states.
(We use a bitmask for states so that if a log message gets lost it won't be fatal) */
typedef struct _meshtastic_PowerMon_Event {
/* Bitwise-OR of States */
bool has_states;
uint64_t states;
} meshtastic_PowerMon_Event;


#ifdef __cplusplus
extern "C" {
#endif

/* Helper constants for enums */
#define _meshtastic_PowerMon_State_MIN meshtastic_PowerMon_State_None
#define _meshtastic_PowerMon_State_MAX meshtastic_PowerMon_State_GPS_HighPower
#define _meshtastic_PowerMon_State_ARRAYSIZE ((meshtastic_PowerMon_State)(meshtastic_PowerMon_State_GPS_HighPower+1))



/* Initializer values for message structs */
#define meshtastic_PowerMon_Event_init_default {false, 0}
#define meshtastic_PowerMon_Event_init_zero {false, 0}

/* Field tags (for use in manual encoding/decoding) */
#define meshtastic_PowerMon_Event_states_tag 1

/* Struct field encoding specification for nanopb */
#define meshtastic_PowerMon_Event_FIELDLIST(X, a) \
X(a, STATIC, OPTIONAL, UINT64, states, 1)
#define meshtastic_PowerMon_Event_CALLBACK NULL
#define meshtastic_PowerMon_Event_DEFAULT NULL

extern const pb_msgdesc_t meshtastic_PowerMon_Event_msg;

/* Defines for backwards compatibility with code written before nanopb-0.4.0 */
#define meshtastic_PowerMon_Event_fields &meshtastic_PowerMon_Event_msg

/* Maximum encoded size of messages (where known) */
#define MESHTASTIC_POWERMON_MESHTASTIC_POWERMON_PB_H_MAX_SIZE meshtastic_PowerMon_Event_size
#define meshtastic_PowerMon_Event_size 11

#ifdef __cplusplus
} /* extern "C" */
#endif

#endif
6 changes: 6 additions & 0 deletions src/sleep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "MeshRadio.h"
#include "MeshService.h"
#include "NodeDB.h"
#include "PowerMon.h"
#include "detect/LoRaRadioType.h"
#include "error.h"
#include "main.h"
Expand Down Expand Up @@ -85,6 +86,11 @@ void setCPUFast(bool on)

void setLed(bool ledOn)
{
if (ledOn)
powerMon->setState(meshtastic_PowerMon_State_LED_On);
else
powerMon->clearState(meshtastic_PowerMon_State_LED_On);

#ifdef LED_PIN
// toggle the led so we can get some rough sense of how often loop is pausing
digitalWrite(LED_PIN, ledOn ^ LED_INVERTED);
Expand Down

0 comments on commit f435851

Please sign in to comment.