SimpleSerialPacket lib

Hey,

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,

http://www.p3psoft.co.uk/files/SimpleSerialPacket.zip

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() {
  Serial.begin(57600);
}

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
  receive_packet.receive();
  
  // 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();
      break;
    //packet was type 1
    case 1:
      //Initialises the packet to send and sets its type
      send_packet.setType(0);
      //adds a byte to the packet
      send_packet.addByte(128);
      //adds an int as 2 bytes to the packet
      send_packet.addInt(1000);
      //adds a long as 4 bytes
      send_packet.addLong(40000000);
      //sends the packet and resets
      send_packet.send();
      break;
    default:
      break;
    }
    //resets the packet to receive another
    receive_packet.reset();
  }
}

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

p3p