forked from gioblu/PJON
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPJON.cpp
More file actions
452 lines (349 loc) · 13.2 KB
/
PJON.cpp
File metadata and controls
452 lines (349 loc) · 13.2 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*-O//\ __ __
|-gfo\ |__| | | | |\ |
|!y°o:\ | __| |__| | \| v1.0
|y"s§+`\ Giovanni Blu Mitolo 2012 - 2016
/so+:-..`\ gioscarab@gmail.com
|+/:ngr-*.`\
|5/:%&-a3f.:;\ PJON is a device communications bus system that connects up to 255
\+//u/+g%{osv,,\ arduino boards over one wire up to 5.29kB/s data communication speed.
\=+&/osw+olds.\\ Contains acknowledge, collision detection, CRC and encpryption all done
\:/+-.-°-:+oss\ with micros() and delayMicroseconds(), with no use of interrupts or timers.
| | \oy\\ Pull down resistor on the bus is generally used to reduce interference.
> <
-| |-
Copyright (c) 2012-2016, Giovanni Blu Mitolo All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
- All advertising materials mentioning features or use of this software
must display the following acknowledgement:
This product includes PJON software developed by Giovanni Blu Mitolo.
- Neither the name of PJON, PJON_ASK nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
This software is provided by the copyright holders and contributors"as is"
and any express or implied warranties, including, but not limited to, the
implied warranties of merchantability and fitness for a particular purpose
are disclaimed. In no event shall the copyright holder or contributors be
liable for any direct, indirect, incidental, special, exemplary, or consequential
damages (including, but not limited to, procurement of substitute goods or services;
loss of use, data, or profits; or business interruption) however caused and on any
theory of liability, whether in contract, strict liability, or tort (including
negligence or otherwise) arising in any way out of the use of this software, even if
advised of the possibility of such damage. */
#include "PJON.h"
/* Initiate PJON passing pin number:
Device's id has to be set through set_id()
before transmitting on the PJON network. */
PJON::PJON(int input_pin) {
_input_pin = input_pin;
this->initialize();
}
/* Initiate PJON passing pin number and the device's id: */
PJON::PJON(int input_pin, uint8_t device_id) {
_input_pin = input_pin;
_device_id = device_id;
this->initialize();
}
/* Initialization tasks: */
void PJON::initialize() {
this->set_error(dummy_error_handler);
this->set_receiver(dummy_receiver_handler);
for(int i = 0; i < MAX_PACKETS; i++) {
packets[i].state = NULL;
packets[i].timing = 0;
packets[i].attempts = 0;
}
}
/* Set the device id, passing a single byte (watch out to id collision) */
void PJON::set_id(uint8_t id) {
_device_id = id;
}
/* Get the device id, returning a single byte (watch out to id collision) */
uint8_t PJON::get_id() {
return _device_id;
}
/* Pass as a parameter a void function you previously defined in your code.
This will be called when a correct message will be received.
Inside there you can code how to react when data is received.
void receiver_function(uint8_t length, uint8_t *payload) {
for(int i = 0; i < length; i++)
Serial.print((char)payload[i]);
Serial.print(" ");
Serial.println(length);
};
network.set_receiver(receiver_function); */
void PJON::set_receiver(receiver r) {
_receiver = r;
}
/* Pass as a parameter a void function you previously defined in your code.
This will be called when an error in communication occurs
void error_handler(uint8_t code, uint8_t data) {
Serial.print(code);
Serial.print(" ");
Serial.println(data);
};
network.set_error(error_handler); */
void PJON::set_error(error e) {
_error = e;
}
/* Check if the channel is free for transmission:
If an entire byte received contains no 1s it means
that there is no active transmission */
boolean PJON::can_start() {
pinModeFast(_input_pin, INPUT);
this->send_bit(0, 2);
if(!this->read_byte())
return true;
return false;
}
/* Send a bit to the pin:
digitalWriteFast is used instead of standard digitalWrite
function to optimize transmission time */
void PJON::send_bit(uint8_t VALUE, int duration) {
digitalWriteFast(_input_pin, VALUE);
delayMicroseconds(duration);
}
/* Every byte is prepended with 2 synchronization padding bits. The first
is a longer than standard logic 1 followed by a standard logic 0.
__________ ___________________________
| SyncPad | Byte |
|______ |___ ___ _____ |
| | | | | | | | | |
| | 1 | 0 | 1 | 0 0 | 1 | 0 | 1 1 | 0 |
|_|____|___|___|_____|___|___|_____|___|
|
ACCEPTANCE
The reception tecnique is based on finding a logic 1 as long as the
first padding bit within a certain threshold, synchronizing to its
falling edge and checking if it is followed by a logic 0. If this
pattern is recognised, reception starts, if not, interference,
synchronization loss or simply absence of communication is
detected at byte level. */
void PJON::send_byte(uint8_t b) {
digitalWriteFast(_input_pin, HIGH);
delayMicroseconds(BIT_SPACER);
digitalWriteFast(_input_pin, LOW);
delayMicroseconds(BIT_WIDTH);
for(uint8_t mask = 0x01; mask; mask <<= 1) {
digitalWriteFast(_input_pin, b & mask);
delayMicroseconds(BIT_WIDTH);
}
}
/* An Example of how the string "@" is formatted and sent:
ID 12 LENGTH 4 CONTENT 64 CRC 130
________________ ________________ ________________ __________________
|Sync | Byte |Sync | Byte |Sync | Byte |Sync | Byte |
|___ | __ |___ | _ |___ | _ |___ | _ _ |
| | | | | | | | | | | | | | | | | | | | | | |
| 1 |0|0000|11|00| 1 |0|00000|1|00| 1 |0|0|1|000000| 1 |0|0|1|0000|1|0|
|___|_|____|__|__|___|_|_____|_|__|___|_|_|_|______|___|_|_|_|____|_|_|
A standard packet transmission is a bidirectional communication between
two devices that can be divided in 3 different phases:
Channel analysis Transmission Response
_____ _____________________________ _____
| C-A | | ID | LENGTH | CONTENT | CRC | | ACK |
<--|-----|---------|----|--------|---------|-----|--> <----|-----|
| 0 | | 12 | 4 | 64 | 130 | | 6 |
|_____| |____|________|_________|_____| |_____| */
int PJON::send_string(uint8_t id, char *string, uint8_t length) {
if(!*string) return FAIL;
if(!this->can_start()) return BUSY;
uint8_t CRC = 0;
pinModeFast(_input_pin, OUTPUT);
this->send_byte(id);
CRC ^= id;
this->send_byte(length + 3);
CRC ^= length + 3;
for(uint8_t i = 0; i < length; i++) {
this->send_byte(string[i]);
CRC ^= string[i];
}
this->send_byte(CRC);
digitalWriteFast(_input_pin, LOW);
if(id == BROADCAST) return ACK;
unsigned long time = micros();
int response = FAIL;
/* Receive byte for an initial BIT_SPACER bit + standard bit total duration.
(freak condition used to avoid micros() overflow bug) */
while(response == FAIL && !(micros() - time >= BIT_SPACER + BIT_WIDTH))
response = this->receive_byte();
if(response == ACK || response == NAK) return response;
return FAIL;
};
/* Insert a packet in the send list:
The added packet will be sent in the next update() call.
Using the timing parameter you can set the delay between every
transmission cyclically sending the packet (use remove() function stop it)
int hi = network.send(99, "HI!", 3, 1000000); // Send hi every second
_________________________________________________________________________
| | | | | | | |
| device_id | length | content | state | attempts | timing | registration |
|___________|________|_________|_______|__________|________|______________| */
int PJON::send(uint8_t id, char *packet, uint8_t length, unsigned long timing) {
if(length >= PACKET_MAX_LENGTH) {
this->_error(CONTENT_TOO_LONG, length);
return FAIL;
}
char *str = (char *) malloc(length);
if(str == NULL) {
this->_error(MEMORY_FULL, FAIL);
return FAIL;
}
memcpy(str, packet, length);
for(uint8_t i = 0; i < MAX_PACKETS; i++)
if(packets[i].state == NULL) {
packets[i].content = str;
packets[i].device_id = id;
packets[i].length = length;
packets[i].state = TO_BE_SENT;
if(timing > 0) {
packets[i].registration = micros();
packets[i].timing = timing;
}
return i;
}
this->_error(PACKETS_BUFFER_FULL, MAX_PACKETS);
return FAIL;
}
/* Update the state of the send list:
check if there are packets to be sent or to be erased
if correctly delivered */
void PJON::update() {
for(uint8_t i = 0; i < MAX_PACKETS; i++) {
if(packets[i].state != NULL)
if(micros() - packets[i].registration > packets[i].timing + pow(packets[i].attempts, 2))
packets[i].state = send_string(packets[i].device_id, packets[i].content, packets[i].length);
if(packets[i].state == ACK) {
if(!packets[i].timing)
this->remove(i);
else {
packets[i].attempts = 0;
packets[i].registration = micros();
packets[i].state = TO_BE_SENT;
}
}
if(packets[i].state == FAIL) {
packets[i].attempts++;
if(packets[i].attempts > MAX_ATTEMPTS) {
this->_error(CONNECTION_LOST, packets[i].device_id);
if(!packets[i].timing)
this->remove(i);
else {
packets[i].attempts = 0;
packets[i].registration = micros();
packets[i].state = TO_BE_SENT;
}
}
}
}
}
/* Remove a packet from the send list: */
void PJON::remove(int id) {
free(packets[id].content);
packets[id].attempts = 0;
packets[id].device_id = NULL;
packets[id].length = NULL;
packets[id].state = NULL;
packets[id].registration = NULL;
}
/* Syncronize with transmitter:
This function is used only in byte syncronization.
READ_DELAY has to be tuned to correctly send and
receive transmissions because this variable shifts
in which portion of the bit, the reading will be
executed by the next read_byte function */
uint8_t PJON::syncronization_bit() {
delayMicroseconds((BIT_WIDTH / 2) - READ_DELAY);
uint8_t bit_value = digitalReadFast(_input_pin);
delayMicroseconds(BIT_WIDTH / 2);
return bit_value;
}
/* Check if a byte is coming from the pin:
This function is looking for padding bits before a byte.
If value is 1 for more than ACCEPTANCE and after
that comes a 0 probably a byte is coming:
________
| Init |
|--------|
|_____ |
| | | |
|1 | |0 |
|__|__|__|
|
ACCEPTANCE */
int PJON::receive_byte() {
/* Initialize the pin and set it to LOW to reduce interference */
pinModeFast(_input_pin, INPUT);
digitalWriteFast(_input_pin, LOW);
unsigned long time = micros();
/* Do nothing until the pin stops to be HIGH or passed more time than
BIT_SPACER duration (freak condition used to avoid micros() overflow bug) */
while(digitalReadFast(_input_pin) && !(micros() - time >= BIT_SPACER));
/* Save how much time passed */
time = micros() - time;
/* is for sure less than BIT_SPACER, and if is more than ACCEPTANCE
(a minimum HIGH duration) and what is coming after is a LOW bit
probably a byte is coming so try to receive it. */
if(time >= ACCEPTANCE && !this->syncronization_bit())
return (int)this->read_byte();
return FAIL;
}
/* Read a byte from the pin */
uint8_t PJON::read_byte() {
uint8_t byte_value = B00000000;
delayMicroseconds(BIT_WIDTH / 2);
for(uint8_t i = 0; i < 8; i++) {
byte_value += digitalReadFast(_input_pin) << i;
delayMicroseconds(BIT_WIDTH);
}
return byte_value;
}
/* Try to receive a packet from the pin: */
int PJON::receive() {
int state;
int package_length = PACKET_MAX_LENGTH;
uint8_t CRC = 0;
for(uint8_t i = 0; i < package_length; i++) {
data[i] = state = this->receive_byte();
if(state == FAIL) return FAIL;
if(i == 0 && data[i] != _device_id && data[i] != BROADCAST)
return BUSY;
if(i == 1)
if(data[i] > 3 && data[i] < PACKET_MAX_LENGTH)
package_length = data[i];
else return FAIL;
CRC ^= data[i];
}
pinModeFast(_input_pin, OUTPUT);
if(!CRC) {
if(data[0] != BROADCAST) {
this->send_byte(ACK);
digitalWriteFast(_input_pin, LOW);
}
this->_receiver(data[1] - 3, data + 2);
return ACK;
} else {
if(data[0] != BROADCAST) {
this->send_byte(NAK);
digitalWriteFast(_input_pin, LOW);
}
return NAK;
}
}
/* Try to receive a packet from the pin repeatedly with a maximum duration: */
int PJON::receive(unsigned long duration) {
int response;
long time = micros();
/* (freak condition used to avoid micros() overflow bug) */
while(!(micros() - time >= duration)) {
response = this->receive();
if(response == ACK)
return ACK;
}
return response;
}