Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BENB potentiometer centering #128

Merged
merged 10 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions grid_common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.10)
idf_component_register(
SRCS
"grid_protocol.c"
"grid_cal.c"
"grid_ain.c"
"grid_led.c"
"grid_sys.c"
Expand Down
27 changes: 27 additions & 0 deletions grid_common/grid_cal.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* grid_cal.c
*
* Created: 25/11/2024 3:01:37 PM
* Author : BENB
*/

#include "grid_cal.h"

struct grid_cal_model grid_cal_state;

uint8_t grid_cal_init(struct grid_cal_model* cal, uint8_t length, uint8_t resolution) {

cal->length = length;

cal->value = malloc(cal->length * sizeof(uint16_t));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cast result of malloc to uint16_t* for CPP compatibility

cal->center = malloc(cal->length * sizeof(uint16_t));

const uint16_t half_value = (1 << resolution) >> 1;

for (uint8_t i = 0; i < length; ++i) {
cal->value[i] = half_value;
cal->center[i] = half_value;
}

return 0;
}
22 changes: 22 additions & 0 deletions grid_common/grid_cal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#ifndef GRID_CAL_H_INCLUDED
#define GRID_CAL_H_INCLUDED

// only for uint definitions
#include <stdint.h>
// only for malloc
#include <stdlib.h>

struct grid_cal_model {

uint8_t length;
uint16_t* value;
uint16_t* center;
};

extern struct grid_cal_model grid_cal_state;

uint8_t grid_cal_init(struct grid_cal_model* cal, uint8_t length, uint8_t resolution);

#endif /* GRID_CAL_H_INCLUDED */
83 changes: 83 additions & 0 deletions grid_common/grid_lua_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -1590,6 +1590,87 @@
return 1;
}

/*static*/ int l_grid_potmeter_calibration_get(lua_State* L) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ Getting worse: Overall Code Complexity
The mean cyclomatic complexity increases from 5.06 to 5.20, threshold = 4


int nargs = lua_gettop(L);

if (nargs != 0) {
// error
strcat(grid_lua_state.stde, "#invalidParams");
return 0;
}

lua_newtable(L);

uint8_t element_count = grid_ui_state.element_list_length;

uint8_t element_idx = 0;
for (uint8_t i = 0; i < element_count; ++i) {

struct grid_ui_element* ele = grid_ui_element_find(&grid_ui_state, i);

if (ele->type == GRID_PARAMETER_ELEMENT_POTMETER) {

int32_t value = grid_cal_state.value[element_idx];

lua_pushinteger(L, i + 1);
lua_pushinteger(L, value);
lua_settable(L, -3);

++element_idx;
}
}

grid_platform_printf("potmeter_calibration_get()\n");

return 1;
}

/*static*/ int l_grid_potmeter_calibration_set(lua_State* L) {

int nargs = lua_gettop(L);

if (nargs != 1) {
// error
strcat(grid_lua_state.stde, "#invalidParams");
return 0;
}

if (!lua_istable(L, -1)) {
strcat(grid_lua_state.stde, "#invalidParams");
return 0;
}

uint8_t element_count = grid_ui_state.element_list_length;

uint8_t element_idx = 0;
for (uint8_t i = 0; i < element_count; ++i) {

struct grid_ui_element* ele = grid_ui_element_find(&grid_ui_state, i);

if (ele->type == GRID_PARAMETER_ELEMENT_POTMETER) {

lua_pushinteger(L, element_idx + 1);
lua_gettable(L, -2);

if (!lua_isinteger(L, -1)) {
strcat(grid_lua_state.stde, "#invalidParams");
return 0;
}

int32_t value = lua_tointeger(L, -1);
lua_pop(L, 1);
grid_cal_state.center[element_idx] = value;

++element_idx;
}
}

grid_platform_printf("potmeter_calibration_set()\n");

return 0;
}

/*static*/ const struct luaL_Reg grid_lua_api_generic_lib[] = {
{"print", l_my_print},
{"grid_send", l_grid_send},
Expand Down Expand Up @@ -1652,6 +1733,8 @@
{GRID_LUA_FNC_G_IMMEDIATE_SEND_short, GRID_LUA_FNC_G_IMMEDIATE_SEND_fnptr},

{GRID_LUA_FNC_G_ELEMENT_COUNT_short, GRID_LUA_FNC_G_ELEMENT_COUNT_fnptr},
{GRID_LUA_FNC_G_POTMETER_CALIBRATION_GET_short, GRID_LUA_FNC_G_POTMETER_CALIBRATION_GET_fnptr},
{GRID_LUA_FNC_G_POTMETER_CALIBRATION_SET_short, GRID_LUA_FNC_G_POTMETER_CALIBRATION_SET_fnptr},

{"print", l_my_print},

Expand Down
3 changes: 3 additions & 0 deletions grid_common/grid_lua_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ extern void grid_platform_delay_ms(uint32_t delay_milliseconds);

/*static*/ int l_grid_element_count(lua_State* L);

/*static*/ int l_grid_potmeter_calibration_get(lua_State* L);
/*static*/ int l_grid_potmeter_calibration_set(lua_State* L);

extern struct luaL_Reg* grid_lua_api_generic_lib_reference;

#endif /* GRID_LUA_API_H_INCLUDED */
8 changes: 8 additions & 0 deletions grid_common/grid_protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,14 @@
#define GRID_LUA_FNC_G_ELEMENT_COUNT_fnptr l_grid_element_count
#define GRID_LUA_FNC_G_ELEMENT_COUNT_usage "element_count(void) Returns the number of elements on the current module."

#define GRID_LUA_FNC_G_POTMETER_CALIBRATION_GET_short "gpcg"
#define GRID_LUA_FNC_G_POTMETER_CALIBRATION_GET_human "potmeter_calibration_get"
#define GRID_LUA_FNC_G_POTMETER_CALIBRATION_GET_fnptr l_grid_potmeter_calibration_get
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Always add usage explaining string (similar to #define GRID_LUA_FNC_G_ELEMENT_COUNT_usage) for use in editor UX

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will add in a commit after 9d39ceb


#define GRID_LUA_FNC_G_POTMETER_CALIBRATION_SET_short "gpcs"
#define GRID_LUA_FNC_G_POTMETER_CALIBRATION_SET_human "potmeter_calibration_set"
#define GRID_LUA_FNC_G_POTMETER_CALIBRATION_SET_fnptr l_grid_potmeter_calibration_set

#define GRID_LUA_FNC_G_GUI_DRAW_PIXEL_short "ggdp"
#define GRID_LUA_FNC_G_GUI_DRAW_PIXEL_human "gui_draw_pixel"
#define GRID_LUA_FNC_G_GUI_DRAW_PIXEL_fnptr l_grid_gui_draw_pixel
Expand Down
1 change: 1 addition & 0 deletions grid_common/grid_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <string.h>

#include "grid_ain.h"
#include "grid_cal.h"
#include "grid_lua_api.h"
#include "grid_protocol.h"

Expand Down
19 changes: 18 additions & 1 deletion grid_common/grid_ui_potmeter.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,29 @@ void grid_ui_element_potmeter_page_change_cb(struct grid_ui_element* ele, uint8_
template_parameter_list[GRID_LUA_FNC_P_POTMETER_VALUE_index] = next;
}

void grid_ui_potmeter_store_input(uint8_t input_channel, uint64_t* last_real_time, uint16_t value, uint8_t adc_bit_depth) {
double restrict_to_range(double x, double min, double max) {
const double t = x < min ? min : x;
return t > max ? max : t;
}

uint16_t potmeter_centered(uint16_t value, uint16_t center, uint16_t max) {

const double offset = center / (double)max - 0.5;
const double x = value / (double)max;
const double tmp = 2.0 * x - 1.0;
const double result = (x - (1.0 - tmp * tmp) * offset);

return restrict_to_range(result, 0.0, 1.0) * max;
}

void grid_ui_potmeter_store_input(uint8_t input_channel, uint64_t* last_real_time, uint16_t value, uint16_t center, uint8_t adc_bit_depth) {

const uint16_t adc_max_value = (1 << adc_bit_depth) - 1;

int32_t* template_parameter_list = grid_ui_state.element_list[input_channel].template_parameter_list;

value = potmeter_centered(value, center, adc_max_value);

int32_t resolution = template_parameter_list[GRID_LUA_FNC_P_POTMETER_MODE_index];

grid_ain_add_sample(&grid_ain_state, input_channel, value, adc_bit_depth, (uint8_t)resolution);
Expand Down
2 changes: 1 addition & 1 deletion grid_common/grid_ui_potmeter.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void grid_ui_element_potmeter_template_parameter_init(struct grid_ui_template_bu
void grid_ui_element_potmeter_event_clear_cb(struct grid_ui_event* eve);
void grid_ui_element_potmeter_page_change_cb(struct grid_ui_element* ele, uint8_t page_old, uint8_t page_new);

void grid_ui_potmeter_store_input(uint8_t input_channel, uint64_t* last_real_time, uint16_t value, uint8_t adc_bit_depth);
void grid_ui_potmeter_store_input(uint8_t input_channel, uint64_t* last_real_time, uint16_t value, uint16_t center, uint8_t adc_bit_depth);

// ========================= POTMETER =========================== //

Expand Down
1 change: 1 addition & 0 deletions grid_esp/components/grid_common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ idf_component_register(

SRCS
"../../../grid_common/grid_protocol.c"
"../../../grid_common/grid_cal.c"
"../../../grid_common/grid_ain.c"
"../../../grid_common/grid_led.c"
"../../../grid_common/grid_sys.c"
Expand Down
8 changes: 6 additions & 2 deletions grid_esp/components/grid_esp32_adc/grid_esp32_adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ void grid_esp32_adc_stop(struct grid_esp32_adc_model* adc) {}

#include "ulp_riscv_lock.h"

/*
static float restrictToRange(float value) {
if (value < 0.0) {
return 0.0;
Expand All @@ -218,6 +219,7 @@ static uint32_t grid_esp32_adc_cal(uint32_t input) {

return input + parameter_1 * strength;
}
*/

void IRAM_ATTR grid_esp32_adc_convert(void) {

Expand All @@ -232,12 +234,14 @@ void IRAM_ATTR grid_esp32_adc_convert(void) {
struct grid_esp32_adc_result result_0;
result_0.channel = 0;
result_0.mux_state = grid_esp32_adc_mux_get_index(&grid_esp32_adc_state);
result_0.value = grid_esp32_adc_cal(ulp_adc_value_1);
// result_0.value = grid_esp32_adc_cal(ulp_adc_value_1);
result_0.value = ulp_adc_value_1;

struct grid_esp32_adc_result result_1;
result_1.channel = 1;
result_1.mux_state = grid_esp32_adc_mux_get_index(&grid_esp32_adc_state);
result_1.value = grid_esp32_adc_cal(ulp_adc_value_2);
// result_1.value = grid_esp32_adc_cal(ulp_adc_value_2);
result_1.value = ulp_adc_value_2;

xRingbufferSendFromISR(adc->ringbuffer_handle, &result_0, sizeof(struct grid_esp32_adc_result), NULL);
xRingbufferSendFromISR(adc->ringbuffer_handle, &result_1, sizeof(struct grid_esp32_adc_result), NULL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void grid_esp32_module_ef44_task(void* arg) {
adc_result->value = 4095 - adc_result->value;
}

grid_ui_potmeter_store_input(multiplexer_lookup[lookup_index], &potmeter_last_real_time[lookup_index], adc_result->value, 12);
// grid_ui_potmeter_store_input(multiplexer_lookup[lookup_index], &potmeter_last_real_time[lookup_index], adc_result->value, 12);
vRingbufferReturnItem(grid_esp32_adc_state.ringbuffer_handle, adc_result);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void grid_esp32_module_pb44_task(void* arg) {

if (multiplexer_lookup[lookup_index] < 8) {

grid_ui_potmeter_store_input(multiplexer_lookup[lookup_index], &potmeter_last_real_time[lookup_index], result->value, 12);
// grid_ui_potmeter_store_input(multiplexer_lookup[lookup_index], &potmeter_last_real_time[lookup_index], result->value, 12);
} else if (multiplexer_lookup[lookup_index] < 16) {

grid_ui_button_store_input(multiplexer_lookup[lookup_index], &potmeter_last_real_time[lookup_index], result->value, 12);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@

static const char* TAG = "module_pbf4";

#define GRID_MODULE_PBF4_POT_NUM 8

void grid_esp32_module_pbf4_task(void* arg) {

grid_cal_init(&grid_cal_state, GRID_MODULE_PBF4_POT_NUM, 12);
uint64_t potmeter_last_real_time[16] = {0};
static const uint8_t multiplexer_lookup[16] = {2, 0, 3, 1, 6, 4, 7, 5, -1, -1, -1, -1, 10, 8, 11, 9};

Expand Down Expand Up @@ -50,7 +53,9 @@ void grid_esp32_module_pbf4_task(void* arg) {

if (multiplexer_lookup[lookup_index] < 8) {

grid_ui_potmeter_store_input(multiplexer_lookup[lookup_index], &potmeter_last_real_time[lookup_index], result->value, 12);
grid_cal_state.value[multiplexer_lookup[lookup_index]] = result->value;
uint16_t center = grid_cal_state.center[multiplexer_lookup[lookup_index]];
grid_ui_potmeter_store_input(multiplexer_lookup[lookup_index], &potmeter_last_real_time[lookup_index], result->value, center, 12);
} else if (multiplexer_lookup[lookup_index] < 12) {

grid_ui_button_store_input(multiplexer_lookup[lookup_index], &potmeter_last_real_time[lookup_index], result->value, 12);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void grid_esp32_module_po16_task(void* arg) {
result->value = 4095 - result->value;
}

grid_ui_potmeter_store_input(multiplexer_lookup[lookup_index], &potmeter_last_real_time[lookup_index], result->value, 12);
// grid_ui_potmeter_store_input(multiplexer_lookup[lookup_index], &potmeter_last_real_time[lookup_index], result->value, 12);
vRingbufferReturnItem(grid_esp32_adc_state.ringbuffer_handle, result);
}

Expand Down
Loading