A function of type void
can be defined and register to be called by the PJON object when a packet is received. This function receives 3 parameters: the received payload of type uint8_t *
, its length of type uint16_t
and a pointer to a data structure of type const PJON_Packet_Info
that contains all packet's metadata:
void receiver_function(uint8_t *payload, uint16_t length, const PJON_Packet_Info &packet_info) {
/* Make use of the payload before sending something, the buffer where payload points to is
overwritten when a new message is dispatched */
Serial.print("Header: ");
Serial.print(packet_info.header, BIN);
// If packet formatted for a shared medium
if(packet_info.header & PJON_MODE_BIT) {
Serial.print(" Receiver bus id: ");
Serial.print(packet_info.receiver_bus_id[0]);
Serial.print(packet_info.receiver_bus_id[1]);
Serial.print(packet_info.receiver_bus_id[2]);
Serial.print(packet_info.receiver_bus_id[3]);
Serial.print(" Receiver id: ");
Serial.print(packet_info.receiver_id);
// If sender info is included
if(packet_info.header & PJON_TX_INFO_BIT) {
Serial.print(" Sender bus id: ");
Serial.print(packet_info.sender_bus_id[0]);
Serial.print(packet_info.sender_bus_id[1]);
Serial.print(packet_info.sender_bus_id[2]);
Serial.print(packet_info.sender_bus_id[3]);
}
}
// If sender device id is included
if(packet_info.header & PJON_TX_INFO_BIT) {
Serial.print(" Sender id: ");
Serial.print(packet_info.sender_id);
}
// Payload Length
Serial.print(" Length: ");
Serial.print(length);
// If port id is included
if(packet_info.header & PJON_PORT_BIT) {
Serial.print(" Port bit: ");
Serial.print(packet_info.port);
}
Serial.println();
}
Register the receiver_function
as the receiver callback:
bus.set_receiver(receiver_function);
To pass custom data to the receiver callback function, se the ClassMemberCallback example. This feature can be used for a lot of different use cases. Could be used to let multiple PJON objects call the same callback function, passing an int
specifying which PJON instance has to be called, or a pointer to the PJON object, or an enum
or whatever.
To receive the receive
function must be called at least once per loop cycle:
uint16_t response = bus.receive();
receive
returns the following values:
PJON_ACK
(6) if a correct reception occurredPJON_NAK
(21) if a mistake is found in CRCPJON_BUSY
(666) if a transmission for other devices is occurringPJON_FAIL
(65535) if no data is received
If you want to dedicate a certain timeframe to reception call the receive
function passing the maximum reception time in microseconds:
uint16_t response = bus.receive(1000);
Consider that SoftwareBitBang, OverSampling or AnalogSampling are strategies able receive data only while bus.receive
is being executed, otherwise data is lost and transmitter will try again in future. In this particular case it is mandatory to dedicate a certain timeframe, depending on the duration of the other tasks, to efficiently receive data and avoid repetitions.