diff --git a/encoder.c b/encoder.c index 9ba66f6..0179632 100644 --- a/encoder.c +++ b/encoder.c @@ -9,18 +9,41 @@ #include "encoder.h" #include "systick.h" +/* Use the big slit on the rotary encoder to determine an absolute position + (modulo a full rotation) if defined */ #define USE_CORRECTION +/* If defined, use this value as value for the lowest detected position of the + big slit. If undefined, the value moves so the lowest seen value is 0. */ #define FIXED_OFFSET 20 +/* Show lots of debug output */ // #define DEBUG_ENCODER_TICKS +/* Number of encoder changes to use for speed averaging */ #define SPEED_AVG_NUM 4 +/* Number of slits in a full rotation */ +#define MARKS 96 +/* Port for both pins on the rotary encoder */ +#define PIN_PORT GPIOC +/* The corresponding RCC */ +#define PIN_RCC RCC_GPIOC +/* GPIO pin that rises first when opening the door */ +#define PIN_OPEN GPIO1 +/* Its interrupt */ +#define PIN_OPEN_IRQ NVIC_EXTI1_IRQ +/* GPIO pin that rises first when closing the door */ +#define PIN_CLOSE GPIO2 +/* Its interrupt */ +#define PIN_CLOSE_IRQ NVIC_EXTI2_IRQ + #ifdef DEBUG_ENCODER_TICKS #define LOG_ENCODER_DEBUG_TICKS(...) printf(__VA_ARGS__) #else #define LOG_ENCODER_DEBUG_TICKS(...) #endif +/* Position */ static volatile int pos = 0; +/* Offset/value of the lowest seen positon of the long slit */ static int offset = #ifdef FIXED_OFFSET FIXED_OFFSET @@ -28,24 +51,23 @@ static int offset = -1 #endif ; +/* Rotation speed measured at the last position change */ static volatile int speed = 0; -static void check(void); +/* Time of the last position change */ static uint32_t last_tick = 0; +/* Timestamp/position pairs for the last n positions (only measured when neither + pin is high). Last value contains just the position, without taking the long + slit into account, to prevent that from affecting the speed. */ static struct tick_pos { uint32_t tick; int pos; } speed_ticks[SPEED_AVG_NUM + 1] = {0}; +/* Times for when one/both/the other pin is high, for detecting the long + slit. */ static uint32_t ticks[3] = {0}; -#define MARKS 96 -#define PIN_OPEN GPIO1 -#define PIN_OPEN_IRQ NVIC_EXTI1_IRQ -#define PIN_CLOSE GPIO2 -#define PIN_CLOSE_IRQ NVIC_EXTI2_IRQ - -#define PIN_PORT GPIOC -#define PIN_RCC RCC_GPIOC +static void check(void); void encoder_setup(void) { rcc_periph_clock_enable(PIN_RCC);