SimpleSerialPacket lib


This is my first attempt at an arduino lib, not used to programming within such tight constraints so there will be allot of optimising to do, would just like some feedback on the general design idea and a little free testing if anyone decides to use it. I also have SimpleSpiPacket which has the same interface, but obviously cant be as streamlined with the limitations of the master - slave communications,

This example is included in the zip

#include <SimpleSerialPacket.h>

SimpleSerialPacket receive_packet;
// best to use a dedicated send packet, although posible to use 1
// you would have to make sure that there was no partial incoming 
// packet in the buffer before sending
SimpleSerialPacket send_packet;

byte temp_byte = 0;
int temp_int = 0;
long temp_long = 0;

void setup() {

void loop() {
  // receives 1 byte per loop so as to not block
  // simpley a matter of changing an if to a while
  // and it would recieve as much of the packet as was available
  // may include it as a define
  // checks to see if theres a valid packet, 
  // has to have had the correct header, payload and checksum
  if(receive_packet.valid()) {
    switch(receive_packet.type() ){
    //packet was type 0
    case 0:
      //gets the first byte and stores it
      temp_byte = receive_packet.getByte();
      //gets the next 2 bytes and converts them to an int
      temp_int = receive_packet.getInt();
      //gets the next 4 bytes and converts them to a long
      temp_long = receive_packet.getLong();
    //packet was type 1
    case 1:
      //Initialises the packet to send and sets its type
      //adds a byte to the packet
      //adds an int as 2 bytes to the packet
      //adds a long as 4 bytes
      //sends the packet and resets
    //resets the packet to receive another

There may be issues with the memory management and/or variable conversion … ect so dont trust this code :slight_smile: