changeset 7:6a2ed0cd19a3 draft

Some cleanup
author Radomir Dopieralski <sheep-devel@sheep.art.pl>
date Mon, 19 Dec 2016 10:36:00 +0100
parents a9ed66598e2a
children 94786ac91ee3
files firmware/main.c
diffstat 1 files changed, 19 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/firmware/main.c	Mon Dec 19 09:26:28 2016 +0100
+++ b/firmware/main.c	Mon Dec 19 10:36:00 2016 +0100
@@ -7,12 +7,11 @@
 #define I2C_BUFFER_SIZE 32
 
 
-unsigned char i2c_buffer[I2C_BUFFER_SIZE];
-unsigned char i2c_cursor = 0;
-unsigned char servo;
-union {
-    unsigned char bytes[2];
-    unsigned int value;
+static uint8_t i2c_buffer[I2C_BUFFER_SIZE];
+static uint8_t i2c_cursor;
+static union {
+    uint8_t bytes[2];
+    uint16_t value;
 } bytes2int;
 
 
@@ -24,7 +23,7 @@
 volatile static uint8_t servo_mask_b[MAX_SERVO];
 volatile static uint8_t servo_mask_c[MAX_SERVO];
 volatile static uint8_t servo_mask_d[MAX_SERVO];
-volatile static uint8_t event;
+volatile static uint8_t servo_event;
 
 // Remap servo numbers to pins.
 // 0 = PB0  1 = PB1  2 = PB2  3 = PB3  4 = PB4  5 = PB5  6 = PB6 7 = PB7
@@ -35,7 +34,7 @@
 };
 
 ISR(TIMER1_COMPA_vect) {
-    event = 0;
+    servo_event = 0;
     PORTB = 0b11111111;
     PORTC = 0b00001111; // leave out RESET, SDA, SCL, PC7
     PORTD = 0b11111111;
@@ -45,11 +44,11 @@
 
 
 ISR(TIMER1_COMPB_vect) {
-    PORTB &= servo_mask_b[event];
-    PORTC &= servo_mask_c[event];
-    PORTD &= servo_mask_d[event];
-    event += 1;
-    OCR1B = servo_delay[event];
+    PORTB &= servo_mask_b[servo_event];
+    PORTC &= servo_mask_c[servo_event];
+    PORTD &= servo_mask_d[servo_event];
+    servo_event += 1;
+    OCR1B = servo_delay[servo_event];
 }
 
 
@@ -101,7 +100,7 @@
     DDRB = 0xff;
     DDRC = 0x0f;
     DDRD = 0xff;
-    for (int i = 0; i < MAX_SERVO; ++i) {
+    for (uint8_t i = 0; i < MAX_SERVO; ++i) {
         servo_pulse[i] = 0;
         servo_delay[i] = 0xffff;
     }
@@ -126,15 +125,15 @@
                 break;
             case 0xa0: // stop or repeated start received
                 if (i2c_cursor > 0) {
-                    servo = i2c_buffer[0];
-                    for (unsigned char i = 1; i < i2c_cursor - 1; i += 2) {
-                        if (servo >= 20) {
-                            servo = 0;
+                    uint8_t i2c_servo = i2c_buffer[0];
+                    for (uint8_t i = 1; i < i2c_cursor - 1; i += 2) {
+                        if (i2c_servo >= 20) {
+                            i2c_servo = 0;
                         }
                         bytes2int.bytes[0] = i2c_buffer[i];
                         bytes2int.bytes[1] = i2c_buffer[i + 1];
-                        servo_pulse[servo_map[servo]] = bytes2int.value;
-                        servo += 1;
+                        servo_pulse[servo_map[i2c_servo]] = bytes2int.value;
+                        i2c_servo += 1;
                     }
                     servo_update();
                 }