summaryrefslogtreecommitdiff
path: root/trunk/darwin/firmware/Sanguino/Sanguino3G/libraries/SimplePacket/SimplePacket.h
blob: cd53140cdbc5e726154952d9855c1a4aec265821 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#ifndef _SIMPLE_PACKET_H_
#define _SIMPLE_PACKET_H_

//include our various libraries.
#include <util/crc16.h>
#include <stdint.h>
#include "HardwareSerial.h"

#define START_BYTE 0xD5
#define MAX_PACKET_LENGTH 32

typedef void (*txFuncPtr)(uint8_t);

// packet states
typedef enum {
  PS_START = 0,
  PS_LEN,
  PS_PAYLOAD,
  PS_CRC,
  PS_LAST
} 
PacketState;

// various error codes
typedef enum {
  RC_GENERIC_ERROR   = 0,
  RC_OK              = 1,
  RC_BUFFER_OVERFLOW = 2,
  RC_CRC_MISMATCH    = 3,
  RC_PACKET_TOO_BIG  = 4,
  RC_CMD_UNSUPPORTED = 5
} 
ResponseCode;

class SimplePacket {
private:
  //variables for our incoming packet.
  PacketState state;
  uint8_t target_length;
  uint8_t rx_length;
  uint8_t rx_data[MAX_PACKET_LENGTH];
  uint8_t rx_crc;
  uint8_t tx_length;
  uint8_t tx_data[MAX_PACKET_LENGTH];
  uint8_t tx_crc;
  ResponseCode response_code;

  txFuncPtr txFunc;

public:

  SimplePacket(txFuncPtr myPtr);
  void init();

  //process a byte from our packet
  void process_byte(uint8_t b);
  bool isFinished();
  uint8_t getLength();
  PacketState getState();
  ResponseCode getResponseCode();

  void unsupported();
  void overflow();

  void sendReply();
  void sendPacket();
  void transmit(uint8_t d);

  void add_32(uint32_t d);
  void add_16(uint16_t d);
  void add_8(uint8_t d);

  uint8_t get_8(uint8_t idx);
  uint16_t get_16(uint8_t idx);
  uint32_t get_32(uint8_t idx);
};

#endif // _SIMPLE_PACKET_H_