Comments
This commit is contained in:
parent
6f81461b2c
commit
70ea0b1f14
40
encoder.c
40
encoder.c
|
@ -9,18 +9,41 @@
|
||||||
#include "encoder.h"
|
#include "encoder.h"
|
||||||
#include "systick.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
|
#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
|
#define FIXED_OFFSET 20
|
||||||
|
/* Show lots of debug output */
|
||||||
// #define DEBUG_ENCODER_TICKS
|
// #define DEBUG_ENCODER_TICKS
|
||||||
|
/* Number of encoder changes to use for speed averaging */
|
||||||
#define SPEED_AVG_NUM 4
|
#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
|
#ifdef DEBUG_ENCODER_TICKS
|
||||||
#define LOG_ENCODER_DEBUG_TICKS(...) printf(__VA_ARGS__)
|
#define LOG_ENCODER_DEBUG_TICKS(...) printf(__VA_ARGS__)
|
||||||
#else
|
#else
|
||||||
#define LOG_ENCODER_DEBUG_TICKS(...)
|
#define LOG_ENCODER_DEBUG_TICKS(...)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* Position */
|
||||||
static volatile int pos = 0;
|
static volatile int pos = 0;
|
||||||
|
/* Offset/value of the lowest seen positon of the long slit */
|
||||||
static int offset =
|
static int offset =
|
||||||
#ifdef FIXED_OFFSET
|
#ifdef FIXED_OFFSET
|
||||||
FIXED_OFFSET
|
FIXED_OFFSET
|
||||||
|
@ -28,24 +51,23 @@ static int offset =
|
||||||
-1
|
-1
|
||||||
#endif
|
#endif
|
||||||
;
|
;
|
||||||
|
/* Rotation speed measured at the last position change */
|
||||||
static volatile int speed = 0;
|
static volatile int speed = 0;
|
||||||
static void check(void);
|
|
||||||
|
|
||||||
|
/* Time of the last position change */
|
||||||
static uint32_t last_tick = 0;
|
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 {
|
static struct tick_pos {
|
||||||
uint32_t tick;
|
uint32_t tick;
|
||||||
int pos;
|
int pos;
|
||||||
} speed_ticks[SPEED_AVG_NUM + 1] = {0};
|
} 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};
|
static uint32_t ticks[3] = {0};
|
||||||
|
|
||||||
#define MARKS 96
|
static void check(void);
|
||||||
#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
|
|
||||||
|
|
||||||
void encoder_setup(void) {
|
void encoder_setup(void) {
|
||||||
rcc_periph_clock_enable(PIN_RCC);
|
rcc_periph_clock_enable(PIN_RCC);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user