53 #define AS_THRESHOLD 1
80 #define AS_NOT_ASSIGNED 255
91 #ifndef AS_RECEIVE_TIME
92 #define AS_RECEIVE_TIME 1000
101 uint32_t back_off(uint8_t attempts)
103 uint32_t result = attempts;
104 for(uint8_t d = 0; d < AS_BACK_OFF_DEGREE; d++) {
105 result *= (uint32_t)(attempts);
114 bool begin(uint8_t did = 0)
122 #elif AS_PRESCALE == 16
126 #elif AS_PRESCALE == 32
132 PJON_DELAY(PJON_RANDOM(AS_INITIAL_DELAY) + did);
133 compute_analog_read_duration();
134 _last_byte = receive_byte();
145 uint32_t time = PJON_MICROS();
147 (uint32_t)(PJON_MICROS() - time) <=
148 (AS_BIT_SPACER + (AS_BIT_WIDTH * 9))
149 )
if(receive_byte() != PJON_FAIL) {
152 PJON_DELAY_MICROSECONDS(PJON_RANDOM(AS_COLLISION_DELAY));
153 if((uint16_t)PJON_ANALOG_READ(_input_pin) > threshold) {
162 void compute_analog_read_duration()
164 uint32_t time = PJON_MICROS();
165 PJON_ANALOG_READ(_input_pin);
166 _analog_read_time = (uint32_t)(PJON_MICROS() - time);
172 static uint8_t get_max_attempts()
174 return AS_MAX_ATTEMPTS;
180 static uint16_t get_receive_time()
182 return AS_RECEIVE_TIME;
188 void handle_collision()
190 PJON_DELAY_MICROSECONDS(PJON_RANDOM(AS_COLLISION_DELAY));
198 uint16_t bit_value = 0;
199 uint16_t high_bit = 0;
200 uint16_t low_bit = 0;
201 uint8_t byte_value = 0;
202 for(
int i = 0; i < 8; i++) {
203 long time = PJON_MICROS();
204 PJON_DELAY_MICROSECONDS((AS_BIT_WIDTH / 2) - AS_READ_DELAY);
205 bit_value = PJON_ANALOG_READ(_input_pin);
206 byte_value += (bit_value > threshold) << i;
209 (((bit_value > threshold) ? bit_value : high_bit) + high_bit) / 2;
212 (((bit_value < threshold) ? bit_value : low_bit) + low_bit) / 2;
214 PJON_DELAY_MICROSECONDS(
215 AS_BIT_WIDTH - (uint32_t)(PJON_MICROS() - time)
218 threshold = (high_bit + low_bit) / 2;
219 _last_update = PJON_MICROS();
239 uint16_t receive_byte()
241 PJON_IO_PULL_DOWN(_input_pin);
242 if(_output_pin != AS_NOT_ASSIGNED && _output_pin != _input_pin) {
243 PJON_IO_PULL_DOWN(_output_pin);
245 uint32_t time = PJON_MICROS();
248 (uint32_t)(PJON_MICROS() - _last_update) >
249 AS_THRESHOLD_DECREASE_INTERVAL
252 _last_update = PJON_MICROS();
256 ((uint16_t)(PJON_ANALOG_READ(_input_pin)) > threshold) &&
257 ((uint32_t)(PJON_MICROS() - time) <= AS_BIT_SPACER)
260 time = PJON_MICROS() - time;
261 if(time < AS_BIT_SPACER * 0.75) {
264 if(time <= AS_BIT_SPACER * 1.25) {
265 PJON_DELAY_MICROSECONDS(AS_BIT_WIDTH);
274 uint16_t receive_response()
276 PJON_IO_PULL_DOWN(_input_pin);
277 if(_output_pin != AS_NOT_ASSIGNED && _output_pin != _input_pin) {
278 PJON_IO_WRITE(_output_pin,
LOW);
280 uint16_t response = AS_FAIL;
281 uint32_t time = PJON_MICROS();
283 (response != PJON_ACK) &&
284 (uint32_t)(PJON_MICROS() - AS_RESPONSE_TIMEOUT) <= time
286 response = receive_byte();
294 uint16_t receive_frame(uint8_t *
data, uint16_t max_length)
298 if(max_length == PJON_PACKET_MAX_LENGTH)
300 (receive_byte() != AS_START) ||
301 (_last_byte == AS_ESC)
305 result = receive_byte();
306 if(result == AS_FAIL) {
310 if(result == AS_START) {
313 if(result == AS_ESC) {
314 result = receive_byte();
316 if((result != AS_START) && (result != AS_ESC) && (result != AS_END)) {
322 if(max_length == 1 && receive_byte() != AS_END) {
348 void send_byte(uint8_t b)
350 PJON_IO_WRITE(_output_pin,
HIGH);
351 PJON_DELAY_MICROSECONDS(AS_BIT_SPACER);
352 PJON_IO_WRITE(_output_pin,
LOW);
353 PJON_DELAY_MICROSECONDS(AS_BIT_WIDTH);
354 for(uint8_t mask = 0x01; mask; mask <<= 1) {
355 PJON_IO_WRITE(_output_pin, b & mask);
356 PJON_DELAY_MICROSECONDS(AS_BIT_WIDTH);
363 void send_response(uint8_t response)
365 PJON_DELAY_MICROSECONDS(AS_BIT_WIDTH);
366 PJON_IO_PULL_DOWN(_input_pin);
367 PJON_IO_MODE(_output_pin, OUTPUT);
369 PJON_IO_PULL_DOWN(_output_pin);
375 void send_frame(uint8_t *
data, uint16_t length)
377 PJON_IO_MODE(_output_pin, OUTPUT);
380 for(uint16_t b = 0; b < length; b++)
382 (
data[b] == AS_START) ||
383 (
data[b] == AS_ESC) ||
387 send_byte(
data[b] ^ AS_ESC);
392 PJON_IO_PULL_DOWN(_output_pin);
398 void set_pin(uint8_t pin)
400 PJON_IO_PULL_DOWN(pin);
409 uint8_t input_pin = AS_NOT_ASSIGNED,
410 uint8_t output_pin = AS_NOT_ASSIGNED
413 PJON_IO_PULL_DOWN(input_pin);
414 PJON_IO_PULL_DOWN(output_pin);
415 _input_pin = input_pin;
416 _output_pin = output_pin;
422 void set_threshold(uint16_t value)
428 uint16_t threshold = AS_THRESHOLD;
431 uint16_t _analog_read_time;
434 uint32_t _last_update;