2022-01-04 19:42:28 +01:00
|
|
|
#pragma once
|
2022-10-13 15:44:27 +02:00
|
|
|
#if HAS_SCREEN
|
2022-03-09 19:01:43 +11:00
|
|
|
#include "ProtobufModule.h"
|
2022-01-12 09:26:42 +01:00
|
|
|
#include "input/InputBroker.h"
|
2022-01-04 19:42:28 +01:00
|
|
|
|
2025-05-24 20:46:33 -04:00
|
|
|
// ============================
|
|
|
|
|
// Enums & Defines
|
|
|
|
|
// ============================
|
|
|
|
|
|
2023-01-21 14:34:29 +01:00
|
|
|
enum cannedMessageModuleRunState {
|
2022-02-21 06:29:15 +01:00
|
|
|
CANNED_MESSAGE_RUN_STATE_DISABLED,
|
2022-01-19 09:55:06 +01:00
|
|
|
CANNED_MESSAGE_RUN_STATE_INACTIVE,
|
|
|
|
|
CANNED_MESSAGE_RUN_STATE_ACTIVE,
|
|
|
|
|
CANNED_MESSAGE_RUN_STATE_SENDING_ACTIVE,
|
2023-12-11 15:11:10 +01:00
|
|
|
CANNED_MESSAGE_RUN_STATE_ACK_NACK_RECEIVED,
|
2022-01-19 09:55:06 +01:00
|
|
|
CANNED_MESSAGE_RUN_STATE_ACTION_SELECT,
|
|
|
|
|
CANNED_MESSAGE_RUN_STATE_ACTION_UP,
|
2022-02-21 06:29:15 +01:00
|
|
|
CANNED_MESSAGE_RUN_STATE_ACTION_DOWN,
|
2025-05-26 01:35:34 -04:00
|
|
|
CANNED_MESSAGE_RUN_STATE_FREETEXT,
|
|
|
|
|
CANNED_MESSAGE_RUN_STATE_MESSAGE_SELECTION,
|
2025-03-28 14:39:15 -04:00
|
|
|
CANNED_MESSAGE_RUN_STATE_DESTINATION_SELECTION
|
2022-01-04 22:02:16 +01:00
|
|
|
};
|
|
|
|
|
|
2023-12-11 23:49:33 +01:00
|
|
|
enum cannedMessageDestinationType {
|
|
|
|
|
CANNED_MESSAGE_DESTINATION_TYPE_NONE,
|
|
|
|
|
CANNED_MESSAGE_DESTINATION_TYPE_NODE,
|
|
|
|
|
CANNED_MESSAGE_DESTINATION_TYPE_CHANNEL
|
|
|
|
|
};
|
|
|
|
|
|
2024-05-23 08:21:27 -04:00
|
|
|
enum CannedMessageModuleIconType { shift, backspace, space, enter };
|
|
|
|
|
|
2025-05-24 20:46:33 -04:00
|
|
|
#define CANNED_MESSAGE_MODULE_MESSAGE_MAX_COUNT 50
|
|
|
|
|
#define CANNED_MESSAGE_MODULE_MESSAGES_SIZE 800
|
|
|
|
|
|
|
|
|
|
#ifndef CANNED_MESSAGE_MODULE_ENABLE
|
|
|
|
|
#define CANNED_MESSAGE_MODULE_ENABLE 0
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// ============================
|
|
|
|
|
// Data Structures
|
|
|
|
|
// ============================
|
|
|
|
|
|
2024-05-23 08:21:27 -04:00
|
|
|
struct Letter {
|
|
|
|
|
String character;
|
|
|
|
|
float width;
|
|
|
|
|
int rectX;
|
|
|
|
|
int rectY;
|
|
|
|
|
int rectWidth;
|
|
|
|
|
int rectHeight;
|
|
|
|
|
};
|
|
|
|
|
|
2025-03-28 14:39:15 -04:00
|
|
|
struct NodeEntry {
|
|
|
|
|
meshtastic_NodeInfoLite *node;
|
|
|
|
|
uint32_t lastHeard;
|
|
|
|
|
};
|
|
|
|
|
|
2025-05-24 20:46:33 -04:00
|
|
|
// ============================
|
|
|
|
|
// Main Class
|
|
|
|
|
// ============================
|
|
|
|
|
|
|
|
|
|
class CannedMessageModule : public SinglePortModule,
|
|
|
|
|
public Observable<const UIFrameEvent *>,
|
|
|
|
|
private concurrency::OSThread {
|
|
|
|
|
public:
|
2022-02-27 01:20:23 -08:00
|
|
|
CannedMessageModule();
|
2025-05-24 20:46:33 -04:00
|
|
|
|
|
|
|
|
// === Message navigation ===
|
2023-01-21 14:34:29 +01:00
|
|
|
const char *getCurrentMessage();
|
|
|
|
|
const char *getPrevMessage();
|
|
|
|
|
const char *getNextMessage();
|
2023-07-30 14:51:26 +02:00
|
|
|
const char *getMessageByIndex(int index);
|
2023-01-21 14:34:29 +01:00
|
|
|
const char *getNodeName(NodeNum node);
|
2025-05-24 20:46:33 -04:00
|
|
|
|
|
|
|
|
// === State/UI ===
|
2022-01-09 10:08:31 +01:00
|
|
|
bool shouldDraw();
|
2024-08-07 10:16:56 +12:00
|
|
|
bool hasMessages();
|
2024-04-27 12:08:25 -04:00
|
|
|
void showTemporaryMessage(const String &message);
|
2025-05-24 20:46:33 -04:00
|
|
|
void resetSearch();
|
2025-03-28 14:39:15 -04:00
|
|
|
void updateFilteredNodes();
|
2025-05-26 01:35:34 -04:00
|
|
|
bool isInterceptingAndFocused();
|
2022-09-20 16:21:32 +02:00
|
|
|
String drawWithCursor(String text, int cursor);
|
|
|
|
|
|
2025-05-24 20:46:33 -04:00
|
|
|
// === Admin Handlers ===
|
|
|
|
|
void handleGetCannedMessageModuleMessages(const meshtastic_MeshPacket &req, meshtastic_AdminMessage *response);
|
|
|
|
|
void handleSetCannedMessageModuleMessages(const char *from_msg);
|
|
|
|
|
|
2024-11-11 20:37:43 +08:00
|
|
|
#ifdef RAK14014
|
|
|
|
|
cannedMessageModuleRunState getRunState() const { return runState; }
|
|
|
|
|
#endif
|
|
|
|
|
|
2025-05-24 20:46:33 -04:00
|
|
|
// === Packet Interest Filter ===
|
|
|
|
|
virtual bool wantPacket(const meshtastic_MeshPacket *p) override {
|
|
|
|
|
if (p->rx_rssi != 0) lastRxRssi = p->rx_rssi;
|
|
|
|
|
if (p->rx_snr > 0) lastRxSnr = p->rx_snr;
|
|
|
|
|
return (p->decoded.portnum == meshtastic_PortNum_ROUTING_APP) ? waitingForAck : false;
|
2023-08-22 16:23:02 +02:00
|
|
|
}
|
|
|
|
|
|
2025-05-24 20:46:33 -04:00
|
|
|
protected:
|
|
|
|
|
// === Thread Entry Point ===
|
2022-01-24 17:24:40 +00:00
|
|
|
virtual int32_t runOnce() override;
|
2022-01-04 19:42:28 +01:00
|
|
|
|
2025-05-24 20:46:33 -04:00
|
|
|
// === Transmission ===
|
2023-12-11 23:49:33 +01:00
|
|
|
void sendText(NodeNum dest, ChannelIndex channel, const char *message, bool wantReplies);
|
2022-01-12 09:26:42 +01:00
|
|
|
int splitConfiguredMessages();
|
2022-01-09 10:08:31 +01:00
|
|
|
int getNextIndex();
|
|
|
|
|
int getPrevIndex();
|
2022-01-04 20:41:45 +01:00
|
|
|
|
2024-11-23 08:54:06 +08:00
|
|
|
#if defined(USE_VIRTUAL_KEYBOARD)
|
2024-05-23 08:21:27 -04:00
|
|
|
void drawKeyboard(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y);
|
|
|
|
|
String keyForCoordinates(uint x, uint y);
|
|
|
|
|
void drawShiftIcon(OLEDDisplay *display, int x, int y, float scale = 1);
|
|
|
|
|
void drawBackspaceIcon(OLEDDisplay *display, int x, int y, float scale = 1);
|
|
|
|
|
void drawEnterIcon(OLEDDisplay *display, int x, int y, float scale = 1);
|
|
|
|
|
#endif
|
|
|
|
|
|
2025-05-24 20:46:33 -04:00
|
|
|
// === Input Handling ===
|
2022-01-09 10:08:31 +01:00
|
|
|
int handleInputEvent(const InputEvent *event);
|
2025-05-24 20:46:33 -04:00
|
|
|
virtual bool wantUIFrame() override { return shouldDraw(); }
|
2023-01-21 14:34:29 +01:00
|
|
|
virtual Observable<const UIFrameEvent *> *getUIFrameObservable() override { return this; }
|
2024-09-25 23:27:04 +12:00
|
|
|
virtual bool interceptingKeyboardInput() override;
|
2024-12-29 01:31:54 +11:00
|
|
|
#if !HAS_TFT
|
|
|
|
|
virtual void drawFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y) override;
|
|
|
|
|
#endif
|
2025-05-24 20:46:33 -04:00
|
|
|
virtual AdminMessageHandleResult handleAdminMessageForModule(
|
|
|
|
|
const meshtastic_MeshPacket &mp,
|
|
|
|
|
meshtastic_AdminMessage *request,
|
|
|
|
|
meshtastic_AdminMessage *response) override;
|
|
|
|
|
|
2023-08-22 16:23:02 +02:00
|
|
|
virtual ProcessMessage handleReceived(const meshtastic_MeshPacket &mp) override;
|
|
|
|
|
|
2022-02-27 02:21:02 -08:00
|
|
|
void loadProtoForModule();
|
|
|
|
|
bool saveProtoForModule();
|
2022-02-27 01:20:23 -08:00
|
|
|
void installDefaultCannedMessageModuleConfig();
|
2022-01-04 19:42:28 +01:00
|
|
|
|
2025-05-24 20:46:33 -04:00
|
|
|
private:
|
|
|
|
|
// === Input Observers ===
|
|
|
|
|
CallbackObserver<CannedMessageModule, const InputEvent *> inputObserver =
|
|
|
|
|
CallbackObserver<CannedMessageModule, const InputEvent *>(this, &CannedMessageModule::handleInputEvent);
|
|
|
|
|
|
|
|
|
|
// === Display and UI ===
|
|
|
|
|
int displayHeight = 64;
|
|
|
|
|
int destIndex = 0;
|
|
|
|
|
int scrollIndex = 0;
|
|
|
|
|
int visibleRows = 0;
|
|
|
|
|
bool needsUpdate = true;
|
|
|
|
|
bool shouldRedraw = false;
|
|
|
|
|
unsigned long lastUpdateMillis = 0;
|
|
|
|
|
String searchQuery;
|
|
|
|
|
String nodeSelectionInput;
|
|
|
|
|
String freetext;
|
|
|
|
|
String temporaryMessage;
|
2022-01-12 09:26:42 +01:00
|
|
|
|
2025-05-24 20:46:33 -04:00
|
|
|
// === Message Storage ===
|
2023-01-21 14:34:29 +01:00
|
|
|
char messageStore[CANNED_MESSAGE_MODULE_MESSAGES_SIZE + 1];
|
2022-02-27 01:49:24 -08:00
|
|
|
char *messages[CANNED_MESSAGE_MODULE_MESSAGE_MAX_COUNT];
|
2022-01-12 09:26:42 +01:00
|
|
|
int messagesCount = 0;
|
2025-05-24 20:46:33 -04:00
|
|
|
int currentMessageIndex = -1;
|
|
|
|
|
|
|
|
|
|
// === Routing & Acknowledgment ===
|
|
|
|
|
NodeNum dest = NODENUM_BROADCAST; // Destination node for outgoing messages (default: broadcast)
|
|
|
|
|
NodeNum incoming = NODENUM_BROADCAST; // Source node from which last ACK/NACK was received
|
|
|
|
|
NodeNum lastSentNode = 0; // Tracks the most recent node we sent a message to (for UI display)
|
|
|
|
|
ChannelIndex channel = 0; // Channel index used when sending a message
|
|
|
|
|
uint8_t numChannels = 0; // Total number of channels available for selection
|
|
|
|
|
ChannelIndex indexChannels[MAX_NUM_CHANNELS] = {0}; // Cached channel indices available for this node
|
|
|
|
|
|
|
|
|
|
bool ack = false; // True = ACK received, False = NACK or failed
|
|
|
|
|
bool waitingForAck = false; // True if we're expecting an ACK and should monitor routing packets
|
|
|
|
|
bool lastAckWasRelayed = false; // True if the ACK was relayed through intermediate nodes
|
|
|
|
|
uint8_t lastAckHopStart = 0; // Hop start value from the received ACK packet
|
|
|
|
|
uint8_t lastAckHopLimit = 0; // Hop limit value from the received ACK packet
|
|
|
|
|
|
|
|
|
|
float lastRxSnr = 0; // SNR from last received ACK (used for diagnostics/UI)
|
|
|
|
|
int32_t lastRxRssi = 0; // RSSI from last received ACK (used for diagnostics/UI)
|
|
|
|
|
|
|
|
|
|
// === State Tracking ===
|
|
|
|
|
cannedMessageModuleRunState runState = CANNED_MESSAGE_RUN_STATE_INACTIVE;
|
|
|
|
|
cannedMessageDestinationType destSelect = CANNED_MESSAGE_DESTINATION_TYPE_NONE;
|
|
|
|
|
char highlight = 0x00;
|
|
|
|
|
char payload = 0x00;
|
|
|
|
|
unsigned int cursor = 0;
|
2022-01-13 12:51:36 +01:00
|
|
|
unsigned long lastTouchMillis = 0;
|
2025-05-24 20:46:33 -04:00
|
|
|
std::vector<uint8_t> activeChannelIndices;
|
|
|
|
|
std::vector<NodeEntry> filteredNodes;
|
|
|
|
|
|
2025-05-26 01:35:34 -04:00
|
|
|
bool isInputSourceAllowed(const InputEvent *event);
|
|
|
|
|
bool isUpEvent(const InputEvent *event);
|
|
|
|
|
bool isDownEvent(const InputEvent *event);
|
|
|
|
|
bool isSelectEvent(const InputEvent *event);
|
|
|
|
|
bool handleTabSwitch(const InputEvent *event);
|
|
|
|
|
int handleDestinationSelectionInput(const InputEvent *event, bool isUp, bool isDown, bool isSelect);
|
|
|
|
|
bool handleMessageSelectorInput(const InputEvent *event, bool isUp, bool isDown, bool isSelect);
|
|
|
|
|
bool handleFreeTextInput(const InputEvent *event);
|
|
|
|
|
bool handleSystemCommandInput(const InputEvent *event);
|
2024-05-23 08:21:27 -04:00
|
|
|
|
2024-11-23 08:54:06 +08:00
|
|
|
#if defined(USE_VIRTUAL_KEYBOARD)
|
2024-05-23 08:21:27 -04:00
|
|
|
Letter keyboard[2][4][10] = {{{{"Q", 20, 0, 0, 0, 0},
|
|
|
|
|
{"W", 22, 0, 0, 0, 0},
|
|
|
|
|
{"E", 17, 0, 0, 0, 0},
|
|
|
|
|
{"R", 16.5, 0, 0, 0, 0},
|
|
|
|
|
{"T", 14, 0, 0, 0, 0},
|
|
|
|
|
{"Y", 15, 0, 0, 0, 0},
|
|
|
|
|
{"U", 16.5, 0, 0, 0, 0},
|
|
|
|
|
{"I", 5, 0, 0, 0, 0},
|
|
|
|
|
{"O", 19.5, 0, 0, 0, 0},
|
|
|
|
|
{"P", 15.5, 0, 0, 0, 0}},
|
|
|
|
|
{{"A", 14, 0, 0, 0, 0},
|
|
|
|
|
{"S", 15, 0, 0, 0, 0},
|
|
|
|
|
{"D", 16.5, 0, 0, 0, 0},
|
|
|
|
|
{"F", 15, 0, 0, 0, 0},
|
|
|
|
|
{"G", 17, 0, 0, 0, 0},
|
|
|
|
|
{"H", 15.5, 0, 0, 0, 0},
|
|
|
|
|
{"J", 12, 0, 0, 0, 0},
|
|
|
|
|
{"K", 15.5, 0, 0, 0, 0},
|
|
|
|
|
{"L", 14, 0, 0, 0, 0},
|
|
|
|
|
{"", 0, 0, 0, 0, 0}},
|
|
|
|
|
{{"⇧", 20, 0, 0, 0, 0},
|
|
|
|
|
{"Z", 14, 0, 0, 0, 0},
|
|
|
|
|
{"X", 14.5, 0, 0, 0, 0},
|
|
|
|
|
{"C", 15.5, 0, 0, 0, 0},
|
|
|
|
|
{"V", 13.5, 0, 0, 0, 0},
|
|
|
|
|
{"B", 15, 0, 0, 0, 0},
|
|
|
|
|
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0}},
|
|
|
|
|
{{".", 8, 0, 0, 0, 0},
|
|
|
|
|
{",", 8, 0, 0, 0, 0},
|
|
|
|
|
{"?", 10, 0, 0, 0, 0},
|
|
|
|
|
{"!", 10, 0, 0, 0, 0},
|
|
|
|
|
{"'", 10, 0, 0, 0, 0},
|
|
|
|
|
{"⌫", 20, 0, 0, 0, 0}},
|
|
|
|
|
{{"ABC", 50, 0, 0, 0, 0}, {" ", 64, 0, 0, 0, 0}, {"↵", 36, 0, 0, 0, 0}}}};
|
|
|
|
|
#endif
|
2022-01-04 19:42:28 +01:00
|
|
|
};
|
|
|
|
|
|
2022-02-27 01:20:23 -08:00
|
|
|
extern CannedMessageModule *cannedMessageModule;
|
2025-05-24 20:46:33 -04:00
|
|
|
#endif
|