Fix rotary encoder relative change function

This commit is contained in:
Mario Hüttel 2020-02-16 18:17:10 +01:00
parent 85ecc3064a
commit 532262f670
4 changed files with 39 additions and 5 deletions

View File

@ -34,4 +34,6 @@
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
#define ABS(a) ((a) < 0 ? (-1*(a)) : (a))
#endif /* __HELPER_MACROS_H__ */

View File

@ -35,7 +35,7 @@ void rotary_encoder_setup(void);
uint32_t rotary_encoder_get_abs_val(void);
int32_t rotary_encoder_get_chage_val(void);
int32_t rotary_encoder_get_change_val(void);
void rotary_encoder_stop(void);

View File

@ -21,6 +21,7 @@
#include <reflow-controller/rotary-encoder.h>
#include <stm-periph/clock-enable-manager.h>
#include <stm-periph/stm32-gpio-macros.h>
#include <helper-macros/helper-macros.h>
static inline void rotary_encoder_setup_pins(void)
{
@ -51,7 +52,7 @@ uint32_t rotary_encoder_get_abs_val(void)
return (uint32_t)ROTARY_ENCODER_TIMER->CNT;
}
int32_t rotary_encoder_get_chage_val(void)
int32_t rotary_encoder_get_change_val(void)
{
static uint32_t last_val = 0;
uint32_t val;
@ -61,10 +62,16 @@ int32_t rotary_encoder_get_chage_val(void)
diff = val - last_val;
if (diff > 0xEFFF) {
diff = 0xFFFF - diff;
if (val > last_val) {
if (diff > (0xFFFF/2))
diff = -(last_val + 0x10000-val);
} else {
if (ABS(diff) > (0xFFFF/2))
diff = 0x10000 - last_val + val;
}
last_val = val;
return diff;
}

View File

@ -29,6 +29,7 @@
#include <stm-periph/unique-id.h>
#include <reflow-controller/calibration.h>
#include <reflow-controller/temp-converter.h>
#include <reflow-controller/rotary-encoder.h>
#ifndef GIT_VER
#define GIT_VER "VERSION NOT SET"
@ -201,6 +202,22 @@ static shellmatta_retCode_t shell_cmd_cal(const shellmatta_handle_t handle,
calibration_sequence_shell_cmd(handle);
return SHELLMATTA_OK;
}
static shellmatta_retCode_t shell_cmd_rot(const shellmatta_handle_t handle,
const char *arguments,
uint32_t length)
{
(void)arguments;
(void)length;
uint32_t rot_val;
int32_t delta;
rot_val = rotary_encoder_get_abs_val();
delta = rotary_encoder_get_change_val();
shellmatta_printf(handle, "Rotary encoder value: %u, delta: %d\r\n", rot_val, delta);
return SHELLMATTA_OK;
}
//typedef struct shellmatta_cmd
//{
// char *cmd; /**< command name */
@ -211,7 +228,7 @@ static shellmatta_retCode_t shell_cmd_cal(const shellmatta_handle_t handle,
// struct shellmatta_cmd *next; /**< pointer to next command or NULL */
//} shellmatta_cmd_t;
static shellmatta_cmd_t cmd[7] = {
static shellmatta_cmd_t cmd[8] = {
{
.cmd = "version",
.cmdAlias = "ver",
@ -266,6 +283,14 @@ static shellmatta_cmd_t cmd[7] = {
.helpText = "Calibrate resistance measurement",
.usageText = "",
.cmdFct = shell_cmd_cal,
.next = &cmd[7],
},
{
.cmd = "rotary-encoder",
.cmdAlias = "rot",
.helpText = "Get current rotary encoder value",
.usageText = "",
.cmdFct = shell_cmd_rot,
.next = NULL,
}
};