mirror of
https://github.com/meshtastic/firmware.git
synced 2025-12-21 02:02:23 +00:00
Abstracted statuses, made display event-driven
This commit is contained in:
62
src/NodeStatus.h
Normal file
62
src/NodeStatus.h
Normal file
@@ -0,0 +1,62 @@
|
||||
#pragma once
|
||||
#include <Arduino.h>
|
||||
#include "lock.h"
|
||||
#include "configuration.h"
|
||||
|
||||
namespace meshtastic {
|
||||
|
||||
/// Describes the state of the GPS system.
|
||||
struct NodeStatus
|
||||
{
|
||||
|
||||
bool isDirty = false;
|
||||
size_t numOnline; // Number of nodes online
|
||||
size_t numTotal; // Number of nodes total
|
||||
|
||||
};
|
||||
|
||||
class NodeStatusHandler
|
||||
{
|
||||
|
||||
private:
|
||||
NodeStatus status;
|
||||
CallbackObserver<NodeStatusHandler, const NodeStatus> nodeObserver = CallbackObserver<NodeStatusHandler, const NodeStatus>(this, &NodeStatusHandler::updateStatus);
|
||||
bool initialized = false;
|
||||
/// Protects all of internal state.
|
||||
Lock lock;
|
||||
|
||||
public:
|
||||
Observable<void *> onNewStatus;
|
||||
|
||||
void observe(Observable<const NodeStatus> *source)
|
||||
{
|
||||
nodeObserver.observe(source);
|
||||
}
|
||||
|
||||
bool isInitialized() const { return initialized; }
|
||||
size_t getNumOnline() const { return status.numOnline; }
|
||||
size_t getNumTotal() const { return status.numTotal; }
|
||||
|
||||
int updateStatus(const NodeStatus newStatus)
|
||||
{
|
||||
// Only update the status if values have actually changed
|
||||
status.isDirty = (
|
||||
newStatus.numOnline != status.numOnline ||
|
||||
newStatus.numTotal != status.numTotal
|
||||
);
|
||||
LockGuard guard(&lock);
|
||||
initialized = true;
|
||||
status.numOnline = newStatus.numOnline;
|
||||
status.numTotal = newStatus.numTotal;
|
||||
if(status.isDirty) {
|
||||
DEBUG_MSG("Node status update: %d online, %d total\n", status.numOnline, status.numTotal);
|
||||
onNewStatus.notifyObservers(NULL);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
extern meshtastic::NodeStatus nodeStatus;
|
||||
Reference in New Issue
Block a user