blob: 40aa1edb63987a6b1ee851b52ff0e0b59c917afd (
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
79
80
81
82
83
84
85
86
87
|
/*
* Class to handle internal communications in the machine via RS485
*
* Adrian Bowyer 3 July 2009
*
*/
/*
A data packet looks like this:
T T F F d a t a '\n'
All these are printable characters. TT is the two-char name of the destination, FF is the two-char name
of the source, data is a char string containing the message, which is terminated by a '\n' character.
If users wish they can include checksum and length information in the data, as long as it is all printable.
The total length of a packet with all of that should not exceed BUF_LEN (defined in configuration.h)
characters. When a packet is received the '\n' character is replaced by 0, thus forming a standard C string.
*/
#ifndef INTERCOM_H
#define INTERCOM_H
// Our RS485 driver
#define serialInterface Serial
// compare two names
#define sameName(a, b) ((a)[0] == (b)[0] && (a)[1] == (b)[1])
// The acknowledge character
#define ACK 'A'
// Size of the transmit and receive buffers
#define BUF_LEN 30
class intercom
{
private:
// The input buffer for incoming packets
char buffer[BUF_LEN];
int buf_pointer;
/*
Any short message can be returned with the next acknowledgement in this string.
This includes things like temperatures and so on.
Assign to it like this:
setReply(" My message");
with a blank first character. This will be overwritten with the ACK
character.
Errors should be flagged with '!':
setReply(" !Horrible error.");
You can append extra information on the end with
addReply("additional info");
Don't exceed BUF_LEN - 4
*/
char reply[BUF_LEN];
bool ok;
void getPacket(char* string, int len);
void sendPacket(char* to, char* string);
void sendPacketWithReply(char* to, char* string, char* resp, int len);
void reset();
void acknowledgeAndReset(char* to);
public:
intercom();
void getAndProcessCommand();
void setReply(char* string);
void addReply(char* string);
char* getReply();
};
extern intercom talker;
#endif
|