Skip to content

Commit

Permalink
modify existing function
Browse files Browse the repository at this point in the history
  • Loading branch information
pdxlocations committed Dec 7, 2024
1 parent c4b9f98 commit aa022a1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 16 deletions.
8 changes: 6 additions & 2 deletions examples/SendReceiveClient/SendReceiveClient.ino
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@ void text_message_callback(uint32_t from, uint32_t to, const char* text) {
Serial.print(to);
Serial.print(" message: ");
Serial.println(text);
if (to == my_node_num){
if (to == 0xFFFFFFFF){
Serial.println("This is a BROADCAST message.");
} else if (to == my_node_num){
Serial.println("This is a DM to me!");
} else {
Serial.println("This is a DM to someone else.");
}
}

Expand Down Expand Up @@ -85,7 +89,7 @@ void setup() {
mt_request_node_report(connected_callback);

// Register a callback function to be called whenever a text message is received
set_directed_text_message_callback(text_message_callback);
set_text_message_callback(text_message_callback);
}

void loop() {
Expand Down
5 changes: 1 addition & 4 deletions src/Meshtastic.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,7 @@ typedef enum {
bool mt_request_node_report(void (*callback)(mt_node_t *, mt_nr_progress_t));

// Set the callback function that gets called when the node receives a text message.
void set_text_message_callback(void (*callback)(uint32_t from, const char * text));

// Set the callback function that gets called when the node receives a text message. Includes the 'to' parameter to catch DM's vs Broadcast.
void set_directed_text_message_callback(void (*callback)(uint32_t from, uint32_t to, const char * text));
void set_text_message_callback(void (*callback)(uint32_t from, uint32_t to, const char * text));

// Send a text message with *text* as payload, to a destination node (optional), on a certain channel (optional).
bool mt_send_text(const char * text, uint32_t dest = BROADCAST_ADDR, uint8_t channel_index = 0);
Expand Down
13 changes: 3 additions & 10 deletions src/mt_protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ uint32_t want_config_id = 0;
uint32_t my_node_num = 0;

bool mt_debugging = false;
void (*text_message_callback)(uint32_t from, const char* text) = NULL;
void (*directed_text_message_callback)(uint32_t from, uint32_t to, const char* text) = NULL;
void (*text_message_callback)(uint32_t from, uint32_t to, const char* text) = NULL;
void (*node_report_callback)(mt_node_t *, mt_nr_progress_t) = NULL;
mt_node_t node;

Expand Down Expand Up @@ -117,14 +116,10 @@ bool mt_send_text(const char * text, uint32_t dest, uint8_t channel_index) {
return _mt_send_toRadio(toRadio);
}

void set_text_message_callback(void (*callback)(uint32_t from, const char* text)) {
void set_text_message_callback(void (*callback)(uint32_t from, uint32_t to, const char* text)) {
text_message_callback = callback;
}

void set_directed_text_message_callback(void (*callback)(uint32_t from, uint32_t to, const char* text)) {
directed_text_message_callback = callback;
}

bool handle_my_info(meshtastic_MyNodeInfo *myNodeInfo) {
my_node_num = myNodeInfo->my_node_num;
return true;
Expand Down Expand Up @@ -195,9 +190,7 @@ bool handle_mesh_packet(meshtastic_MeshPacket *meshPacket) {
if (meshPacket->which_payload_variant == meshtastic_MeshPacket_decoded_tag) {
if (meshPacket->decoded.portnum == meshtastic_PortNum_TEXT_MESSAGE_APP) {
if (text_message_callback != NULL)
text_message_callback(meshPacket->from, (const char*)meshPacket->decoded.payload.bytes);
if (directed_text_message_callback != NULL)
directed_text_message_callback(meshPacket->from, meshPacket->to, (const char*)meshPacket->decoded.payload.bytes);
text_message_callback(meshPacket->from, meshPacket->to, (const char*)meshPacket->decoded.payload.bytes);
} else {
// TODO handle other portnums
return false;
Expand Down

0 comments on commit aa022a1

Please sign in to comment.