firmware/src/plugins/CannedMessagePlugin.cpp

164 lines
4.2 KiB
C++
Raw Normal View History

2022-01-04 18:42:28 +00:00
#include "configuration.h"
#include "CannedMessagePlugin.h"
#include "MeshService.h"
2022-01-11 15:02:55 +00:00
#include "input/InputBroker.h"
2022-01-04 18:42:28 +00:00
#include <assert.h>
CannedMessagePlugin *cannedMessagePlugin;
2022-01-11 15:02:55 +00:00
CannedMessagePlugin::CannedMessagePlugin()
2022-01-09 09:08:31 +00:00
: SinglePortPlugin("canned", PortNum_TEXT_MESSAGE_APP),
concurrency::OSThread("CannedMessagePlugin")
2022-01-04 18:42:28 +00:00
{
2022-01-11 15:02:55 +00:00
if (true) // if module enabled
{
this->inputObserver.observe(inputBroker);
}
2022-01-04 18:42:28 +00:00
}
2022-01-09 09:08:31 +00:00
int CannedMessagePlugin::handleInputEvent(const InputEvent *event)
2022-01-04 18:42:28 +00:00
{
2022-01-11 15:02:55 +00:00
if (false) // test event->origin
{
return 0;
}
2022-01-09 09:08:31 +00:00
bool validEvent = false;
if (event->inputEvent == INPUT_EVENT_UP)
{
2022-01-09 20:14:23 +00:00
DEBUG_MSG("Canned message event UP\n");
2022-01-09 09:08:31 +00:00
this->action = CANNED_MESSAGE_ACTION_UP;
validEvent = true;
}
if (event->inputEvent == INPUT_EVENT_DOWN)
{
2022-01-09 20:14:23 +00:00
DEBUG_MSG("Canned message event DOWN\n");
2022-01-09 09:08:31 +00:00
this->action = CANNED_MESSAGE_ACTION_DOWN;
validEvent = true;
}
if (event->inputEvent == INPUT_EVENT_SELECT)
{
2022-01-09 20:14:23 +00:00
DEBUG_MSG("Canned message event Select\n");
2022-01-09 09:08:31 +00:00
this->action = CANNED_MESSAGE_ACTION_SELECT;
validEvent = true;
}
2022-01-04 18:42:28 +00:00
2022-01-09 09:08:31 +00:00
if (validEvent)
{
// Let runOnce to be called immediately.
runned(millis());
setInterval(0);
}
2022-01-04 18:42:28 +00:00
2022-01-09 09:08:31 +00:00
return 0;
2022-01-04 18:42:28 +00:00
}
2022-01-04 18:43:06 +00:00
void CannedMessagePlugin::sendText(NodeNum dest,
const char* message,
bool wantReplies)
2022-01-04 18:42:28 +00:00
{
MeshPacket *p = allocDataPacket();
p->to = dest;
2022-01-04 21:02:16 +00:00
p->decoded.payload.size = strlen(message);
memcpy(p->decoded.payload.bytes, message, p->decoded.payload.size);
2022-01-04 18:42:28 +00:00
// PacketId prevPacketId = p->id; // In case we need it later.
DEBUG_MSG("Sending message id=%d, msg=%.*s\n",
p->id, p->decoded.payload.size, p->decoded.payload.bytes);
service.sendToMesh(p);
}
int32_t CannedMessagePlugin::runOnce()
{
2022-01-09 20:14:23 +00:00
DEBUG_MSG("Check status\n");
2022-01-04 21:02:16 +00:00
if (this->sendingState == SENDING_STATE_ACTIVE)
{
// TODO: might have some feedback of sendig state
this->sendingState = SENDING_STATE_NONE;
this->notifyObservers(NULL);
}
2022-01-09 09:08:31 +00:00
else if ((this->action != CANNED_MESSAGE_ACTION_NONE)
2022-01-04 18:43:06 +00:00
&& (this->currentMessageIndex == -1))
2022-01-04 18:42:28 +00:00
{
2022-01-04 18:43:06 +00:00
this->currentMessageIndex = 0;
2022-01-05 14:52:08 +00:00
DEBUG_MSG("First touch.\n");
2022-01-04 18:42:28 +00:00
}
2022-01-09 09:08:31 +00:00
else if (this->action == CANNED_MESSAGE_ACTION_SELECT)
2022-01-04 18:42:28 +00:00
{
2022-01-04 18:43:06 +00:00
sendText(
NODENUM_BROADCAST,
cannedMessagePluginMessages[this->currentMessageIndex],
true);
2022-01-04 21:02:16 +00:00
this->sendingState = SENDING_STATE_ACTIVE;
this->currentMessageIndex = -1;
return 2000;
2022-01-04 18:42:28 +00:00
}
2022-01-09 09:08:31 +00:00
else if (this->action == CANNED_MESSAGE_ACTION_UP)
2022-01-04 18:42:28 +00:00
{
this->currentMessageIndex = getPrevIndex();
2022-01-05 14:52:08 +00:00
DEBUG_MSG("MOVE UP. Current message:%ld\n",
millis());
2022-01-04 18:42:28 +00:00
}
2022-01-09 09:08:31 +00:00
else if (this->action == CANNED_MESSAGE_ACTION_DOWN)
2022-01-04 18:42:28 +00:00
{
this->currentMessageIndex = this->getNextIndex();
2022-01-05 14:52:08 +00:00
DEBUG_MSG("MOVE DOWN. Current message:%ld\n",
millis());
2022-01-04 18:42:28 +00:00
}
2022-01-09 09:08:31 +00:00
if (this->action != CANNED_MESSAGE_ACTION_NONE)
2022-01-04 18:43:06 +00:00
{
2022-01-09 09:08:31 +00:00
this->action = CANNED_MESSAGE_ACTION_NONE;
2022-01-04 18:43:06 +00:00
this->notifyObservers(NULL);
}
return 30000;
2022-01-04 18:42:28 +00:00
}
2022-01-09 09:08:31 +00:00
String CannedMessagePlugin::getCurrentMessage()
2022-01-04 18:42:28 +00:00
{
2022-01-09 09:08:31 +00:00
return cannedMessagePluginMessages[this->currentMessageIndex];
}
String CannedMessagePlugin::getPrevMessage()
{
return cannedMessagePluginMessages[this->getPrevIndex()];
}
String CannedMessagePlugin::getNextMessage()
{
return cannedMessagePluginMessages[this->getNextIndex()];
}
bool CannedMessagePlugin::shouldDraw()
{
return (currentMessageIndex != -1) || (this->sendingState != SENDING_STATE_NONE);
}
cannedMessagePluginSendigState CannedMessagePlugin::getSendingState()
{
return this->sendingState;
2022-01-04 18:42:28 +00:00
}
2022-01-09 09:08:31 +00:00
int CannedMessagePlugin::getNextIndex()
2022-01-04 18:42:28 +00:00
{
2022-01-09 09:08:31 +00:00
if (this->currentMessageIndex >=
(sizeof(cannedMessagePluginMessages) / CANNED_MESSAGE_PLUGIN_MESSAGE_MAX_LEN) - 1)
2022-01-04 18:42:28 +00:00
{
2022-01-09 09:08:31 +00:00
return 0;
2022-01-04 18:42:28 +00:00
}
2022-01-09 09:08:31 +00:00
else
2022-01-04 18:42:28 +00:00
{
2022-01-09 09:08:31 +00:00
return this->currentMessageIndex + 1;
2022-01-04 18:42:28 +00:00
}
}
2022-01-09 09:08:31 +00:00
int CannedMessagePlugin::getPrevIndex()
2022-01-04 18:42:28 +00:00
{
2022-01-09 09:08:31 +00:00
if (this->currentMessageIndex <= 0)
2022-01-04 18:42:28 +00:00
{
2022-01-09 09:08:31 +00:00
return
sizeof(cannedMessagePluginMessages) / CANNED_MESSAGE_PLUGIN_MESSAGE_MAX_LEN - 1;
2022-01-04 18:42:28 +00:00
}
2022-01-09 09:08:31 +00:00
else
2022-01-04 18:42:28 +00:00
{
2022-01-09 09:08:31 +00:00
return this->currentMessageIndex - 1;
2022-01-04 18:42:28 +00:00
}
2022-01-09 09:08:31 +00:00
}