Skip to content
Snippets Groups Projects
Commit 1e394891 authored by moon2's avatar moon2 :speech_balloon: Committed by q3k
Browse files

micropython: captouch calib

parent 9c96331d
No related branches found
No related tags found
No related merge requests found
...@@ -160,8 +160,6 @@ void manual_captouch_readout(uint8_t top) ...@@ -160,8 +160,6 @@ void manual_captouch_readout(uint8_t top)
xQueueSend(gpio_evt_queue, &chip, NULL); xQueueSend(gpio_evt_queue, &chip, NULL);
} }
void espan_handle_captouch(uint16_t pressed_top, uint16_t pressed_bot);
static uint16_t pressed_top, pressed_bot; static uint16_t pressed_top, pressed_bot;
void gpio_event_handler(void* arg) void gpio_event_handler(void* arg)
{ {
...@@ -177,7 +175,6 @@ void gpio_event_handler(void* arg) ...@@ -177,7 +175,6 @@ void gpio_event_handler(void* arg)
if(chip == &chip_top) pressed_top = pressed; if(chip == &chip_top) pressed_top = pressed;
if(chip == &chip_bot) pressed_bot = pressed; if(chip == &chip_bot) pressed_bot = pressed;
//espan_handle_captouch(pressed_top, pressed_bot);
} }
} }
} }
...@@ -186,10 +183,10 @@ static uint8_t top_map[] = {2, 2, 2, 0, 0, 8, 8, 8, 6, 6, 4, 4}; ...@@ -186,10 +183,10 @@ static uint8_t top_map[] = {2, 2, 2, 0, 0, 8, 8, 8, 6, 6, 4, 4};
static uint8_t bot_map[] = {1, 1, 3, 3, 5, 5, 7, 7, 9, 9}; static uint8_t bot_map[] = {1, 1, 3, 3, 5, 5, 7, 7, 9, 9};
uint16_t read_captouch(){ uint16_t read_captouch(){
uint16_t petals = 0; uint16_t petals = 0;
uint16_t top = pressed_top; uint16_t top = pressed_top;
uint16_t bot = pressed_bot; uint16_t bot = pressed_bot;
for(int i=0; i<12; i++) { for(int i=0; i<12; i++) {
if(top & (1 << i)) { if(top & (1 << i)) {
petals |= (1<<top_map[i]); petals |= (1<<top_map[i]);
...@@ -205,6 +202,11 @@ uint16_t read_captouch(){ ...@@ -205,6 +202,11 @@ uint16_t read_captouch(){
return petals; return petals;
} }
void captouch_force_calibration(){
ad714x_i2c_write(&chip_top, 2, (1 << 14));
ad714x_i2c_write(&chip_bot, 2, (1 << 14));
}
static void captouch_init_chip(const struct ad714x_chip* chip, const struct ad7147_device_config device_config) static void captouch_init_chip(const struct ad714x_chip* chip, const struct ad7147_device_config device_config)
{ {
uint16_t data; uint16_t data;
...@@ -221,8 +223,7 @@ static void captouch_init_chip(const struct ad714x_chip* chip, const struct ad71 ...@@ -221,8 +223,7 @@ static void captouch_init_chip(const struct ad714x_chip* chip, const struct ad71
ad714x_set_stage_config(chip, i, &stage_config); ad714x_set_stage_config(chip, i, &stage_config);
} }
// Force calibration captouch_force_calibration();
ad714x_i2c_write(chip, 2, (1 << 14));
gpio_config_t io_conf = {}; gpio_config_t io_conf = {};
io_conf.intr_type = GPIO_INTR_NEGEDGE; io_conf.intr_type = GPIO_INTR_NEGEDGE;
...@@ -232,15 +233,13 @@ static void captouch_init_chip(const struct ad714x_chip* chip, const struct ad71 ...@@ -232,15 +233,13 @@ static void captouch_init_chip(const struct ad714x_chip* chip, const struct ad71
io_conf.pull_down_en = 0; io_conf.pull_down_en = 0;
gpio_config(&io_conf); gpio_config(&io_conf);
gpio_evt_queue = xQueueCreate(10, sizeof(const struct ad714x_chip*)); // gpio_isr_handler_add(chip->gpio, gpio_isr_handler, (void *)chip);
xTaskCreate(gpio_event_handler, "gpio_event_handler", 2048 * 2, NULL, configMAX_PRIORITIES - 2, NULL);
gpio_isr_handler_add(chip->gpio, gpio_isr_handler, (void *)chip);
} }
void captouch_init(void) void captouch_init(void)
{ {
gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT); //gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);
captouch_init_chip(&chip_top, (struct ad7147_device_config){.sequence_stage_num = 11, captouch_init_chip(&chip_top, (struct ad7147_device_config){.sequence_stage_num = 11,
.decimation = 1, .decimation = 1,
.stage0_cal_en = 1, .stage0_cal_en = 1,
...@@ -294,6 +293,9 @@ void captouch_init(void) ...@@ -294,6 +293,9 @@ void captouch_init(void)
.stage8_high_int_enable = 1, .stage8_high_int_enable = 1,
.stage9_high_int_enable = 1, .stage9_high_int_enable = 1,
}); });
gpio_evt_queue = xQueueCreate(10, sizeof(const struct ad714x_chip*));
xTaskCreate(gpio_event_handler, "gpio_event_handler", 2048 * 2, NULL, configMAX_PRIORITIES - 2, NULL);
} }
static void captouch_print_debug_info_chip(const struct ad714x_chip* chip) static void captouch_print_debug_info_chip(const struct ad714x_chip* chip)
......
...@@ -6,4 +6,5 @@ void captouch_print_debug_info(void); ...@@ -6,4 +6,5 @@ void captouch_print_debug_info(void);
void gpio_event_handler(void * arg); void gpio_event_handler(void * arg);
void manual_captouch_readout(uint8_t top); void manual_captouch_readout(uint8_t top);
void captouch_get_cross(int paddle, int * x, int * y); void captouch_get_cross(int paddle, int * x, int * y);
void captouch_force_calibration();
uint16_t read_captouch(); uint16_t read_captouch();
...@@ -23,7 +23,6 @@ static const char *TAG = "espan"; ...@@ -23,7 +23,6 @@ static const char *TAG = "espan";
#define CONFIG_I2C_MASTER_SDA 10 #define CONFIG_I2C_MASTER_SDA 10
#define CONFIG_I2C_MASTER_SCL 9 #define CONFIG_I2C_MASTER_SCL 9
static esp_err_t i2c_master_init(void) static esp_err_t i2c_master_init(void)
{ {
int i2c_master_port = I2C_MASTER_NUM; int i2c_master_port = I2C_MASTER_NUM;
...@@ -42,53 +41,7 @@ static esp_err_t i2c_master_init(void) ...@@ -42,53 +41,7 @@ static esp_err_t i2c_master_init(void)
return i2c_driver_install(i2c_master_port, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0); return i2c_driver_install(i2c_master_port, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
} }
// channel -> paddle #define CAPTOUCH_POLLING_PERIOD 10
uint8_t top_map[] = {2, 2, 2, 0, 0, 8, 8, 8, 6, 6, 4, 4};
uint8_t bot_map[] = {1, 1, 3, 3, 5, 5, 7, 7, 9, 9};
static bool active_paddles[10];
void espan_handle_captouch(uint16_t pressed_top, uint16_t pressed_bot)
{
bool paddles[10] = {0,};
for(int i=0; i<12; i++) {
if(pressed_top & (1 << i)) {
paddles[top_map[i]] = true;
}
}
for(int i=0; i<10; i++) {
if(pressed_bot & (1 << i)) {
paddles[bot_map[i]] = true;
}
}
bool changed = false;
for(int i=0; i<10; i++) {
if(active_paddles[i] == false && paddles[i] == true) {
//if(!(i == 2 || i == 8)) synth_start(i);
//leds_animate(i);
active_paddles[i] = true;
changed = true;
} else if(active_paddles[i] == true && paddles[i] == false) {
active_paddles[i] = false;
changed = true;
//if(!(i == 2 || i == 8)) synth_stop(i);
}
}
if(changed) {
//display_show(active_paddles);
}
}
#define VIB_DEPTH 0.01
#define VIB 0
#define VIOLIN 1
#define VIOLIN_DECAY 10
#define VIOLIN_VOL_BOOST 0.004
#define VIOLIN_SENS_POW 2
#define CAPTOUCH_POLLING_PERIOD 3
void os_app_main(void) void os_app_main(void)
{ {
...@@ -104,79 +57,11 @@ void os_app_main(void) ...@@ -104,79 +57,11 @@ void os_app_main(void)
captouch_init(); captouch_init();
mp_hal_stdout_tx_str("task inits done\n\r"); mp_hal_stdout_tx_str("task inits done\n\r");
//not sure how slow captouch_get_cross is so duplicating edge detection here;
bool prev_petals[10] = {0};
//pitch bend as movement relative to inital touch pos to make intonation easier
int petal_refs[10] = {0};
int petal_tot_prev[10] = {0};
int petal_min[10] = {0};
int petal_max[10] = {0};
int petal_dir_prev[10] = {0};
int i = 0;
void * asdasd = &i;
while(1) { while(1) {
manual_captouch_readout(1); manual_captouch_readout(1);
vTaskDelay((CAPTOUCH_POLLING_PERIOD) / portTICK_PERIOD_MS); vTaskDelay((CAPTOUCH_POLLING_PERIOD) / portTICK_PERIOD_MS);
manual_captouch_readout(0); manual_captouch_readout(0);
vTaskDelay((CAPTOUCH_POLLING_PERIOD) / portTICK_PERIOD_MS); vTaskDelay((CAPTOUCH_POLLING_PERIOD) / portTICK_PERIOD_MS);
continue;
/*
i = (i + 1) % 10;
if(!(i == 2 || i == 8)) continue;
if(VIB){
if(active_paddles[i]){
int x = 0;
int y = 0;
captouch_get_cross(i, &x, &y);
int tot = (x >> 1) + (y >> 1);
if(!prev_petals[i]){
prev_petals[i] = 1;
petal_refs[i] = tot;
synth_set_bend(i, 0);
} else {
float bend = tot - petal_refs[i];
bend *= VIB_DEPTH;
synth_set_bend(i, bend);
}
} else {
prev_petals[i] = 0;
}
}
if(VIOLIN){
if(active_paddles[i]){
int x = 0;
int y = 0;
captouch_get_cross(i, &x, &y);
int tot = (x >> 1) + (y >> 1);
uint8_t dir = tot > petal_tot_prev[i];
if(dir != petal_dir_prev[i]){
if(dir){
petal_min[i] = petal_tot_prev[i];
} else {
petal_max[i] = petal_tot_prev[i];
}
petal_refs[i] = tot;
float vol = (VIOLIN_VOL_BOOST) * (petal_max[i] - petal_min[i]);
for(int i = 1; i < VIOLIN_SENS_POW; i++){
vol *= vol;
}
vol += synth_get_env(i);
if(vol > 1.) vol = 1;
if(vol < 0.) vol = 0;
synth_set_vol(i, vol);
synth_start(i);
}
petal_tot_prev[i] = tot;
petal_dir_prev[i] = dir;
}
}
vTaskDelay(10 / portTICK_PERIOD_MS);
//captouch_print_debug_info();
*/
} }
ESP_ERROR_CHECK(i2c_driver_delete(I2C_MASTER_NUM)); ESP_ERROR_CHECK(i2c_driver_delete(I2C_MASTER_NUM));
......
...@@ -16,13 +16,19 @@ ...@@ -16,13 +16,19 @@
STATIC mp_obj_t mp_get_captouch(size_t n_args, const mp_obj_t *args) { STATIC mp_obj_t mp_get_captouch(size_t n_args, const mp_obj_t *args) {
uint16_t captouch = read_captouch(); uint16_t captouch = read_captouch();
uint8_t pad = mp_obj_get_int(args[0]); uint16_t pad = mp_obj_get_int(args[0]);
uint8_t output = (captouch >> pad) & 1; uint8_t output = (captouch >> pad) & 1;
return mp_obj_new_int(output); return mp_obj_new_int(output);
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_get_captouch_obj, 1, 2, mp_get_captouch); STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_get_captouch_obj, 1, 2, mp_get_captouch);
STATIC mp_obj_t mp_captouch_autocalib(size_t n_args, const mp_obj_t *args) {
captouch_force_calibration();
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_captouch_autocalib_obj, 0, 2, mp_captouch_autocalib);
STATIC mp_obj_t mp_set_global_volume_dB(size_t n_args, const mp_obj_t *args) { STATIC mp_obj_t mp_set_global_volume_dB(size_t n_args, const mp_obj_t *args) {
mp_float_t x = mp_obj_get_float(args[0]); mp_float_t x = mp_obj_get_float(args[0]);
int8_t d = x; int8_t d = x;
...@@ -77,6 +83,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_update_leds_obj, 0, 2, mp_update_l ...@@ -77,6 +83,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_update_leds_obj, 0, 2, mp_update_l
STATIC const mp_rom_map_elem_t mp_module_hardware_globals_table[] = { STATIC const mp_rom_map_elem_t mp_module_hardware_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_badge_audio) }, { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_badge_audio) },
{ MP_ROM_QSTR(MP_QSTR_get_captouch), MP_ROM_PTR(&mp_get_captouch_obj) }, { MP_ROM_QSTR(MP_QSTR_get_captouch), MP_ROM_PTR(&mp_get_captouch_obj) },
{ MP_ROM_QSTR(MP_QSTR_captouch_autocalib), MP_ROM_PTR(&mp_captouch_autocalib_obj) },
{ MP_ROM_QSTR(MP_QSTR_set_global_volume_dB), MP_ROM_PTR(&mp_set_global_volume_dB_obj) }, { MP_ROM_QSTR(MP_QSTR_set_global_volume_dB), MP_ROM_PTR(&mp_set_global_volume_dB_obj) },
{ MP_ROM_QSTR(MP_QSTR_count_sources), MP_ROM_PTR(&mp_count_sources_obj) }, { MP_ROM_QSTR(MP_QSTR_count_sources), MP_ROM_PTR(&mp_count_sources_obj) },
{ MP_ROM_QSTR(MP_QSTR_dump_all_sources), MP_ROM_PTR(&mp_dump_all_sources_obj) }, { MP_ROM_QSTR(MP_QSTR_dump_all_sources), MP_ROM_PTR(&mp_dump_all_sources_obj) },
......
...@@ -3,4 +3,4 @@ ...@@ -3,4 +3,4 @@
#esp.osdebug(None) #esp.osdebug(None)
#import webrepl #import webrepl
#webrepl.start() #webrepl.start()
import cap_touch_demo import main
from synth import tinysynth from synth import tinysynth
from hardware import * from hardware import *
import time import time
from machine import Pin
p = Pin(0, Pin.IN)
set_global_volume_dB(0) set_global_volume_dB(0)
synths = [] synths = []
...@@ -11,7 +14,13 @@ for synth in synths: ...@@ -11,7 +14,13 @@ for synth in synths:
synth.decay(100) synth.decay(100)
synth.waveform(1) synth.waveform(1)
chords = [[0,3,7,10,12],[-2,2,5,8,10],[-2,3,7,10,14],[-4,0,3,8,12],[-1,2,5,7,11]] chords = [\
[-4,0,3,8,10],\
[-3,0,5,7,12],\
[-1,2,5,7,11],\
[0,3,7,12,14],\
[3,7,10,14,15]\
]
chord = chords[3] chord = chords[3]
chord_index = -1 chord_index = -1
...@@ -29,11 +38,15 @@ def set_chord(i): ...@@ -29,11 +38,15 @@ def set_chord(i):
set_chord(3) set_chord(3)
def cap_touch_demo_start(delay): def cap_touch_demo_start():
global chord_index global chord_index
global chord global chord
global p
while True: while True:
update_leds() update_leds()
if(p.value() == 0):
captouch_autocalib()
time.sleep_ms(100)
for i in range(10): for i in range(10):
if(get_captouch(i)): if(get_captouch(i)):
if(i%2): if(i%2):
...@@ -44,5 +57,3 @@ def cap_touch_demo_start(delay): ...@@ -44,5 +57,3 @@ def cap_touch_demo_start(delay):
synths[k].tone(chord[k]) synths[k].tone(chord[k])
synths[k].start() synths[k].start()
print("synth " +str(k)) print("synth " +str(k))
time.sleep_ms(delay)
from machine import Pin
p = Pin(0, Pin.IN)
while (p.value() == 1):
pass
import cap_touch_demo
cap_touch_demo.cap_touch_demo_start()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment