diff --git a/stm-firmware/include/helper-macros/helper-macros.h b/stm-firmware/include/helper-macros/helper-macros.h index d359e03..5ef4c12 100644 --- a/stm-firmware/include/helper-macros/helper-macros.h +++ b/stm-firmware/include/helper-macros/helper-macros.h @@ -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__ */ diff --git a/stm-firmware/include/reflow-controller/rotary-encoder.h b/stm-firmware/include/reflow-controller/rotary-encoder.h index b436ea1..a82e8e8 100644 --- a/stm-firmware/include/reflow-controller/rotary-encoder.h +++ b/stm-firmware/include/reflow-controller/rotary-encoder.h @@ -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); diff --git a/stm-firmware/rotary-encoder.c b/stm-firmware/rotary-encoder.c index 437ca89..6b7c1a4 100644 --- a/stm-firmware/rotary-encoder.c +++ b/stm-firmware/rotary-encoder.c @@ -21,6 +21,7 @@ #include #include #include +#include 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; } diff --git a/stm-firmware/shell.c b/stm-firmware/shell.c index 5502c66..ccbe1d0 100644 --- a/stm-firmware/shell.c +++ b/stm-firmware/shell.c @@ -29,7 +29,9 @@ #include #include #include + #include +#include #ifndef GIT_VER #define GIT_VER "VERSION NOT SET" @@ -46,8 +48,8 @@ static shellmatta_retCode_t write_shell_callback(const char *data, uint32_t len) } static shellmatta_retCode_t shell_cmd_ver(const shellmatta_handle_t handle, - const char *arguments, - uint32_t length) + const char *arguments, + uint32_t length) { (void)arguments; (void)length; @@ -58,22 +60,22 @@ static shellmatta_retCode_t shell_cmd_ver(const shellmatta_handle_t handle, unique_id_get(&high_id, &mid_id, &low_id); shellmatta_printf(handle, "Reflow Oven Controller Firmware " xstr(GIT_VER) "\r\n" - "Compiled: " __DATE__ " at " __TIME__ "\r\n" - "Serial: %08X-%08X-%08X", high_id, mid_id, low_id); + "Compiled: " __DATE__ " at " __TIME__ "\r\n" + "Serial: %08X-%08X-%08X", high_id, mid_id, low_id); return SHELLMATTA_OK; } static shellmatta_retCode_t shell_cmd_digio_get(const shellmatta_handle_t handle, - const char *arguments, - uint32_t length) + const char *arguments, + uint32_t length) { (void)arguments; (void)length; shellmatta_printf(handle, - "DIGIO0 DIGIO1 DIGIO2 DIGIO3 LED0 LED1 LS\r\n" - "%d %d %d %d %d %d %d\r\n", + "DIGIO0 DIGIO1 DIGIO2 DIGIO3 LED0 LED1 LS\r\n" + "%d %d %d %d %d %d %d\r\n", digio_get(0), digio_get(1), digio_get(2), digio_get(3), led_get(0), led_get(1), loudspeaker_get()); @@ -81,8 +83,8 @@ static shellmatta_retCode_t shell_cmd_digio_get(const shellmatta_handle_t hand } static shellmatta_retCode_t shell_cmd_digio_set(const shellmatta_handle_t handle, - const char *arguments, - uint32_t length) + const char *arguments, + uint32_t length) { (void)length; (void)handle; @@ -193,8 +195,8 @@ static shellmatta_retCode_t shell_cmd_uptime(const shellmatta_handle_t handle, } static shellmatta_retCode_t shell_cmd_cal(const shellmatta_handle_t handle, - const char *arguments, - uint32_t length) + const char *arguments, + uint32_t length) { (void)arguments; (void)length; @@ -204,19 +206,36 @@ static shellmatta_retCode_t shell_cmd_cal(const shellmatta_handle_t handle, } static shellmatta_retCode_t shell_get_sp(const shellmatta_handle_t handle, - const char *arguments, - uint32_t length) + const char *arguments, + uint32_t length) { (void)arguments; (void)length; shellmatta_printf(handle, - "Stack pointer: %p\r\n" - "Stack usage: 0x%x bytes\r\n" - "Lim to heap: 0x%x bytes\r\n", - read_stack_pointer(), - stack_check_get_usage(), - stack_check_get_free()); + "Stack pointer: %p\r\n" + "Stack usage: 0x%x bytes\r\n" + "Lim to heap: 0x%x bytes\r\n", + read_stack_pointer(), + stack_check_get_usage(), + stack_check_get_free()); + 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; } @@ -230,7 +249,7 @@ static shellmatta_retCode_t shell_get_sp(const shellmatta_handle_t handle, // struct shellmatta_cmd *next; /**< pointer to next command or NULL */ //} shellmatta_cmd_t; -static shellmatta_cmd_t cmd[8] = { +static shellmatta_cmd_t cmd[9] = { { .cmd = "version", .cmdAlias = "ver", @@ -293,6 +312,14 @@ static shellmatta_cmd_t cmd[8] = { .helpText = "Get the stack pointer", .usageText = "", .cmdFct = shell_get_sp, + .next = &cmd[8], + }, + { + .cmd = "rotary-encoder", + .cmdAlias = "rot", + .helpText = "Get current rotary encoder value", + .usageText = "", + .cmdFct = shell_cmd_rot, .next = NULL, } }; @@ -303,7 +330,7 @@ shellmatta_handle_t shell_init(void) shellmatta_retCode_t ret; ret = shellmatta_doInit(&shell, &handle, shell_buffer, sizeof(shell_buffer), history_buffer, sizeof(history_buffer), - "\e[1;32mEnter command:\e[m\r\n", cmd, write_shell_callback); + "\e[1;32mEnter command:\e[m\r\n", cmd, write_shell_callback); if (ret != SHELLMATTA_OK) handle = NULL; @@ -313,10 +340,10 @@ shellmatta_handle_t shell_init(void) void shell_handle_input(shellmatta_handle_t shell, const char *data, size_t len) { - if (!shell) - return; + if (!shell) + return; - shellmatta_processData(shell, (char *)data, (uint32_t)len); + shellmatta_processData(shell, (char *)data, (uint32_t)len); } void shell_print_string(shellmatta_handle_t shell, const char *string)