diff --git a/components/badge23/captouch.c b/components/badge23/captouch.c index 0826f250fe053318519fc10ae83d58d67823b5da..43da2702a0ae6474d719ba2b81433b9e427e10ad 100644 --- a/components/badge23/captouch.c +++ b/components/badge23/captouch.c @@ -18,6 +18,8 @@ static const uint8_t bot_stages = 12; static const uint8_t top_segment_map[] = {1,3,2,2,3,1,1,3,2,1,3,2}; //PETAL_SEGMENT_* static const uint8_t bot_segment_map[] = {3,0,3,0,0,0,3,0,3,1,2,3}; //PETAL_SEGMENT_* static const uint8_t bot_stage_config[] = {0,1,2,3,5,6,7,8,9,10,11,12}; +#define DEFAULT_THRES_TOP 8000 +#define DEFAULT_THRES_BOT 12000 #define AD7147_ADDR_TOP 0b101100 #define AD7147_ADDR_BOT 0b101101 #endif @@ -27,8 +29,11 @@ static const uint8_t top_map[] = {2, 2, 2, 0, 0, 8, 8, 8, 6, 6, 4, 4}; static const uint8_t top_stages = 12; static const uint8_t bot_map[] = {1, 1, 3, 3, 5, 5, 7, 7, 9, 9}; static const uint8_t bot_stages = 10; -static const uint8_t top_segment_map[] = {1,2,3,1,2,3,1,2,3,1,2,3}; //idk -static const uint8_t bot_segment_map[] = {0,3,0,3,0,3,0,3,0,3,0,3}; //idk +static const uint8_t top_segment_map[] = {1,2,0,1,2,1,2,0,1,2,1,2}; //idk +static const uint8_t bot_segment_map[] = {3,0,3,0,3,0,0,3,0,3}; //idk +static const uint8_t bot_stage_config[] = {0,1,2,3,4,5,6,7,8,9,10,11}; +#define DEFAULT_THRES_TOP 2000 +#define DEFAULT_THRES_BOT 12000 #define AD7147_ADDR_TOP 0b101101 #define AD7147_ADDR_BOT 0b101100 #endif @@ -44,13 +49,12 @@ static const char *TAG = "captouch"; #define TIMEOUT_MS 1000 -#define DEFAULT_THRES_TOP 8000 -#define DEFAULT_THRES_BOT 12000 static struct ad714x_chip *chip_top; static struct ad714x_chip *chip_bot; typedef struct{ + uint8_t config_mask; uint16_t amb_values[4]; //ordered according to PETAL_SEGMENT_* uint16_t cdc_values[4]; //ordered according to PETAL_SEGMENT_* uint16_t thres_values[4]; //ordered according to PETAL_SEGMENT_* @@ -212,8 +216,7 @@ static void captouch_init_chip(const struct ad714x_chip* chip, const struct ad71 } } -void captouch_init(void) -{ +static void captouch_init_petals(){ for(int i = 0; i < 10; i++){ for(int j = 0; j < 4; j++){ petals[i].amb_values[j] = 0; @@ -224,7 +227,61 @@ void captouch_init(void) petals[i].thres_values[j] = DEFAULT_THRES_TOP; } } + petals[i].config_mask = 0; + } + for(int i = 0; i < bot_stages; i++){ + petals[bot_map[i]].config_mask |= 1 << bot_segment_map[i]; + } + for(int i = 0; i < top_stages; i++){ + petals[top_map[i]].config_mask |= 1 << top_segment_map[i]; } +} + +int32_t captouch_get_petal_rad(uint8_t petal){ + uint8_t cf = petals[petal].config_mask; + if(cf == 0b1110){ //LEFT, RIGHT, BASE + int32_t left = petals[petal].cdc_values[PETAL_SEGMENT_LEFT]; + left -= petals[petal].amb_values[PETAL_SEGMENT_LEFT]; + int32_t right = petals[petal].cdc_values[PETAL_SEGMENT_RIGHT]; + right -= petals[petal].amb_values[PETAL_SEGMENT_RIGHT]; + int32_t base = petals[petal].cdc_values[PETAL_SEGMENT_BASE]; + base -= petals[petal].amb_values[PETAL_SEGMENT_BASE]; + return (left + right)/2 - base; + } + if(cf == 0b111){ //LEFT, RIGHT, TIP + int32_t left = petals[petal].cdc_values[PETAL_SEGMENT_LEFT]; + left -= petals[petal].amb_values[PETAL_SEGMENT_LEFT]; + int32_t right = petals[petal].cdc_values[PETAL_SEGMENT_RIGHT]; + right -= petals[petal].amb_values[PETAL_SEGMENT_RIGHT]; + int32_t tip = petals[petal].cdc_values[PETAL_SEGMENT_TIP]; + tip -= petals[petal].amb_values[PETAL_SEGMENT_TIP]; + return (-left - right)/2 + tip; + } + if(cf == 0b1001){ //TIP, BASE + int32_t tip = petals[petal].cdc_values[PETAL_SEGMENT_TIP]; + tip -= petals[petal].amb_values[PETAL_SEGMENT_TIP]; + int32_t base = petals[petal].cdc_values[PETAL_SEGMENT_BASE]; + base -= petals[petal].amb_values[PETAL_SEGMENT_BASE]; + return tip - base; + } + return 0; +} + +int32_t captouch_get_petal_phi(uint8_t petal){ + uint8_t cf = petals[petal].config_mask; + if((cf == 0b1110) || (cf == 0b110) || (cf == 0b111)){ //LEFT, RIGHT, (BASE) + int32_t left = petals[petal].cdc_values[PETAL_SEGMENT_LEFT]; + left -= petals[petal].amb_values[PETAL_SEGMENT_LEFT]; + int32_t right = petals[petal].cdc_values[PETAL_SEGMENT_RIGHT]; + right -= petals[petal].amb_values[PETAL_SEGMENT_RIGHT]; + return left - right; + } + return 0; +} + +void captouch_init(void) +{ + captouch_init_petals(); chip_top = &chip_top_rev5; chip_bot = &chip_bot_rev5; diff --git a/components/badge23/include/badge23/captouch.h b/components/badge23/include/badge23/captouch.h index 452bd38e7ae51a78d7e77dd1b199cc8ee3c842f1..1153b947ad33a5c4a8981b78ae5888bb2982d108 100644 --- a/components/badge23/include/badge23/captouch.h +++ b/components/badge23/include/badge23/captouch.h @@ -11,3 +11,5 @@ uint16_t captouch_get_petal_pad_raw(uint8_t petal, uint8_t pad, uint8_t amb); void captouch_force_calibration(); void captouch_set_petal_pad_threshold(uint8_t petal, uint8_t pad, uint16_t thres); uint16_t read_captouch(); +int32_t captouch_get_petal_phi(uint8_t petal); +int32_t captouch_get_petal_rad(uint8_t petal); diff --git a/python_payload/cap_touch_demo.py b/python_payload/cap_touch_demo.py index bab2f22de565f26fa6833b8fda65da2c0d00adca..4fdeb3d5ac72c9555bd330a0054d63123df107f6 100644 --- a/python_payload/cap_touch_demo.py +++ b/python_payload/cap_touch_demo.py @@ -10,20 +10,16 @@ def init(): def run(): hardware.display_fill(0) time.sleep_ms(30) - for i in range(0,10,2): + for i in range(10): size = (hardware.get_captouch(i) * 4) + 4 - x = 70 + (utils.captouch_get_rad(i)/1000) - x += (utils.captouch_get_phi(i)/600)*1j + x = 70 + (hardware.captouch_get_petal_rad(i)/1000) + x += (hardware.captouch_get_petal_phi(i)/600)*1j rot = cmath.exp(2j *math.pi * i / 10) x = x * rot - utils.draw_rect(int(x.imag+120-(size/2)),int(x.real+120-(size/2)),size,size,0b1111100000011111) - for i in range(1,10,2): - size = (hardware.get_captouch(i) * 4) + 4 - x = 70 + (utils.captouch_get_rad(i)/1000) - x += (utils.captouch_get_phi(i)/600)*1j - rot = cmath.exp(2j *math.pi * i / 10) - x = x * rot - utils.draw_rect(int(x.imag+120-(size/2)),int(x.real+120-(size/2)),size,size,0b0000011111111111) + col = 0b1111100000011111 + if i%2: + col = 0b0000011111111111 + utils.draw_rect(int(x.imag+120-(size/2)),int(x.real+120-(size/2)),size,size,col) hardware.display_update() def foreground(): diff --git a/python_payload/utils.py b/python_payload/utils.py index 9e0793b8661e9410995f296c5bc0f0afa861387f..88ca4d3ba33ff0c8f5f66d5182b3f4dc97871ba9 100644 --- a/python_payload/utils.py +++ b/python_payload/utils.py @@ -5,26 +5,6 @@ RED = 0b1111100000000000 GREEN = 0b0000011111100000 BLUE = 0b0000000000011111 -def captouch_get_phi(petal): - if(petal%2): - return 0 - else: - l = captouch_get_petal_pad(petal,1) - r = captouch_get_petal_pad(petal,2) - b = captouch_get_petal_pad(petal,3) - return l - r - -def captouch_get_rad(petal): - if(petal%2): - t = captouch_get_petal_pad(petal,0) - b = captouch_get_petal_pad(petal,3) - return t - b - else: - l = captouch_get_petal_pad(petal,1) - r = captouch_get_petal_pad(petal,2) - b = captouch_get_petal_pad(petal,3) - return (l + r)/2 - b - def clear_all_leds(): for i in range(40): set_led_rgb(i, 0, 0, 0) diff --git a/usermodule/mp_hardware.c b/usermodule/mp_hardware.c index 9300df56826023a9b6f59df29cd0053d948f52a9..56f649e0227e453cb1af386f8527dac55448421b 100644 --- a/usermodule/mp_hardware.c +++ b/usermodule/mp_hardware.c @@ -74,6 +74,22 @@ STATIC mp_obj_t mp_captouch_get_petal_pad(size_t n_args, const mp_obj_t *args) { } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_captouch_get_petal_pad_obj, 2, 3, mp_captouch_get_petal_pad); +STATIC mp_obj_t mp_captouch_get_petal_rad(size_t n_args, const mp_obj_t *args) { + uint8_t petal = mp_obj_get_int(args[0]); + int32_t ret = captouch_get_petal_rad(petal); + + return mp_obj_new_int(ret); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_captouch_get_petal_rad_obj, 1, 2, mp_captouch_get_petal_rad); + +STATIC mp_obj_t mp_captouch_get_petal_phi(size_t n_args, const mp_obj_t *args) { + uint8_t petal = mp_obj_get_int(args[0]); + int32_t ret = captouch_get_petal_phi(petal); + + return mp_obj_new_int(ret); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_captouch_get_petal_phi_obj, 1, 2, mp_captouch_get_petal_phi); + STATIC mp_obj_t mp_captouch_set_petal_pad_threshold(size_t n_args, const mp_obj_t *args) { uint8_t petal = mp_obj_get_int(args[0]); uint8_t pad = mp_obj_get_int(args[1]); @@ -152,6 +168,8 @@ STATIC const mp_rom_map_elem_t mp_module_hardware_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_get_captouch), MP_ROM_PTR(&mp_get_captouch_obj) }, { MP_ROM_QSTR(MP_QSTR_captouch_get_petal_pad_raw), MP_ROM_PTR(&mp_captouch_get_petal_pad_raw_obj) }, { MP_ROM_QSTR(MP_QSTR_captouch_get_petal_pad), MP_ROM_PTR(&mp_captouch_get_petal_pad_obj) }, + { MP_ROM_QSTR(MP_QSTR_captouch_get_petal_rad), MP_ROM_PTR(&mp_captouch_get_petal_rad_obj) }, + { MP_ROM_QSTR(MP_QSTR_captouch_get_petal_phi), MP_ROM_PTR(&mp_captouch_get_petal_phi_obj) }, { MP_ROM_QSTR(MP_QSTR_captouch_set_petal_pad_threshold), MP_ROM_PTR(&mp_captouch_set_petal_pad_threshold_obj) }, { MP_ROM_QSTR(MP_QSTR_captouch_autocalib), MP_ROM_PTR(&mp_captouch_autocalib_obj) }, { MP_ROM_QSTR(MP_QSTR_get_button), MP_ROM_PTR(&mp_get_button_obj) },