44 #ifndef OS_RECEIVE_TIME
45 #define OS_RECEIVE_TIME 1000
52 #define OS_NOT_ASSIGNED 255
62 uint32_t back_off(uint8_t attempts)
64 uint32_t result = attempts;
65 for(uint8_t d = 0; d < OS_BACK_OFF_DEGREE; d++) {
66 result *= (uint32_t)(attempts);
75 bool begin(uint8_t did = 0)
77 PJON_DELAY(PJON_RANDOM(OS_INITIAL_DELAY) + did);
89 uint32_t time = PJON_MICROS();
90 PJON_IO_MODE(_input_pin, INPUT);
91 while((uint32_t)(PJON_MICROS() - time) < OS_TIMEOUT) {
92 value = (value * 0.999) + (PJON_IO_READ(_input_pin) * 0.001);
97 if(PJON_IO_READ(_input_pin)) {
100 PJON_DELAY_MICROSECONDS(PJON_RANDOM(OS_COLLISION_DELAY));
101 if(PJON_IO_READ(_input_pin)) {
109 static uint8_t get_max_attempts()
111 return OS_MAX_ATTEMPTS;
117 static uint16_t get_receive_time()
119 return OS_RECEIVE_TIME;
125 void handle_collision()
127 PJON_DELAY_MICROSECONDS(PJON_RANDOM(OS_COLLISION_DELAY));
135 uint8_t byte_value = 0B00000000;
136 for(uint8_t i = 0; i < 8; i++) {
137 uint32_t time = PJON_MICROS();
139 while((uint32_t)(PJON_MICROS() - time) < OS_BIT_WIDTH) {
140 value = ((value * 0.999) + (PJON_IO_READ(_input_pin) * 0.001));
142 byte_value += (value > 0.5) << i;
150 uint16_t receive_byte()
163 uint16_t receive_response()
165 if(_output_pin != _input_pin && _output_pin != OS_NOT_ASSIGNED) {
166 PJON_IO_WRITE(_output_pin,
LOW);
168 uint16_t response = OS_FAIL;
169 uint32_t time = PJON_MICROS();
171 response == OS_FAIL &&
172 (uint32_t)(PJON_MICROS() - OS_TIMEOUT) <= time
174 PJON_IO_WRITE(_input_pin,
LOW);
176 response = receive_byte();
178 if(response != PJON_ACK) {
179 PJON_IO_MODE(_output_pin, OUTPUT);
180 PJON_IO_WRITE(_output_pin,
HIGH);
181 PJON_DELAY_MICROSECONDS(OS_BIT_SPACER / 2);
182 PJON_IO_PULL_DOWN(_output_pin);
191 uint16_t receive_frame(uint8_t *
data, uint16_t max_length)
194 if(max_length == PJON_PACKET_MAX_LENGTH) {
195 uint32_t time = PJON_MICROS();
197 if(!sync() || !sync() || !sync()) {
201 (uint32_t)(PJON_MICROS() - time) <
202 ((OS_BIT_WIDTH * 3) + (OS_BIT_SPACER * 3))
207 result = receive_byte();
208 if(result == OS_FAIL) {
231 void send_byte(uint8_t b)
234 for(uint8_t mask = 0x01; mask; mask <<= 1) {
235 PJON_IO_WRITE(_output_pin, b & mask);
236 PJON_DELAY_MICROSECONDS(OS_BIT_WIDTH);
245 void send_response(uint8_t response)
247 PJON_IO_PULL_DOWN(_input_pin);
248 PJON_IO_MODE(_output_pin, OUTPUT);
251 PJON_IO_PULL_DOWN(_output_pin);
256 void send_frame(uint8_t *
data, uint16_t length)
258 PJON_IO_MODE(_output_pin, OUTPUT);
260 for(uint16_t b = 0; b < length; b++) {
263 PJON_IO_PULL_DOWN(_output_pin);
269 PJON_IO_PULL_DOWN(_input_pin);
270 if(_output_pin != OS_NOT_ASSIGNED && _output_pin != _input_pin) {
271 PJON_IO_PULL_DOWN(_output_pin);
274 uint32_t time = PJON_MICROS();
278 ((uint32_t)(PJON_MICROS() - time) < OS_BIT_SPACER) &&
279 PJON_IO_READ(_input_pin)
281 value = (value * 0.999) + (PJON_IO_READ(_input_pin) * 0.001);
286 if(PJON_MICROS() - time < OS_ACCEPTANCE) {
291 time = PJON_MICROS();
292 while((uint32_t)(PJON_MICROS() - time) < OS_BIT_WIDTH) {
293 value = (value * 0.999) + (PJON_IO_READ(_input_pin) * 0.001);
305 void pulse(uint8_t n)
308 PJON_IO_WRITE(_output_pin,
HIGH);
309 PJON_DELAY_MICROSECONDS(OS_BIT_SPACER);
310 PJON_IO_WRITE(_output_pin,
LOW);
311 PJON_DELAY_MICROSECONDS(OS_BIT_WIDTH);
317 void set_pin(uint8_t pin)
319 PJON_IO_PULL_DOWN(pin);
328 uint8_t input_pin = OS_NOT_ASSIGNED,
329 uint8_t output_pin = OS_NOT_ASSIGNED
332 PJON_IO_PULL_DOWN(input_pin);
333 PJON_IO_PULL_DOWN(output_pin);
334 _input_pin = input_pin;
335 _output_pin = output_pin;