Skip to content

Commit effbddf

Browse files
BENB refactor encoder logic
1 parent 5a1a69b commit effbddf

2 files changed

Lines changed: 21 additions & 26 deletions

File tree

‎common/src/c/grid_ui_encoder.c‎

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "grid_ui_encoder.h"
22

33
#include <assert.h>
4+
#include <math.h>
45
#include <stdlib.h>
56
#include <string.h>
67

@@ -192,21 +193,16 @@ int16_t grid_ui_encoder_rotation_delta(uint8_t old_value, uint8_t new_value, uin
192193
return delta;
193194
}
194195

195-
uint8_t grid_ui_encoder_update_trigger(struct grid_ui_element* ele, uint64_t* last_real_time, int16_t delta) {
196+
void grid_ui_encoder_update_trigger(struct grid_ui_element* ele, uint64_t* last_real_time, int16_t delta) {
196197

197198
// limit lastrealtime
198199
uint64_t now = grid_platform_rtc_get_micros();
199200
uint64_t elapsed_us = grid_platform_rtc_get_diff(now, *last_real_time);
200201
elapsed_us = MIN(elapsed_us, GRID_PARAMETER_ELAPSED_LIMIT * MS_TO_US);
201202

202-
if (delta == 0) {
203-
return 0; // did not trigger
204-
}
205-
206-
// positive delta means we wish to move closer to max value, megative delta means we wish to move closer to min value
207-
208203
// update lastrealtime
209204
*last_real_time = now;
205+
210206
int32_t* template_parameter_list = ele->template_parameter_list;
211207
template_parameter_list[GRID_LUA_FNC_E_ENCODER_ELAPSED_index] = elapsed_us / MS_TO_US;
212208

@@ -222,14 +218,15 @@ uint8_t grid_ui_encoder_update_trigger(struct grid_ui_element* ele, uint64_t* la
222218

223219
double elapsed_ms = clampu32(elapsed_us, 1000, 25000) / MS_TO_US;
224220

225-
double minmaxscale = (max - min) / 128.0;
226-
227-
double velocityparam = template_parameter_list[GRID_LUA_FNC_E_ENCODER_VELOCITY_index] / 100.0;
221+
double vel_param = template_parameter_list[GRID_LUA_FNC_E_ENCODER_VELOCITY_index] / 100.0;
222+
double sen_param = template_parameter_list[GRID_LUA_FNC_E_ENCODER_SENSITIVITY_index] / 100.0;
228223

229-
// implement configurable velocity parameters here
230-
double velocityfactor = ((25 * 25 - elapsed_ms * elapsed_ms) / 75.0) * minmaxscale * velocityparam + (1.0 * template_parameter_list[GRID_LUA_FNC_E_ENCODER_SENSITIVITY_index] / 100.0);
224+
double vel_comp = (625.0 - elapsed_ms * elapsed_ms) / 75.0 * vel_param;
225+
double sen_comp = sen_param;
226+
double minmaxscale = (max - min) / 128.0;
227+
double factor = vel_comp * minmaxscale + sen_comp;
231228

232-
int32_t delta_velocity = delta * velocityfactor;
229+
int32_t delta_velocity = delta * factor;
233230

234231
if (delta_velocity == 0) {
235232
return 0; // did not trigger
@@ -272,12 +269,16 @@ uint8_t grid_ui_encoder_update_trigger(struct grid_ui_element* ele, uint64_t* la
272269
struct grid_ui_event* eve = grid_ui_event_find(ele, GRID_PARAMETER_EVENT_ENCODER);
273270

274271
grid_ui_event_state_set(eve, GRID_EVE_STATE_TRIG);
275-
276-
return 1; // did trigger
277272
}
278273

279274
void grid_ui_encoder_store_input(struct grid_ui_encoder_state* state, struct grid_ui_encoder_sample sample) {
280275

276+
state->initial_samples += (state->initial_samples <= GRID_UI_ENCODER_INIT_SAMPLES);
277+
278+
if (state->initial_samples <= GRID_UI_ENCODER_INIT_SAMPLES) {
279+
return;
280+
}
281+
281282
struct grid_ui_element* ele = state->parent;
282283

283284
// Handle button input using embedded button state
@@ -286,23 +287,17 @@ void grid_ui_encoder_store_input(struct grid_ui_encoder_state* state, struct gri
286287
// Reconstruct rotation value from phases
287288
uint8_t new_value = sample.phase_a | (sample.phase_b << 1);
288289

289-
// extract old value from state, rewrite state with new
290+
// Extract old value from state, rewrite state with new
290291
uint8_t old_value = state->last_nibble;
291292
state->last_nibble = new_value;
292293

293-
state->initial_samples += (state->initial_samples <= GRID_UI_ENCODER_INIT_SAMPLES);
294-
295294
if (old_value == new_value) {
296-
// no change since the last time we read the shift register
297295
return;
298296
}
299297

300-
int16_t delta = grid_ui_encoder_rotation_delta(old_value, new_value, state->detent, &state->encoder_last_leave_dir); // delta can be -1, 0 or 1
301-
302-
delta *= state->direction;
298+
int16_t delta = grid_ui_encoder_rotation_delta(old_value, new_value, state->detent, &state->encoder_last_leave_dir);
303299

304-
// Evaluate the results
305-
if (state->initial_samples > GRID_UI_ENCODER_INIT_SAMPLES) {
306-
grid_ui_encoder_update_trigger(ele, &state->encoder_last_real_time, delta);
300+
if (delta) {
301+
grid_ui_encoder_update_trigger(ele, &state->encoder_last_real_time, delta * state->direction);
307302
}
308303
}

‎common/src/c/grid_ui_encoder.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void grid_ui_element_encoder_event_clear_cb(struct grid_ui_event* eve);
4747
void grid_ui_element_encoder_page_change_cb(struct grid_ui_element* ele, uint8_t page_old, uint8_t page_new);
4848

4949
int16_t grid_ui_encoder_rotation_delta(uint8_t old_value, uint8_t new_value, uint8_t detent, int8_t* dir_lock);
50-
uint8_t grid_ui_encoder_update_trigger(struct grid_ui_element* ele, uint64_t* encoder_last_real_time, int16_t delta);
50+
void grid_ui_encoder_update_trigger(struct grid_ui_element* ele, uint64_t* encoder_last_real_time, int16_t delta);
5151

5252
static inline struct grid_ui_encoder_state* grid_ui_encoder_get_state(struct grid_ui_element* ele) { return (struct grid_ui_encoder_state*)ele->primary_state; }
5353

0 commit comments

Comments
 (0)