diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json deleted file mode 100644 index 31ea4c72e..000000000 --- a/.vscode/c_cpp_properties.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "configurations": [ - { - "name": "Linux", - "includePath": [ - "${workspaceFolder}/**" - ], - "defines": [], - "compilerPath": "/usr/bin/gcc", - "cStandard": "gnu17", - "cppStandard": "gnu++17", - "intelliSenseMode": "linux-gcc-x64" - } - ], - "version": 4 -} diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index d364a2ffd..000000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,241 +0,0 @@ -{ - "files.associations": { - "grid_esp32_port.h": "c", - "grid_port.h": "c", - "grid_sys.h": "c", - "semphr.h": "c", - "task.h": "c", - "ledc.h": "c", - "esp_timer.h": "c", - "cstring": "c", - "string.h": "c", - "complex": "c", - "string_view": "c", - "regex": "c", - "grid_esp32_module_pbf4.h": "c", - "grid_esp32_module_bu16.h": "c", - "grid_esp32_module_en16.h": "c", - "grid_esp32_module_ef44.h": "c", - "functional": "c", - "esp_rom_gpio.h": "c", - "grid_esp32_usb.h": "c", - "grid_esp32_nvm.h": "c", - "grid_esp32_swd.h": "c", - "grid_led.h": "c", - "adc_continuous.h": "c", - "grid_esp32_adc.h": "c", - "adc_oneshot.h": "c", - "grid_esp32_pins.h": "c", - "array": "c", - "string": "c", - "grid_msg.h": "c", - "grid_usb.h": "c", - "spi_master.h": "c", - "freertos.h": "c", - "grid_esp32_encoder.h": "c", - "grid_ain.h": "c", - "stdlib.h": "c", - "gpio.h": "c", - "grid_esp32.h": "c", - "cpu_hal.h": "c", - "algorithm": "c", - "strstream": "c", - "*.tcc": "c", - "bitset": "c", - "cassert": "c", - "cctype": "c", - "cerrno": "c", - "cfloat": "c", - "chrono": "c", - "climits": "c", - "clocale": "c", - "cmath": "c", - "cstdarg": "c", - "cstddef": "c", - "cstdio": "c", - "cstdlib": "c", - "ctime": "c", - "cwchar": "c", - "cwctype": "c", - "deque": "c", - "memory": "c", - "iterator": "c", - "numeric": "c", - "random": "c", - "fstream": "c", - "iomanip": "c", - "iosfwd": "c", - "iostream": "c", - "istream": "c", - "limits": "c", - "list": "c", - "map": "c", - "new": "c", - "ostream": "c", - "queue": "c", - "ratio": "c", - "set": "c", - "sstream": "c", - "stack": "c", - "streambuf": "c", - "tuple": "c", - "type_traits": "c", - "typeinfo": "c", - "utility": "c", - "vector": "c", - "version": "c", - "grid_protocol.h": "c", - "*.inc": "c", - "grid_buf.h": "c", - "sdkconfig.h": "c", - "system_error": "c", - "ulp_riscv_print.h": "c", - "ulp_main.h": "c", - "ulp_riscv_uart_ulp_core.h": "c", - "ulp_riscv_adc_ulp_core.h": "c", - "esp_sleep_internal.h": "c", - "mutex": "c", - "tinyusb.h": "c", - "compare": "c", - "variant": "c", - "grid_esp32_module_po16.h": "c", - "atmel_start_pins.h": "c", - "optional": "c", - "grid_esp32_led.h": "c", - "grid_decode.h": "c", - "esp_check.h": "c", - "lualib.h": "c", - "lua.h": "c", - "led_strip_encoder.h": "c", - "grid_d51_module.h": "c", - "grid_module_d51_pbf4.h": "c", - "grid_module.h": "c", - "stdint.h": "c", - "uart.h": "c", - "esp_app_trace_util.h": "c", - "esp_app_trace_port.h": "c", - "initializer_list": "c", - "ets_sys.h": "c", - "valarray": "c", - "pico_firmware.h": "c", - "esp_sysview_trace.h": "c", - "gpio_ll.h": "c", - "ulp_grid_esp32_adc.h": "c", - "spi_flash_mmap.h": "c", - "grid_esp32_module_pb44.h": "c", - "grid_esp32_module_tek2.h": "c", - "grid_esp32_trace.h": "c", - "scandir.h": "c", - "dirent.h": "c", - "esp_littlefs.h": "c", - "stdio.h": "c", - "types.h": "c", - "dma.h": "c", - "*.ipp": "c", - "grid_pico_platform.h": "c", - "grid_pico_spi.h": "c", - "stddef.h": "c", - "grid_esp32_platform.h": "c", - "grid_esp32_lcd.h": "c", - "grid_gui.h": "c", - "grid_font.h": "c", - "grid_lua_api.h": "c", - "lauxlib.h": "c", - "grid_lua_api_gui.h": "c", - "grid_d51.h": "c", - "grid_ui_encoder.h": "c", - "grid_ui_endless.h": "c", - "bit": "c", - "grid_ui_system.h": "c", - "grid_esp32_module_tek1.h": "c", - "grid_ui_lcd.h": "c", - "semaphore": "c", - "grid_ui_button.h": "c", - "blit32.h": "c" - }, - "files.exclude": { - ".archive": true, - "Documentation": true, - "LICENSE": true, - "diagram.svg": true, - "grid_make/CMSIS": true, - "grid_make/armcc": true, - "grid_make/hal": true, - "grid_make/hpl": true, - "grid_make/hri": true, - "grid_make/samd51a": true, - }, - "actionButtons": { - "defaultColor": "#ff0034", - // Can also use string color names. - "loadNpmCommands": false, - // Disables automatic generation of actions for npm commands. - "reloadButton": "♻️", - // Custom reload button text or icon (default ↻). null value enables automatic reload on configuration change - "commands": [ - { - "name": "Clean", - "color": "white", - "singleInstance": true, - "command": "git clean -dxf", - }, - { - "name": "pre-commit formatting", - "color": "white", - "singleInstance": true, - "command": "pre-commit run --show-diff-on-failure --color=always --all-files", - }, - { - "name": "Fuzzer Docker", - "color": "white", - "singleInstance": true, - "command": "podman run --privileged -it -v $PWD:/project -w /project mozillasecurity/libfuzzer bash", - // make nightly - }, - { - "name": "D51 Docker", - "color": "white", - "singleInstance": true, - "command": "podman run --privileged -it -v /dev:/dev -v /media:/media -v $PWD:/project -w /project/grid_make/gcc sukuwc/fw-tools:v1 make nightly", - // make nightly - }, - { - "name": "ESP32 Docker", - "color": "white", - "singleInstance": true, - "command": "podman run --privileged -it -v /dev:/dev -v $PWD:/project -w /project/grid_esp espressif/idf:latest", - // clear && idf.py build && idf.py -p $(ls /dev/ttyACM*) flash - // ./burn_bootloader.sh - // sed -i '10s|.*|#include "../../../../../../../../../project/grid_esp/components/grid_esp32_trace/trace_hooks.h"|' /opt/esp/idf/components/freertos/config/include/freertos/FreeRTOSConfig.h - // clear && idf.py build && esptool.py --chip esp32s3 -p $(ls /dev/ttyACM*) --before=default_reset --after=no_reset write_flash --flash_mode dio --flash_size detect --flash_freq 80m 0x10000 - // build/grid_fw.bin && otatool.py -p $(ls /dev/ttyACM*) switch_ota_partition --slot 0 - }, - { - "name": "ESP32 Monitor", - "color": "white", - "singleInstance": true, - "command": "podman run --privileged -it -v /dev:/dev -v $PWD:/project -w /project/grid_esp espressif/idf:latest", - // idf.py -p $(ls /dev/ttyUSB*) monitor - // idf.py -p $(ls /dev/ttyUSB0) monitor - }, - { - "name": "PICO Terminal", - "color": "white", - "singleInstance": true, - "command": "podman run -it -v $PWD:/pico/project -w /pico/project/grid_pico atoktoto/pico-builder:latest", - // mkdir -p ./build && cd build && cmake .. && make - // apt update && apt install xxd - // git clean -dxf - // clear && make && xxd -i ./main/main.bin > ../../grid_esp/main/pico_firmware.h && sed -i 's/__main_main_bin/pico_firmware/g; s/unsigned/const unsigned/g' ../../grid_esp/main/pico_firmware.h - }, - { - "name": "PICO Convert", - "color": "white", - "singleInstance": true, - "command": - "xxd -i ./grid_pico/build/main/main.bin > ./grid_esp/main/pico_firmware.h && sed -i 's/__grid_pico_build_main_main_bin/pico_firmware/g; s/unsigned/const unsigned/g' ./grid_esp/main/pico_firmware.h", - } - ] - }, - "cmake.sourceDirectory": "/home/suku/Documents/grid-fw/grid_common" -} diff --git a/grid_common/grid_ain.h b/grid_common/grid_ain.h index 833fac921..98dae7071 100644 --- a/grid_common/grid_ain.h +++ b/grid_common/grid_ain.h @@ -4,6 +4,39 @@ #include #include +#define GRID_MUX_UNUSED ((uint8_t)-1) + +#define GRID_AIN_INTERNAL_RESOLUTION 12 + +#define GRID_ADC_MAX 4095 +#define GRID_POTMETER_DEADZONE 128 +#define GRID_POTMETER_CENTER 2192 + +struct grid_adc_result { + uint8_t channel; + uint8_t mux_state; + uint16_t value; +}; + +typedef void (*grid_process_analog_t)(struct grid_adc_result* result); + +#define GRID_ADC_INVERT_COND(value, element_index, invert_bm) ((value) ^ (GRID_ADC_MAX * (((invert_bm) >> (element_index)) & 1))) + +// Find first valid mux position based on bitmask +#define GRID_MUX_FIRST_VALID(index, mask) \ + do { \ + (index) = 0; \ + while (!((mask) & (1 << (index)))) { \ + (index)++; \ + } \ + } while (0) + +// Advance mux index to next valid position based on bitmask +#define GRID_MUX_INCREMENT(index, mask) \ + do { \ + (index) = ((index) + 1) % 8; \ + } while (!((mask) & (1 << (index)))) + struct ain_chan_t { uint16_t capa; diff --git a/grid_common/grid_asc.c b/grid_common/grid_asc.c index 277408f71..d7791a0fd 100644 --- a/grid_common/grid_asc.c +++ b/grid_common/grid_asc.c @@ -12,15 +12,17 @@ void grid_asc_array_set_factors(struct grid_asc* asc, size_t capacity, uint8_t s } } -bool grid_asc_process(struct grid_asc* asc, uint16_t rx, uint16_t* tx) { +bool grid_asc_process(struct grid_asc* asc, uint8_t index, uint16_t rx, uint16_t* tx) { - asc->sum += rx; + struct grid_asc* entry = &asc[index]; - *tx = asc->sum / (asc->count + 1); + entry->sum += rx; - asc->count = (asc->count + 1) % asc->factor; + *tx = entry->sum / (entry->count + 1); - asc->sum *= asc->count != 0; + entry->count = (entry->count + 1) % entry->factor; - return asc->count == 0; + entry->sum *= entry->count != 0; + + return entry->count == 0; } diff --git a/grid_common/grid_asc.h b/grid_common/grid_asc.h index 1488dc3e2..436290689 100644 --- a/grid_common/grid_asc.h +++ b/grid_common/grid_asc.h @@ -12,6 +12,6 @@ struct grid_asc { }; void grid_asc_array_set_factors(struct grid_asc* asc, size_t capacity, uint8_t start, uint8_t length, uint8_t factor); -bool grid_asc_process(struct grid_asc* ads, uint16_t rx, uint16_t* tx); +bool grid_asc_process(struct grid_asc* asc, uint8_t index, uint16_t rx, uint16_t* tx); #endif /* GRID_ASC_H */ diff --git a/grid_common/grid_platform.h b/grid_common/grid_platform.h index bf57d887c..1c2b290f3 100644 --- a/grid_common/grid_platform.h +++ b/grid_common/grid_platform.h @@ -92,4 +92,8 @@ extern void grid_platform_lcd_set_backlight(uint8_t backlight); extern uint8_t grid_platform_get_adc_bit_depth(); +extern void grid_platform_mux_init(uint8_t mux_positions_bm); + +extern void grid_platform_mux_write(uint8_t index); + #endif /* GRID_PLATFORM_H */ diff --git a/grid_common/grid_ui.c b/grid_common/grid_ui.c index ee8847ce3..6298c5430 100644 --- a/grid_common/grid_ui.c +++ b/grid_common/grid_ui.c @@ -141,6 +141,9 @@ struct grid_ui_element* grid_ui_element_model_init(struct grid_ui_model* parent, ele->timer_event_helper = 0; ele->timer_source_is_midi = 0; + ele->primary_state = NULL; + ele->secondary_state = NULL; + return ele; } diff --git a/grid_common/grid_ui.h b/grid_common/grid_ui.h index d5096c9df..77dbfff51 100644 --- a/grid_common/grid_ui.h +++ b/grid_common/grid_ui.h @@ -80,6 +80,9 @@ struct grid_ui_element { uint8_t event_list_length; struct grid_ui_event* event_list; + + void* primary_state; + void* secondary_state; }; enum grid_ui_bulk_status_t { diff --git a/grid_common/grid_ui_button.c b/grid_common/grid_ui_button.c index f066a2ad9..dae88f5fd 100644 --- a/grid_common/grid_ui_button.c +++ b/grid_common/grid_ui_button.c @@ -30,12 +30,25 @@ const char grid_ui_button_init_actionstring[] = GRID_ACTIONSTRING_BUTTON_INIT; const char grid_ui_button_change_actionstring[] = GRID_ACTIONSTRING_BUTTON_BUTTON; const char grid_ui_button_timer_actionstring[] = GRID_ACTIONSTRING_SYSTEM_TIMER; -void grid_ui_button_state_init(struct grid_ui_button_state* state, uint8_t adc_bit_depth, double threshold, double hysteresis) { +void grid_ui_button_state_init(struct grid_ui_model* ui, uint8_t element_index, uint8_t adc_bit_depth, double threshold, double hysteresis) { + assert(ui); + assert(element_index < ui->element_list_length); assert(adc_bit_depth >= 1 && adc_bit_depth <= 16); assert(threshold >= 0. && threshold <= 1.); assert(hysteresis >= 0. && hysteresis <= 1.); + struct grid_ui_element* ele = &ui->element_list[element_index]; + + // Get button state based on element type + struct grid_ui_button_state* state; + if (ele->type == GRID_PARAMETER_ELEMENT_BUTTON) { + state = (struct grid_ui_button_state*)ele->primary_state; + } else { + // Encoder or Endless - button state is in secondary_state + state = (struct grid_ui_button_state*)ele->secondary_state; + } + // The minimum observed bit depth is a fourth of the maximum by default, // or just 1 LSB if the input bit depth is 2 bits or less uint8_t min_range_depth = adc_bit_depth > 2 ? adc_bit_depth - 2 : 0; @@ -173,6 +186,9 @@ void grid_ui_element_button_init(struct grid_ui_element* ele) { ele->type = GRID_PARAMETER_ELEMENT_BUTTON; + ele->primary_state = grid_platform_allocate_volatile(sizeof(struct grid_ui_button_state)); + memset(ele->primary_state, 0, sizeof(struct grid_ui_button_state)); + grid_ui_element_malloc_events(ele, 3); grid_ui_event_init(ele, 0, GRID_PARAMETER_EVENT_INIT, GRID_LUA_FNC_A_INIT_short, grid_ui_button_init_actionstring); // Element Initialization Event @@ -226,9 +242,21 @@ void grid_ui_element_button_page_change_cb(struct grid_ui_element* ele, uint8_t // } } -void grid_ui_button_store_input(struct grid_ui_element* ele, struct grid_ui_button_state* state, uint16_t value, uint8_t adc_bit_depth) { +void grid_ui_button_store_input(struct grid_ui_model* ui, uint8_t element_index, uint16_t value) { - assert(ele); + assert(ui); + assert(element_index < ui->element_list_length); + + struct grid_ui_element* ele = &ui->element_list[element_index]; + + // Get button state based on element type + struct grid_ui_button_state* state; + if (ele->type == GRID_PARAMETER_ELEMENT_BUTTON) { + state = (struct grid_ui_button_state*)ele->primary_state; + } else { + // Encoder or Endless - button state is in secondary_state + state = (struct grid_ui_button_state*)ele->secondary_state; + } int32_t* template_parameter_list = ele->template_parameter_list; diff --git a/grid_common/grid_ui_button.h b/grid_common/grid_ui_button.h index 3a29f1eec..c8301816c 100644 --- a/grid_common/grid_ui_button.h +++ b/grid_common/grid_ui_button.h @@ -25,7 +25,7 @@ struct grid_ui_button_state { uint64_t curr_time; }; -void grid_ui_button_state_init(struct grid_ui_button_state* state, uint8_t adc_bit_depth, double threshold, double hysteresis); +void grid_ui_button_state_init(struct grid_ui_model* ui, uint8_t element_index, uint8_t adc_bit_depth, double threshold, double hysteresis); void grid_ui_element_button_init(struct grid_ui_element* ele); void grid_ui_element_button_template_parameter_init(struct grid_ui_template_buffer* buf); @@ -33,7 +33,7 @@ void grid_ui_element_button_template_parameter_init(struct grid_ui_template_buff void grid_ui_element_button_event_clear_cb(struct grid_ui_event* eve); void grid_ui_element_button_page_change_cb(struct grid_ui_element* ele, uint8_t page_old, uint8_t page_new); -void grid_ui_button_store_input(struct grid_ui_element* ele, struct grid_ui_button_state* state, uint16_t value, uint8_t adc_bit_depth); +void grid_ui_button_store_input(struct grid_ui_model* ui, uint8_t element_index, uint16_t value); // ========================= BUTTON =========================== // diff --git a/grid_common/grid_ui_encoder.c b/grid_common/grid_ui_encoder.c index 9d70692c9..05b6c5836 100644 --- a/grid_common/grid_ui_encoder.c +++ b/grid_common/grid_ui_encoder.c @@ -41,10 +41,15 @@ const char grid_ui_encoder_encoderchange_actionstring[] = GRID_ACTIONSTRING_ENCO const char grid_ui_encoder_buttonchange_actionstring[] = GRID_ACTIONSTRING_BUTTON_BUTTON; const char grid_ui_encoder_timer_actionstring[] = GRID_ACTIONSTRING_SYSTEM_TIMER; -void grid_ui_encoder_state_init(struct grid_ui_encoder_state* state, uint8_t detent, int8_t direction) { +void grid_ui_encoder_state_init(struct grid_ui_model* ui, uint8_t element_index, uint8_t detent, int8_t direction, uint8_t button_adc_bit_depth, double button_threshold, double button_hysteresis) { + assert(ui); + assert(element_index < ui->element_list_length); assert(direction == 1 || direction == -1); + struct grid_ui_element* ele = &ui->element_list[element_index]; + struct grid_ui_encoder_state* state = (struct grid_ui_encoder_state*)ele->primary_state; + state->encoder_last_real_time = 0; state->button_last_real_time = 0; state->last_nibble = 0; @@ -52,12 +57,19 @@ void grid_ui_encoder_state_init(struct grid_ui_encoder_state* state, uint8_t det state->encoder_last_leave_dir = 0; state->initial_samples = 0; state->direction = direction; + + grid_ui_button_state_init(ui, element_index, button_adc_bit_depth, button_threshold, button_hysteresis); } void grid_ui_element_encoder_init(struct grid_ui_element* ele) { ele->type = GRID_PARAMETER_ELEMENT_ENCODER; + ele->primary_state = grid_platform_allocate_volatile(sizeof(struct grid_ui_encoder_state)); + ele->secondary_state = grid_platform_allocate_volatile(sizeof(struct grid_ui_button_state)); + memset(ele->primary_state, 0, sizeof(struct grid_ui_encoder_state)); + memset(ele->secondary_state, 0, sizeof(struct grid_ui_button_state)); + grid_ui_element_malloc_events(ele, 4); grid_ui_event_init(ele, 0, GRID_PARAMETER_EVENT_INIT, GRID_LUA_FNC_A_INIT_short, grid_ui_encoder_init_actionstring); // Element Initialization Event @@ -266,7 +278,19 @@ uint8_t grid_ui_encoder_update_trigger(struct grid_ui_element* ele, uint64_t* la return 1; // did trigger } -void grid_ui_encoder_store_input(struct grid_ui_element* ele, struct grid_ui_encoder_state* state, uint8_t new_value) { +void grid_ui_encoder_store_input(struct grid_ui_model* ui, uint8_t element_index, struct grid_ui_encoder_sample sample) { + + assert(ui); + assert(element_index < ui->element_list_length); + + // Handle button input for the encoder element + grid_ui_button_store_input(ui, element_index, sample.button); + + struct grid_ui_element* ele = &ui->element_list[element_index]; + struct grid_ui_encoder_state* state = (struct grid_ui_encoder_state*)ele->primary_state; + + // Reconstruct rotation value from phases + uint8_t new_value = sample.phase_a | (sample.phase_b << 1); // extract old value from state, rewrite state with new uint8_t old_value = state->last_nibble; diff --git a/grid_common/grid_ui_encoder.h b/grid_common/grid_ui_encoder.h index 7f2348acc..62bfdb9b6 100644 --- a/grid_common/grid_ui_encoder.h +++ b/grid_common/grid_ui_encoder.h @@ -6,8 +6,26 @@ #include "grid_protocol.h" #include "grid_ui.h" +struct grid_encoder_result { + uint8_t* data; + uint8_t length; +}; + +typedef void (*grid_process_encoder_t)(struct grid_encoder_result* result); + #define GRID_UI_ENCODER_INIT_SAMPLES 2 +struct grid_ui_encoder_sample { + uint8_t phase_a; + uint8_t phase_b; + uint8_t button; +}; + +#define GRID_UI_ENCODER_NIBBLE_FROM_BUFFER(buf, idx) (((buf)[(idx) / 2] >> (4 * ((idx) % 2))) & 0x0F) + +#define GRID_UI_ENCODER_SAMPLE_FROM_NIBBLE(nibble) \ + (struct grid_ui_encoder_sample) { .phase_a = (nibble) & 1, .phase_b = ((nibble) >> 1) & 1, .button = ((nibble) >> 2) & 1 } + struct grid_ui_encoder_state { uint64_t encoder_last_real_time; uint64_t button_last_real_time; @@ -18,7 +36,7 @@ struct grid_ui_encoder_state { int8_t direction; }; -void grid_ui_encoder_state_init(struct grid_ui_encoder_state* state, uint8_t detent, int8_t direction); +void grid_ui_encoder_state_init(struct grid_ui_model* ui, uint8_t element_index, uint8_t detent, int8_t direction, uint8_t button_adc_bit_depth, double button_threshold, double button_hysteresis); void grid_ui_element_encoder_init(struct grid_ui_element* ele); void grid_ui_element_encoder_template_parameter_init(struct grid_ui_template_buffer* buf); @@ -29,7 +47,7 @@ void grid_ui_element_encoder_page_change_cb(struct grid_ui_element* ele, uint8_t int16_t grid_ui_encoder_rotation_delta(uint8_t old_value, uint8_t new_value, uint8_t detent, int8_t* dir_lock); uint8_t grid_ui_encoder_update_trigger(struct grid_ui_element* ele, uint64_t* encoder_last_real_time, int16_t delta); -void grid_ui_encoder_store_input(struct grid_ui_element* ele, struct grid_ui_encoder_state* state, uint8_t new_value); +void grid_ui_encoder_store_input(struct grid_ui_model* ui, uint8_t element_index, struct grid_ui_encoder_sample sample); // ========================= ENCODER =========================== // diff --git a/grid_common/grid_ui_endless.c b/grid_common/grid_ui_endless.c index 6fe4b5960..5c8e50c5f 100644 --- a/grid_common/grid_ui_endless.c +++ b/grid_common/grid_ui_endless.c @@ -9,6 +9,7 @@ #include "grid_math.h" #include "grid_platform.h" #include "grid_sys.h" +#include "grid_ui_button.h" #include "grid_ui_system.h" const luaL_Reg GRID_LUA_EP_INDEX_META[] = {{GRID_LUA_FNC_EP_ELEMENT_INDEX_short, XAFTERX(GRID_LUA_FNC_GTV_NAME, GRID_LUA_FNC_EP_ELEMENT_INDEX_index)}, @@ -41,10 +42,28 @@ const char grid_ui_endless_endlesschange_actionstring[] = GRID_ACTIONSTRING_ENDL const char grid_ui_endless_buttonchange_actionstring[] = GRID_ACTIONSTRING_ENDLESS_BUTTON; const char grid_ui_endless_timer_actionstring[] = GRID_ACTIONSTRING_SYSTEM_TIMER; +void grid_ui_endless_state_init(struct grid_ui_model* ui, uint8_t element_index, uint8_t adc_bit_depth, uint8_t button_adc_bit_depth, double button_threshold, double button_hysteresis) { + + assert(ui); + assert(element_index < ui->element_list_length); + + struct grid_ui_element* ele = &ui->element_list[element_index]; + struct grid_ui_endless_state* state = (struct grid_ui_endless_state*)ele->primary_state; + + state->adc_bit_depth = adc_bit_depth; + + grid_ui_button_state_init(ui, element_index, button_adc_bit_depth, button_threshold, button_hysteresis); +} + void grid_ui_element_endless_init(struct grid_ui_element* ele) { ele->type = GRID_PARAMETER_ELEMENT_ENDLESS; + ele->primary_state = grid_platform_allocate_volatile(sizeof(struct grid_ui_endless_state)); + ele->secondary_state = grid_platform_allocate_volatile(sizeof(struct grid_ui_button_state)); + memset(ele->primary_state, 0, sizeof(struct grid_ui_endless_state)); + memset(ele->secondary_state, 0, sizeof(struct grid_ui_button_state)); + grid_ui_element_malloc_events(ele, 4); grid_ui_event_init(ele, 0, GRID_PARAMETER_EVENT_INIT, GRID_LUA_FNC_A_INIT_short, grid_ui_endless_init_actionstring); // Element Initialization Event @@ -301,37 +320,48 @@ static uint16_t grid_ui_endless_calculate_angle(uint16_t phase_a, uint16_t phase return value_degrees; } -void grid_ui_endless_store_input(struct grid_ui_element* ele, uint8_t input_channel, uint8_t adc_bit_depth, struct grid_ui_endless_state* new_value, struct grid_ui_endless_state* old_value) { +void grid_ui_endless_store_input(struct grid_ui_model* ui, uint8_t element_index, struct grid_ui_endless_sample sample) { + + assert(ui); + assert(element_index < ui->element_list_length); + + // Handle button input for the endless element + grid_ui_button_store_input(ui, element_index, sample.button_value); - assert(ele); + struct grid_ui_element* ele = &ui->element_list[element_index]; + struct grid_ui_endless_state* state = (struct grid_ui_endless_state*)ele->primary_state; + uint8_t adc_bit_depth = state->adc_bit_depth; - if (!memcmp(old_value, new_value, sizeof(struct grid_ui_endless_state))) { + // Check if current values differ from previous + if (sample.phase_a == state->prev_phase_a && sample.phase_b == state->prev_phase_b && sample.button_value == state->prev_button_value) { // no change return; } int32_t* template_parameter_list = ele->template_parameter_list; - int stabilized = grid_ain_stabilized(&grid_ain_state, input_channel); + int stabilized = grid_ain_stabilized(&grid_ain_state, element_index); if (!stabilized) { - memcpy(old_value, new_value, sizeof(struct grid_ui_endless_state)); + state->prev_phase_a = sample.phase_a; + state->prev_phase_b = sample.phase_b; + state->prev_button_value = sample.button_value; } - uint16_t value_degrees_new = grid_ui_endless_calculate_angle(new_value->phase_a, new_value->phase_b, 12); - uint16_t value_degrees_old = grid_ui_endless_calculate_angle(old_value->phase_a, old_value->phase_b, 12); + uint16_t value_degrees_new = grid_ui_endless_calculate_angle(sample.phase_a, sample.phase_b, adc_bit_depth); + uint16_t value_degrees_old = grid_ui_endless_calculate_angle(state->prev_phase_a, state->prev_phase_b, adc_bit_depth); int32_t resolution = 9; if (resolution < 1) { resolution = 1; - } else if (resolution > 12) { - resolution = 12; + } else if (resolution > adc_bit_depth) { + resolution = adc_bit_depth; } - grid_ain_add_sample(&grid_ain_state, input_channel, value_degrees_new, 12, (uint8_t)resolution); + grid_ain_add_sample(&grid_ain_state, element_index, value_degrees_new, adc_bit_depth, (uint8_t)resolution); - if (grid_ain_get_changed(&grid_ain_state, input_channel)) { + if (grid_ain_get_changed(&grid_ain_state, element_index)) { int16_t delta = (value_degrees_new - value_degrees_old); @@ -344,10 +374,10 @@ void grid_ui_endless_store_input(struct grid_ui_element* ele, uint8_t input_chan if (abs(delta) > 10) { template_parameter_list[GRID_LUA_FNC_EP_ENDLESS_DIRECTION_index] = value_degrees_new / 20; - grid_ui_endless_update_trigger(ele, stabilized, delta, &old_value->encoder_last_real_time, &old_value->delta_vel_frac); + grid_ui_endless_update_trigger(ele, stabilized, delta, &state->encoder_last_real_time, &state->delta_vel_frac); - old_value->phase_a = new_value->phase_a; - old_value->phase_b = new_value->phase_b; + state->prev_phase_a = sample.phase_a; + state->prev_phase_b = sample.phase_b; } } } diff --git a/grid_common/grid_ui_endless.h b/grid_common/grid_ui_endless.h index e237020a2..3efb732d3 100644 --- a/grid_common/grid_ui_endless.h +++ b/grid_common/grid_ui_endless.h @@ -6,21 +6,30 @@ #include "grid_protocol.h" #include "grid_ui.h" -struct grid_ui_endless_state { +struct grid_ui_endless_sample { uint16_t phase_a; uint16_t phase_b; uint16_t button_value; +}; + +struct grid_ui_endless_state { uint64_t encoder_last_real_time; double delta_vel_frac; + uint16_t prev_phase_a; + uint16_t prev_phase_b; + uint16_t prev_button_value; + uint8_t adc_bit_depth; }; +void grid_ui_endless_state_init(struct grid_ui_model* ui, uint8_t element_index, uint8_t adc_bit_depth, uint8_t button_adc_bit_depth, double button_threshold, double button_hysteresis); + void grid_ui_element_endless_init(struct grid_ui_element* ele); void grid_ui_element_endless_template_parameter_init(struct grid_ui_template_buffer* buf); void grid_ui_element_endless_event_clear_cb(struct grid_ui_event* eve); void grid_ui_element_endless_page_change_cb(struct grid_ui_element* ele, uint8_t page_old, uint8_t page_new); -void grid_ui_endless_store_input(struct grid_ui_element* ele, uint8_t input_channel, uint8_t adc_bit_depth, struct grid_ui_endless_state* new_value, struct grid_ui_endless_state* old_value); +void grid_ui_endless_store_input(struct grid_ui_model* ui, uint8_t element_index, struct grid_ui_endless_sample sample); uint8_t grid_ui_endless_update_trigger(struct grid_ui_element* ele, int stabilized, int16_t delta, uint64_t* endless_last_real_time, double* delta_frac); diff --git a/grid_common/grid_ui_potmeter.c b/grid_common/grid_ui_potmeter.c index 45fa09635..173ccc7e0 100644 --- a/grid_common/grid_ui_potmeter.c +++ b/grid_common/grid_ui_potmeter.c @@ -29,9 +29,16 @@ const char grid_ui_potmeter_init_actionstring[] = GRID_ACTIONSTRING_POTMETER_INI const char grid_ui_potmeter_potmeterchange_actionstring[] = GRID_ACTIONSTRING_POTMETER_POTMETER; const char grid_ui_potmeter_timer_actionstring[] = GRID_ACTIONSTRING_SYSTEM_TIMER; -void grid_ui_potmeter_state_init(struct grid_ui_potmeter_state* state, uint8_t adc_bit_depth, uint16_t deadzone, uint16_t center) { +void grid_ui_potmeter_state_init(struct grid_ui_model* ui, uint8_t element_index, uint8_t adc_bit_depth, uint16_t deadzone, uint16_t center) { + + assert(ui); + assert(element_index < ui->element_list_length); + + struct grid_ui_element* ele = &ui->element_list[element_index]; + struct grid_ui_potmeter_state* state = (struct grid_ui_potmeter_state*)ele->primary_state; state->last_real_time = 0; + state->adc_bit_depth = adc_bit_depth; assert(adc_bit_depth); assert(deadzone < (1 << (adc_bit_depth - 1))); @@ -47,6 +54,9 @@ void grid_ui_element_potmeter_init(struct grid_ui_element* ele) { ele->type = GRID_PARAMETER_ELEMENT_POTMETER; + ele->primary_state = grid_platform_allocate_volatile(sizeof(struct grid_ui_potmeter_state)); + memset(ele->primary_state, 0, sizeof(struct grid_ui_potmeter_state)); + grid_ui_element_malloc_events(ele, 3); grid_ui_event_init(ele, 0, GRID_PARAMETER_EVENT_INIT, GRID_LUA_FNC_A_INIT_short, grid_ui_potmeter_init_actionstring); // Element Initialization Event @@ -69,7 +79,7 @@ void grid_ui_element_potmeter_init(struct grid_ui_element* ele) { ele->page_change_cb = &grid_ui_element_potmeter_page_change_cb; } -void grid_ui_element_potmeter_update_value(int32_t* template_parameter_list, uint8_t input_channel, uint8_t adc_bit_depth) { +void grid_ui_element_potmeter_update_value(int32_t* template_parameter_list, uint8_t element_index, uint8_t adc_bit_depth) { int32_t resolution = template_parameter_list[GRID_LUA_FNC_P_POTMETER_MODE_index]; resolution = clampi32(resolution, 1, 12); @@ -79,7 +89,7 @@ void grid_ui_element_potmeter_update_value(int32_t* template_parameter_list, uin int32_t min = MIN(tmin, tmax); int32_t max = MAX(tmin, tmax); - int32_t new_value = grid_ain_get_average_scaled(&grid_ain_state, input_channel, adc_bit_depth, resolution, min, max); + int32_t new_value = grid_ain_get_average_scaled(&grid_ain_state, element_index, adc_bit_depth, resolution, min, max); if (tmin > tmax) { new_value = mirrori32(new_value, min, max); @@ -117,16 +127,19 @@ void grid_ui_element_potmeter_page_change_cb(struct grid_ui_element* ele, uint8_ grid_ui_element_potmeter_update_value(template_parameter_list, element_index, adc_bit_depth); } -void grid_ui_potmeter_store_input(struct grid_ui_element* ele, uint8_t input_channel, struct grid_ui_potmeter_state* state, uint16_t value, uint8_t adc_bit_depth) { +void grid_ui_potmeter_store_input(struct grid_ui_model* ui, uint8_t element_index, uint16_t value) { - assert(ele); + assert(ui); + assert(element_index < ui->element_list_length); - // const uint16_t adc_max_value = (1 << adc_bit_depth) - 1; + struct grid_ui_element* ele = &ui->element_list[element_index]; + struct grid_ui_potmeter_state* state = (struct grid_ui_potmeter_state*)ele->primary_state; + uint8_t adc_bit_depth = state->adc_bit_depth; int32_t* template_parameter_list = ele->template_parameter_list; uint16_t value_asc; - if (grid_asc_process(&grid_cal_state.sigcond[input_channel], value, &value_asc)) { + if (grid_asc_process(grid_cal_state.sigcond, element_index, value, &value_asc)) { grid_cal_limits_value_update(&state->limits, value_asc); @@ -139,18 +152,18 @@ void grid_ui_potmeter_store_input(struct grid_ui_element* ele, uint8_t input_cha return; } - value = grid_cal_next(&grid_cal_state, input_channel, value); + value = grid_cal_next(&grid_cal_state, element_index, 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); + grid_ain_add_sample(&grid_ain_state, element_index, value, adc_bit_depth, (uint8_t)resolution); // limit lastrealtime uint64_t now = grid_platform_rtc_get_micros(); uint64_t elapsed_us = grid_platform_rtc_get_diff(now, state->last_real_time); elapsed_us = MIN(elapsed_us, GRID_PARAMETER_ELAPSED_LIMIT * MS_TO_US); - if (grid_ain_get_changed(&grid_ain_state, input_channel)) { + if (grid_ain_get_changed(&grid_ain_state, element_index)) { // update lastrealtime state->last_real_time = now; @@ -159,12 +172,12 @@ void grid_ui_potmeter_store_input(struct grid_ui_element* ele, uint8_t input_cha grid_ui_element_potmeter_update_value(template_parameter_list, ele->index, adc_bit_depth); // for display in editor - int32_t state = grid_ain_get_average_scaled(&grid_ain_state, input_channel, adc_bit_depth, resolution, 0, 127); + int32_t state = grid_ain_get_average_scaled(&grid_ain_state, element_index, adc_bit_depth, resolution, 0, 127); template_parameter_list[GRID_LUA_FNC_P_POTMETER_STATE_index] = state; struct grid_ui_event* eve = grid_ui_event_find(ele, GRID_PARAMETER_EVENT_POTMETER); - if (grid_ain_stabilized(&grid_ain_state, input_channel)) { + if (grid_ain_stabilized(&grid_ain_state, element_index)) { grid_ui_event_state_set(eve, GRID_EVE_STATE_TRIG); } } diff --git a/grid_common/grid_ui_potmeter.h b/grid_common/grid_ui_potmeter.h index 9e47f2476..164199c05 100644 --- a/grid_common/grid_ui_potmeter.h +++ b/grid_common/grid_ui_potmeter.h @@ -12,9 +12,10 @@ struct grid_ui_potmeter_state { struct grid_cal_limits limits; struct grid_cal_center center; struct grid_cal_detent detent; + uint8_t adc_bit_depth; }; -void grid_ui_potmeter_state_init(struct grid_ui_potmeter_state* state, uint8_t adc_bit_depth, uint16_t deadzone, uint16_t center); +void grid_ui_potmeter_state_init(struct grid_ui_model* ui, uint8_t element_index, uint8_t adc_bit_depth, uint16_t deadzone, uint16_t center); void grid_ui_element_potmeter_init(struct grid_ui_element* ele); void grid_ui_element_potmeter_template_parameter_init(struct grid_ui_template_buffer* buf); @@ -22,7 +23,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(struct grid_ui_element* ele, uint8_t input_channel, struct grid_ui_potmeter_state* state, uint16_t value, uint8_t adc_bit_depth); +void grid_ui_potmeter_store_input(struct grid_ui_model* ui, uint8_t element_index, uint16_t value); // ========================= POTMETER =========================== // diff --git a/grid_esp/components/grid_esp32_adc/CMakeLists.txt b/grid_esp/components/grid_esp32_adc/CMakeLists.txt index 8edc6ec32..f556c03b0 100644 --- a/grid_esp/components/grid_esp32_adc/CMakeLists.txt +++ b/grid_esp/components/grid_esp32_adc/CMakeLists.txt @@ -4,7 +4,7 @@ idf_component_register( INCLUDE_DIRS "." REQUIRES - "driver" "hal" "esp_adc" "ulp" "grid_esp32_pins" + "driver" "hal" "esp_adc" "ulp" "grid_esp32_pins" "grid_common" ) # diff --git a/grid_esp/components/grid_esp32_adc/grid_esp32_adc.c b/grid_esp/components/grid_esp32_adc/grid_esp32_adc.c index 891b07ef8..139aec187 100644 --- a/grid_esp/components/grid_esp32_adc/grid_esp32_adc.c +++ b/grid_esp/components/grid_esp32_adc/grid_esp32_adc.c @@ -19,7 +19,9 @@ #include "rom/ets_sys.h" +#include "grid_ain.h" #include "grid_esp32_pins.h" +#include "grid_platform.h" #include "esp_heap_caps.h" #include "freertos/ringbuf.h" @@ -45,7 +47,7 @@ extern const uint8_t ulp_grid_esp32_adc_bin_end[] asm("_binary_ulp_grid_esp32_ad struct grid_esp32_adc_model DRAM_ATTR grid_esp32_adc_state; -void grid_esp32_adc_mux_init(struct grid_esp32_adc_model* adc, uint8_t mux_overflow) { +void grid_esp32_adc_mux_init(struct grid_esp32_adc_model* adc, uint8_t mux_positions_bm) { gpio_set_direction(GRID_ESP32_PINS_MUX_0_A, GPIO_MODE_OUTPUT); gpio_set_direction(GRID_ESP32_PINS_MUX_0_B, GPIO_MODE_OUTPUT); @@ -55,25 +57,26 @@ void grid_esp32_adc_mux_init(struct grid_esp32_adc_model* adc, uint8_t mux_overf gpio_set_direction(GRID_ESP32_PINS_MUX_1_B, GPIO_MODE_OUTPUT); gpio_set_direction(GRID_ESP32_PINS_MUX_1_C, GPIO_MODE_OUTPUT); - adc->mux_index = 0; + adc->mux_positions_bm = mux_positions_bm; + + GRID_MUX_FIRST_VALID(adc->mux_index, adc->mux_positions_bm); grid_esp32_adc_mux_update(adc); - adc->mux_overflow = mux_overflow; - ulp_mux_overflow = adc->mux_overflow; + ulp_mux_positions_bm = mux_positions_bm; } -void IRAM_ATTR grid_esp32_adc_mux_increment(struct grid_esp32_adc_model* adc) { adc->mux_index = (adc->mux_index + 1) % adc->mux_overflow; } +void IRAM_ATTR grid_esp32_adc_mux_increment(struct grid_esp32_adc_model* adc) { GRID_MUX_INCREMENT(adc->mux_index, adc->mux_positions_bm); } void IRAM_ATTR grid_esp32_adc_mux_update(struct grid_esp32_adc_model* adc) { - gpio_ll_set_level(&GPIO, GRID_ESP32_PINS_MUX_0_A, adc->mux_index >> 0 & 0x1); - gpio_ll_set_level(&GPIO, GRID_ESP32_PINS_MUX_0_B, adc->mux_index >> 1 & 0x1); - gpio_ll_set_level(&GPIO, GRID_ESP32_PINS_MUX_0_C, adc->mux_index >> 2 & 0x1); + gpio_ll_set_level(&GPIO, GRID_ESP32_PINS_MUX_0_A, (adc->mux_index >> 0) & 1); + gpio_ll_set_level(&GPIO, GRID_ESP32_PINS_MUX_0_B, (adc->mux_index >> 1) & 1); + gpio_ll_set_level(&GPIO, GRID_ESP32_PINS_MUX_0_C, (adc->mux_index >> 2) & 1); - gpio_ll_set_level(&GPIO, GRID_ESP32_PINS_MUX_1_A, adc->mux_index >> 0 & 0x1); - gpio_ll_set_level(&GPIO, GRID_ESP32_PINS_MUX_1_B, adc->mux_index >> 1 & 0x1); - gpio_ll_set_level(&GPIO, GRID_ESP32_PINS_MUX_1_C, adc->mux_index >> 2 & 0x1); + gpio_ll_set_level(&GPIO, GRID_ESP32_PINS_MUX_1_A, (adc->mux_index >> 0) & 1); + gpio_ll_set_level(&GPIO, GRID_ESP32_PINS_MUX_1_B, (adc->mux_index >> 1) & 1); + gpio_ll_set_level(&GPIO, GRID_ESP32_PINS_MUX_1_C, (adc->mux_index >> 2) & 1); } #include "esp_private/adc_share_hw_ctrl.h" @@ -103,13 +106,17 @@ static void adc_init_ulp(struct grid_esp32_adc_model* adc) { ESP_ERROR_CHECK(ulp_riscv_load_binary(binary, size)); } -void grid_esp32_adc_init(struct grid_esp32_adc_model* adc, grid_process_analog_t process_analog) { +void grid_esp32_adc_init(struct grid_esp32_adc_model* adc, uint8_t mux_positions_bm, uint8_t mux_dependent, grid_process_analog_t process_analog) { assert(process_analog); adc->process_analog = process_analog; adc_init_ulp(adc); + + adc->mux_dependent = mux_dependent != 0; + + grid_platform_mux_init(mux_positions_bm); } static void IRAM_ATTR ulp_isr(void* arg) { @@ -123,10 +130,9 @@ static void IRAM_ATTR ulp_isr(void* arg) { } } -void grid_esp32_adc_start(struct grid_esp32_adc_model* adc, uint8_t mux_dependent) { +void grid_esp32_adc_start(struct grid_esp32_adc_model* adc) { - // Set flag for both processors indicating which one does mux addressing - adc->mux_dependent = mux_dependent != 0; + // Set flag for ULP processor indicating which one does mux addressing ulp_mux_dependent = adc->mux_dependent; // Register ISR handler @@ -157,7 +163,7 @@ void IRAM_ATTR grid_esp32_adc_conv_mux() { for (int i = 0; i < 2; ++i) { - struct grid_esp32_adc_result result; + struct grid_adc_result result; result.channel = i; result.mux_state = mux_state; result.value = adc_value[i]; @@ -183,9 +189,14 @@ void IRAM_ATTR grid_esp32_adc_conv_nomux() { for (int i = 0; i < 8; ++i) { + // Skip positions not in the valid mask + if (!(adc->mux_positions_bm & (1 << i))) { + continue; + } + for (int j = 0; j < 2; ++j) { - struct grid_esp32_adc_result result; + struct grid_adc_result result; result.channel = j; result.mux_state = i; result.value = adc_value[i][j]; diff --git a/grid_esp/components/grid_esp32_adc/grid_esp32_adc.h b/grid_esp/components/grid_esp32_adc/grid_esp32_adc.h index 2d9d872c4..d257110fa 100644 --- a/grid_esp/components/grid_esp32_adc/grid_esp32_adc.h +++ b/grid_esp/components/grid_esp32_adc/grid_esp32_adc.h @@ -17,6 +17,8 @@ #include "driver/gptimer.h" +#include "grid_ain.h" + #ifdef __cplusplus extern "C" { #endif @@ -32,34 +34,27 @@ extern "C" { #define ADC_TIMER_PERIOD_USEC 1000 -typedef void (*grid_process_analog_t)(void* user); - struct grid_esp32_adc_model { uint8_t mux_index; - uint8_t mux_overflow; + // Bitmask of valid mux positions (bit N = position N valid) + // Positions: 76543210 + uint8_t mux_positions_bm; uint8_t mux_dependent; grid_process_analog_t process_analog; }; -struct grid_esp32_adc_result { - - uint8_t channel; - uint8_t mux_state; - uint16_t value; -}; - extern struct grid_esp32_adc_model grid_esp32_adc_state; -void grid_esp32_adc_init(struct grid_esp32_adc_model* adc, grid_process_analog_t process_analog); +void grid_esp32_adc_init(struct grid_esp32_adc_model* adc, uint8_t mux_positions_bm, uint8_t mux_dependent, grid_process_analog_t process_analog); -void grid_esp32_adc_mux_init(struct grid_esp32_adc_model* adc, uint8_t mux_overflow); +void grid_esp32_adc_mux_init(struct grid_esp32_adc_model* adc, uint8_t mux_positions_bm); void grid_esp32_adc_mux_increment(struct grid_esp32_adc_model* adc); void grid_esp32_adc_mux_update(struct grid_esp32_adc_model* adc); -void grid_esp32_adc_start(struct grid_esp32_adc_model* adc, uint8_t mux_dependent); +void grid_esp32_adc_start(struct grid_esp32_adc_model* adc); void grid_esp32_adc_stop(struct grid_esp32_adc_model* adc); void grid_esp32_adc_conv_mux(); diff --git a/grid_esp/components/grid_esp32_adc/ulp/main.c b/grid_esp/components/grid_esp32_adc/ulp/main.c index 78948d2f9..e69d97d90 100644 --- a/grid_esp/components/grid_esp32_adc/ulp/main.c +++ b/grid_esp/components/grid_esp32_adc/ulp/main.c @@ -14,19 +14,28 @@ #include "hal/adc_types.h" +#include "grid_ain.h" #include "grid_esp32_pins.h" const uint32_t ADC_CHANNELS[2] = {ADC_CHANNEL_1, ADC_CHANNEL_0}; // volatile, to avoid being optimized away volatile uint32_t mux_dependent = 0; -volatile uint32_t mux_overflow = 0; volatile uint32_t mux_index = 0; +volatile uint32_t mux_positions_bm = 0xFF; volatile uint32_t adc_result_ready = 0; volatile uint32_t adc_result_taken = 1; volatile uint32_t adc_value[8][2] = {0}; -void grid_ulp_adc_mux_init(void) { +void grid_platform_mux_write(uint8_t index) { + mux_index = index; + ulp_riscv_gpio_output_level(GRID_ESP32_PINS_MUX_1_A, (mux_index >> 0) & 1); + ulp_riscv_gpio_output_level(GRID_ESP32_PINS_MUX_1_B, (mux_index >> 1) & 1); + ulp_riscv_gpio_output_level(GRID_ESP32_PINS_MUX_1_C, (mux_index >> 2) & 1); +} + +void grid_platform_mux_init(uint8_t positions_bm) { + mux_positions_bm = positions_bm; ulp_riscv_gpio_init(GRID_ESP32_PINS_MUX_1_A); ulp_riscv_gpio_init(GRID_ESP32_PINS_MUX_1_B); @@ -35,15 +44,10 @@ void grid_ulp_adc_mux_init(void) { ulp_riscv_gpio_output_enable(GRID_ESP32_PINS_MUX_1_A); ulp_riscv_gpio_output_enable(GRID_ESP32_PINS_MUX_1_B); ulp_riscv_gpio_output_enable(GRID_ESP32_PINS_MUX_1_C); -} -static inline void grid_ulp_adc_mux_increment() { mux_index = (mux_index + 1) % mux_overflow; } + GRID_MUX_FIRST_VALID(mux_index, mux_positions_bm); -static inline void grid_ulp_adc_mux_update() { - - ulp_riscv_gpio_output_level(GRID_ESP32_PINS_MUX_1_A, mux_index & 0x1); - ulp_riscv_gpio_output_level(GRID_ESP32_PINS_MUX_1_B, mux_index & 0x2); - ulp_riscv_gpio_output_level(GRID_ESP32_PINS_MUX_1_C, mux_index & 0x4); + grid_platform_mux_write(mux_index); } static inline uint32_t adc_read_with_mux(adc_unit_t adc_n, volatile uint32_t ret[2]) { @@ -56,9 +60,6 @@ static inline uint32_t adc_read_with_mux(adc_unit_t adc_n, volatile uint32_t ret adc_oneshot_ll_set_channel(adc_n, ADC_CHANNELS[0]); adc_oneshot_ll_start(adc_n); - // Increment mux here, while the conversion is running - grid_ulp_adc_mux_increment(); - while (adc_oneshot_ll_get_event(event) != true) { ; } @@ -74,8 +75,9 @@ static inline uint32_t adc_read_with_mux(adc_unit_t adc_n, volatile uint32_t ret ; } - // Update mux pins here, as early as possible - grid_ulp_adc_mux_update(); + // Advance to next mux position + GRID_MUX_INCREMENT(mux_index, mux_positions_bm); + grid_platform_mux_write(mux_index); ret[1] = adc_oneshot_ll_get_raw_result(adc_n); adc_oneshot_ll_disable_all_unit(); @@ -84,7 +86,7 @@ static inline uint32_t adc_read_with_mux(adc_unit_t adc_n, volatile uint32_t ret int main(void) { if (!mux_dependent) { - grid_ulp_adc_mux_init(); + grid_platform_mux_init(mux_positions_bm); } while (1) { @@ -100,10 +102,11 @@ int main(void) { } else { - for (int i = 0; i < mux_overflow; ++i) { - - adc_read_with_mux(ADC_UNIT_1, adc_value[i]); - } + uint32_t start_pos = mux_index; + do { + uint32_t pos = mux_index; + adc_read_with_mux(ADC_UNIT_1, adc_value[pos]); + } while (mux_index != start_pos); } while (adc_result_ready) { diff --git a/grid_esp/components/grid_esp32_encoder/CMakeLists.txt b/grid_esp/components/grid_esp32_encoder/CMakeLists.txt index 34a8cf14e..c9f74bd84 100644 --- a/grid_esp/components/grid_esp32_encoder/CMakeLists.txt +++ b/grid_esp/components/grid_esp32_encoder/CMakeLists.txt @@ -4,5 +4,5 @@ idf_component_register( INCLUDE_DIRS "." REQUIRES - "driver" "grid_esp32_pins" + "driver" "grid_esp32_pins" "grid_common" ) diff --git a/grid_esp/components/grid_esp32_encoder/grid_esp32_encoder.c b/grid_esp/components/grid_esp32_encoder/grid_esp32_encoder.c index bf90f7770..b33f2092c 100644 --- a/grid_esp/components/grid_esp32_encoder/grid_esp32_encoder.c +++ b/grid_esp/components/grid_esp32_encoder/grid_esp32_encoder.c @@ -14,24 +14,18 @@ static IRAM_ATTR bool i2s_recv_callback(i2s_chan_handle_t handle, i2s_event_data struct grid_esp32_encoder_model* encoder = user_ctx; - ++encoder->dma_frame_count; - - if (encoder->dma_frame_count < encoder->dma_frame_div) { - return true; - } - - encoder->dma_frame_count = 0; - - encoder->process_encoder(event->dma_buf); + struct grid_encoder_result result = {.data = &((uint8_t*)event->dma_buf)[1], .length = encoder->transfer_length - 1}; + encoder->process_encoder(&result); return true; } -void grid_esp32_encoder_init(struct grid_esp32_encoder_model* encoder, uint32_t divider, grid_process_encoder_t process_encoder) { +void grid_esp32_encoder_init(struct grid_esp32_encoder_model* encoder, uint8_t transfer_length, uint32_t clock_rate, grid_process_encoder_t process_encoder) { + + uint32_t bits_per_frame = I2S_DATA_BIT_WIDTH_32BIT * 4; + uint32_t sample_rate = clock_rate / bits_per_frame; - assert(GRID_ESP32_ENCODER_I2S_SRATE % divider == 0); - encoder->dma_frame_div = divider; - encoder->dma_frame_count = 0; + encoder->transfer_length = transfer_length; assert(process_encoder); encoder->process_encoder = process_encoder; @@ -42,7 +36,7 @@ void grid_esp32_encoder_init(struct grid_esp32_encoder_model* encoder, uint32_t ESP_ERROR_CHECK(i2s_new_channel(&rx_chan_cfg, NULL, &encoder->rx_chan)); i2s_tdm_config_t rx_tdm_cfg = { - .clk_cfg = I2S_TDM_CLK_DEFAULT_CONFIG(GRID_ESP32_ENCODER_I2S_SRATE), + .clk_cfg = I2S_TDM_CLK_DEFAULT_CONFIG(sample_rate), .slot_cfg = I2S_TDM_PCM_SHORT_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_32BIT, I2S_SLOT_MODE_STEREO, I2S_TDM_SLOT0 | I2S_TDM_SLOT1 | I2S_TDM_SLOT2 | I2S_TDM_SLOT3), .gpio_cfg = { diff --git a/grid_esp/components/grid_esp32_encoder/grid_esp32_encoder.h b/grid_esp/components/grid_esp32_encoder/grid_esp32_encoder.h index fd00f5c87..de73d04b6 100644 --- a/grid_esp/components/grid_esp32_encoder/grid_esp32_encoder.h +++ b/grid_esp/components/grid_esp32_encoder/grid_esp32_encoder.h @@ -36,25 +36,19 @@ extern "C" { #include "esp_rom_gpio.h" #include "hal/gpio_ll.h" -enum { - GRID_ESP32_ENCODER_I2S_SRATE = 2000, -}; - -typedef void (*grid_process_encoder_t)(void* user); +#include "grid_ui_encoder.h" struct grid_esp32_encoder_model { i2s_chan_handle_t rx_chan; - - uint32_t dma_frame_div; - uint32_t dma_frame_count; + uint8_t transfer_length; grid_process_encoder_t process_encoder; }; extern struct grid_esp32_encoder_model grid_esp32_encoder_state; -void grid_esp32_encoder_init(struct grid_esp32_encoder_model* encoder, uint32_t divider, grid_process_encoder_t process_encoder); +void grid_esp32_encoder_init(struct grid_esp32_encoder_model* encoder, uint8_t transfer_length, uint32_t clock_rate, grid_process_encoder_t process_encoder); #ifdef __cplusplus } diff --git a/grid_esp/components/grid_esp32_module_bu16/grid_esp32_module_bu16.c b/grid_esp/components/grid_esp32_module_bu16/grid_esp32_module_bu16.c index d5f3cfd69..75b3c3a25 100644 --- a/grid_esp/components/grid_esp32_module_bu16/grid_esp32_module_bu16.c +++ b/grid_esp/components/grid_esp32_module_bu16/grid_esp32_module_bu16.c @@ -11,6 +11,7 @@ #include +#include "grid_ain.h" #include "grid_asc.h" #include "grid_cal.h" #include "grid_config.h" @@ -23,57 +24,54 @@ // static const char* TAG = "module_bu16"; -#define GRID_MODULE_BU16_BUT_NUM 16 +#define GRID_MODULE_BU16_BUTTON_COUNT 16 -static struct grid_ui_button_state* DRAM_ATTR ui_button_state = NULL; static struct grid_asc* DRAM_ATTR asc_state = NULL; -static struct grid_ui_element* DRAM_ATTR elements = NULL; -void IRAM_ATTR bu16_process_analog(void* user) { +static DRAM_ATTR const uint8_t mux_element_lookup[2][8] = { + {0, 1, 4, 5, 8, 9, 12, 13}, + {2, 3, 6, 7, 10, 11, 14, 15}, +}; +static DRAM_ATTR uint16_t element_invert_bm = 0; - static DRAM_ATTR const uint8_t multiplexer_lookup[16] = {2, 0, 3, 1, 6, 4, 7, 5, 10, 8, 11, 9, 14, 12, 15, 13}; +void IRAM_ATTR bu16_process_analog(struct grid_adc_result* result) { - assert(user); + assert(result); - struct grid_esp32_adc_result* result = (struct grid_esp32_adc_result*)user; + uint8_t element_index = mux_element_lookup[result->channel][result->mux_state]; - uint8_t lookup_index = result->mux_state * 2 + result->channel; - uint8_t mux_position = multiplexer_lookup[lookup_index]; - struct grid_ui_element* ele = &elements[mux_position]; + uint16_t raw = result->value; + uint16_t inverted = GRID_ADC_INVERT_COND(raw, element_index, element_invert_bm); - if (!grid_asc_process(&asc_state[lookup_index], result->value, &result->value)) { + uint16_t processed; + if (!grid_asc_process(asc_state, element_index, inverted, &processed)) { return; } - if (mux_position < 16) { - - grid_ui_button_store_input(ele, &ui_button_state[mux_position], result->value, 12); - } + grid_ui_button_store_input(&grid_ui_state, element_index, processed); } void grid_esp32_module_bu16_init(struct grid_sys_model* sys, struct grid_ui_model* ui, struct grid_esp32_adc_model* adc, struct grid_config_model* conf, struct grid_cal_model* cal) { - ui_button_state = grid_platform_allocate_volatile(GRID_MODULE_BU16_BUT_NUM * sizeof(struct grid_ui_button_state)); asc_state = grid_platform_allocate_volatile(16 * sizeof(struct grid_asc)); - memset(ui_button_state, 0, GRID_MODULE_BU16_BUT_NUM * sizeof(struct grid_ui_button_state)); memset(asc_state, 0, 16 * sizeof(struct grid_asc)); - for (int i = 0; i < GRID_MODULE_BU16_BUT_NUM; ++i) { - grid_ui_button_state_init(&ui_button_state[i], 12, 0.5, 0.2); + for (int i = 0; i < GRID_MODULE_BU16_BUTTON_COUNT; ++i) { + grid_ui_button_state_init(ui, i, GRID_AIN_INTERNAL_RESOLUTION, 0.5, 0.2); } grid_asc_array_set_factors(asc_state, 16, 0, 16, 1); - elements = grid_ui_model_get_elements(ui); - grid_config_init(conf, cal); - grid_cal_init(cal, ui->element_list_length, 12); + grid_cal_init(cal, ui->element_list_length, GRID_AIN_INTERNAL_RESOLUTION); if (grid_hwcfg_module_is_rev_h(sys)) { - for (int i = 0; i < 16; ++i) { - assert(grid_cal_set(cal, i, GRID_CAL_LIMITS, &ui_button_state[i].limits) == 0); + for (int i = 0; i < GRID_MODULE_BU16_BUTTON_COUNT; ++i) { + struct grid_ui_element* ele = &ui->element_list[i]; + struct grid_ui_button_state* state = (struct grid_ui_button_state*)ele->primary_state; + assert(grid_cal_set(cal, i, GRID_CAL_LIMITS, &state->limits) == 0); } while (grid_ui_bulk_conf_init(ui, GRID_UI_BULK_CONFREAD_PROGRESS, 0, NULL)) { @@ -85,8 +83,7 @@ void grid_esp32_module_bu16_init(struct grid_sys_model* sys, struct grid_ui_mode } } - grid_esp32_adc_init(adc, bu16_process_analog); - grid_esp32_adc_mux_init(adc, 8); uint8_t mux_dependent = !grid_hwcfg_module_is_rev_h(sys); - grid_esp32_adc_start(adc, mux_dependent); + grid_esp32_adc_init(adc, 0b11111111, mux_dependent, bu16_process_analog); + grid_esp32_adc_start(adc); } diff --git a/grid_esp/components/grid_esp32_module_ef44/grid_esp32_module_ef44.c b/grid_esp/components/grid_esp32_module_ef44/grid_esp32_module_ef44.c index 7f045dbb9..148f8bc09 100644 --- a/grid_esp/components/grid_esp32_module_ef44/grid_esp32_module_ef44.c +++ b/grid_esp/components/grid_esp32_module_ef44/grid_esp32_module_ef44.c @@ -8,6 +8,7 @@ #include +#include "grid_ain.h" #include "grid_asc.h" #include "grid_cal.h" #include "grid_config.h" @@ -28,93 +29,74 @@ // static const char* TAG = "module_ef44"; -#define GRID_MODULE_EF44_BUT_NUM 4 +#define GRID_MODULE_EF44_BUTTON_COUNT 4 -#define GRID_MODULE_EF44_ENC_NUM 4 +#define GRID_MODULE_EF44_ENCODER_COUNT 4 -#define GRID_MODULE_EF44_POT_NUM 4 +#define GRID_MODULE_EF44_POTMETER_COUNT 4 -static struct grid_ui_button_state* DRAM_ATTR ui_button_state = NULL; -static struct grid_ui_encoder_state* DRAM_ATTR ui_encoder_state = NULL; -static struct grid_ui_potmeter_state* DRAM_ATTR ui_potmeter_state = NULL; static struct grid_asc* DRAM_ATTR asc_state = NULL; -static struct grid_ui_element* DRAM_ATTR elements = NULL; -void IRAM_ATTR ef44_process_analog(void* user) { +static DRAM_ATTR const uint8_t mux_element_lookup[2][2] = { + {6, 7}, + {4, 5}, +}; +static DRAM_ATTR uint16_t element_invert_bm = 0; - static DRAM_ATTR const uint8_t multiplexer_lookup[16] = {6, 4, 7, 5, -1, -1, -1, -1, -1, -1, -1, -1, 10, 8, 11, 9}; +void IRAM_ATTR ef44_process_analog(struct grid_adc_result* result) { - assert(user); + assert(result); - struct grid_esp32_adc_result* result = (struct grid_esp32_adc_result*)user; + uint8_t element_index = mux_element_lookup[result->channel][result->mux_state]; - uint8_t lookup_index = result->mux_state * 2 + result->channel; - uint8_t mux_position = multiplexer_lookup[lookup_index]; - struct grid_ui_element* ele = &elements[mux_position]; + uint16_t raw = result->value; + uint16_t inverted = GRID_ADC_INVERT_COND(raw, element_index, element_invert_bm); - if (!grid_asc_process(&asc_state[lookup_index], result->value, &result->value)) { + uint16_t processed; + if (!grid_asc_process(asc_state, element_index, inverted, &processed)) { return; } - if (mux_position < 4) { - - } else if (mux_position < 8) { - - grid_ui_potmeter_store_input(ele, mux_position, &ui_potmeter_state[mux_position - 4], result->value, 12); - } + grid_ui_potmeter_store_input(&grid_ui_state, element_index, processed); } -void IRAM_ATTR ef44_process_encoder(void* dma_buf) { - - static DRAM_ATTR uint8_t encoder_lookup[GRID_MODULE_EF44_ENC_NUM] = {2, 3, 0, 1}; +void IRAM_ATTR ef44_process_encoder(struct grid_encoder_result* result) { - // Skip hwcfg byte - uint8_t* bytes = &((uint8_t*)dma_buf)[1]; + static DRAM_ATTR uint8_t encoder_lookup[GRID_MODULE_EF44_ENCODER_COUNT] = {2, 3, 0, 1}; - for (uint8_t j = 0; j < GRID_MODULE_EF44_ENC_NUM; ++j) { + for (uint8_t i = 0; i < GRID_MODULE_EF44_ENCODER_COUNT; ++i) { - uint8_t value = (bytes[j / 2] >> (4 * (j % 2))) & 0x0F; - uint8_t idx = encoder_lookup[j]; - struct grid_ui_element* ele = &elements[idx]; + assert(i / 2 < result->length); + uint8_t nibble = GRID_UI_ENCODER_NIBBLE_FROM_BUFFER(result->data, i); + uint8_t element_index = encoder_lookup[i]; - grid_ui_encoder_store_input(ele, &ui_encoder_state[idx], value); - - uint8_t button_value = value & 0b00000100; - - grid_ui_button_store_input(ele, &ui_button_state[idx], button_value, 1); + struct grid_ui_encoder_sample sample = GRID_UI_ENCODER_SAMPLE_FROM_NIBBLE(nibble); + grid_ui_encoder_store_input(&grid_ui_state, element_index, sample); } } void grid_esp32_module_ef44_init(struct grid_sys_model* sys, struct grid_ui_model* ui, struct grid_esp32_adc_model* adc, struct grid_esp32_encoder_model* enc, struct grid_config_model* conf, struct grid_cal_model* cal) { - ui_button_state = grid_platform_allocate_volatile(GRID_MODULE_EF44_BUT_NUM * sizeof(struct grid_ui_button_state)); - ui_encoder_state = grid_platform_allocate_volatile(GRID_MODULE_EF44_ENC_NUM * sizeof(struct grid_ui_encoder_state)); - ui_potmeter_state = grid_platform_allocate_volatile(GRID_MODULE_EF44_POT_NUM * sizeof(struct grid_ui_potmeter_state)); - asc_state = grid_platform_allocate_volatile(16 * sizeof(struct grid_asc)); - memset(ui_button_state, 0, GRID_MODULE_EF44_BUT_NUM * sizeof(struct grid_ui_button_state)); - memset(ui_encoder_state, 0, GRID_MODULE_EF44_ENC_NUM * sizeof(struct grid_ui_encoder_state)); - memset(ui_potmeter_state, 0, GRID_MODULE_EF44_POT_NUM * sizeof(struct grid_ui_potmeter_state)); - memset(asc_state, 0, 16 * sizeof(struct grid_asc)); - - for (int i = 0; i < GRID_MODULE_EF44_BUT_NUM; ++i) { - grid_ui_button_state_init(&ui_button_state[i], 1, 0.5, 0.2); - } + asc_state = grid_platform_allocate_volatile(8 * sizeof(struct grid_asc)); + memset(asc_state, 0, 8 * sizeof(struct grid_asc)); - for (int i = 0; i < GRID_MODULE_EF44_POT_NUM; ++i) { - grid_ui_potmeter_state_init(&ui_potmeter_state[i], 12, 128, 2192); + // Potmeters are elements 4-7 + for (int i = 0; i < GRID_MODULE_EF44_POTMETER_COUNT; ++i) { + grid_ui_potmeter_state_init(ui, GRID_MODULE_EF44_ENCODER_COUNT + i, GRID_AIN_INTERNAL_RESOLUTION, GRID_POTMETER_DEADZONE, GRID_POTMETER_CENTER); } - grid_asc_array_set_factors(asc_state, 16, 0, 16, 16); - - elements = grid_ui_model_get_elements(ui); + grid_asc_array_set_factors(asc_state, 8, 4, 4, 16); grid_config_init(conf, cal); - grid_cal_init(cal, ui->element_list_length, 12); + grid_cal_init(cal, ui->element_list_length, GRID_AIN_INTERNAL_RESOLUTION); - for (int i = 4; i < 8; ++i) { - assert(grid_cal_set(cal, i, GRID_CAL_LIMITS, &ui_potmeter_state[i - 4].limits) == 0); + // Potmeters are elements 4-7 + for (int i = GRID_MODULE_EF44_ENCODER_COUNT; i < GRID_MODULE_EF44_ENCODER_COUNT + GRID_MODULE_EF44_POTMETER_COUNT; ++i) { + struct grid_ui_element* ele = &ui->element_list[i]; + struct grid_ui_potmeter_state* state = (struct grid_ui_potmeter_state*)ele->primary_state; + assert(grid_cal_set(cal, i, GRID_CAL_LIMITS, &state->limits) == 0); } while (grid_ui_bulk_conf_init(ui, GRID_UI_BULK_CONFREAD_PROGRESS, 0, NULL)) { @@ -125,15 +107,18 @@ void grid_esp32_module_ef44_init(struct grid_sys_model* sys, struct grid_ui_mode vTaskDelay(1); } - grid_esp32_encoder_init(enc, 1, ef44_process_encoder); + uint8_t transfer_length = 1 + GRID_MODULE_EF44_ENCODER_COUNT / 2; + // I2S clock rate chosen so callback fires at 2000 Hz: rate = 2000 * 32bit * 4slots + uint32_t clock_rate = 2000 * I2S_DATA_BIT_WIDTH_32BIT * 4; + grid_esp32_encoder_init(enc, transfer_length, clock_rate, ef44_process_encoder); uint8_t detent = grid_hwcfg_module_encoder_is_detent(&grid_sys_state); int8_t direction = grid_hwcfg_module_encoder_dir(sys); - for (uint8_t i = 0; i < GRID_MODULE_EF44_ENC_NUM; i++) { - grid_ui_encoder_state_init(&ui_encoder_state[i], detent, direction); + // Encoders are elements 0-3 + for (uint8_t i = 0; i < GRID_MODULE_EF44_ENCODER_COUNT; i++) { + grid_ui_encoder_state_init(ui, i, detent, direction, 1, 0.5, 0.2); } - grid_esp32_adc_init(adc, ef44_process_analog); - grid_esp32_adc_mux_init(adc, 2); uint8_t mux_dependent = !grid_hwcfg_module_is_rev_h(sys); - grid_esp32_adc_start(adc, mux_dependent); + grid_esp32_adc_init(adc, 0b00000011, mux_dependent, ef44_process_analog); + grid_esp32_adc_start(adc); } diff --git a/grid_esp/components/grid_esp32_module_en16/grid_esp32_module_en16.c b/grid_esp/components/grid_esp32_module_en16/grid_esp32_module_en16.c index 2107fa556..82daa779b 100644 --- a/grid_esp/components/grid_esp32_module_en16/grid_esp32_module_en16.c +++ b/grid_esp/components/grid_esp32_module_en16/grid_esp32_module_en16.c @@ -22,52 +22,33 @@ // static const char* TAG = "module_en16"; -#define GRID_MODULE_EN16_BUT_NUM 16 +#define GRID_MODULE_EN16_BUTTON_COUNT 16 -#define GRID_MODULE_EN16_ENC_NUM 16 +#define GRID_MODULE_EN16_ENCODER_COUNT 16 -static struct grid_ui_button_state* DRAM_ATTR ui_button_state = NULL; -static struct grid_ui_encoder_state* DRAM_ATTR ui_encoder_state = NULL; -static struct grid_ui_element* DRAM_ATTR elements = NULL; +void IRAM_ATTR en16_process_encoder(struct grid_encoder_result* result) { -void IRAM_ATTR en16_process_encoder(void* dma_buf) { + static DRAM_ATTR uint8_t encoder_lookup[GRID_MODULE_EN16_ENCODER_COUNT] = {14, 15, 10, 11, 6, 7, 2, 3, 12, 13, 8, 9, 4, 5, 0, 1}; - static DRAM_ATTR uint8_t encoder_lookup[GRID_MODULE_EN16_ENC_NUM] = {14, 15, 10, 11, 6, 7, 2, 3, 12, 13, 8, 9, 4, 5, 0, 1}; + for (uint8_t i = 0; i < GRID_MODULE_EN16_ENCODER_COUNT; ++i) { - // Skip hwcfg byte - uint8_t* bytes = &((uint8_t*)dma_buf)[1]; + uint8_t nibble = GRID_UI_ENCODER_NIBBLE_FROM_BUFFER(result->data, i); + uint8_t element_index = encoder_lookup[i]; - for (uint8_t j = 0; j < GRID_MODULE_EN16_ENC_NUM; ++j) { - - uint8_t value = (bytes[j / 2] >> (4 * (j % 2))) & 0x0F; - uint8_t idx = encoder_lookup[j]; - struct grid_ui_element* ele = &elements[idx]; - - grid_ui_encoder_store_input(ele, &ui_encoder_state[idx], value); - - uint8_t button_value = value & 0b00000100; - - grid_ui_button_store_input(ele, &ui_button_state[idx], button_value, 1); + struct grid_ui_encoder_sample sample = GRID_UI_ENCODER_SAMPLE_FROM_NIBBLE(nibble); + grid_ui_encoder_store_input(&grid_ui_state, element_index, sample); } } void grid_esp32_module_en16_init(struct grid_sys_model* sys, struct grid_ui_model* ui, struct grid_esp32_encoder_model* enc) { - ui_button_state = grid_platform_allocate_volatile(GRID_MODULE_EN16_BUT_NUM * sizeof(struct grid_ui_button_state)); - ui_encoder_state = grid_platform_allocate_volatile(GRID_MODULE_EN16_ENC_NUM * sizeof(struct grid_ui_encoder_state)); - memset(ui_button_state, 0, GRID_MODULE_EN16_BUT_NUM * sizeof(struct grid_ui_button_state)); - memset(ui_encoder_state, 0, GRID_MODULE_EN16_ENC_NUM * sizeof(struct grid_ui_encoder_state)); - - for (int i = 0; i < GRID_MODULE_EN16_BUT_NUM; ++i) { - grid_ui_button_state_init(&ui_button_state[i], 1, 0.5, 0.2); - } - - elements = grid_ui_model_get_elements(ui); - - grid_esp32_encoder_init(enc, 1, en16_process_encoder); + uint8_t transfer_length = 1 + GRID_MODULE_EN16_ENCODER_COUNT / 2; + // I2S clock rate chosen so callback fires at 2000 Hz: rate = 2000 * 32bit * 4slots + uint32_t clock_rate = 2000 * I2S_DATA_BIT_WIDTH_32BIT * 4; + grid_esp32_encoder_init(enc, transfer_length, clock_rate, en16_process_encoder); uint8_t detent = grid_hwcfg_module_encoder_is_detent(&grid_sys_state); int8_t direction = grid_hwcfg_module_encoder_dir(sys); - for (uint8_t i = 0; i < GRID_MODULE_EN16_ENC_NUM; i++) { - grid_ui_encoder_state_init(&ui_encoder_state[i], detent, direction); + for (uint8_t i = 0; i < GRID_MODULE_EN16_ENCODER_COUNT; i++) { + grid_ui_encoder_state_init(ui, i, detent, direction, 1, 0.5, 0.2); } } diff --git a/grid_esp/components/grid_esp32_module_pbf4/grid_esp32_module_pbf4.c b/grid_esp/components/grid_esp32_module_pbf4/grid_esp32_module_pbf4.c index 99d9f09bf..168f888e9 100644 --- a/grid_esp/components/grid_esp32_module_pbf4/grid_esp32_module_pbf4.c +++ b/grid_esp/components/grid_esp32_module_pbf4/grid_esp32_module_pbf4.c @@ -8,6 +8,7 @@ #include +#include "grid_ain.h" #include "grid_asc.h" #include "grid_cal.h" #include "grid_config.h" @@ -25,86 +26,85 @@ // static const char* TAG = "module_pbf4"; -#define GRID_MODULE_PBF4_BUT_NUM 4 +#define GRID_MODULE_PBF4_BUTTON_COUNT 4 -#define GRID_MODULE_PBF4_POT_NUM 8 +#define GRID_MODULE_PBF4_POTMETER_COUNT 8 -static struct grid_ui_button_state* DRAM_ATTR ui_button_state = NULL; -static struct grid_ui_potmeter_state* DRAM_ATTR ui_potmeter_state = NULL; static struct grid_asc* DRAM_ATTR asc_state = NULL; -static struct grid_ui_element* DRAM_ATTR elements = NULL; -void IRAM_ATTR pbf4_process_analog(void* user) { +#define X GRID_MUX_UNUSED +static DRAM_ATTR const uint8_t mux_element_lookup[2][8] = { + {0, 1, 4, 5, X, X, 8, 9}, + {2, 3, 6, 7, X, X, 10, 11}, +}; +#undef X +static DRAM_ATTR uint16_t element_invert_bm = 0b0000000000001111; - static DRAM_ATTR const uint8_t multiplexer_lookup[16] = {2, 0, 3, 1, 6, 4, 7, 5, -1, -1, -1, -1, 10, 8, 11, 9}; - static DRAM_ATTR const uint8_t invert_result_lookup[16] = {1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; +void IRAM_ATTR pbf4_process_analog(struct grid_adc_result* result) { - assert(user); + assert(result); - struct grid_esp32_adc_result* result = (struct grid_esp32_adc_result*)user; + uint8_t element_index = mux_element_lookup[result->channel][result->mux_state]; - uint8_t lookup_index = result->mux_state * 2 + result->channel; - uint8_t mux_position = multiplexer_lookup[lookup_index]; - struct grid_ui_element* ele = &elements[mux_position]; + assert(element_index != GRID_MUX_UNUSED); - if (invert_result_lookup[lookup_index]) { - result->value = 4095 - result->value; - } + uint16_t raw = result->value; + uint16_t inverted = GRID_ADC_INVERT_COND(raw, element_index, element_invert_bm); - if (!grid_asc_process(&asc_state[lookup_index], result->value, &result->value)) { + uint16_t processed; + if (!grid_asc_process(asc_state, element_index, inverted, &processed)) { return; } - if (mux_position < 8) { - - grid_ui_potmeter_store_input(ele, mux_position, &ui_potmeter_state[mux_position], result->value, 12); - - } else if (mux_position < 12) { - - grid_ui_button_store_input(ele, &ui_button_state[mux_position - 8], result->value, 12); + if (element_index < GRID_MODULE_PBF4_POTMETER_COUNT) { + grid_ui_potmeter_store_input(&grid_ui_state, element_index, processed); + } else { + grid_ui_button_store_input(&grid_ui_state, element_index, processed); } } void grid_esp32_module_pbf4_init(struct grid_sys_model* sys, struct grid_ui_model* ui, struct grid_esp32_adc_model* adc, struct grid_config_model* conf, struct grid_cal_model* cal) { - ui_button_state = grid_platform_allocate_volatile(GRID_MODULE_PBF4_BUT_NUM * sizeof(struct grid_ui_button_state)); - ui_potmeter_state = grid_platform_allocate_volatile(GRID_MODULE_PBF4_POT_NUM * sizeof(struct grid_ui_potmeter_state)); - asc_state = grid_platform_allocate_volatile(16 * sizeof(struct grid_asc)); - memset(ui_button_state, 0, GRID_MODULE_PBF4_BUT_NUM * sizeof(struct grid_ui_button_state)); - memset(ui_potmeter_state, 0, GRID_MODULE_PBF4_POT_NUM * sizeof(struct grid_ui_potmeter_state)); - memset(asc_state, 0, 16 * sizeof(struct grid_asc)); + asc_state = grid_platform_allocate_volatile(12 * sizeof(struct grid_asc)); + memset(asc_state, 0, 12 * sizeof(struct grid_asc)); - for (int i = 0; i < GRID_MODULE_PBF4_BUT_NUM; ++i) { - grid_ui_button_state_init(&ui_button_state[i], 12, 0.5, 0.2); + // Buttons are elements 8-11 + for (int i = 0; i < GRID_MODULE_PBF4_BUTTON_COUNT; ++i) { + grid_ui_button_state_init(ui, GRID_MODULE_PBF4_POTMETER_COUNT + i, GRID_AIN_INTERNAL_RESOLUTION, 0.5, 0.2); } - for (int i = 0; i < GRID_MODULE_PBF4_POT_NUM; ++i) { - grid_ui_potmeter_state_init(&ui_potmeter_state[i], 12, 128, 2192); + // Potmeters are elements 0-7 + for (int i = 0; i < GRID_MODULE_PBF4_POTMETER_COUNT; ++i) { + grid_ui_potmeter_state_init(ui, i, GRID_AIN_INTERNAL_RESOLUTION, GRID_POTMETER_DEADZONE, GRID_POTMETER_CENTER); } - grid_asc_array_set_factors(asc_state, 16, 0, 16, 8); + grid_asc_array_set_factors(asc_state, 12, 0, 8, 8); if (grid_hwcfg_module_is_rev_h(sys)) { - grid_asc_array_set_factors(asc_state, 16, 12, 4, 1); + grid_asc_array_set_factors(asc_state, 12, 8, 4, 1); } - elements = grid_ui_model_get_elements(ui); - grid_config_init(conf, cal); grid_cal_init(cal, ui->element_list_length, 12); - for (int i = 0; i < 4; ++i) { - assert(grid_cal_set(cal, i, GRID_CAL_CENTER, &ui_potmeter_state[i].center) == 0); - assert(grid_cal_set(cal, i, GRID_CAL_DETENT, &ui_potmeter_state[i].detent) == 0); - } - - for (int i = 0; i < 8; ++i) { - assert(grid_cal_set(cal, i, GRID_CAL_LIMITS, &ui_potmeter_state[i].limits) == 0); + // Potmeter calibration (elements 0-7, first 4 have center detent) + for (int i = 0; i < GRID_MODULE_PBF4_POTMETER_COUNT; ++i) { + struct grid_ui_element* ele = &ui->element_list[i]; + struct grid_ui_potmeter_state* state = (struct grid_ui_potmeter_state*)ele->primary_state; + assert(grid_cal_set(cal, i, GRID_CAL_LIMITS, &state->limits) == 0); + if (i < 4) { + assert(grid_cal_set(cal, i, GRID_CAL_CENTER, &state->center) == 0); + assert(grid_cal_set(cal, i, GRID_CAL_DETENT, &state->detent) == 0); + } } + // Button calibration (elements 8-11, rev_h only) if (grid_hwcfg_module_is_rev_h(sys)) { - for (int i = 8; i < 12; ++i) { - assert(grid_cal_set(cal, i, GRID_CAL_LIMITS, &ui_button_state[i - 8].limits) == 0); + for (int i = 0; i < GRID_MODULE_PBF4_BUTTON_COUNT; ++i) { + uint8_t element_index = GRID_MODULE_PBF4_POTMETER_COUNT + i; + struct grid_ui_element* ele = &ui->element_list[element_index]; + struct grid_ui_button_state* state = (struct grid_ui_button_state*)ele->primary_state; + assert(grid_cal_set(cal, element_index, GRID_CAL_LIMITS, &state->limits) == 0); } } @@ -116,8 +116,7 @@ void grid_esp32_module_pbf4_init(struct grid_sys_model* sys, struct grid_ui_mode vTaskDelay(1); } - grid_esp32_adc_init(adc, pbf4_process_analog); - grid_esp32_adc_mux_init(adc, 8); uint8_t mux_dependent = !grid_hwcfg_module_is_rev_h(sys); - grid_esp32_adc_start(adc, mux_dependent); + grid_esp32_adc_init(adc, 0b11001111, mux_dependent, pbf4_process_analog); + grid_esp32_adc_start(adc); } diff --git a/grid_esp/components/grid_esp32_module_po16/grid_esp32_module_po16.c b/grid_esp/components/grid_esp32_module_po16/grid_esp32_module_po16.c index 17d5d311c..d4dcc9fbf 100644 --- a/grid_esp/components/grid_esp32_module_po16/grid_esp32_module_po16.c +++ b/grid_esp/components/grid_esp32_module_po16/grid_esp32_module_po16.c @@ -8,6 +8,7 @@ #include +#include "grid_ain.h" #include "grid_asc.h" #include "grid_cal.h" #include "grid_config.h" @@ -24,62 +25,54 @@ // static const char* TAG = "module_po16"; -#define GRID_MODULE_PO16_POT_NUM 16 +#define GRID_MODULE_PO16_POTMETER_COUNT 16 -static struct grid_ui_potmeter_state* DRAM_ATTR ui_potmeter_state = NULL; -static struct grid_asc* DRAM_ATTR asc_state = NULL; -static struct grid_ui_element* DRAM_ATTR elements = NULL; - -void IRAM_ATTR po16_process_analog(void* user) { +static DRAM_ATTR const uint8_t mux_element_lookup[2][8] = { + {0, 1, 4, 5, 8, 9, 12, 13}, + {2, 3, 6, 7, 10, 11, 14, 15}, +}; +static DRAM_ATTR uint16_t element_invert_bm = 0b1111111111111111; - static DRAM_ATTR const uint8_t multiplexer_lookup[16] = {2, 0, 3, 1, 6, 4, 7, 5, 10, 8, 11, 9, 14, 12, 15, 13}; - static DRAM_ATTR const uint8_t invert_result_lookup[16] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; +static struct grid_asc* DRAM_ATTR asc_state = NULL; - assert(user); +void IRAM_ATTR po16_process_analog(struct grid_adc_result* result) { - struct grid_esp32_adc_result* result = (struct grid_esp32_adc_result*)user; + assert(result); - uint8_t lookup_index = result->mux_state * 2 + result->channel; - uint8_t mux_position = multiplexer_lookup[lookup_index]; - struct grid_ui_element* ele = &elements[mux_position]; + uint8_t element_index = mux_element_lookup[result->channel][result->mux_state]; - if (invert_result_lookup[lookup_index]) { - result->value = 4095 - result->value; - } + uint16_t raw = result->value; + uint16_t inverted = GRID_ADC_INVERT_COND(raw, element_index, element_invert_bm); - if (!grid_asc_process(&asc_state[lookup_index], result->value, &result->value)) { + uint16_t processed; + if (!grid_asc_process(asc_state, element_index, inverted, &processed)) { return; } - if (mux_position < 16) { - - grid_ui_potmeter_store_input(ele, mux_position, &ui_potmeter_state[mux_position], result->value, 12); - } + grid_ui_potmeter_store_input(&grid_ui_state, element_index, processed); } void grid_esp32_module_po16_init(struct grid_sys_model* sys, struct grid_ui_model* ui, struct grid_esp32_adc_model* adc, struct grid_config_model* conf, struct grid_cal_model* cal) { - ui_potmeter_state = grid_platform_allocate_volatile(GRID_MODULE_PO16_POT_NUM * sizeof(struct grid_ui_potmeter_state)); asc_state = grid_platform_allocate_volatile(16 * sizeof(struct grid_asc)); - memset(ui_potmeter_state, 0, GRID_MODULE_PO16_POT_NUM * sizeof(struct grid_ui_potmeter_state)); memset(asc_state, 0, 16 * sizeof(struct grid_asc)); - for (int i = 0; i < GRID_MODULE_PO16_POT_NUM; ++i) { - grid_ui_potmeter_state_init(&ui_potmeter_state[i], 12, 128, 2192); + for (int i = 0; i < GRID_MODULE_PO16_POTMETER_COUNT; ++i) { + grid_ui_potmeter_state_init(ui, i, GRID_AIN_INTERNAL_RESOLUTION, GRID_POTMETER_DEADZONE, GRID_POTMETER_CENTER); } grid_asc_array_set_factors(asc_state, 16, 0, 16, 8); - elements = grid_ui_model_get_elements(ui); - grid_config_init(conf, cal); - grid_cal_init(cal, ui->element_list_length, 12); + grid_cal_init(cal, ui->element_list_length, GRID_AIN_INTERNAL_RESOLUTION); - for (int i = 0; i < 16; ++i) { - assert(grid_cal_set(cal, i, GRID_CAL_LIMITS, &ui_potmeter_state[i].limits) == 0); - assert(grid_cal_set(cal, i, GRID_CAL_CENTER, &ui_potmeter_state[i].center) == 0); - assert(grid_cal_set(cal, i, GRID_CAL_DETENT, &ui_potmeter_state[i].detent) == 0); + for (int i = 0; i < GRID_MODULE_PO16_POTMETER_COUNT; ++i) { + struct grid_ui_element* ele = &ui->element_list[i]; + struct grid_ui_potmeter_state* state = (struct grid_ui_potmeter_state*)ele->primary_state; + assert(grid_cal_set(cal, i, GRID_CAL_LIMITS, &state->limits) == 0); + assert(grid_cal_set(cal, i, GRID_CAL_CENTER, &state->center) == 0); + assert(grid_cal_set(cal, i, GRID_CAL_DETENT, &state->detent) == 0); } while (grid_ui_bulk_conf_init(ui, GRID_UI_BULK_CONFREAD_PROGRESS, 0, NULL)) { @@ -90,8 +83,7 @@ void grid_esp32_module_po16_init(struct grid_sys_model* sys, struct grid_ui_mode vTaskDelay(1); } - grid_esp32_adc_init(adc, po16_process_analog); - grid_esp32_adc_mux_init(adc, 8); uint8_t mux_dependent = !grid_hwcfg_module_is_rev_h(sys); - grid_esp32_adc_start(adc, mux_dependent); + grid_esp32_adc_init(adc, 0b11111111, mux_dependent, po16_process_analog); + grid_esp32_adc_start(adc); } diff --git a/grid_esp/components/grid_esp32_module_tek1/grid_esp32_module_tek1.c b/grid_esp/components/grid_esp32_module_tek1/grid_esp32_module_tek1.c deleted file mode 100644 index 30c1bb75a..000000000 --- a/grid_esp/components/grid_esp32_module_tek1/grid_esp32_module_tek1.c +++ /dev/null @@ -1,353 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "grid_esp32_module_tek1.h" - -#include - -#include "grid_ain.h" -#include "grid_asc.h" -#include "grid_cal.h" -#include "grid_config.h" -#include "grid_module.h" -#include "grid_platform.h" -#include "grid_sys.h" -#include "grid_ui.h" - -#include "grid_ui_button.h" -#include "grid_ui_endless.h" -#include "grid_ui_lcd.h" -#include "grid_ui_system.h" - -#include "grid_esp32_lcd.h" -#include "grid_font.h" -#include "grid_gui.h" - -#include "grid_lua_api.h" - -#include "grid_esp32_adc.h" -#include "grid_esp32_encoder.h" - -// static const char* TAG = "module_tek1"; - -static DRAM_ATTR uint8_t is_vsn_rev_h_8bit_hwcfg = 0; - -#define GRID_MODULE_TEK1_POT_NUM 2 - -#define GRID_MODULE_TEK1_BUT_NUM 17 - -#define GRID_MODULE_TEK1_SMOL_BUT_NUM 8 - -static struct grid_ui_button_state* DRAM_ATTR ui_button_state = NULL; -static struct grid_ui_endless_state* DRAM_ATTR new_endless_state = NULL; -static struct grid_ui_endless_state* DRAM_ATTR old_endless_state = NULL; -static struct grid_asc* DRAM_ATTR asc_state = NULL; -static struct grid_ui_element* DRAM_ATTR elements = NULL; - -void IRAM_ATTR vsn1l_process_analog(void* user) { - - static DRAM_ATTR const uint8_t multiplexer_lookup[16] = {8, 9, 8, 10, 8, 11, -1, 12, 2, 0, 3, 1, 6, 4, 7, 5}; - - assert(user); - - struct grid_esp32_adc_result* result = (struct grid_esp32_adc_result*)user; - - uint8_t lookup_index = result->mux_state * 2 + result->channel; - uint8_t mux_position = multiplexer_lookup[lookup_index]; - struct grid_ui_element* ele = &elements[mux_position]; - - if (!grid_asc_process(&asc_state[lookup_index], result->value, &result->value)) { - return; - } - - if (mux_position < 8) { - - grid_ui_button_store_input(ele, &ui_button_state[mux_position], result->value, 12); - - } else if (mux_position < 9) { - - switch (lookup_index) { - case 0: { - - new_endless_state[0].phase_a = result->value; - } break; - case 2: { - - new_endless_state[0].phase_b = result->value; - } break; - case 4: { - - new_endless_state[0].button_value = result->value; - grid_ui_button_store_input(ele, &ui_button_state[mux_position], result->value, 12); - grid_ui_endless_store_input(ele, mux_position, 12, &new_endless_state[0], &old_endless_state[0]); - } break; - } - } else if (mux_position < 13 && is_vsn_rev_h_8bit_hwcfg) { - - grid_ui_button_store_input(ele, &ui_button_state[mux_position], result->value, 12); - } -} - -void IRAM_ATTR vsn1l_process_encoder(void* dma_buf) { - - static DRAM_ATTR uint8_t encoder_lookup[GRID_MODULE_TEK1_SMOL_BUT_NUM] = {9, 10, 11, 12, -1, -1, -1, -1}; - - // Skip hwcfg byte - uint8_t* bytes = &((uint8_t*)dma_buf)[1]; - - for (uint8_t i = 0; i < GRID_MODULE_TEK1_SMOL_BUT_NUM; ++i) { - - uint8_t bit = bytes[i / 8] & (1 << i); - uint16_t value = bit * (1 << (12 - 1)); - uint8_t idx = encoder_lookup[i]; - struct grid_ui_element* ele = &elements[idx]; - - if (idx >= 9 && idx < 13) { - - grid_ui_button_store_input(ele, &ui_button_state[idx], value, 12); - } - } -} - -void IRAM_ATTR vsn1r_process_analog(void* user) { - - static DRAM_ATTR const uint8_t multiplexer_lookup[16] = {9, 8, 10, 8, 11, 8, 12, -1, 2, 0, 3, 1, 6, 4, 7, 5}; - - assert(user); - - struct grid_esp32_adc_result* result = (struct grid_esp32_adc_result*)user; - - uint8_t lookup_index = result->mux_state * 2 + result->channel; - uint8_t mux_position = multiplexer_lookup[lookup_index]; - struct grid_ui_element* ele = &elements[mux_position]; - - if (!grid_asc_process(&asc_state[lookup_index], result->value, &result->value)) { - return; - } - - if (mux_position < 8) { - - grid_ui_button_store_input(ele, &ui_button_state[mux_position], result->value, 12); - - } else if (mux_position < 9) { - - switch (lookup_index) { - case 1: { - - new_endless_state[0].phase_a = result->value; - } break; - case 3: { - - new_endless_state[0].phase_b = result->value; - } break; - case 5: { - - new_endless_state[0].button_value = result->value; - grid_ui_button_store_input(ele, &ui_button_state[mux_position], result->value, 12); - grid_ui_endless_store_input(ele, mux_position, 12, &new_endless_state[0], &old_endless_state[0]); - } break; - } - } else if (mux_position < 13 && is_vsn_rev_h_8bit_hwcfg) { - - grid_ui_button_store_input(ele, &ui_button_state[mux_position], result->value, 12); - } -} - -void IRAM_ATTR vsn1r_process_encoder(void* dma_buf) { - - static DRAM_ATTR uint8_t encoder_lookup[GRID_MODULE_TEK1_SMOL_BUT_NUM] = {-1, -1, -1, -1, 9, 10, 11, 12}; - - // Skip hwcfg byte - uint8_t* bytes = &((uint8_t*)dma_buf)[1]; - - for (uint8_t i = 0; i < GRID_MODULE_TEK1_SMOL_BUT_NUM; ++i) { - - uint8_t bit = bytes[i / 8] & (1 << i); - uint16_t value = (bit > 0) * (1 << (12 - 1)); - uint8_t idx = encoder_lookup[i]; - struct grid_ui_element* ele = &elements[idx]; - - if (idx >= 9 && idx < 13) { - - grid_ui_button_store_input(ele, &ui_button_state[idx], value, 12); - } - } -} - -void IRAM_ATTR vsn2_process_analog(void* user) { - - static DRAM_ATTR const uint8_t multiplexer_lookup[16] = {13, 8, 14, 9, 15, 10, 16, 11, 2, 0, 3, 1, 6, 4, 7, 5}; - - assert(user); - - struct grid_esp32_adc_result* result = (struct grid_esp32_adc_result*)user; - - uint8_t lookup_index = result->mux_state * 2 + result->channel; - uint8_t mux_position = multiplexer_lookup[lookup_index]; - struct grid_ui_element* ele = &elements[mux_position]; - - if (!grid_asc_process(&asc_state[lookup_index], result->value, &result->value)) { - return; - } - - if (mux_position < 8) { - - grid_ui_button_store_input(ele, &ui_button_state[mux_position], result->value, 12); - - } else if (is_vsn_rev_h_8bit_hwcfg) { - - grid_ui_button_store_input(ele, &ui_button_state[mux_position], result->value, 12); - } -} - -void IRAM_ATTR vsn2_process_encoder(void* dma_buf) { - - static DRAM_ATTR uint8_t encoder_lookup[GRID_MODULE_TEK1_SMOL_BUT_NUM] = {8, 9, 10, 11, 13, 14, 15, 16}; - - // Skip hwcfg byte - uint8_t* bytes = &((uint8_t*)dma_buf)[1]; - - for (uint8_t i = 0; i < GRID_MODULE_TEK1_SMOL_BUT_NUM; ++i) { - - uint8_t bit = bytes[i / 8] & (1 << i); - uint16_t value = (bit > 0) * (1 << (12 - 1)); - uint8_t idx = encoder_lookup[i]; - struct grid_ui_element* ele = &elements[idx]; - - if ((idx >= 8 && idx < 12) || (idx >= 13 && idx < 17)) { - - grid_ui_button_store_input(ele, &ui_button_state[idx], value, 12); - } - } -} - -void grid_esp32_module_tek1_init(struct grid_sys_model* sys, struct grid_ui_model* ui, struct grid_esp32_adc_model* adc, struct grid_config_model* conf, struct grid_cal_model* cal, - struct grid_esp32_lcd_model* lcds) { - - // Allocate transfer buffer - uint32_t width = LCD_HRES; - uint32_t height = LCD_VRES; - uint32_t lcd_tx_lines = 16; - uint32_t lcd_tx_bytes = height * lcd_tx_lines * COLMOD_RGB888_BYTES; - // uint8_t* xferbuf = malloc(lcd_tx_bytes); - - // Initialize LCD - grid_esp32_lcd_spi_bus_init(lcd_tx_bytes); - - // Wait for the coprocessor to pull the LCD reset pin high - vTaskDelay(pdMS_TO_TICKS(500)); - - bool is_tek1_reva = grid_sys_get_hwcfg(&grid_sys_state) == GRID_MODULE_TEK1_RevA; - - uint32_t hwcfg = grid_sys_get_hwcfg(sys); - - // Initialize LCD panel at index 0 for VSN1L - if (grid_hwcfg_module_is_vsnl(sys) || is_tek1_reva) { - - struct grid_esp32_lcd_model* lcd = &grid_esp32_lcd_states[0]; - struct grid_ui_element* elements = grid_ui_model_get_elements(ui); - - grid_esp32_lcd_panel_init(lcd, &elements[13], 0, GRID_LCD_CLK_SLOW); - grid_esp32_lcd_panel_init(lcd, &elements[13], 0, GRID_LCD_CLK_FAST); - grid_esp32_lcd_panel_reset(lcd); - grid_esp32_lcd_panel_set_frctrl2(lcd, LCD_FRCTRL_40HZ); - } - - // Initialize LCD panel at index 1 for VSN1R - if (grid_hwcfg_module_is_vsnr(sys)) { - - struct grid_esp32_lcd_model* lcd = &grid_esp32_lcd_states[1]; - struct grid_ui_element* elements = grid_ui_model_get_elements(ui); - - grid_esp32_lcd_panel_init(lcd, &elements[13], 1, GRID_LCD_CLK_SLOW); - grid_esp32_lcd_panel_init(lcd, &elements[13], 1, GRID_LCD_CLK_FAST); - grid_esp32_lcd_panel_reset(lcd); - grid_esp32_lcd_panel_set_frctrl2(lcd, LCD_FRCTRL_40HZ); - } - - // Initialize LCD panel at index 0 and 1 for VSN2 - if (grid_hwcfg_module_is_vsn2(sys)) { - - struct grid_esp32_lcd_model* lcds = grid_esp32_lcd_states; - struct grid_ui_element* elements = grid_ui_model_get_elements(ui); - - grid_esp32_lcd_panel_init(&lcds[0], &elements[12], 0, GRID_LCD_CLK_SLOW); - grid_esp32_lcd_panel_init(&lcds[0], &elements[12], 0, GRID_LCD_CLK_FAST); - grid_esp32_lcd_panel_reset(&lcds[0]); - grid_esp32_lcd_panel_set_frctrl2(&lcds[0], LCD_FRCTRL_40HZ); - - grid_esp32_lcd_panel_init(&lcds[1], &elements[17], 1, GRID_LCD_CLK_SLOW); - grid_esp32_lcd_panel_init(&lcds[1], &elements[17], 1, GRID_LCD_CLK_FAST); - grid_esp32_lcd_panel_reset(&lcds[1]); - grid_esp32_lcd_panel_set_frctrl2(&lcds[1], LCD_FRCTRL_40HZ); - } - - // Mark the LCD as ready - grid_esp32_lcd_set_ready(true); - - ui_button_state = grid_platform_allocate_volatile(GRID_MODULE_TEK1_BUT_NUM * sizeof(struct grid_ui_button_state)); - new_endless_state = grid_platform_allocate_volatile(GRID_MODULE_TEK1_POT_NUM * sizeof(struct grid_ui_endless_state)); - old_endless_state = grid_platform_allocate_volatile(GRID_MODULE_TEK1_POT_NUM * sizeof(struct grid_ui_endless_state)); - asc_state = grid_platform_allocate_volatile(16 * sizeof(struct grid_asc)); - memset(ui_button_state, 0, GRID_MODULE_TEK1_BUT_NUM * sizeof(struct grid_ui_button_state)); - memset(new_endless_state, 0, GRID_MODULE_TEK1_POT_NUM * sizeof(struct grid_ui_endless_state)); - memset(old_endless_state, 0, GRID_MODULE_TEK1_POT_NUM * sizeof(struct grid_ui_endless_state)); - memset(asc_state, 0, 16 * sizeof(struct grid_asc)); - - for (int i = 0; i < GRID_MODULE_TEK1_BUT_NUM; ++i) { - grid_ui_button_state_init(&ui_button_state[i], 12, 0.5, 0.2); - } - - grid_asc_array_set_factors(asc_state, 16, 0, 16, 8); - if (grid_hwcfg_module_is_rev_h(sys)) { - grid_asc_array_set_factors(asc_state, 16, 8, 8, 1); - } - - elements = grid_ui_model_get_elements(ui); - - grid_config_init(conf, cal); - - grid_cal_init(cal, ui->element_list_length, 12); - - if (grid_hwcfg_module_is_rev_h(sys)) { - - for (int i = 0; i < 8; ++i) { - assert(grid_cal_set(cal, i, GRID_CAL_LIMITS, &ui_button_state[i].limits) == 0); - } - - while (grid_ui_bulk_conf_init(ui, GRID_UI_BULK_CONFREAD_PROGRESS, 0, NULL)) { - vTaskDelay(1); - } - - while (grid_ui_bulk_is_in_progress(ui, GRID_UI_BULK_CONFREAD_PROGRESS)) { - vTaskDelay(1); - } - } - - grid_process_encoder_t process_encoder = NULL; - - if (grid_hwcfg_module_is_vsnl(sys) || is_tek1_reva) { - process_encoder = vsn1l_process_encoder; - grid_esp32_adc_init(adc, vsn1l_process_analog); - } else if (grid_hwcfg_module_is_vsnr(sys)) { - process_encoder = vsn1r_process_encoder; - grid_esp32_adc_init(adc, vsn1r_process_analog); - } else if (grid_hwcfg_module_is_vsn2(sys)) { - process_encoder = vsn2_process_encoder; - grid_esp32_adc_init(adc, vsn2_process_analog); - } - - is_vsn_rev_h_8bit_hwcfg = 1 - grid_platform_get_hwcfg_bit(16); - - if (!is_vsn_rev_h_8bit_hwcfg) { - - grid_esp32_encoder_init(&grid_esp32_encoder_state, 10, process_encoder); - } - - grid_esp32_adc_mux_init(adc, 8); - uint8_t mux_dependent = !grid_hwcfg_module_is_rev_h(sys); - grid_esp32_adc_start(adc, mux_dependent); -} diff --git a/grid_esp/components/grid_esp32_module_tek2/CMakeLists.txt b/grid_esp/components/grid_esp32_module_tek2/CMakeLists.txt deleted file mode 100644 index 8e9003fb5..000000000 --- a/grid_esp/components/grid_esp32_module_tek2/CMakeLists.txt +++ /dev/null @@ -1,8 +0,0 @@ -idf_component_register( - SRCS - "grid_esp32_module_tek2.c" - INCLUDE_DIRS - "." - REQUIRES - "grid_esp32_adc" "grid_common" -) diff --git a/grid_esp/components/grid_esp32_module_tek2/LICENSE b/grid_esp/components/grid_esp32_module_tek2/LICENSE deleted file mode 100644 index f288702d2..000000000 --- a/grid_esp/components/grid_esp32_module_tek2/LICENSE +++ /dev/null @@ -1,674 +0,0 @@ - GNU GENERAL PUBLIC LICENSE - Version 3, 29 June 2007 - - Copyright (C) 2007 Free Software Foundation, Inc. - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - - Preamble - - The GNU General Public License is a free, copyleft license for -software and other kinds of works. - - The licenses for most software and other practical works are designed -to take away your freedom to share and change the works. By contrast, -the GNU General Public License is intended to guarantee your freedom to -share and change all versions of a program--to make sure it remains free -software for all its users. We, the Free Software Foundation, use the -GNU General Public License for most of our software; it applies also to -any other work released this way by its authors. You can apply it to -your programs, too. - - When we speak of free software, we are referring to freedom, not -price. Our General Public Licenses are designed to make sure that you -have the freedom to distribute copies of free software (and charge for -them if you wish), that you receive source code or can get it if you -want it, that you can change the software or use pieces of it in new -free programs, and that you know you can do these things. - - To protect your rights, we need to prevent others from denying you -these rights or asking you to surrender the rights. Therefore, you have -certain responsibilities if you distribute copies of the software, or if -you modify it: responsibilities to respect the freedom of others. - - For example, if you distribute copies of such a program, whether -gratis or for a fee, you must pass on to the recipients the same -freedoms that you received. You must make sure that they, too, receive -or can get the source code. And you must show them these terms so they -know their rights. - - Developers that use the GNU GPL protect your rights with two steps: -(1) assert copyright on the software, and (2) offer you this License -giving you legal permission to copy, distribute and/or modify it. - - For the developers' and authors' protection, the GPL clearly explains -that there is no warranty for this free software. For both users' and -authors' sake, the GPL requires that modified versions be marked as -changed, so that their problems will not be attributed erroneously to -authors of previous versions. - - Some devices are designed to deny users access to install or run -modified versions of the software inside them, although the manufacturer -can do so. This is fundamentally incompatible with the aim of -protecting users' freedom to change the software. The systematic -pattern of such abuse occurs in the area of products for individuals to -use, which is precisely where it is most unacceptable. Therefore, we -have designed this version of the GPL to prohibit the practice for those -products. If such problems arise substantially in other domains, we -stand ready to extend this provision to those domains in future versions -of the GPL, as needed to protect the freedom of users. - - Finally, every program is threatened constantly by software patents. -States should not allow patents to restrict development and use of -software on general-purpose computers, but in those that do, we wish to -avoid the special danger that patents applied to a free program could -make it effectively proprietary. To prevent this, the GPL assures that -patents cannot be used to render the program non-free. - - The precise terms and conditions for copying, distribution and -modification follow. - - TERMS AND CONDITIONS - - 0. Definitions. - - "This License" refers to version 3 of the GNU General Public License. - - "Copyright" also means copyright-like laws that apply to other kinds of -works, such as semiconductor masks. - - "The Program" refers to any copyrightable work licensed under this -License. Each licensee is addressed as "you". "Licensees" and -"recipients" may be individuals or organizations. - - To "modify" a work means to copy from or adapt all or part of the work -in a fashion requiring copyright permission, other than the making of an -exact copy. The resulting work is called a "modified version" of the -earlier work or a work "based on" the earlier work. - - A "covered work" means either the unmodified Program or a work based -on the Program. - - To "propagate" a work means to do anything with it that, without -permission, would make you directly or secondarily liable for -infringement under applicable copyright law, except executing it on a -computer or modifying a private copy. Propagation includes copying, -distribution (with or without modification), making available to the -public, and in some countries other activities as well. - - To "convey" a work means any kind of propagation that enables other -parties to make or receive copies. Mere interaction with a user through -a computer network, with no transfer of a copy, is not conveying. - - An interactive user interface displays "Appropriate Legal Notices" -to the extent that it includes a convenient and prominently visible -feature that (1) displays an appropriate copyright notice, and (2) -tells the user that there is no warranty for the work (except to the -extent that warranties are provided), that licensees may convey the -work under this License, and how to view a copy of this License. If -the interface presents a list of user commands or options, such as a -menu, a prominent item in the list meets this criterion. - - 1. Source Code. - - The "source code" for a work means the preferred form of the work -for making modifications to it. "Object code" means any non-source -form of a work. - - A "Standard Interface" means an interface that either is an official -standard defined by a recognized standards body, or, in the case of -interfaces specified for a particular programming language, one that -is widely used among developers working in that language. - - The "System Libraries" of an executable work include anything, other -than the work as a whole, that (a) is included in the normal form of -packaging a Major Component, but which is not part of that Major -Component, and (b) serves only to enable use of the work with that -Major Component, or to implement a Standard Interface for which an -implementation is available to the public in source code form. A -"Major Component", in this context, means a major essential component -(kernel, window system, and so on) of the specific operating system -(if any) on which the executable work runs, or a compiler used to -produce the work, or an object code interpreter used to run it. - - The "Corresponding Source" for a work in object code form means all -the source code needed to generate, install, and (for an executable -work) run the object code and to modify the work, including scripts to -control those activities. However, it does not include the work's -System Libraries, or general-purpose tools or generally available free -programs which are used unmodified in performing those activities but -which are not part of the work. For example, Corresponding Source -includes interface definition files associated with source files for -the work, and the source code for shared libraries and dynamically -linked subprograms that the work is specifically designed to require, -such as by intimate data communication or control flow between those -subprograms and other parts of the work. - - The Corresponding Source need not include anything that users -can regenerate automatically from other parts of the Corresponding -Source. - - The Corresponding Source for a work in source code form is that -same work. - - 2. Basic Permissions. - - All rights granted under this License are granted for the term of -copyright on the Program, and are irrevocable provided the stated -conditions are met. This License explicitly affirms your unlimited -permission to run the unmodified Program. The output from running a -covered work is covered by this License only if the output, given its -content, constitutes a covered work. This License acknowledges your -rights of fair use or other equivalent, as provided by copyright law. - - You may make, run and propagate covered works that you do not -convey, without conditions so long as your license otherwise remains -in force. You may convey covered works to others for the sole purpose -of having them make modifications exclusively for you, or provide you -with facilities for running those works, provided that you comply with -the terms of this License in conveying all material for which you do -not control copyright. Those thus making or running the covered works -for you must do so exclusively on your behalf, under your direction -and control, on terms that prohibit them from making any copies of -your copyrighted material outside their relationship with you. - - Conveying under any other circumstances is permitted solely under -the conditions stated below. Sublicensing is not allowed; section 10 -makes it unnecessary. - - 3. Protecting Users' Legal Rights From Anti-Circumvention Law. - - No covered work shall be deemed part of an effective technological -measure under any applicable law fulfilling obligations under article -11 of the WIPO copyright treaty adopted on 20 December 1996, or -similar laws prohibiting or restricting circumvention of such -measures. - - When you convey a covered work, you waive any legal power to forbid -circumvention of technological measures to the extent such circumvention -is effected by exercising rights under this License with respect to -the covered work, and you disclaim any intention to limit operation or -modification of the work as a means of enforcing, against the work's -users, your or third parties' legal rights to forbid circumvention of -technological measures. - - 4. Conveying Verbatim Copies. - - You may convey verbatim copies of the Program's source code as you -receive it, in any medium, provided that you conspicuously and -appropriately publish on each copy an appropriate copyright notice; -keep intact all notices stating that this License and any -non-permissive terms added in accord with section 7 apply to the code; -keep intact all notices of the absence of any warranty; and give all -recipients a copy of this License along with the Program. - - You may charge any price or no price for each copy that you convey, -and you may offer support or warranty protection for a fee. - - 5. Conveying Modified Source Versions. - - You may convey a work based on the Program, or the modifications to -produce it from the Program, in the form of source code under the -terms of section 4, provided that you also meet all of these conditions: - - a) The work must carry prominent notices stating that you modified - it, and giving a relevant date. - - b) The work must carry prominent notices stating that it is - released under this License and any conditions added under section - 7. This requirement modifies the requirement in section 4 to - "keep intact all notices". - - c) You must license the entire work, as a whole, under this - License to anyone who comes into possession of a copy. This - License will therefore apply, along with any applicable section 7 - additional terms, to the whole of the work, and all its parts, - regardless of how they are packaged. This License gives no - permission to license the work in any other way, but it does not - invalidate such permission if you have separately received it. - - d) If the work has interactive user interfaces, each must display - Appropriate Legal Notices; however, if the Program has interactive - interfaces that do not display Appropriate Legal Notices, your - work need not make them do so. - - A compilation of a covered work with other separate and independent -works, which are not by their nature extensions of the covered work, -and which are not combined with it such as to form a larger program, -in or on a volume of a storage or distribution medium, is called an -"aggregate" if the compilation and its resulting copyright are not -used to limit the access or legal rights of the compilation's users -beyond what the individual works permit. Inclusion of a covered work -in an aggregate does not cause this License to apply to the other -parts of the aggregate. - - 6. Conveying Non-Source Forms. - - You may convey a covered work in object code form under the terms -of sections 4 and 5, provided that you also convey the -machine-readable Corresponding Source under the terms of this License, -in one of these ways: - - a) Convey the object code in, or embodied in, a physical product - (including a physical distribution medium), accompanied by the - Corresponding Source fixed on a durable physical medium - customarily used for software interchange. - - b) Convey the object code in, or embodied in, a physical product - (including a physical distribution medium), accompanied by a - written offer, valid for at least three years and valid for as - long as you offer spare parts or customer support for that product - model, to give anyone who possesses the object code either (1) a - copy of the Corresponding Source for all the software in the - product that is covered by this License, on a durable physical - medium customarily used for software interchange, for a price no - more than your reasonable cost of physically performing this - conveying of source, or (2) access to copy the - Corresponding Source from a network server at no charge. - - c) Convey individual copies of the object code with a copy of the - written offer to provide the Corresponding Source. This - alternative is allowed only occasionally and noncommercially, and - only if you received the object code with such an offer, in accord - with subsection 6b. - - d) Convey the object code by offering access from a designated - place (gratis or for a charge), and offer equivalent access to the - Corresponding Source in the same way through the same place at no - further charge. You need not require recipients to copy the - Corresponding Source along with the object code. If the place to - copy the object code is a network server, the Corresponding Source - may be on a different server (operated by you or a third party) - that supports equivalent copying facilities, provided you maintain - clear directions next to the object code saying where to find the - Corresponding Source. Regardless of what server hosts the - Corresponding Source, you remain obligated to ensure that it is - available for as long as needed to satisfy these requirements. - - e) Convey the object code using peer-to-peer transmission, provided - you inform other peers where the object code and Corresponding - Source of the work are being offered to the general public at no - charge under subsection 6d. - - A separable portion of the object code, whose source code is excluded -from the Corresponding Source as a System Library, need not be -included in conveying the object code work. - - A "User Product" is either (1) a "consumer product", which means any -tangible personal property which is normally used for personal, family, -or household purposes, or (2) anything designed or sold for incorporation -into a dwelling. In determining whether a product is a consumer product, -doubtful cases shall be resolved in favor of coverage. For a particular -product received by a particular user, "normally used" refers to a -typical or common use of that class of product, regardless of the status -of the particular user or of the way in which the particular user -actually uses, or expects or is expected to use, the product. A product -is a consumer product regardless of whether the product has substantial -commercial, industrial or non-consumer uses, unless such uses represent -the only significant mode of use of the product. - - "Installation Information" for a User Product means any methods, -procedures, authorization keys, or other information required to install -and execute modified versions of a covered work in that User Product from -a modified version of its Corresponding Source. The information must -suffice to ensure that the continued functioning of the modified object -code is in no case prevented or interfered with solely because -modification has been made. - - If you convey an object code work under this section in, or with, or -specifically for use in, a User Product, and the conveying occurs as -part of a transaction in which the right of possession and use of the -User Product is transferred to the recipient in perpetuity or for a -fixed term (regardless of how the transaction is characterized), the -Corresponding Source conveyed under this section must be accompanied -by the Installation Information. But this requirement does not apply -if neither you nor any third party retains the ability to install -modified object code on the User Product (for example, the work has -been installed in ROM). - - The requirement to provide Installation Information does not include a -requirement to continue to provide support service, warranty, or updates -for a work that has been modified or installed by the recipient, or for -the User Product in which it has been modified or installed. Access to a -network may be denied when the modification itself materially and -adversely affects the operation of the network or violates the rules and -protocols for communication across the network. - - Corresponding Source conveyed, and Installation Information provided, -in accord with this section must be in a format that is publicly -documented (and with an implementation available to the public in -source code form), and must require no special password or key for -unpacking, reading or copying. - - 7. Additional Terms. - - "Additional permissions" are terms that supplement the terms of this -License by making exceptions from one or more of its conditions. -Additional permissions that are applicable to the entire Program shall -be treated as though they were included in this License, to the extent -that they are valid under applicable law. If additional permissions -apply only to part of the Program, that part may be used separately -under those permissions, but the entire Program remains governed by -this License without regard to the additional permissions. - - When you convey a copy of a covered work, you may at your option -remove any additional permissions from that copy, or from any part of -it. (Additional permissions may be written to require their own -removal in certain cases when you modify the work.) You may place -additional permissions on material, added by you to a covered work, -for which you have or can give appropriate copyright permission. - - Notwithstanding any other provision of this License, for material you -add to a covered work, you may (if authorized by the copyright holders of -that material) supplement the terms of this License with terms: - - a) Disclaiming warranty or limiting liability differently from the - terms of sections 15 and 16 of this License; or - - b) Requiring preservation of specified reasonable legal notices or - author attributions in that material or in the Appropriate Legal - Notices displayed by works containing it; or - - c) Prohibiting misrepresentation of the origin of that material, or - requiring that modified versions of such material be marked in - reasonable ways as different from the original version; or - - d) Limiting the use for publicity purposes of names of licensors or - authors of the material; or - - e) Declining to grant rights under trademark law for use of some - trade names, trademarks, or service marks; or - - f) Requiring indemnification of licensors and authors of that - material by anyone who conveys the material (or modified versions of - it) with contractual assumptions of liability to the recipient, for - any liability that these contractual assumptions directly impose on - those licensors and authors. - - All other non-permissive additional terms are considered "further -restrictions" within the meaning of section 10. If the Program as you -received it, or any part of it, contains a notice stating that it is -governed by this License along with a term that is a further -restriction, you may remove that term. If a license document contains -a further restriction but permits relicensing or conveying under this -License, you may add to a covered work material governed by the terms -of that license document, provided that the further restriction does -not survive such relicensing or conveying. - - If you add terms to a covered work in accord with this section, you -must place, in the relevant source files, a statement of the -additional terms that apply to those files, or a notice indicating -where to find the applicable terms. - - Additional terms, permissive or non-permissive, may be stated in the -form of a separately written license, or stated as exceptions; -the above requirements apply either way. - - 8. Termination. - - You may not propagate or modify a covered work except as expressly -provided under this License. Any attempt otherwise to propagate or -modify it is void, and will automatically terminate your rights under -this License (including any patent licenses granted under the third -paragraph of section 11). - - However, if you cease all violation of this License, then your -license from a particular copyright holder is reinstated (a) -provisionally, unless and until the copyright holder explicitly and -finally terminates your license, and (b) permanently, if the copyright -holder fails to notify you of the violation by some reasonable means -prior to 60 days after the cessation. - - Moreover, your license from a particular copyright holder is -reinstated permanently if the copyright holder notifies you of the -violation by some reasonable means, this is the first time you have -received notice of violation of this License (for any work) from that -copyright holder, and you cure the violation prior to 30 days after -your receipt of the notice. - - Termination of your rights under this section does not terminate the -licenses of parties who have received copies or rights from you under -this License. If your rights have been terminated and not permanently -reinstated, you do not qualify to receive new licenses for the same -material under section 10. - - 9. Acceptance Not Required for Having Copies. - - You are not required to accept this License in order to receive or -run a copy of the Program. Ancillary propagation of a covered work -occurring solely as a consequence of using peer-to-peer transmission -to receive a copy likewise does not require acceptance. However, -nothing other than this License grants you permission to propagate or -modify any covered work. These actions infringe copyright if you do -not accept this License. Therefore, by modifying or propagating a -covered work, you indicate your acceptance of this License to do so. - - 10. Automatic Licensing of Downstream Recipients. - - Each time you convey a covered work, the recipient automatically -receives a license from the original licensors, to run, modify and -propagate that work, subject to this License. You are not responsible -for enforcing compliance by third parties with this License. - - An "entity transaction" is a transaction transferring control of an -organization, or substantially all assets of one, or subdividing an -organization, or merging organizations. If propagation of a covered -work results from an entity transaction, each party to that -transaction who receives a copy of the work also receives whatever -licenses to the work the party's predecessor in interest had or could -give under the previous paragraph, plus a right to possession of the -Corresponding Source of the work from the predecessor in interest, if -the predecessor has it or can get it with reasonable efforts. - - You may not impose any further restrictions on the exercise of the -rights granted or affirmed under this License. For example, you may -not impose a license fee, royalty, or other charge for exercise of -rights granted under this License, and you may not initiate litigation -(including a cross-claim or counterclaim in a lawsuit) alleging that -any patent claim is infringed by making, using, selling, offering for -sale, or importing the Program or any portion of it. - - 11. Patents. - - A "contributor" is a copyright holder who authorizes use under this -License of the Program or a work on which the Program is based. The -work thus licensed is called the contributor's "contributor version". - - A contributor's "essential patent claims" are all patent claims -owned or controlled by the contributor, whether already acquired or -hereafter acquired, that would be infringed by some manner, permitted -by this License, of making, using, or selling its contributor version, -but do not include claims that would be infringed only as a -consequence of further modification of the contributor version. For -purposes of this definition, "control" includes the right to grant -patent sublicenses in a manner consistent with the requirements of -this License. - - Each contributor grants you a non-exclusive, worldwide, royalty-free -patent license under the contributor's essential patent claims, to -make, use, sell, offer for sale, import and otherwise run, modify and -propagate the contents of its contributor version. - - In the following three paragraphs, a "patent license" is any express -agreement or commitment, however denominated, not to enforce a patent -(such as an express permission to practice a patent or covenant not to -sue for patent infringement). To "grant" such a patent license to a -party means to make such an agreement or commitment not to enforce a -patent against the party. - - If you convey a covered work, knowingly relying on a patent license, -and the Corresponding Source of the work is not available for anyone -to copy, free of charge and under the terms of this License, through a -publicly available network server or other readily accessible means, -then you must either (1) cause the Corresponding Source to be so -available, or (2) arrange to deprive yourself of the benefit of the -patent license for this particular work, or (3) arrange, in a manner -consistent with the requirements of this License, to extend the patent -license to downstream recipients. "Knowingly relying" means you have -actual knowledge that, but for the patent license, your conveying the -covered work in a country, or your recipient's use of the covered work -in a country, would infringe one or more identifiable patents in that -country that you have reason to believe are valid. - - If, pursuant to or in connection with a single transaction or -arrangement, you convey, or propagate by procuring conveyance of, a -covered work, and grant a patent license to some of the parties -receiving the covered work authorizing them to use, propagate, modify -or convey a specific copy of the covered work, then the patent license -you grant is automatically extended to all recipients of the covered -work and works based on it. - - A patent license is "discriminatory" if it does not include within -the scope of its coverage, prohibits the exercise of, or is -conditioned on the non-exercise of one or more of the rights that are -specifically granted under this License. You may not convey a covered -work if you are a party to an arrangement with a third party that is -in the business of distributing software, under which you make payment -to the third party based on the extent of your activity of conveying -the work, and under which the third party grants, to any of the -parties who would receive the covered work from you, a discriminatory -patent license (a) in connection with copies of the covered work -conveyed by you (or copies made from those copies), or (b) primarily -for and in connection with specific products or compilations that -contain the covered work, unless you entered into that arrangement, -or that patent license was granted, prior to 28 March 2007. - - Nothing in this License shall be construed as excluding or limiting -any implied license or other defenses to infringement that may -otherwise be available to you under applicable patent law. - - 12. No Surrender of Others' Freedom. - - If conditions are imposed on you (whether by court order, agreement or -otherwise) that contradict the conditions of this License, they do not -excuse you from the conditions of this License. If you cannot convey a -covered work so as to satisfy simultaneously your obligations under this -License and any other pertinent obligations, then as a consequence you may -not convey it at all. For example, if you agree to terms that obligate you -to collect a royalty for further conveying from those to whom you convey -the Program, the only way you could satisfy both those terms and this -License would be to refrain entirely from conveying the Program. - - 13. Use with the GNU Affero General Public License. - - Notwithstanding any other provision of this License, you have -permission to link or combine any covered work with a work licensed -under version 3 of the GNU Affero General Public License into a single -combined work, and to convey the resulting work. The terms of this -License will continue to apply to the part which is the covered work, -but the special requirements of the GNU Affero General Public License, -section 13, concerning interaction through a network will apply to the -combination as such. - - 14. Revised Versions of this License. - - The Free Software Foundation may publish revised and/or new versions of -the GNU General Public License from time to time. Such new versions will -be similar in spirit to the present version, but may differ in detail to -address new problems or concerns. - - Each version is given a distinguishing version number. If the -Program specifies that a certain numbered version of the GNU General -Public License "or any later version" applies to it, you have the -option of following the terms and conditions either of that numbered -version or of any later version published by the Free Software -Foundation. If the Program does not specify a version number of the -GNU General Public License, you may choose any version ever published -by the Free Software Foundation. - - If the Program specifies that a proxy can decide which future -versions of the GNU General Public License can be used, that proxy's -public statement of acceptance of a version permanently authorizes you -to choose that version for the Program. - - Later license versions may give you additional or different -permissions. However, no additional obligations are imposed on any -author or copyright holder as a result of your choosing to follow a -later version. - - 15. Disclaimer of Warranty. - - THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY -APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT -HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY -OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, -THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM -IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF -ALL NECESSARY SERVICING, REPAIR OR CORRECTION. - - 16. Limitation of Liability. - - IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING -WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS -THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY -GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE -USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF -DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD -PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), -EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF -SUCH DAMAGES. - - 17. Interpretation of Sections 15 and 16. - - If the disclaimer of warranty and limitation of liability provided -above cannot be given local legal effect according to their terms, -reviewing courts shall apply local law that most closely approximates -an absolute waiver of all civil liability in connection with the -Program, unless a warranty or assumption of liability accompanies a -copy of the Program in return for a fee. - - END OF TERMS AND CONDITIONS - - How to Apply These Terms to Your New Programs - - If you develop a new program, and you want it to be of the greatest -possible use to the public, the best way to achieve this is to make it -free software which everyone can redistribute and change under these terms. - - To do so, attach the following notices to the program. It is safest -to attach them to the start of each source file to most effectively -state the exclusion of warranty; and each file should have at least -the "copyright" line and a pointer to where the full notice is found. - - - Copyright (C) - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . - -Also add information on how to contact you by electronic and paper mail. - - If the program does terminal interaction, make it output a short -notice like this when it starts in an interactive mode: - - Copyright (C) - This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. - This is free software, and you are welcome to redistribute it - under certain conditions; type `show c' for details. - -The hypothetical commands `show w' and `show c' should show the appropriate -parts of the General Public License. Of course, your program's commands -might be different; for a GUI interface, you would use an "about box". - - You should also get your employer (if you work as a programmer) or school, -if any, to sign a "copyright disclaimer" for the program, if necessary. -For more information on this, and how to apply and follow the GNU GPL, see -. - - The GNU General Public License does not permit incorporating your program -into proprietary programs. If your program is a subroutine library, you -may consider it more useful to permit linking proprietary applications with -the library. If this is what you want to do, use the GNU Lesser General -Public License instead of this License. But first, please read -. diff --git a/grid_esp/components/grid_esp32_module_tek2/grid_esp32_module_tek2.c b/grid_esp/components/grid_esp32_module_tek2/grid_esp32_module_tek2.c deleted file mode 100644 index 487d500a9..000000000 --- a/grid_esp/components/grid_esp32_module_tek2/grid_esp32_module_tek2.c +++ /dev/null @@ -1,123 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "grid_esp32_module_tek2.h" - -#include - -#include "grid_asc.h" -#include "grid_cal.h" -#include "grid_config.h" -#include "grid_module.h" -#include "grid_platform.h" -#include "grid_sys.h" -#include "grid_ui.h" - -#include "grid_ui_button.h" -#include "grid_ui_endless.h" -#include "grid_ui_system.h" - -#include "grid_esp32_adc.h" - -// static const char* TAG = "module_tek2"; - -#define GRID_MODULE_TEK2_POT_NUM 2 - -#define GRID_MODULE_TEK2_BUT_NUM 10 - -static struct grid_ui_button_state* DRAM_ATTR ui_button_state = NULL; -static struct grid_ui_endless_state* DRAM_ATTR new_endless_state = NULL; -static struct grid_ui_endless_state* DRAM_ATTR old_endless_state = NULL; -static struct grid_asc* DRAM_ATTR asc_state = NULL; -static struct grid_ui_element* DRAM_ATTR elements = NULL; - -void IRAM_ATTR tek2_process_analog(void* user) { - - static DRAM_ATTR const uint8_t multiplexer_lookup[16] = {9, 8, 9, 8, 9, 8, -1, -1, 2, 0, 3, 1, 6, 4, 7, 5}; - - assert(user); - - struct grid_esp32_adc_result* result = (struct grid_esp32_adc_result*)user; - - uint8_t lookup_index = result->mux_state * 2 + result->channel; - uint8_t mux_position = multiplexer_lookup[lookup_index]; - struct grid_ui_element* ele = &elements[mux_position]; - uint8_t endless_index = mux_position % 2; - - if (!grid_asc_process(&asc_state[lookup_index], result->value, &result->value)) { - return; - } - - if (mux_position < 8) { - - grid_ui_button_store_input(ele, &ui_button_state[mux_position], result->value, 12); - } else if (mux_position < 10) { - - switch (lookup_index) { - case 0: - case 1: { - new_endless_state[endless_index].phase_a = result->value; - } break; - case 2: - case 3: { - new_endless_state[endless_index].phase_b = result->value; - } break; - case 4: - case 5: { - new_endless_state[endless_index].button_value = result->value; - grid_ui_button_store_input(ele, &ui_button_state[mux_position], result->value, 12); - grid_ui_endless_store_input(ele, mux_position, 12, &new_endless_state[endless_index], &old_endless_state[endless_index]); - } break; - } - } -} - -void grid_esp32_module_tek2_init(struct grid_sys_model* sys, struct grid_ui_model* ui, struct grid_esp32_adc_model* adc, struct grid_config_model* conf, struct grid_cal_model* cal) { - - ui_button_state = grid_platform_allocate_volatile(GRID_MODULE_TEK2_BUT_NUM * sizeof(struct grid_ui_button_state)); - new_endless_state = grid_platform_allocate_volatile(GRID_MODULE_TEK2_POT_NUM * sizeof(struct grid_ui_endless_state)); - old_endless_state = grid_platform_allocate_volatile(GRID_MODULE_TEK2_POT_NUM * sizeof(struct grid_ui_endless_state)); - asc_state = grid_platform_allocate_volatile(16 * sizeof(struct grid_asc)); - memset(ui_button_state, 0, GRID_MODULE_TEK2_BUT_NUM * sizeof(struct grid_ui_button_state)); - memset(new_endless_state, 0, GRID_MODULE_TEK2_POT_NUM * sizeof(struct grid_ui_endless_state)); - memset(old_endless_state, 0, GRID_MODULE_TEK2_POT_NUM * sizeof(struct grid_ui_endless_state)); - memset(asc_state, 0, 16 * sizeof(struct grid_asc)); - - for (int i = 0; i < GRID_MODULE_TEK2_BUT_NUM; ++i) { - grid_ui_button_state_init(&ui_button_state[i], 12, 0.5, 0.2); - } - - grid_asc_array_set_factors(asc_state, 16, 0, 16, 8); - if (grid_hwcfg_module_is_rev_h(sys)) { - grid_asc_array_set_factors(asc_state, 16, 8, 8, 1); - } - - elements = grid_ui_model_get_elements(ui); - - grid_config_init(conf, cal); - - grid_cal_init(cal, ui->element_list_length, 12); - - if (grid_hwcfg_module_is_rev_h(sys)) { - - for (int i = 0; i < 8; ++i) { - assert(grid_cal_set(cal, i, GRID_CAL_LIMITS, &ui_button_state[i].limits) == 0); - } - - while (grid_ui_bulk_conf_init(ui, GRID_UI_BULK_CONFREAD_PROGRESS, 0, NULL)) { - vTaskDelay(1); - } - - while (grid_ui_bulk_is_in_progress(ui, GRID_UI_BULK_CONFREAD_PROGRESS)) { - vTaskDelay(1); - } - } - - grid_esp32_adc_init(&grid_esp32_adc_state, tek2_process_analog); - grid_esp32_adc_mux_init(&grid_esp32_adc_state, 8); - uint8_t mux_dependent = !grid_hwcfg_module_is_rev_h(sys); - grid_esp32_adc_start(&grid_esp32_adc_state, mux_dependent); -} diff --git a/grid_esp/components/grid_esp32_module_tek2/grid_esp32_module_tek2.h b/grid_esp/components/grid_esp32_module_tek2/grid_esp32_module_tek2.h deleted file mode 100644 index bb18cdbd3..000000000 --- a/grid_esp/components/grid_esp32_module_tek2/grid_esp32_module_tek2.h +++ /dev/null @@ -1,23 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ -#pragma once - -#ifdef __cplusplus -extern "C" { -#endif - -#include "grid_cal.h" -#include "grid_config.h" -#include "grid_sys.h" -#include "grid_ui.h" - -#include "grid_esp32_adc.h" - -void grid_esp32_module_tek2_init(struct grid_sys_model* sys, struct grid_ui_model* ui, struct grid_esp32_adc_model* adc, struct grid_config_model* conf, struct grid_cal_model* cal); - -#ifdef __cplusplus -} -#endif diff --git a/grid_esp/components/grid_esp32_module_tek1/CMakeLists.txt b/grid_esp/components/grid_esp32_module_vsnx/CMakeLists.txt similarity index 83% rename from grid_esp/components/grid_esp32_module_tek1/CMakeLists.txt rename to grid_esp/components/grid_esp32_module_vsnx/CMakeLists.txt index 254f9a6f6..9db9167a0 100644 --- a/grid_esp/components/grid_esp32_module_tek1/CMakeLists.txt +++ b/grid_esp/components/grid_esp32_module_vsnx/CMakeLists.txt @@ -1,6 +1,6 @@ idf_component_register( SRCS - "grid_esp32_module_tek1.c" + "grid_esp32_module_vsnx.c" INCLUDE_DIRS "." REQUIRES diff --git a/grid_esp/components/grid_esp32_module_tek1/LICENSE b/grid_esp/components/grid_esp32_module_vsnx/LICENSE similarity index 100% rename from grid_esp/components/grid_esp32_module_tek1/LICENSE rename to grid_esp/components/grid_esp32_module_vsnx/LICENSE diff --git a/grid_esp/components/grid_esp32_module_vsnx/grid_esp32_module_vsnx.c b/grid_esp/components/grid_esp32_module_vsnx/grid_esp32_module_vsnx.c new file mode 100644 index 000000000..b0021338d --- /dev/null +++ b/grid_esp/components/grid_esp32_module_vsnx/grid_esp32_module_vsnx.c @@ -0,0 +1,225 @@ +/* + * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "grid_esp32_module_vsnx.h" + +#include + +#include "grid_ain.h" +#include "grid_asc.h" +#include "grid_cal.h" +#include "grid_config.h" +#include "grid_module.h" +#include "grid_platform.h" +#include "grid_sys.h" +#include "grid_ui.h" + +#include "grid_ui_button.h" +#include "grid_ui_endless.h" +#include "grid_ui_lcd.h" +#include "grid_ui_system.h" + +#include "grid_esp32_lcd.h" +#include "grid_font.h" +#include "grid_gui.h" + +#include "grid_lua_api.h" + +#include "grid_esp32_adc.h" +#include "grid_esp32_encoder.h" + +// static const char* TAG = "module_vsnx"; + +#define GRID_MODULE_VSNX_BUTTON_COUNT 8 +#define GRID_MODULE_VSNX_MINIBUTTON_COUNT 8 + +static struct grid_asc* DRAM_ATTR asc_state = NULL; +#define GRID_MODULE_VSNX_ENDLESS_MAXCOUNT 2 +static struct grid_ui_endless_sample DRAM_ATTR endless_sample[GRID_MODULE_VSNX_ENDLESS_MAXCOUNT] = {0}; + +#define X GRID_MUX_UNUSED +static DRAM_ATTR const uint8_t vsn1l_mux_element_lookup[2][8] = { + {8, 8, 8, X, 2, 3, 6, 7}, + {X, X, X, X, 0, 1, 4, 5}, +}; +static DRAM_ATTR const uint8_t vsn1r_mux_element_lookup[2][8] = { + {X, X, X, X, 2, 3, 6, 7}, + {8, 8, 8, X, 0, 1, 4, 5}, +}; +static DRAM_ATTR const uint8_t vsn2_mux_element_lookup[2][8] = { + {X, X, X, X, 2, 3, 6, 7}, + {X, X, X, X, 0, 1, 4, 5}, +}; +static DRAM_ATTR const uint8_t tek2_mux_element_lookup[2][8] = { + {8, 8, 8, X, 0, 1, 4, 5}, + {9, 9, 9, X, 2, 3, 6, 7}, +}; +#undef X + +// Pointer to 2D lookup table [2][8], indexed as [channel][mux_state] +static DRAM_ATTR const uint8_t (*vsnx_mux_lookup)[8] = NULL; + +void IRAM_ATTR vsnx_process_analog(struct grid_adc_result* result) { + + assert(result); + + uint8_t element_index = vsnx_mux_lookup[result->channel][result->mux_state]; + + if (element_index == GRID_MUX_UNUSED) { + return; + } + + if (!grid_asc_process(asc_state, element_index, result->value, &result->value)) { + return; + } + + if (element_index < GRID_MODULE_VSNX_BUTTON_COUNT) { + + grid_ui_button_store_input(&grid_ui_state, element_index, result->value); + + } else { + + uint8_t endless_idx = element_index - GRID_MODULE_VSNX_BUTTON_COUNT; + struct grid_ui_endless_sample* sample_ptr = &endless_sample[endless_idx]; + + if (result->mux_state == 0) { + sample_ptr->phase_a = result->value; + } else if (result->mux_state == 1) { + sample_ptr->phase_b = result->value; + } else if (result->mux_state == 2) { + sample_ptr->button_value = result->value; + grid_ui_endless_store_input(&grid_ui_state, element_index, *sample_ptr); + } + } +} + +#define X GRID_MUX_UNUSED +static DRAM_ATTR const uint8_t vsn1l_minibutton_lookup[GRID_MODULE_VSNX_MINIBUTTON_COUNT] = {9, 10, 11, 12, X, X, X, X}; +static DRAM_ATTR const uint8_t vsn1r_minibutton_lookup[GRID_MODULE_VSNX_MINIBUTTON_COUNT] = {X, X, X, X, 9, 10, 11, 12}; +static DRAM_ATTR const uint8_t vsn2_minibutton_lookup[GRID_MODULE_VSNX_MINIBUTTON_COUNT] = {8, 9, 10, 11, 13, 14, 15, 16}; +static DRAM_ATTR const uint8_t tek2_minibutton_lookup[GRID_MODULE_VSNX_MINIBUTTON_COUNT] = {X, X, X, X, X, X, X, X}; +#undef X + +static DRAM_ATTR const uint8_t* vsnx_minibutton_lookup = NULL; + +void IRAM_ATTR vsnx_process_minibutton(struct grid_encoder_result* result) { + + uint8_t minibutton_state_bm = result->data[0]; + + for (uint8_t i = 0; i < GRID_MODULE_VSNX_MINIBUTTON_COUNT; ++i) { + + uint8_t element_index = vsnx_minibutton_lookup[i]; + + if (element_index != GRID_MUX_UNUSED) { + uint8_t bit = (minibutton_state_bm >> i) & 1; + grid_ui_button_store_input(&grid_ui_state, element_index, bit); + } + } +} + +static void init_lcd_panel(struct grid_ui_element* element, uint8_t lcd_index, uint8_t cs_index) { + struct grid_esp32_lcd_model* lcd = &grid_esp32_lcd_states[lcd_index]; + grid_esp32_lcd_panel_init(lcd, element, cs_index, GRID_LCD_CLK_SLOW); + grid_esp32_lcd_panel_init(lcd, element, cs_index, GRID_LCD_CLK_FAST); + grid_esp32_lcd_panel_reset(lcd); + grid_esp32_lcd_panel_set_frctrl2(lcd, LCD_FRCTRL_40HZ); +} + +static void vsnx_lcd_init(struct grid_sys_model* sys, struct grid_ui_model* ui) { + + uint32_t lcd_tx_bytes = LCD_VRES * 16 * COLMOD_RGB888_BYTES; + grid_esp32_lcd_spi_bus_init(lcd_tx_bytes); + + // Wait for the coprocessor to pull the LCD reset pin high + vTaskDelay(pdMS_TO_TICKS(500)); + + struct grid_ui_element* elements = grid_ui_model_get_elements(ui); + + if (grid_hwcfg_module_is_vsnl(sys)) { + init_lcd_panel(&elements[13], 0, 0); + } else if (grid_hwcfg_module_is_vsnr(sys)) { + init_lcd_panel(&elements[13], 1, 1); + } else if (grid_hwcfg_module_is_vsn2(sys)) { + init_lcd_panel(&elements[12], 0, 0); + init_lcd_panel(&elements[17], 1, 1); + } + + grid_esp32_lcd_set_ready(true); +} + +void grid_esp32_module_vsnx_init(struct grid_sys_model* sys, struct grid_ui_model* ui, struct grid_esp32_adc_model* adc, struct grid_config_model* conf, struct grid_cal_model* cal, + struct grid_esp32_lcd_model* lcds) { + + if (!grid_hwcfg_module_is_tek2(sys)) { + vsnx_lcd_init(sys, ui); + } + + asc_state = grid_platform_allocate_volatile(16 * sizeof(struct grid_asc)); + memset(asc_state, 0, 16 * sizeof(struct grid_asc)); + + // Initialize button states for all button and endless elements + for (int i = 0; i < ui->element_list_length - 1; ++i) { // -1 to skip system element + struct grid_ui_element* ele = &ui->element_list[i]; + + if (ele->type == GRID_PARAMETER_ELEMENT_BUTTON) { + uint8_t bit_depth = (i < 8) ? GRID_AIN_INTERNAL_RESOLUTION : 1; + grid_ui_button_state_init(ui, i, bit_depth, 0.5, 0.2); + } else if (ele->type == GRID_PARAMETER_ELEMENT_ENDLESS) { + grid_ui_endless_state_init(ui, i, GRID_AIN_INTERNAL_RESOLUTION, GRID_AIN_INTERNAL_RESOLUTION, 0.5, 0.2); + } + } + + grid_asc_array_set_factors(asc_state, 16, 0, 16, 8); + if (grid_hwcfg_module_is_rev_h(sys)) { + grid_asc_array_set_factors(asc_state, 16, 8, 8, 1); + } + + grid_config_init(conf, cal); + + grid_cal_init(cal, ui->element_list_length, GRID_AIN_INTERNAL_RESOLUTION); + + if (grid_hwcfg_module_is_rev_h(sys)) { + + for (int i = 0; i < GRID_MODULE_VSNX_BUTTON_COUNT; ++i) { + struct grid_ui_element* ele = &ui->element_list[i]; + struct grid_ui_button_state* state = (struct grid_ui_button_state*)ele->primary_state; + assert(grid_cal_set(cal, i, GRID_CAL_LIMITS, &state->limits) == 0); + } + + while (grid_ui_bulk_conf_init(ui, GRID_UI_BULK_CONFREAD_PROGRESS, 0, NULL)) { + vTaskDelay(1); + } + + while (grid_ui_bulk_is_in_progress(ui, GRID_UI_BULK_CONFREAD_PROGRESS)) { + vTaskDelay(1); + } + } + + if (grid_hwcfg_module_is_vsnl(sys)) { + vsnx_mux_lookup = vsn1l_mux_element_lookup; + vsnx_minibutton_lookup = vsn1l_minibutton_lookup; + } else if (grid_hwcfg_module_is_vsnr(sys)) { + vsnx_mux_lookup = vsn1r_mux_element_lookup; + vsnx_minibutton_lookup = vsn1r_minibutton_lookup; + } else if (grid_hwcfg_module_is_vsn2(sys)) { + vsnx_mux_lookup = vsn2_mux_element_lookup; + vsnx_minibutton_lookup = vsn2_minibutton_lookup; + } else if (grid_hwcfg_module_is_tek2(sys)) { + vsnx_mux_lookup = tek2_mux_element_lookup; + vsnx_minibutton_lookup = tek2_minibutton_lookup; + } + + // Encoder driver is used to read minibuttons via shift registers + uint8_t transfer_length = 2; + // I2S clock rate chosen so callback fires at 200 Hz: rate = 200 * 32bit * 4slots + uint32_t clock_rate = 200 * I2S_DATA_BIT_WIDTH_32BIT * 4; + grid_esp32_encoder_init(&grid_esp32_encoder_state, transfer_length, clock_rate, vsnx_process_minibutton); + + uint8_t mux_positions = grid_hwcfg_module_is_tek2(sys) ? 0b11110111 : 0b11111111; + uint8_t mux_dependent = !grid_hwcfg_module_is_rev_h(sys); + grid_esp32_adc_init(adc, mux_positions, mux_dependent, vsnx_process_analog); + grid_esp32_adc_start(adc); +} diff --git a/grid_esp/components/grid_esp32_module_tek1/grid_esp32_module_tek1.h b/grid_esp/components/grid_esp32_module_vsnx/grid_esp32_module_vsnx.h similarity index 89% rename from grid_esp/components/grid_esp32_module_tek1/grid_esp32_module_tek1.h rename to grid_esp/components/grid_esp32_module_vsnx/grid_esp32_module_vsnx.h index 3e20e7b2d..06948a59c 100644 --- a/grid_esp/components/grid_esp32_module_tek1/grid_esp32_module_tek1.h +++ b/grid_esp/components/grid_esp32_module_vsnx/grid_esp32_module_vsnx.h @@ -17,7 +17,7 @@ extern "C" { #include "grid_esp32_adc.h" #include "grid_esp32_lcd.h" -void grid_esp32_module_tek1_init(struct grid_sys_model* sys, struct grid_ui_model* ui, struct grid_esp32_adc_model* adc, struct grid_config_model* conf, struct grid_cal_model* cal, +void grid_esp32_module_vsnx_init(struct grid_sys_model* sys, struct grid_ui_model* ui, struct grid_esp32_adc_model* adc, struct grid_config_model* conf, struct grid_cal_model* cal, struct grid_esp32_lcd_model* lcds); #ifdef __cplusplus diff --git a/grid_esp/main/grid_esp32.c b/grid_esp/main/grid_esp32.c index c47b7c144..e1db50e75 100644 --- a/grid_esp/main/grid_esp32.c +++ b/grid_esp/main/grid_esp32.c @@ -6,6 +6,8 @@ #include "grid_esp32.h" +#include "grid_esp32_adc.h" + static const char* TAG = "grid_esp32"; void vTaskGetRunTimeStats2(char* pcWriteBuffer) { @@ -367,3 +369,11 @@ uint8_t grid_platform_enable_grid_transmitter(uint8_t direction) { void grid_platform_system_reset() { ets_printf("grid_platform_system_reset NOT IMPLEMENTED!!!\r\n"); } uint8_t IRAM_ATTR grid_platform_get_adc_bit_depth() { return 12; } + +void grid_platform_mux_init(uint8_t mux_positions_bm) { grid_esp32_adc_mux_init(&grid_esp32_adc_state, mux_positions_bm); } + +void IRAM_ATTR grid_platform_mux_write(uint8_t index) { + + grid_esp32_adc_state.mux_index = index; + grid_esp32_adc_mux_update(&grid_esp32_adc_state); +} diff --git a/grid_esp/main/grid_fw.c b/grid_esp/main/grid_fw.c index f6d8eb619..871d3f3ad 100644 --- a/grid_esp/main/grid_fw.c +++ b/grid_esp/main/grid_fw.c @@ -45,8 +45,7 @@ #include "grid_esp32_module_en16.h" #include "grid_esp32_module_pbf4.h" #include "grid_esp32_module_po16.h" -#include "grid_esp32_module_tek1.h" -#include "grid_esp32_module_tek2.h" +#include "grid_esp32_module_vsnx.h" #include "pico_firmware.h" #include "grid_esp32_trace.h" @@ -190,19 +189,13 @@ void grid_ui_element_lcd_template_parameter_init_vsn_right(struct grid_ui_templa template_parameter_list[GRID_LUA_FNC_L_SCREEN_HEIGHT_index] = 240; } -void grid_module_tek1_ui_init(struct grid_ain_model* ain, struct grid_led_model* led, struct grid_ui_model* ui, struct grid_sys_model* sys) { - - bool is_tek1_reva = grid_sys_get_hwcfg(&grid_sys_state) == GRID_MODULE_TEK1_RevA; +void grid_module_vsnx_ui_init(struct grid_ain_model* ain, struct grid_led_model* led, struct grid_ui_model* ui, struct grid_sys_model* sys) { // 16 pot, depth of 5, 14bit internal, 7bit result; grid_ain_init(ain, 16, 4); // TODO: 12 ain for TEK2 grid_led_init(led, 13 + 5); // TODO: 18 led for TEK2 - if (is_tek1_reva) { - - // TODO - - } else if (grid_hwcfg_module_is_vsnl(&grid_sys_state)) { + if (grid_hwcfg_module_is_vsnl(&grid_sys_state)) { for (uint8_t i = 0; i < 8; ++i) { grid_led_lookup_alloc_single(led, i, i + 10); @@ -223,7 +216,7 @@ void grid_module_tek1_ui_init(struct grid_ain_model* ain, struct grid_led_model* } } - if (grid_hwcfg_module_is_vsnl(&grid_sys_state) || is_tek1_reva) { + if (grid_hwcfg_module_is_vsnl(&grid_sys_state)) { grid_ui_model_init(ui, 14 + 1); @@ -426,7 +419,7 @@ void app_main(void) { } else if (grid_hwcfg_module_is_tek2(&grid_sys_state)) { grid_module_tek2_ui_init(&grid_ain_state, &grid_led_state, &grid_ui_state); } else if (grid_hwcfg_module_is_vsnx(&grid_sys_state)) { - grid_module_tek1_ui_init(&grid_ain_state, &grid_led_state, &grid_ui_state, &grid_sys_state); + grid_module_vsnx_ui_init(&grid_ain_state, &grid_led_state, &grid_ui_state, &grid_sys_state); } else { ets_printf("UI Init failed: Unknown Module\r\n"); } @@ -537,10 +530,8 @@ void app_main(void) { grid_esp32_module_en16_init(&grid_sys_state, &grid_ui_state, &grid_esp32_encoder_state); } else if (grid_hwcfg_module_is_ef44(&grid_sys_state)) { grid_esp32_module_ef44_init(&grid_sys_state, &grid_ui_state, &grid_esp32_adc_state, &grid_esp32_encoder_state, &grid_config_state, &grid_cal_state); - } else if (grid_hwcfg_module_is_tek2(&grid_sys_state)) { - grid_esp32_module_tek2_init(&grid_sys_state, &grid_ui_state, &grid_esp32_adc_state, &grid_config_state, &grid_cal_state); - } else if (grid_hwcfg_module_is_vsnx(&grid_sys_state)) { - grid_esp32_module_tek1_init(&grid_sys_state, &grid_ui_state, &grid_esp32_adc_state, &grid_config_state, &grid_cal_state, grid_esp32_lcd_states); + } else if (grid_hwcfg_module_is_tek2(&grid_sys_state) || grid_hwcfg_module_is_vsnx(&grid_sys_state)) { + grid_esp32_module_vsnx_init(&grid_sys_state, &grid_ui_state, &grid_esp32_adc_state, &grid_config_state, &grid_cal_state, grid_esp32_lcd_states); } else { ets_printf("Task Init failed: Unknown Module\r\n"); } diff --git a/grid_make/gcc/Makefile b/grid_make/gcc/Makefile index 2b8b0dbc9..0eb08d7cd 100644 --- a/grid_make/gcc/Makefile +++ b/grid_make/gcc/Makefile @@ -54,6 +54,8 @@ grid/d51/grid_d51_nvm.o \ ../grid_common/grid_led.o \ ../grid_common/grid_littlefs.o \ grid/d51/grid_d51_module.o \ +grid/d51/grid_d51_adc.o \ +grid/d51/grid_d51_encoder.o \ grid/grid_d51_module_bu16.o \ grid/grid_d51_module_en16.o \ grid/grid_d51_module_pbf4.o \ @@ -144,6 +146,8 @@ OBJS_AS_ARGS += \ "../grid_common/grid_led.o" \ "../grid_common/grid_littlefs.o" \ "grid/d51/grid_d51_module.o" \ +"grid/d51/grid_d51_adc.o" \ +"grid/d51/grid_d51_encoder.o" \ "grid/grid_d51_module_bu16.o" \ "grid/grid_d51_module_en16.o" \ "grid/grid_d51_module_pbf4.o" \ @@ -250,6 +254,8 @@ DEPS_AS_ARGS += \ "../grid_common/grid_led.d" \ "../grid_common/grid_littlefs.d" \ "grid/d51/grid_d51_module.d" \ +"grid/d51/grid_d51_adc.d" \ +"grid/d51/grid_d51_encoder.d" \ "grid/grid_d51_module_bu16.d" \ "grid/grid_d51_module_en16.d" \ "grid/grid_d51_module_pbf4.d" \ diff --git a/grid_make/grid/d51/grid_d51.c b/grid_make/grid/d51/grid_d51.c index b12d93ddc..f5254f39c 100644 --- a/grid_make/grid/d51/grid_d51.c +++ b/grid_make/grid/d51/grid_d51.c @@ -752,6 +752,22 @@ void grid_platform_system_reset() { NVIC_SystemReset(); } uint8_t grid_platform_get_adc_bit_depth() { return 16; } +void grid_platform_mux_init(uint8_t mux_positions_bm) { + + gpio_set_pin_direction(MUX_A, GPIO_DIRECTION_OUT); + gpio_set_pin_direction(MUX_B, GPIO_DIRECTION_OUT); + gpio_set_pin_direction(MUX_C, GPIO_DIRECTION_OUT); + + grid_d51_adc_mux_init(&grid_d51_adc_state, mux_positions_bm); +} + +void grid_platform_mux_write(uint8_t index) { + + gpio_set_pin_level(MUX_A, (index >> 0) & 1); + gpio_set_pin_level(MUX_B, (index >> 1) & 1); + gpio_set_pin_level(MUX_C, (index >> 2) & 1); +} + static uint64_t micros = 0; void grid_platform_rtc_set_micros(uint64_t mic) { micros = mic; } diff --git a/grid_make/grid/d51/grid_d51_adc.c b/grid_make/grid/d51/grid_d51_adc.c new file mode 100644 index 000000000..f7e1b9782 --- /dev/null +++ b/grid_make/grid/d51/grid_d51_adc.c @@ -0,0 +1,69 @@ +#include "grid_d51_adc.h" + +#include "grid_ain.h" +#include "grid_d51_module.h" +#include "grid_platform.h" + +struct grid_d51_adc_model grid_d51_adc_state; + +static struct adc_async_descriptor* adcs[2] = {&ADC_1, &ADC_0}; + +static void adc_transfer_complete_cb(void) { + + struct grid_d51_adc_model* adc = &grid_d51_adc_state; + + if (adc->adc_complete_count == 0) { + adc->adc_complete_count++; + return; + } + + uint8_t mux_state = adc->mux_index; + + for (int channel = 0; channel < 2; channel++) { + uint16_t raw = 0; + adc_async_read_channel(adcs[channel], 0, &raw, 2); + + struct grid_adc_result result = { + .channel = channel, + .mux_state = mux_state, + .value = raw >> 4, + }; + + adc->process_analog(&result); + } + + GRID_MUX_INCREMENT(adc->mux_index, adc->mux_positions_bm); + grid_platform_mux_write(adc->mux_index); + + adc->adc_complete_count = 0; + + adc_async_start_conversion(&ADC_0); + adc_async_start_conversion(&ADC_1); +} + +void grid_d51_adc_init(struct grid_d51_adc_model* adc, uint8_t mux_positions_bm, grid_process_analog_t process_analog) { + + adc->process_analog = process_analog; + adc->adc_complete_count = 0; + + adc_async_register_callback(&ADC_0, 0, ADC_ASYNC_CONVERT_CB, adc_transfer_complete_cb); + adc_async_register_callback(&ADC_1, 0, ADC_ASYNC_CONVERT_CB, adc_transfer_complete_cb); + + adc_async_enable_channel(&ADC_0, 0); + adc_async_enable_channel(&ADC_1, 0); + + grid_platform_mux_init(mux_positions_bm); +} + +void grid_d51_adc_mux_init(struct grid_d51_adc_model* adc, uint8_t mux_positions_bm) { + + adc->mux_positions_bm = mux_positions_bm; + GRID_MUX_FIRST_VALID(adc->mux_index, adc->mux_positions_bm); + grid_platform_mux_write(adc->mux_index); +} + +void grid_d51_adc_start(struct grid_d51_adc_model* adc) { + + adc_async_start_conversion(&ADC_0); + adc_async_start_conversion(&ADC_1); +} diff --git a/grid_make/grid/d51/grid_d51_adc.h b/grid_make/grid/d51/grid_d51_adc.h new file mode 100644 index 000000000..5ae628b8f --- /dev/null +++ b/grid_make/grid/d51/grid_d51_adc.h @@ -0,0 +1,21 @@ +#ifndef GRID_D51_ADC_H +#define GRID_D51_ADC_H + +#include + +#include "grid_ain.h" + +struct grid_d51_adc_model { + uint8_t mux_index; + uint8_t mux_positions_bm; + volatile uint8_t adc_complete_count; + grid_process_analog_t process_analog; +}; + +extern struct grid_d51_adc_model grid_d51_adc_state; + +void grid_d51_adc_init(struct grid_d51_adc_model* adc, uint8_t mux_positions_bm, grid_process_analog_t process_analog); +void grid_d51_adc_mux_init(struct grid_d51_adc_model* adc, uint8_t mux_positions_bm); +void grid_d51_adc_start(struct grid_d51_adc_model* adc); + +#endif diff --git a/grid_make/grid/d51/grid_d51_encoder.c b/grid_make/grid/d51/grid_d51_encoder.c new file mode 100644 index 000000000..3f73dc42f --- /dev/null +++ b/grid_make/grid/d51/grid_d51_encoder.c @@ -0,0 +1,46 @@ +#include "grid_d51_encoder.h" + +#include + +#include "grid_d51_module.h" +#include "grid_platform.h" + +struct grid_d51_encoder_model grid_d51_encoder_state; + +static void spi_start_transfer(struct grid_d51_encoder_model* enc) { + + gpio_set_pin_level(PIN_UI_SPI_CS0, true); + spi_m_async_enable(&UI_SPI); + spi_m_async_transfer(&UI_SPI, NULL, enc->rx_buffer, enc->transfer_length); +} + +static void spi_transfer_complete_cb(void) { + + struct grid_d51_encoder_model* enc = &grid_d51_encoder_state; + + gpio_set_pin_level(PIN_UI_SPI_CS0, false); + + struct grid_encoder_result result = {.data = &enc->rx_buffer[1], .length = enc->transfer_length - 1}; + enc->process_encoder(&result); + + spi_start_transfer(enc); +} + +void grid_d51_encoder_init(struct grid_d51_encoder_model* enc, uint8_t transfer_length, uint32_t clock_rate, grid_process_encoder_t process_encoder) { + + enc->rx_buffer = grid_platform_allocate_volatile(transfer_length); + memset(enc->rx_buffer, 0, transfer_length); + + enc->transfer_length = transfer_length; + enc->process_encoder = process_encoder; + + gpio_set_pin_level(PIN_UI_SPI_CS0, false); + gpio_set_pin_direction(PIN_UI_SPI_CS0, GPIO_DIRECTION_OUT); + + spi_m_async_set_mode(&UI_SPI, SPI_MODE_3); + spi_m_async_set_baudrate(&UI_SPI, clock_rate); + + spi_m_async_register_callback(&UI_SPI, SPI_M_ASYNC_CB_XFER, spi_transfer_complete_cb); + + spi_start_transfer(enc); +} diff --git a/grid_make/grid/d51/grid_d51_encoder.h b/grid_make/grid/d51/grid_d51_encoder.h new file mode 100644 index 000000000..7394ef0af --- /dev/null +++ b/grid_make/grid/d51/grid_d51_encoder.h @@ -0,0 +1,18 @@ +#ifndef GRID_D51_ENCODER_H +#define GRID_D51_ENCODER_H + +#include + +#include "grid_ui_encoder.h" + +struct grid_d51_encoder_model { + uint8_t* rx_buffer; + uint8_t transfer_length; + grid_process_encoder_t process_encoder; +}; + +extern struct grid_d51_encoder_model grid_d51_encoder_state; + +void grid_d51_encoder_init(struct grid_d51_encoder_model* enc, uint8_t transfer_length, uint32_t clock_rate, grid_process_encoder_t process_encoder); + +#endif diff --git a/grid_make/grid/d51/grid_d51_module.c b/grid_make/grid/d51/grid_d51_module.c index 3e9efae64..e33121706 100644 --- a/grid_make/grid/d51/grid_d51_module.c +++ b/grid_make/grid/d51/grid_d51_module.c @@ -1,6 +1,9 @@ #include "grid_d51_module.h" +#include "grid_cal.h" +#include "grid_config.h" #include "grid_d51_nvm.h" +#include "grid_module.h" // Define all of the peripheral interrupt callbacks @@ -39,23 +42,35 @@ void grid_module_common_init(void) { // disable ui interrupts grid_d51_nvic_set_interrupt_priority_mask(1); + if (grid_hwcfg_module_is_po16(&grid_sys_state)) { + grid_module_po16_ui_init(&grid_ain_state, &grid_led_state, &grid_ui_state); + } else if (grid_hwcfg_module_is_bu16(&grid_sys_state)) { + grid_module_bu16_ui_init(&grid_ain_state, &grid_led_state, &grid_ui_state); + } else if (grid_hwcfg_module_is_pbf4(&grid_sys_state)) { + grid_module_pbf4_ui_init(&grid_ain_state, &grid_led_state, &grid_ui_state); + } else if (grid_hwcfg_module_is_en16(&grid_sys_state)) { + grid_module_en16_ui_init(NULL, &grid_led_state, &grid_ui_state); + } else if (grid_hwcfg_module_is_ef44(&grid_sys_state)) { + grid_module_ef44_ui_init(&grid_ain_state, &grid_led_state, &grid_ui_state); + } + grid_d51_nvm_mount(&grid_d51_nvm_state, false); if (grid_hwcfg_module_is_po16(&grid_sys_state)) { printf("Init Module: PO16\r\n"); - grid_module_po16_init(); + grid_d51_module_po16_init(&grid_sys_state, &grid_ui_state, &grid_d51_adc_state, &grid_config_state, &grid_cal_state); } else if (grid_hwcfg_module_is_bu16(&grid_sys_state)) { printf("Init Module: BU16\r\n"); - grid_module_bu16_init(); + grid_d51_module_bu16_init(&grid_sys_state, &grid_ui_state, &grid_d51_adc_state, &grid_config_state, &grid_cal_state); } else if (grid_hwcfg_module_is_pbf4(&grid_sys_state)) { printf("Init Module: PBF4\r\n"); - grid_module_pbf4_init(); + grid_d51_module_pbf4_init(&grid_sys_state, &grid_ui_state, &grid_d51_adc_state, &grid_config_state, &grid_cal_state); } else if (grid_hwcfg_module_is_en16(&grid_sys_state)) { printf("Init Module: EN16\r\n"); - grid_module_en16_init(); + grid_d51_module_en16_init(&grid_sys_state, &grid_ui_state, &grid_d51_encoder_state); } else if (grid_hwcfg_module_is_ef44(&grid_sys_state)) { printf("Init Module: EF44\r\n"); - grid_module_ef44_init(); + grid_d51_module_ef44_init(&grid_sys_state, &grid_ui_state, &grid_d51_adc_state, &grid_d51_encoder_state, &grid_config_state, &grid_cal_state); } else { printf("Init Module: Unknown Module\r\n"); } diff --git a/grid_make/grid/d51/grid_d51_module.h b/grid_make/grid/d51/grid_d51_module.h index bcb301bf7..80922d48f 100644 --- a/grid_make/grid/d51/grid_d51_module.h +++ b/grid_make/grid/d51/grid_d51_module.h @@ -12,6 +12,9 @@ #include "grid_protocol.h" #include "grid_transport.h" +#include "grid_d51_adc.h" +#include "grid_d51_encoder.h" + #include "grid_d51_module_bu16.h" #include "grid_d51_module_ef44.h" #include "grid_d51_module_en16.h" diff --git a/grid_make/grid/grid_d51_module_bu16.c b/grid_make/grid/grid_d51_module_bu16.c index 394e13b43..38eb0297d 100644 --- a/grid_make/grid/grid_d51_module_bu16.c +++ b/grid_make/grid/grid_d51_module_bu16.c @@ -1,95 +1,48 @@ #include "grid_d51_module_bu16.h" +#include + +#include "grid_ain.h" +#include "grid_asc.h" +#include "grid_d51_adc.h" +#include "grid_platform.h" #include "grid_ui_button.h" #include "grid_ui_system.h" -static volatile uint8_t adc_complete_count = 0; - -static volatile uint8_t multiplexer_index = 0; -static volatile uint8_t multiplexer_lookup[16] = {0, 1, 4, 5, 8, 9, 12, 13, 2, 3, 6, 7, 10, 11, 14, 15}; +#define GRID_MODULE_BU16_BUTTON_COUNT 16 -#define GRID_MODULE_BU16_BUT_NUM 16 +static const uint8_t mux_element_lookup[2][8] = { + {0, 1, 4, 5, 8, 9, 12, 13}, // MUX_0 -> ADC_1 + {2, 3, 6, 7, 10, 11, 14, 15}, // MUX_1 -> ADC_0 +}; +static uint16_t element_invert_bm = 0; -static struct grid_ui_button_state ui_button_state[GRID_MODULE_BU16_BUT_NUM] = {0}; -static struct grid_ui_element* elements = NULL; +static struct grid_asc* asc_state = NULL; -static void hardware_start_transfer(void) { +static void bu16_process_analog(struct grid_adc_result* result) { - adc_async_start_conversion(&ADC_0); - adc_async_start_conversion(&ADC_1); -} + uint8_t element_index = mux_element_lookup[result->channel][result->mux_state]; + uint16_t inverted = GRID_ADC_INVERT_COND(result->value, element_index, element_invert_bm); -static void adc_transfer_complete_cb(void) { - - if (adc_complete_count == 0) { - adc_complete_count++; + uint16_t processed; + if (!grid_asc_process(asc_state, element_index, inverted, &processed)) { return; } - /* Read conversion results */ - - uint16_t adcresult_0 = 0; - uint16_t adcresult_1 = 0; - - uint8_t adc_index_0 = multiplexer_lookup[multiplexer_index + 8]; - uint8_t adc_index_1 = multiplexer_lookup[multiplexer_index + 0]; - - adc_async_read_channel(&ADC_0, 0, &adcresult_0, 2); - adc_async_read_channel(&ADC_1, 0, &adcresult_1, 2); - - /* Update the multiplexer */ - - multiplexer_index++; - multiplexer_index %= 8; - - gpio_set_pin_level(MUX_A, multiplexer_index / 1 % 2); - gpio_set_pin_level(MUX_B, multiplexer_index / 2 % 2); - gpio_set_pin_level(MUX_C, multiplexer_index / 4 % 2); - - // FAKE CALIBRATION to compensate oversampling and decimation - uint32_t input_0 = adcresult_0 * 1.03; - if (input_0 > (1 << 16) - 1) { - input_0 = (1 << 16) - 1; - } - adcresult_0 = input_0; - - uint32_t input_1 = adcresult_1 * 1.03; - if (input_1 > (1 << 16) - 1) { - input_1 = (1 << 16) - 1; - } - adcresult_1 = input_1; - - struct grid_ui_element* ele_0 = &elements[adc_index_0]; - - grid_ui_button_store_input(ele_0, &ui_button_state[adc_index_0], adcresult_0, 16); - - struct grid_ui_element* ele_1 = &elements[adc_index_1]; - - grid_ui_button_store_input(ele_1, &ui_button_state[adc_index_1], adcresult_1, 16); - - adc_complete_count = 0; - hardware_start_transfer(); + grid_ui_button_store_input(&grid_ui_state, element_index, processed); } -static void hardware_init(void) { +void grid_d51_module_bu16_init(struct grid_sys_model* sys, struct grid_ui_model* ui, struct grid_d51_adc_model* adc, struct grid_config_model* conf, struct grid_cal_model* cal) { - adc_async_register_callback(&ADC_0, 0, ADC_ASYNC_CONVERT_CB, adc_transfer_complete_cb); - adc_async_register_callback(&ADC_1, 0, ADC_ASYNC_CONVERT_CB, adc_transfer_complete_cb); + asc_state = grid_platform_allocate_volatile(16 * sizeof(struct grid_asc)); + memset(asc_state, 0, 16 * sizeof(struct grid_asc)); - adc_async_enable_channel(&ADC_0, 0); - adc_async_enable_channel(&ADC_1, 0); -} - -void grid_module_bu16_init() { - - grid_module_bu16_ui_init(&grid_ain_state, &grid_led_state, &grid_ui_state); - - for (int i = 0; i < GRID_MODULE_BU16_BUT_NUM; ++i) { - grid_ui_button_state_init(&ui_button_state[i], 16, 0.5, 0.2); + for (int i = 0; i < GRID_MODULE_BU16_BUTTON_COUNT; ++i) { + grid_ui_button_state_init(ui, i, GRID_AIN_INTERNAL_RESOLUTION, 0.5, 0.2); } - elements = grid_ui_model_get_elements(&grid_ui_state); + grid_asc_array_set_factors(asc_state, 16, 0, 16, 1); - hardware_init(); - hardware_start_transfer(); -}; + grid_d51_adc_init(adc, 0b11111111, bu16_process_analog); + grid_d51_adc_start(adc); +} diff --git a/grid_make/grid/grid_d51_module_bu16.h b/grid_make/grid/grid_d51_module_bu16.h index fbc897c87..6192e5569 100644 --- a/grid_make/grid/grid_d51_module_bu16.h +++ b/grid_make/grid/grid_d51_module_bu16.h @@ -3,6 +3,12 @@ #include "grid_d51_module.h" -void grid_module_bu16_init(void); +struct grid_sys_model; +struct grid_ui_model; +struct grid_d51_adc_model; +struct grid_config_model; +struct grid_cal_model; + +void grid_d51_module_bu16_init(struct grid_sys_model* sys, struct grid_ui_model* ui, struct grid_d51_adc_model* adc, struct grid_config_model* conf, struct grid_cal_model* cal); #endif diff --git a/grid_make/grid/grid_d51_module_ef44.c b/grid_make/grid/grid_d51_module_ef44.c index 0de178f4f..8f17f3108 100644 --- a/grid_make/grid/grid_d51_module_ef44.c +++ b/grid_make/grid/grid_d51_module_ef44.c @@ -1,5 +1,12 @@ #include "grid_d51_module_ef44.h" +#include + +#include "grid_ain.h" +#include "grid_asc.h" +#include "grid_d51_adc.h" +#include "grid_d51_encoder.h" +#include "grid_platform.h" #include "grid_ui_button.h" #include "grid_ui_encoder.h" #include "grid_ui_potmeter.h" @@ -8,157 +15,86 @@ #include "grid_cal.h" #include "grid_config.h" -static volatile uint8_t adc_complete_count = 0; -static volatile uint8_t multiplexer_index = 0; - -static uint64_t last_real_time[4] = {0}; - -static uint8_t UI_SPI_TX_BUFFER[14] = {0}; -static uint8_t UI_SPI_RX_BUFFER[14] = {0}; - -#define GRID_MODULE_EF44_BUT_NUM 4 - -#define GRID_MODULE_EF44_ENC_NUM 4 - -#define GRID_MODULE_EF44_POT_NUM 4 - -static struct grid_ui_button_state ui_button_state[GRID_MODULE_EF44_BUT_NUM] = {0}; -static struct grid_ui_encoder_state ui_encoder_state[GRID_MODULE_EF44_ENC_NUM] = {0}; -static struct grid_ui_potmeter_state ui_potmeter_state[GRID_MODULE_EF44_POT_NUM] = {0}; -static struct grid_ui_element* elements = NULL; - -static void hardware_spi_start_transfer(void) { - - gpio_set_pin_level(PIN_UI_SPI_CS0, true); - spi_m_async_enable(&UI_SPI); - spi_m_async_transfer(&UI_SPI, UI_SPI_TX_BUFFER, UI_SPI_RX_BUFFER, 8); -} - -static void hardware_adc_start_transfer(void) { - - adc_async_start_conversion(&ADC_0); - adc_async_start_conversion(&ADC_1); -} - -static void spi_transfer_complete_cb(void) { - - /* Transfer completed */ +#include - // Set the shift registers to continuously load data until new transaction is - // issued - gpio_set_pin_level(PIN_UI_SPI_CS0, false); +#define GRID_MODULE_EF44_BUTTON_COUNT 4 +#define GRID_MODULE_EF44_ENCODER_COUNT 4 +#define GRID_MODULE_EF44_POTMETER_COUNT 4 - uint8_t encoder_position_lookup[GRID_MODULE_EF44_ENC_NUM] = {2, 3, 0, 1}; +static const uint8_t mux_element_lookup[2][2] = { + {4, 5}, // MUX_0 -> ADC_1 + {6, 7}, // MUX_1 -> ADC_0 +}; +static uint16_t element_invert_bm = 0; - // Buffer is only 8 bytes but we check all 16 encoders separately - for (uint8_t j = 0; j < GRID_MODULE_EF44_ENC_NUM; j++) { +static struct grid_asc* asc_state = NULL; - uint8_t new_value = (UI_SPI_RX_BUFFER[j / 2] >> (4 * (j % 2))) & 0x0F; +static void ef44_process_encoder(struct grid_encoder_result* result) { - uint8_t i = encoder_position_lookup[j]; + uint8_t encoder_position_lookup[GRID_MODULE_EF44_ENCODER_COUNT] = {2, 3, 0, 1}; - struct grid_ui_element* ele = &elements[i]; + for (uint8_t i = 0; i < GRID_MODULE_EF44_ENCODER_COUNT; i++) { - grid_ui_encoder_store_input(ele, &ui_encoder_state[i], new_value); + assert(i / 2 < result->length); + uint8_t nibble = GRID_UI_ENCODER_NIBBLE_FROM_BUFFER(result->data, i); + uint8_t element_index = encoder_position_lookup[i]; - uint8_t button_value = new_value & 0b00000100; - - grid_ui_button_store_input(ele, &ui_button_state[i], button_value, 1); + struct grid_ui_encoder_sample sample = GRID_UI_ENCODER_SAMPLE_FROM_NIBBLE(nibble); + grid_ui_encoder_store_input(&grid_ui_state, element_index, sample); } - - hardware_spi_start_transfer(); } -static void adc_transfer_complete_cb(void) { +static void ef44_process_analog(struct grid_adc_result* result) { + + uint8_t element_index = mux_element_lookup[result->channel][result->mux_state]; + uint16_t inverted = GRID_ADC_INVERT_COND(result->value, element_index, element_invert_bm); - if (adc_complete_count == 0) { - adc_complete_count++; + uint16_t processed; + if (!grid_asc_process(asc_state, element_index, inverted, &processed)) { return; } - /* Read conversion results */ - - uint16_t adcresult_0 = 0; - uint16_t adcresult_1 = 0; - - uint8_t adc_index_0 = multiplexer_index + 2; - uint8_t adc_index_1 = multiplexer_index; - - adc_async_read_channel(&ADC_0, 0, &adcresult_0, 2); - adc_async_read_channel(&ADC_1, 0, &adcresult_1, 2); - - /* Update the multiplexer */ - - multiplexer_index++; - multiplexer_index %= 2; - - gpio_set_pin_level(MUX_A, multiplexer_index / 1 % 2); - gpio_set_pin_level(MUX_B, multiplexer_index / 2 % 2); - gpio_set_pin_level(MUX_C, multiplexer_index / 4 % 2); - - struct grid_ui_element* ele_0 = &elements[adc_index_0 + 4]; - - grid_ui_potmeter_store_input(ele_0, adc_index_0 + 4, &ui_potmeter_state[adc_index_0], adcresult_0 >> 4, 12); - - struct grid_ui_element* ele_1 = &elements[adc_index_1 + 4]; - - grid_ui_potmeter_store_input(ele_1, adc_index_1 + 4, &ui_potmeter_state[adc_index_1], adcresult_1 >> 4, 12); - - adc_complete_count = 0; - hardware_adc_start_transfer(); + grid_ui_potmeter_store_input(&grid_ui_state, element_index, processed); } -static void hardware_init(void) { - - gpio_set_pin_level(PIN_UI_SPI_CS0, false); - gpio_set_pin_direction(PIN_UI_SPI_CS0, GPIO_DIRECTION_OUT); +void grid_d51_module_ef44_init(struct grid_sys_model* sys, struct grid_ui_model* ui, struct grid_d51_adc_model* adc, struct grid_d51_encoder_model* enc, struct grid_config_model* conf, + struct grid_cal_model* cal) { - spi_m_async_set_mode(&UI_SPI, SPI_MODE_3); - spi_m_async_set_baudrate(&UI_SPI, - 100000); // was 400000 check clock div setting + asc_state = grid_platform_allocate_volatile(8 * sizeof(struct grid_asc)); + memset(asc_state, 0, 8 * sizeof(struct grid_asc)); - spi_m_async_register_callback(&UI_SPI, SPI_M_ASYNC_CB_XFER, spi_transfer_complete_cb); - - adc_async_register_callback(&ADC_0, 0, ADC_ASYNC_CONVERT_CB, adc_transfer_complete_cb); - adc_async_register_callback(&ADC_1, 0, ADC_ASYNC_CONVERT_CB, adc_transfer_complete_cb); - - adc_async_enable_channel(&ADC_0, 0); - adc_async_enable_channel(&ADC_1, 0); -} - -void grid_module_ef44_init() { - - grid_module_ef44_ui_init(&grid_ain_state, &grid_led_state, &grid_ui_state); - - for (int i = 0; i < GRID_MODULE_EF44_BUT_NUM; ++i) { - grid_ui_button_state_init(&ui_button_state[i], 1, 0.5, 0.2); + // Potmeters are elements 4-7 + for (int i = 0; i < GRID_MODULE_EF44_POTMETER_COUNT; ++i) { + grid_ui_potmeter_state_init(ui, GRID_MODULE_EF44_ENCODER_COUNT + i, GRID_AIN_INTERNAL_RESOLUTION, GRID_POTMETER_DEADZONE, GRID_POTMETER_CENTER); } - for (int i = 0; i < GRID_MODULE_EF44_POT_NUM; ++i) { - grid_ui_potmeter_state_init(&ui_potmeter_state[i], 12, 64, 2048); - } + grid_asc_array_set_factors(asc_state, 8, 4, 4, 1); - uint8_t detent = grid_hwcfg_module_encoder_is_detent(&grid_sys_state); - int8_t direction = grid_hwcfg_module_encoder_dir(&grid_sys_state); - for (uint8_t i = 0; i < GRID_MODULE_EF44_ENC_NUM; i++) { - grid_ui_encoder_state_init(&ui_encoder_state[i], detent, direction); + uint8_t detent = grid_hwcfg_module_encoder_is_detent(sys); + int8_t direction = grid_hwcfg_module_encoder_dir(sys); + // Encoders are elements 0-3 + for (uint8_t i = 0; i < GRID_MODULE_EF44_ENCODER_COUNT; i++) { + grid_ui_encoder_state_init(ui, i, detent, direction, 1, 0.5, 0.2); } - elements = grid_ui_model_get_elements(&grid_ui_state); + grid_config_init(conf, cal); - grid_config_init(&grid_config_state, &grid_cal_state); + grid_cal_init(cal, ui->element_list_length, GRID_AIN_INTERNAL_RESOLUTION); - grid_cal_init(&grid_cal_state, grid_ui_state.element_list_length, 12); - - for (int i = 4; i < 8; ++i) { - assert(grid_cal_set(&grid_cal_state, i, GRID_CAL_LIMITS, &ui_potmeter_state[i - 4].limits) == 0); + // Potmeters are elements 4-7 + for (int i = GRID_MODULE_EF44_ENCODER_COUNT; i < GRID_MODULE_EF44_ENCODER_COUNT + GRID_MODULE_EF44_POTMETER_COUNT; ++i) { + struct grid_ui_element* ele = &ui->element_list[i]; + struct grid_ui_potmeter_state* state = (struct grid_ui_potmeter_state*)ele->primary_state; + assert(grid_cal_set(cal, i, GRID_CAL_LIMITS, &state->limits) == 0); } - assert(grid_ui_bulk_conf_init(&grid_ui_state, GRID_UI_BULK_CONFREAD_PROGRESS, 0, NULL) == 0); - grid_ui_bulk_confread_next(&grid_ui_state); - - hardware_init(); + assert(grid_ui_bulk_conf_init(ui, GRID_UI_BULK_CONFREAD_PROGRESS, 0, NULL) == 0); + grid_ui_bulk_confread_next(ui); - hardware_spi_start_transfer(); - hardware_adc_start_transfer(); + uint8_t transfer_length = 1 + GRID_MODULE_EF44_ENCODER_COUNT / 2; + // SPI clock rate chosen so callback fires at 2000 Hz: rate = 2000 * transfer_length * 8 + uint32_t clock_rate = 2000 * transfer_length * 8; + grid_d51_encoder_init(enc, transfer_length, clock_rate, ef44_process_encoder); + grid_d51_adc_init(adc, 0b00000011, ef44_process_analog); + grid_d51_adc_start(adc); } diff --git a/grid_make/grid/grid_d51_module_ef44.h b/grid_make/grid/grid_d51_module_ef44.h index 85d31c0fa..64075b314 100644 --- a/grid_make/grid/grid_d51_module_ef44.h +++ b/grid_make/grid/grid_d51_module_ef44.h @@ -3,6 +3,14 @@ #include "grid_d51_module.h" -void grid_module_ef44_init(void); +struct grid_sys_model; +struct grid_ui_model; +struct grid_d51_adc_model; +struct grid_d51_encoder_model; +struct grid_config_model; +struct grid_cal_model; + +void grid_d51_module_ef44_init(struct grid_sys_model* sys, struct grid_ui_model* ui, struct grid_d51_adc_model* adc, struct grid_d51_encoder_model* enc, struct grid_config_model* conf, + struct grid_cal_model* cal); #endif diff --git a/grid_make/grid/grid_d51_module_en16.c b/grid_make/grid/grid_d51_module_en16.c index 23294faae..9142ca2a0 100644 --- a/grid_make/grid/grid_d51_module_en16.c +++ b/grid_make/grid/grid_d51_module_en16.c @@ -1,87 +1,41 @@ #include "grid_d51_module_en16.h" +#include + +#include "grid_d51_encoder.h" +#include "grid_platform.h" #include "grid_ui_button.h" #include "grid_ui_encoder.h" #include "grid_ui_system.h" -static uint8_t UI_SPI_TX_BUFFER[14] = {0}; -static uint8_t UI_SPI_RX_BUFFER[14] = {0}; - -#define GRID_MODULE_EN16_BUT_NUM 16 - -#define GRID_MODULE_EN16_ENC_NUM 16 - -static struct grid_ui_button_state ui_button_state[GRID_MODULE_EN16_BUT_NUM] = {0}; -static struct grid_ui_encoder_state ui_encoder_state[GRID_MODULE_EN16_ENC_NUM] = {0}; -static struct grid_ui_element* elements = NULL; - -static void hardware_start_transfer(void) { - - gpio_set_pin_level(PIN_UI_SPI_CS0, true); - spi_m_async_enable(&UI_SPI); - spi_m_async_transfer(&UI_SPI, UI_SPI_TX_BUFFER, UI_SPI_RX_BUFFER, 8); -} - -static void spi_transfer_complete_cb(void) { - - /* Transfer completed */ - - // printf("%d\r\n", _irq_get_current()); - - // Set the shift registers to continuously load data until new transaction is - // issued - gpio_set_pin_level(PIN_UI_SPI_CS0, false); - - uint8_t encoder_position_lookup[GRID_MODULE_EN16_ENC_NUM] = {14, 15, 10, 11, 6, 7, 2, 3, 12, 13, 8, 9, 4, 5, 0, 1}; +#define GRID_MODULE_EN16_BUTTON_COUNT 16 - // Buffer is only 8 bytes but we check all 16 encoders separately - for (uint8_t j = 0; j < GRID_MODULE_EN16_ENC_NUM; j++) { +#define GRID_MODULE_EN16_ENCODER_COUNT 16 - uint8_t new_value = (UI_SPI_RX_BUFFER[j / 2] >> (4 * (j % 2))) & 0x0F; +static void en16_process_encoder(struct grid_encoder_result* result) { - uint8_t i = encoder_position_lookup[j]; + uint8_t encoder_position_lookup[GRID_MODULE_EN16_ENCODER_COUNT] = {14, 15, 10, 11, 6, 7, 2, 3, 12, 13, 8, 9, 4, 5, 0, 1}; - struct grid_ui_element* ele = &elements[i]; + for (uint8_t i = 0; i < GRID_MODULE_EN16_ENCODER_COUNT; i++) { - grid_ui_encoder_store_input(ele, &ui_encoder_state[i], new_value); + uint8_t nibble = GRID_UI_ENCODER_NIBBLE_FROM_BUFFER(result->data, i); + uint8_t element_index = encoder_position_lookup[i]; - uint8_t button_value = new_value & 0b00000100; - - grid_ui_button_store_input(ele, &ui_button_state[i], button_value, 1); + struct grid_ui_encoder_sample sample = GRID_UI_ENCODER_SAMPLE_FROM_NIBBLE(nibble); + grid_ui_encoder_store_input(&grid_ui_state, element_index, sample); } - - hardware_start_transfer(); -} - -static void hardware_init(void) { - - gpio_set_pin_level(PIN_UI_SPI_CS0, false); - gpio_set_pin_direction(PIN_UI_SPI_CS0, GPIO_DIRECTION_OUT); - - spi_m_async_set_mode(&UI_SPI, SPI_MODE_3); - spi_m_async_set_baudrate(&UI_SPI, - 100000); // was 400000 check clock div setting - - spi_m_async_register_callback(&UI_SPI, SPI_M_ASYNC_CB_XFER, spi_transfer_complete_cb); } -void grid_module_en16_init() { - - grid_module_en16_ui_init(NULL, &grid_led_state, &grid_ui_state); - - for (int i = 0; i < GRID_MODULE_EN16_BUT_NUM; ++i) { - grid_ui_button_state_init(&ui_button_state[i], 1, 0.5, 0.2); - } +void grid_d51_module_en16_init(struct grid_sys_model* sys, struct grid_ui_model* ui, struct grid_d51_encoder_model* enc) { - uint8_t detent = grid_hwcfg_module_encoder_is_detent(&grid_sys_state); - int8_t direction = grid_hwcfg_module_encoder_dir(&grid_sys_state); - for (uint8_t i = 0; i < GRID_MODULE_EN16_ENC_NUM; i++) { - grid_ui_encoder_state_init(&ui_encoder_state[i], detent, direction); + uint8_t detent = grid_hwcfg_module_encoder_is_detent(sys); + int8_t direction = grid_hwcfg_module_encoder_dir(sys); + for (uint8_t i = 0; i < GRID_MODULE_EN16_ENCODER_COUNT; i++) { + grid_ui_encoder_state_init(ui, i, detent, direction, 1, 0.5, 0.2); } - elements = grid_ui_model_get_elements(&grid_ui_state); - - hardware_init(); - - hardware_start_transfer(); + uint8_t transfer_length = 1 + GRID_MODULE_EN16_ENCODER_COUNT / 2; + // SPI clock rate chosen so callback fires at 2000 Hz: rate = 2000 * transfer_length * 8 + uint32_t clock_rate = 2000 * transfer_length * 8; + grid_d51_encoder_init(enc, transfer_length, clock_rate, en16_process_encoder); } diff --git a/grid_make/grid/grid_d51_module_en16.h b/grid_make/grid/grid_d51_module_en16.h index 2d2243f09..a24ed72c8 100644 --- a/grid_make/grid/grid_d51_module_en16.h +++ b/grid_make/grid/grid_d51_module_en16.h @@ -3,6 +3,10 @@ #include "grid_d51_module.h" -void grid_module_en16_init(void); +struct grid_sys_model; +struct grid_ui_model; +struct grid_d51_encoder_model; + +void grid_d51_module_en16_init(struct grid_sys_model* sys, struct grid_ui_model* ui, struct grid_d51_encoder_model* enc); #endif diff --git a/grid_make/grid/grid_d51_module_pbf4.c b/grid_make/grid/grid_d51_module_pbf4.c index 2570d41de..b99e92196 100644 --- a/grid_make/grid/grid_d51_module_pbf4.c +++ b/grid_make/grid/grid_d51_module_pbf4.c @@ -1,5 +1,11 @@ #include "grid_d51_module_pbf4.h" +#include + +#include "grid_ain.h" +#include "grid_asc.h" +#include "grid_d51_adc.h" +#include "grid_platform.h" #include "grid_ui_button.h" #include "grid_ui_potmeter.h" #include "grid_ui_system.h" @@ -7,129 +13,75 @@ #include "grid_cal.h" #include "grid_config.h" -static volatile uint8_t adc_complete_count = 0; - -static uint8_t multiplexer_index = 0; -static const uint8_t multiplexer_lookup[16] = {0, 1, 4, 5, 8, 9, 12, 13, 2, 3, 6, 7, 10, 11, 14, 15}; +#include -#define GRID_MODULE_PBF4_BUT_NUM 4 +#define GRID_MODULE_PBF4_BUTTON_COUNT 4 +#define GRID_MODULE_PBF4_POTMETER_COUNT 8 -#define GRID_MODULE_PBF4_POT_NUM 8 +#define X GRID_MUX_UNUSED +static const uint8_t mux_element_lookup[2][8] = { + {0, 1, 4, 5, X, X, 8, 9}, // MUX_0 -> ADC_1 + {2, 3, 6, 7, X, X, 10, 11}, // MUX_1 -> ADC_0 +}; +#undef X +static uint16_t element_invert_bm = 0b0000000000001111; -static struct grid_ui_button_state ui_button_state[GRID_MODULE_PBF4_BUT_NUM] = {0}; -static struct grid_ui_potmeter_state ui_potmeter_state[GRID_MODULE_PBF4_POT_NUM] = {0}; -static struct grid_ui_element* elements = NULL; +static struct grid_asc* asc_state = NULL; -static void hardware_start_transfer(void) { +static void pbf4_process_analog(struct grid_adc_result* result) { - adc_async_start_conversion(&ADC_0); - adc_async_start_conversion(&ADC_1); -} + uint8_t element_index = mux_element_lookup[result->channel][result->mux_state]; + assert(element_index != GRID_MUX_UNUSED); -static void adc_transfer_complete_cb(void) { + uint16_t inverted = GRID_ADC_INVERT_COND(result->value, element_index, element_invert_bm); - if (adc_complete_count == 0) { - adc_complete_count++; + uint16_t processed; + if (!grid_asc_process(asc_state, element_index, inverted, &processed)) { return; } - /* Read conversion results */ - - uint16_t adcresult_0 = 0; - uint16_t adcresult_1 = 0; - - uint8_t adc_index_0 = multiplexer_lookup[multiplexer_index + 8]; - uint8_t adc_index_1 = multiplexer_lookup[multiplexer_index + 0]; - - adc_async_read_channel(&ADC_0, 0, &adcresult_0, 2); - adc_async_read_channel(&ADC_1, 0, &adcresult_1, 2); - - /* Update the multiplexer */ - - multiplexer_index++; - multiplexer_index %= 8; - - gpio_set_pin_level(MUX_A, multiplexer_index / 1 % 2); - gpio_set_pin_level(MUX_B, multiplexer_index / 2 % 2); - gpio_set_pin_level(MUX_C, multiplexer_index / 4 % 2); - - if (adc_index_0 < 8 || adc_index_0 > 13) { - // mux position is valid - - if (adc_index_0 > 13) { - - // adjust button index - adc_index_0 -= 4; - adc_index_1 -= 4; - } - - if (adc_index_0 < 4) { - - // adjust potentiometer polarity - adcresult_0 = 65535 - adcresult_0; - adcresult_1 = 65535 - adcresult_1; - } - - struct grid_ui_element* ele_0 = &elements[adc_index_0]; - - if (adc_index_0 > 7) { - grid_ui_button_store_input(ele_0, &ui_button_state[adc_index_0 - 8], adcresult_0 >> 4, 12); - } else { - grid_ui_potmeter_store_input(ele_0, adc_index_0, &ui_potmeter_state[adc_index_0], adcresult_0 >> 4, 12); - } - - struct grid_ui_element* ele_1 = &elements[adc_index_1]; - - if (adc_index_1 > 7) { - grid_ui_button_store_input(ele_1, &ui_button_state[adc_index_1 - 8], adcresult_1 >> 4, 12); - } else { - grid_ui_potmeter_store_input(ele_1, adc_index_1, &ui_potmeter_state[adc_index_1], adcresult_1 >> 4, 12); - } + if (element_index >= GRID_MODULE_PBF4_POTMETER_COUNT) { + grid_ui_button_store_input(&grid_ui_state, element_index, processed); + } else { + grid_ui_potmeter_store_input(&grid_ui_state, element_index, processed); } - - adc_complete_count = 0; - hardware_start_transfer(); } -static void hardware_init(void) { +void grid_d51_module_pbf4_init(struct grid_sys_model* sys, struct grid_ui_model* ui, struct grid_d51_adc_model* adc, struct grid_config_model* conf, struct grid_cal_model* cal) { - adc_async_register_callback(&ADC_0, 0, ADC_ASYNC_CONVERT_CB, adc_transfer_complete_cb); - adc_async_register_callback(&ADC_1, 0, ADC_ASYNC_CONVERT_CB, adc_transfer_complete_cb); + asc_state = grid_platform_allocate_volatile(12 * sizeof(struct grid_asc)); + memset(asc_state, 0, 12 * sizeof(struct grid_asc)); - adc_async_enable_channel(&ADC_0, 0); - adc_async_enable_channel(&ADC_1, 0); -} - -void grid_module_pbf4_init() { - - grid_module_pbf4_ui_init(&grid_ain_state, &grid_led_state, &grid_ui_state); - - for (int i = 0; i < GRID_MODULE_PBF4_BUT_NUM; ++i) { - grid_ui_button_state_init(&ui_button_state[i], 12, 0.5, 0.2); + // Buttons are elements 8-11 + for (int i = 0; i < GRID_MODULE_PBF4_BUTTON_COUNT; ++i) { + grid_ui_button_state_init(ui, GRID_MODULE_PBF4_POTMETER_COUNT + i, GRID_AIN_INTERNAL_RESOLUTION, 0.5, 0.2); } - for (int i = 0; i < GRID_MODULE_PBF4_POT_NUM; ++i) { - grid_ui_potmeter_state_init(&ui_potmeter_state[i], 12, 64, 2048); + // Potmeters are elements 0-7 + for (int i = 0; i < GRID_MODULE_PBF4_POTMETER_COUNT; ++i) { + grid_ui_potmeter_state_init(ui, i, GRID_AIN_INTERNAL_RESOLUTION, GRID_POTMETER_DEADZONE, GRID_POTMETER_CENTER); } - elements = grid_ui_model_get_elements(&grid_ui_state); + grid_asc_array_set_factors(asc_state, 12, 0, 12, 1); - grid_config_init(&grid_config_state, &grid_cal_state); + grid_config_init(conf, cal); - grid_cal_init(&grid_cal_state, grid_ui_state.element_list_length, 12); + grid_cal_init(cal, ui->element_list_length, 12); - for (int i = 0; i < 4; ++i) { - assert(grid_cal_set(&grid_cal_state, i, GRID_CAL_CENTER, &ui_potmeter_state[i].center) == 0); - assert(grid_cal_set(&grid_cal_state, i, GRID_CAL_DETENT, &ui_potmeter_state[i].detent) == 0); - } - - for (int i = 0; i < 8; ++i) { - assert(grid_cal_set(&grid_cal_state, i, GRID_CAL_LIMITS, &ui_potmeter_state[i].limits) == 0); + // Potmeter calibration (elements 0-7, first 4 have center detent) + for (int i = 0; i < GRID_MODULE_PBF4_POTMETER_COUNT; ++i) { + struct grid_ui_element* ele = &ui->element_list[i]; + struct grid_ui_potmeter_state* state = (struct grid_ui_potmeter_state*)ele->primary_state; + assert(grid_cal_set(cal, i, GRID_CAL_LIMITS, &state->limits) == 0); + if (i < 4) { + assert(grid_cal_set(cal, i, GRID_CAL_CENTER, &state->center) == 0); + assert(grid_cal_set(cal, i, GRID_CAL_DETENT, &state->detent) == 0); + } } - assert(grid_ui_bulk_conf_init(&grid_ui_state, GRID_UI_BULK_CONFREAD_PROGRESS, 0, NULL) == 0); - grid_ui_bulk_confread_next(&grid_ui_state); + assert(grid_ui_bulk_conf_init(ui, GRID_UI_BULK_CONFREAD_PROGRESS, 0, NULL) == 0); + grid_ui_bulk_confread_next(ui); - hardware_init(); - hardware_start_transfer(); + grid_d51_adc_init(adc, 0b11001111, pbf4_process_analog); + grid_d51_adc_start(adc); } diff --git a/grid_make/grid/grid_d51_module_pbf4.h b/grid_make/grid/grid_d51_module_pbf4.h index ce891b896..967a7047d 100644 --- a/grid_make/grid/grid_d51_module_pbf4.h +++ b/grid_make/grid/grid_d51_module_pbf4.h @@ -3,6 +3,12 @@ #include "grid_d51_module.h" -void grid_module_pbf4_init(void); +struct grid_sys_model; +struct grid_ui_model; +struct grid_d51_adc_model; +struct grid_config_model; +struct grid_cal_model; + +void grid_d51_module_pbf4_init(struct grid_sys_model* sys, struct grid_ui_model* ui, struct grid_d51_adc_model* adc, struct grid_config_model* conf, struct grid_cal_model* cal); #endif diff --git a/grid_make/grid/grid_d51_module_po16.c b/grid_make/grid/grid_d51_module_po16.c index c27657485..8e5e6fc05 100644 --- a/grid_make/grid/grid_d51_module_po16.c +++ b/grid_make/grid/grid_d51_module_po16.c @@ -1,106 +1,72 @@ #include "grid_d51_module_po16.h" +#include + +#include "grid_ain.h" +#include "grid_platform.h" #include "grid_ui_potmeter.h" #include "grid_ui_system.h" +#include "grid_asc.h" #include "grid_cal.h" #include "grid_config.h" +#include "grid_d51_adc.h" -static volatile uint8_t adc_complete_count = 0; - -static uint8_t multiplexer_index = 0; -static const uint8_t multiplexer_lookup[16] = {0, 1, 4, 5, 8, 9, 12, 13, 2, 3, 6, 7, 10, 11, 14, 15}; +#include -#define GRID_MODULE_PO16_POT_NUM 16 +#define GRID_MODULE_PO16_POTMETER_COUNT 16 -static struct grid_ui_potmeter_state ui_potmeter_state[GRID_MODULE_PO16_POT_NUM] = {0}; -static struct grid_ui_element* elements = NULL; +static const uint8_t mux_element_lookup[2][8] = { + {0, 1, 4, 5, 8, 9, 12, 13}, // MUX_0 -> ADC_1 + {2, 3, 6, 7, 10, 11, 14, 15}, // MUX_1 -> ADC_0 +}; +static uint16_t element_invert_bm = 0; -static void hardware_start_transfer(void) { +static struct grid_asc* asc_state = NULL; - adc_async_start_conversion(&ADC_0); - adc_async_start_conversion(&ADC_1); -} +static void po16_process_analog(struct grid_adc_result* result) { -static void adc_transfer_complete_cb(void) { + uint8_t element_index = mux_element_lookup[result->channel][result->mux_state]; + uint16_t inverted = GRID_ADC_INVERT_COND(result->value, element_index, element_invert_bm); - if (adc_complete_count == 0) { - adc_complete_count++; + uint16_t processed; + if (!grid_asc_process(asc_state, element_index, inverted, &processed)) { return; } - /* Read conversion results */ - - uint16_t adcresult_0 = 0; - uint16_t adcresult_1 = 0; - - uint8_t adc_index_0 = multiplexer_lookup[multiplexer_index + 8]; - uint8_t adc_index_1 = multiplexer_lookup[multiplexer_index + 0]; - - adc_async_read_channel(&ADC_0, 0, &adcresult_0, 2); - adc_async_read_channel(&ADC_1, 0, &adcresult_1, 2); - - /* Update the multiplexer */ - - multiplexer_index++; - multiplexer_index %= 8; - - gpio_set_pin_level(MUX_A, multiplexer_index / 1 % 2); - gpio_set_pin_level(MUX_B, multiplexer_index / 2 % 2); - gpio_set_pin_level(MUX_C, multiplexer_index / 4 % 2); - - // POT POLARITY IS REVERSED ON PO16_RevC - if (grid_hwcfg_module_is_po16_reverse_polarity(&grid_sys_state)) { - - // Reverse the 16bit result - adcresult_0 = 65535 - adcresult_0; - adcresult_1 = 65535 - adcresult_1; - } - - struct grid_ui_element* ele_0 = &elements[adc_index_0]; - - grid_ui_potmeter_store_input(ele_0, adc_index_0, &ui_potmeter_state[adc_index_0], adcresult_0 >> 4, 12); - - struct grid_ui_element* ele_1 = &elements[adc_index_1]; - - grid_ui_potmeter_store_input(ele_1, adc_index_1, &ui_potmeter_state[adc_index_1], adcresult_1 >> 4, 12); - - adc_complete_count = 0; - hardware_start_transfer(); + grid_ui_potmeter_store_input(&grid_ui_state, element_index, processed); } -static void hardware_init(void) { - - adc_async_register_callback(&ADC_0, 0, ADC_ASYNC_CONVERT_CB, adc_transfer_complete_cb); - adc_async_register_callback(&ADC_1, 0, ADC_ASYNC_CONVERT_CB, adc_transfer_complete_cb); +void grid_d51_module_po16_init(struct grid_sys_model* sys, struct grid_ui_model* ui, struct grid_d51_adc_model* adc, struct grid_config_model* conf, struct grid_cal_model* cal) { - adc_async_enable_channel(&ADC_0, 0); - adc_async_enable_channel(&ADC_1, 0); -} + asc_state = grid_platform_allocate_volatile(16 * sizeof(struct grid_asc)); + memset(asc_state, 0, 16 * sizeof(struct grid_asc)); -void grid_module_po16_init() { - - grid_module_po16_ui_init(&grid_ain_state, &grid_led_state, &grid_ui_state); + if (grid_hwcfg_module_is_po16_reverse_polarity(sys)) { + element_invert_bm = 0b1111111111111111; + } - for (int i = 0; i < GRID_MODULE_PO16_POT_NUM; ++i) { - grid_ui_potmeter_state_init(&ui_potmeter_state[i], 12, 64, 2048); + for (int i = 0; i < GRID_MODULE_PO16_POTMETER_COUNT; ++i) { + grid_ui_potmeter_state_init(ui, i, GRID_AIN_INTERNAL_RESOLUTION, GRID_POTMETER_DEADZONE, GRID_POTMETER_CENTER); } - elements = grid_ui_model_get_elements(&grid_ui_state); + grid_asc_array_set_factors(asc_state, 16, 0, 16, 1); - grid_config_init(&grid_config_state, &grid_cal_state); + grid_config_init(conf, cal); - grid_cal_init(&grid_cal_state, grid_ui_state.element_list_length, 12); + grid_cal_init(cal, ui->element_list_length, GRID_AIN_INTERNAL_RESOLUTION); - for (int i = 0; i < 16; ++i) { - assert(grid_cal_set(&grid_cal_state, i, GRID_CAL_LIMITS, &ui_potmeter_state[i].limits) == 0); - assert(grid_cal_set(&grid_cal_state, i, GRID_CAL_CENTER, &ui_potmeter_state[i].center) == 0); - assert(grid_cal_set(&grid_cal_state, i, GRID_CAL_DETENT, &ui_potmeter_state[i].detent) == 0); + for (int i = 0; i < GRID_MODULE_PO16_POTMETER_COUNT; ++i) { + struct grid_ui_element* ele = &ui->element_list[i]; + struct grid_ui_potmeter_state* state = (struct grid_ui_potmeter_state*)ele->primary_state; + assert(grid_cal_set(cal, i, GRID_CAL_LIMITS, &state->limits) == 0); + assert(grid_cal_set(cal, i, GRID_CAL_CENTER, &state->center) == 0); + assert(grid_cal_set(cal, i, GRID_CAL_DETENT, &state->detent) == 0); } - assert(grid_ui_bulk_conf_init(&grid_ui_state, GRID_UI_BULK_CONFREAD_PROGRESS, 0, NULL) == 0); - grid_ui_bulk_confread_next(&grid_ui_state); + assert(grid_ui_bulk_conf_init(ui, GRID_UI_BULK_CONFREAD_PROGRESS, 0, NULL) == 0); + grid_ui_bulk_confread_next(ui); - hardware_init(); - hardware_start_transfer(); + grid_d51_adc_init(adc, 0b11111111, po16_process_analog); + grid_d51_adc_start(adc); } diff --git a/grid_make/grid/grid_d51_module_po16.h b/grid_make/grid/grid_d51_module_po16.h index ec7ed00d9..c26975ca4 100644 --- a/grid_make/grid/grid_d51_module_po16.h +++ b/grid_make/grid/grid_d51_module_po16.h @@ -3,6 +3,12 @@ #include "grid_d51_module.h" -void grid_module_po16_init(void); +struct grid_sys_model; +struct grid_ui_model; +struct grid_d51_adc_model; +struct grid_config_model; +struct grid_cal_model; + +void grid_d51_module_po16_init(struct grid_sys_model* sys, struct grid_ui_model* ui, struct grid_d51_adc_model* adc, struct grid_config_model* conf, struct grid_cal_model* cal); #endif