Implement interface for plugins to have custom UI Frames

pull/705/head
Charles Crossan 2021-02-21 16:46:46 -05:00
rodzic 087945d7cb
commit ae76ce4024
5 zmienionych plików z 125 dodań i 12 usunięć

Wyświetl plik

@ -64,6 +64,10 @@ uint8_t imgBattery[16] = {0xFF, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
// Threshold values for the GPS lock accuracy bar display
uint32_t dopThresholds[5] = {2000, 1000, 500, 200, 100};
// At some point, we're going to ask all of the plugins if they would like to display a screen frame
// we'll need to hold onto pointers for the plugins that can draw a frame.
std::vector<MeshPlugin *> pluginFrames;
// Stores the last 4 of our hardware ID, to make finding the device for pairing easier
static char ourId[5];
@ -144,6 +148,14 @@ static void drawSleepScreen(OLEDDisplay *display, OLEDDisplayUiState *state, int
drawIconScreen("Sleeping...", display, state, x, y);
}
static void drawPluginFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y)
{
DEBUG_MSG("Drawing Plugin Frame %d\n\n", state->currentFrame);
MeshPlugin &pi = *pluginFrames.at(state->currentFrame);
pi.drawFrame(display,state,x,y);
}
static void drawFrameBluetooth(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y)
{
display->setTextAlignment(TEXT_ALIGN_CENTER);
@ -887,6 +899,11 @@ void Screen::setFrames()
DEBUG_MSG("showing standard frames\n");
showingNormalScreen = true;
pluginFrames = MeshPlugin::GetMeshPluginsWithUIFrames();
DEBUG_MSG("Showing %d plugin frames\n", pluginFrames.size());
int totalFrameCount = MAX_NUM_NODES + NUM_EXTRA_FRAMES + pluginFrames.size();
DEBUG_MSG("Total frame count: %d\n", totalFrameCount);
// We don't show the node info our our node (if we have it yet - we should)
size_t numnodes = nodeStatus->getNumTotal();
if (numnodes > 0)
@ -894,6 +911,18 @@ void Screen::setFrames()
size_t numframes = 0;
// put all of the plugin frames first.
// this is a little bit of a dirty hack; since we're going to call
// the same drawPluginFrame handler here for all of these plugin frames
// and then we'll just assume that the state->currentFrame value
// is the same offset into the pluginFrames vector
// so that we can invoke the plugin's callback
for (auto i = pluginFrames.begin(); i != pluginFrames.end(); ++i) {
normalFrames[numframes++] = drawPluginFrame;
}
DEBUG_MSG("Added plugins. numframes: %d", numframes);
// If we have a critical fault, show it first
if (myNodeInfo.error_code)
normalFrames[numframes++] = drawCriticalFaultFrame;
@ -922,6 +951,8 @@ void Screen::setFrames()
}
#endif
DEBUG_MSG("Finished building frames. numframes: %d\n", numframes);
ui.setFrames(normalFrames, numframes);
ui.enableAllIndicators();

Wyświetl plik

@ -76,3 +76,17 @@ void setReplyTo(MeshPacket *p, const MeshPacket &to) {
p->to = to.from;
p->want_ack = to.want_ack;
}
std::vector<MeshPlugin *> MeshPlugin::GetMeshPluginsWithUIFrames() {
std::vector<MeshPlugin *> pluginsWithUIFrames;
for (auto i = plugins->begin(); i != plugins->end(); ++i) {
auto &pi = **i;
if ( pi.wantUIFrame()) {
DEBUG_MSG("Plugin wants a UI Frame\n");
pluginsWithUIFrames.push_back(&pi);
}
}
return pluginsWithUIFrames;
}

Wyświetl plik

@ -2,6 +2,8 @@
#include "mesh/MeshTypes.h"
#include <vector>
#include <OLEDDisplay.h>
#include <OLEDDisplayUi.h>
/** A baseclass for any mesh "plugin".
*
* A plugin allows you to add new features to meshtastic device code, without needing to know messaging details.
@ -28,6 +30,10 @@ class MeshPlugin
*/
static void callPlugins(const MeshPacket &mp);
static std::vector<MeshPlugin *> GetMeshPluginsWithUIFrames();
virtual void drawFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y) { return; }
protected:
const char *name;
@ -61,6 +67,13 @@ class MeshPlugin
* so that subclasses can (optionally) send a response back to the original sender. */
virtual MeshPacket *allocReply() { return NULL; }
/***
* @return true if you want to be alloced a UI screen frame
*/
virtual bool wantUIFrame() { return false; }
private:
/** Messages can be received that have the want_response bit set. If set, this callback will be invoked

Wyświetl plik

@ -7,6 +7,8 @@
#include "main.h"
#include "../mesh/generated/environmental_measurement.pb.h"
#include <DHT.h>
#include <OLEDDisplay.h>
#include <OLEDDisplayUi.h>
EnvironmentalMeasurementPlugin *environmentalMeasurementPlugin;
EnvironmentalMeasurementPluginRadio *environmentalMeasurementPluginRadio;
@ -17,7 +19,7 @@ uint32_t sensor_read_error_count = 0;
#define DHT_11_GPIO_PIN 13
//TODO: Make a related radioconfig preference to allow less-frequent reads
#define ENVIRONMENTAL_MEASUREMENT_APP_ENABLED false // DISABLED by default; set this to true if you want to use the plugin
#define ENVIRONMENTAL_MEASUREMENT_APP_ENABLED true // DISABLED by default; set this to true if you want to use the plugin
#define DHT_SENSOR_MINIMUM_WAIT_TIME_BETWEEN_READS 1000
#define SENSOR_READ_ERROR_COUNT_THRESHOLD 5
#define SENSOR_READ_MULTIPLIER 3
@ -26,6 +28,24 @@ uint32_t sensor_read_error_count = 0;
DHT dht(DHT_11_GPIO_PIN,DHT11);
#ifdef HAS_EINK
// The screen is bigger so use bigger fonts
#define FONT_SMALL ArialMT_Plain_16
#define FONT_MEDIUM ArialMT_Plain_24
#define FONT_LARGE ArialMT_Plain_24
#else
#define FONT_SMALL ArialMT_Plain_10
#define FONT_MEDIUM ArialMT_Plain_16
#define FONT_LARGE ArialMT_Plain_24
#endif
#define fontHeight(font) ((font)[1] + 1) // height is position 1
#define FONT_HEIGHT_SMALL fontHeight(FONT_SMALL)
#define FONT_HEIGHT_MEDIUM fontHeight(FONT_MEDIUM)
int32_t EnvironmentalMeasurementPlugin::runOnce() {
#ifndef NO_ESP32
if (!ENVIRONMENTAL_MEASUREMENT_APP_ENABLED){
@ -70,13 +90,17 @@ int32_t EnvironmentalMeasurementPlugin::runOnce() {
#endif
}
bool EnvironmentalMeasurementPluginRadio::handleReceivedProtobuf(const MeshPacket &mp, const EnvironmentalMeasurement &p)
void EnvironmentalMeasurementPluginRadio::drawFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y)
{
if (!ENVIRONMENTAL_MEASUREMENT_APP_ENABLED){
// If this plugin is not enabled, don't handle the packet, and allow other plugins to consume
return false;
}
bool wasBroadcast = mp.to == NODENUM_BROADCAST;
display->setTextAlignment(TEXT_ALIGN_LEFT);
display->setFont(FONT_MEDIUM);
display->drawString(x, y, "Environment");
display->setFont(FONT_SMALL);
display->drawString(x, y += fontHeight(FONT_MEDIUM), lastSender+": T:"+ String(lastMeasurement.temperature,2) + " H:" + String(lastMeasurement.relative_humidity,2));
}
String GetSenderName(const MeshPacket &mp) {
String sender;
if (nodeDB.getNode(mp.from)){
@ -85,18 +109,34 @@ bool EnvironmentalMeasurementPluginRadio::handleReceivedProtobuf(const MeshPacke
else {
sender = "UNK";
}
return sender;
}
bool EnvironmentalMeasurementPluginRadio::handleReceivedProtobuf(const MeshPacket &mp, const EnvironmentalMeasurement &p)
{
if (!ENVIRONMENTAL_MEASUREMENT_APP_ENABLED){
// If this plugin is not enabled, don't handle the packet, and allow other plugins to consume
return false;
}
bool wasBroadcast = mp.to == NODENUM_BROADCAST;
String sender = GetSenderName(mp);
// Show new nodes on LCD screen
if (DISPLAY_RECEIVEID_MEASUREMENTS_ON_SCREEN && wasBroadcast) {
String lcd = String("Env Measured: ") + sender + "\n" +
String lcd = String("Env Measured: ") +sender + "\n" +
"T: " + p.temperature + "\n" +
"H: " + p.relative_humidity + "\n";
screen->print(lcd.c_str());
}
DEBUG_MSG("-----------------------------------------\n");
DEBUG_MSG("EnvironmentalMeasurement: Received data from %s\n",sender);
DEBUG_MSG("EnvironmentalMeasurement: Received data from %s\n", sender);
DEBUG_MSG("EnvironmentalMeasurement->relative_humidity: %f\n", p.relative_humidity);
DEBUG_MSG("EnvironmentalMeasurement->temperature: %f\n", p.temperature);
lastMeasurement = p;
lastSender = sender;
return false; // Let others look at this message also if they want
}

Wyświetl plik

@ -1,6 +1,8 @@
#pragma once
#include "ProtobufPlugin.h"
#include "../mesh/generated/environmental_measurement.pb.h"
#include <OLEDDisplay.h>
#include <OLEDDisplayUi.h>
class EnvironmentalMeasurementPlugin : private concurrency::OSThread
@ -32,6 +34,8 @@ class EnvironmentalMeasurementPluginRadio : public ProtobufPlugin<EnvironmentalM
*/
bool sendOurEnvironmentalMeasurement(NodeNum dest = NODENUM_BROADCAST, bool wantReplies = false);
virtual void drawFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y);
protected:
/** Called to handle a particular incoming message
@ -40,6 +44,17 @@ class EnvironmentalMeasurementPluginRadio : public ProtobufPlugin<EnvironmentalM
*/
virtual bool handleReceivedProtobuf(const MeshPacket &mp, const EnvironmentalMeasurement &p);
virtual bool wantUIFrame() {
return true;
}
private:
EnvironmentalMeasurement lastMeasurement;
String lastSender;
};
extern EnvironmentalMeasurementPluginRadio *environmentalMeasurementPluginRadio;