Permalink
Browse files

Merge pull request #2 from pantonvich/packets

Add Packet Clear and Count Features
  • Loading branch information...
pantonvich committed Aug 2, 2016
2 parents 9851a22 + 530136a commit dc62a7eaf5a82483945dc621c90a7efeb9b0dca4
Showing with 32 additions and 4 deletions.
  1. +30 −4 PJON.h
  2. +2 −0 keywords.txt
34 PJON.h
@@ -332,7 +332,29 @@ limitations under the License. */
packets[id].state = 0;
};


/* Remove all packets from the list:
set device_id to zero(0) to clear all packets */

void remove_all(uint8_t device_id){
for(uint8_t i = 0; i < MAX_PACKETS; i++) {
if(packets[i].state == 0) continue;
if (device_id == 0 || packets[i].device_id == device_id)
remove(i);
}
}

/* Get count of the packets for a device_id:
set device_id to zero(0) to count all packets */

uint8_t get_packet_count(uint8_t device_id){
uint8_t packet_count = 0;
for(uint8_t i = 0; i < MAX_PACKETS; i++) {
if(packets[i].state == 0) continue;
if (device_id == 0 || packets[i].device_id == device_id)
packet_count++;
}
return packet_count;
}
/* Insert a packet in the send list:
The added packet will be sent in the next update() call.
Using the timing parameter you can set the delay between every
@@ -695,16 +717,18 @@ limitations under the License. */
check if there are packets to be sent or to be erased
if correctly delivered */

void update() {
uint8_t update() {
uint8_t packet_count = 0;
for(uint8_t i = 0; i < MAX_PACKETS; i++) {
if(packets[i].state == 0) continue;
packet_count++;
if((uint32_t)(micros() - packets[i].registration) > packets[i].timing + pow(packets[i].attempts, 3))
packets[i].state = send_string(packets[i].device_id, packets[i].content, packets[i].length, packets[i].header);
else continue;

if(packets[i].state == ACK) {
if(!packets[i].timing) {
if(_auto_delete) remove(i);
if(_auto_delete) {remove(i); packet_count--;}
} else {
packets[i].attempts = 0;
packets[i].registration = micros();
@@ -718,10 +742,11 @@ limitations under the License. */
if(packets[i].content[0] == ACQUIRE_ID) {
_device_id = packets[i].device_id;
remove(i);
packet_count--;
continue;
} else _error(CONNECTION_LOST, packets[i].device_id);
if(!packets[i].timing) {
if(_auto_delete) remove(i);
if(_auto_delete) {remove(i); packet_count--;}
} else {
packets[i].attempts = 0;
packets[i].registration = micros();
@@ -730,6 +755,7 @@ limitations under the License. */
}
}
}
return packet_count;
};

uint8_t data[PACKET_MAX_LENGTH];
@@ -28,6 +28,8 @@ set_error KEYWORD2
set_id KEYWORD2
set_pin KEYWORD2
set_pins KEYWORD2
remove_all KEYWORD2
get_packet_count KEYWORD2

#######################################
# Instances (KEYWORD2)

0 comments on commit dc62a7e

Please sign in to comment.