aboutsummaryrefslogtreecommitdiff
path: root/c/arduino/framing.h
blob: fc2cd12da3439f41ffe7078c798cc95f2e9dddcc (plain) (blame)
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
#ifndef FRAMING_H
#define FRAMING_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stdbool.h>
#include <inttypes.h>

#define MAX_FRAME_SIZE 128
#define FRAME_ESCAPE 0x10
#define FRAME_START 0x02
#define FRAME_STOP 0x03

typedef enum {
  WAITING,
  RECEIVING,
  ESCAPING
} link_state;

typedef void (*receive_frame_function)(int16_t size, uint8_t* data);
typedef void (*send_byte_function)(uint8_t byte);


typedef struct {
  link_state state; // current state
  uint8_t buffer[MAX_FRAME_SIZE]; // in data buffer
  uint8_t staged; // previously read byte, will be put in buffer after next read
  int16_t position; //current position of next byte to be read in buffer
  uint8_t checksum; //current value of calculated checksum
  
  receive_frame_function receiver;
  send_byte_function sender;
  
} framer;

void init_framer(framer* f);
void receive_byte(framer* f, uint8_t byte);
void send_frame(framer* f, int16_t size, const uint8_t * const data_out);


#ifdef __cplusplus
} // extern "C"
#endif

#endif /* FRAMING_H */