Add doc note about threading and use OSThread to make GPIO watching work

Thanks to @mc-hamster for the idea
This commit is contained in:
Kevin Hester
2020-12-11 18:29:32 +08:00
parent af88a34f75
commit 1e5d0b25ad
4 changed files with 104 additions and 14 deletions

View File

@@ -1,17 +1,26 @@
#pragma once
#include "ProtobufPlugin.h"
#include "remote_hardware.pb.h"
#include "concurrency/OSThread.h"
/**
* A plugin that provides easy low-level remote access to device hardware.
*/
class RemoteHardwarePlugin : public ProtobufPlugin<HardwareMessage>
class RemoteHardwarePlugin : public ProtobufPlugin<HardwareMessage>, private concurrency::OSThread
{
/// The current set of GPIOs we've been asked to watch for changes
uint64_t watchGpios = 0;
/// The previously read value of watched pins
uint64_t previousWatch = 0;
/// The timestamp of our last watch event (we throttle watches to 1 change every 30 seconds)
uint32_t lastWatchMsec = 0;
public:
/** Constructor
* name is for debugging output
*/
RemoteHardwarePlugin() : ProtobufPlugin("remotehardware", PortNum_REMOTE_HARDWARE_APP, HardwareMessage_fields) {}
RemoteHardwarePlugin();
protected:
/** Called to handle a particular incoming message
@@ -19,6 +28,16 @@ class RemoteHardwarePlugin : public ProtobufPlugin<HardwareMessage>
@return true if you've guaranteed you've handled this message and no other handlers should be considered for it
*/
virtual bool handleReceivedProtobuf(const MeshPacket &mp, const HardwareMessage &p);
/**
* Periodically read the gpios we have been asked to WATCH, if they have changed,
* broadcast a message with the change information.
*
* The method that will be called each time our thread gets a chance to run
*
* Returns desired period for next invocation (or RUN_SAME for no change)
*/
virtual int32_t runOnce();
};
extern RemoteHardwarePlugin remoteHardwarePlugin;