comparison controller.ino @ 4:e2ec8dc3a6d8 default tip

Always empty the receive buffer
author Radomir Dopieralski <sheep-devel@sheep.art.pl>
date Thu, 15 Sep 2016 00:03:04 +0200
parents 9f41bcaa3cee
children
comparison
equal deleted inserted replaced
3:ecf4808f2b29 4:e2ec8dc3a6d8
2 #include "pwm.h" 2 #include "pwm.h"
3 3
4 4
5 const uint8_t I2C_ADDRESS = 0x09; 5 const uint8_t I2C_ADDRESS = 0x09;
6 const uint8_t MAX_PINS = 18; 6 const uint8_t MAX_PINS = 18;
7 const uint32_t PWM_PERIOD = 20000L; 7 const uint32_t PWM_PERIOD = 20000L; // 50Hz
8 8
9 volatile uint8_t *ports[MAX_PINS] = { 9 volatile uint8_t *ports[MAX_PINS] = {
10 &PORTD, &PORTD, 10 &PORTD, &PORTD,
11 &PORTD, &PORTD, &PORTD, &PORTD, &PORTD, &PORTD, &PORTB, &PORTB, 11 &PORTD, &PORTD, &PORTD, &PORTD, &PORTD, &PORTD, &PORTB, &PORTB,
12 &PORTB, &PORTB, &PORTB, &PORTB, &PORTC, &PORTC, &PORTC, &PORTC, 12 &PORTB, &PORTB, &PORTB, &PORTB, &PORTC, &PORTC, &PORTC, &PORTC,
23 union { 23 union {
24 byte bytes[2]; 24 byte bytes[2];
25 uint16_t value; 25 uint16_t value;
26 } bytes2int; 26 } bytes2int;
27 uint8_t servo = Wire.read(); 27 uint8_t servo = Wire.read();
28 while (Wire.available() >= 2) { 28 while ((Wire.available() >= 2) && (servo < MAX_PINS)) {
29 if (servo >= MAX_PINS) {
30 return;
31 }
32 bytes2int.bytes[0] = Wire.read(); 29 bytes2int.bytes[0] = Wire.read();
33 bytes2int.bytes[1] = Wire.read(); 30 bytes2int.bytes[1] = Wire.read();
34 pwm_set_phase_batch(servo, bytes2int.value); 31 pwm_set_phase_batch(servo, bytes2int.value);
35 ++servo; 32 ++servo;
33 }
34 while (Wire.available()) {
35 Wire.read();
36 } 36 }
37 changed = true; 37 changed = true;
38 } 38 }
39 39
40 40