-
Notifications
You must be signed in to change notification settings - Fork 7
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
Changes from 2 commits
12859a9
ee51637
a366623
d6609e1
306d864
bfcab66
9d39ceb
b5a1b57
c7eaf7b
4803634
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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)); | ||
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; | ||
} |
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 */ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1590,6 +1590,87 @@ | |
return 1; | ||
} | ||
|
||
/*static*/ int l_grid_potmeter_calibration_get(lua_State* L) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ℹ Getting worse: Overall Code Complexity |
||
|
||
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}, | ||
|
@@ -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}, | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment.
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