diff --git a/badge23/captouch.c b/badge23/captouch.c index 94e4dca54f84ea26c9ef2adf5368d24ffb458dbf..e9cb80d5b0b2f8cf751d028b46232a14288aad29 100644 --- a/badge23/captouch.c +++ b/badge23/captouch.c @@ -177,7 +177,7 @@ void gpio_event_handler(void* arg) if(chip == &chip_top) pressed_top = pressed; if(chip == &chip_bot) pressed_bot = pressed; - espan_handle_captouch(pressed_top, pressed_bot); + //espan_handle_captouch(pressed_top, pressed_bot); } } } @@ -188,14 +188,16 @@ static uint8_t bot_map[] = {1, 1, 3, 3, 5, 5, 7, 7, 9, 9}; uint16_t read_captouch(){ uint16_t petals = 0; + uint16_t top = pressed_top; + uint16_t bot = pressed_bot; for(int i=0; i<12; i++) { - if(pressed_top & (1 << i)) { + if(top & (1 << i)) { petals |= (1<<top_map[i]); } } for(int i=0; i<10; i++) { - if(pressed_bot & (1 << i)) { + if(bot & (1 << i)) { petals |= (1<<bot_map[i]); } } diff --git a/badge23/espan.c b/badge23/espan.c index 8aba033add84acf7cd458c687ec67c316b72bf43..647b47d4af0006a5f4245cf92682de47a0dc0378 100644 --- a/badge23/espan.c +++ b/badge23/espan.c @@ -67,7 +67,7 @@ void espan_handle_captouch(uint16_t pressed_top, uint16_t pressed_bot) 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); + //leds_animate(i); active_paddles[i] = true; changed = true; } else if(active_paddles[i] == true && paddles[i] == false) { @@ -88,6 +88,7 @@ void espan_handle_captouch(uint16_t pressed_top, uint16_t pressed_bot) #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) { @@ -115,9 +116,9 @@ void os_app_main(void) void * asdasd = &i; while(1) { manual_captouch_readout(1); - vTaskDelay(3 / portTICK_PERIOD_MS); + vTaskDelay((CAPTOUCH_POLLING_PERIOD) / portTICK_PERIOD_MS); manual_captouch_readout(0); - vTaskDelay(3 / portTICK_PERIOD_MS); + vTaskDelay((CAPTOUCH_POLLING_PERIOD) / portTICK_PERIOD_MS); continue; /* diff --git a/badge23/leds.c b/badge23/leds.c index f3141922690680bfecb5f93067ff80649c6aa2cf..f77e9b574a7d4f9ff743433c18240874e03900a6 100644 --- a/badge23/leds.c +++ b/badge23/leds.c @@ -128,7 +128,7 @@ struct RGB HSVToRGB(struct HSV hsv) { return rgb; } -static void renderLEDs() +void renderLEDs() { spi_device_queue_trans(spi_led, &spiTransObject, portMAX_DELAY); } @@ -242,7 +242,7 @@ static void _leds_init() { TaskHandle_t handle; - xTaskCreate(&leds_task, "LEDs player", 4096, NULL, configMAX_PRIORITIES - 2, &handle); + //xTaskCreate(&leds_task, "LEDs player", 4096, NULL, configMAX_PRIORITIES - 2, &handle); } static bool leds_active(void) @@ -336,6 +336,35 @@ static void leds_task(void* arg) { } } +void leds_set_single_rgb(uint8_t index, uint8_t red, uint8_t green, uint8_t blue){ + uint8_t c[3]; + c[0] = red; + c[1] = green; + c[2] = blue; + setPixel(&leds, index, c); +} +void leds_set_single_hsv(uint8_t index, float hue, float sat, float val){ + struct RGB rgb; + struct HSV hsv; + hsv.H = hue; + hsv.S = sat; + hsv.V = val; + + rgb = HSVToRGB(hsv); + + uint8_t c[3]; + c[0] = rgb.R; + c[1] = rgb.G; + c[2] = rgb.B; + setPixel(&leds, index, c); +} + +void leds_update(){ + vTaskDelay(10 / portTICK_PERIOD_MS); + renderLEDs(); + vTaskDelay(10 / portTICK_PERIOD_MS); +} + void leds_init() { _leds_init(); } void leds_animate(int leaf) { diff --git a/badge23/leds.h b/badge23/leds.h index a14fc13bd1aa5503a70c13161998dfdef61de1c7..f86056cb74cc8f8f522248e041f985786ffb114b 100644 --- a/badge23/leds.h +++ b/badge23/leds.h @@ -1,4 +1,9 @@ #pragma once +#include <stdint.h> void leds_init(); void leds_animate(int leaf); +void leds_set_single_rgb(uint8_t index, uint8_t red, uint8_t green, uint8_t blue); +void leds_set_single_hsv(uint8_t index, float hue, float sat, float value); +void leds_update(); + diff --git a/micropython/ports/esp32/badge23_mp_hardware.c b/micropython/ports/esp32/badge23_mp_hardware.c index c5032613ce3f61b3e4de61c3237aef43cc7af9e8..8e5bccbc4ccaf4873dc3ed76a4d77dbd24a4ae2c 100644 --- a/micropython/ports/esp32/badge23_mp_hardware.c +++ b/micropython/ports/esp32/badge23_mp_hardware.c @@ -11,6 +11,7 @@ #include "py/builtin.h" #include "py/runtime.h" #include "../badge23/audio.h" +#include "../badge23/leds.h" #include "../badge23/captouch.h" STATIC mp_obj_t mp_get_captouch(size_t n_args, const mp_obj_t *args) { @@ -46,12 +47,42 @@ STATIC mp_obj_t mp_dump_all_sources(size_t n_args, const mp_obj_t *args) { } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_dump_all_sources_obj, 0, 2, mp_dump_all_sources); + +STATIC mp_obj_t mp_set_led_rgb(size_t n_args, const mp_obj_t *args) { + uint8_t index = mp_obj_get_int(args[0]); + uint8_t red = mp_obj_get_int(args[1]); + uint8_t green = mp_obj_get_int(args[2]); + uint8_t blue = mp_obj_get_int(args[3]); + leds_set_single_rgb(index, red, green, blue); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_set_led_rgb_obj, 4, 5, mp_set_led_rgb); + +STATIC mp_obj_t mp_set_led_hsv(size_t n_args, const mp_obj_t *args) { + uint8_t index = mp_obj_get_int(args[0]); + float hue = mp_obj_get_float(args[1]); + float sat = mp_obj_get_float(args[2]); + float val = mp_obj_get_float(args[3]); + leds_set_single_hsv(index, hue, sat, val); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_set_led_hsv_obj, 4, 5, mp_set_led_hsv); + +STATIC mp_obj_t mp_update_leds(size_t n_args, const mp_obj_t *args) { + leds_update(); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_update_leds_obj, 0, 2, mp_update_leds); + 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_get_captouch), MP_ROM_PTR(&mp_get_captouch_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_dump_all_sources), MP_ROM_PTR(&mp_dump_all_sources_obj) }, + { MP_ROM_QSTR(MP_QSTR_set_led_rgb), MP_ROM_PTR(&mp_set_led_rgb_obj) }, + { MP_ROM_QSTR(MP_QSTR_set_led_hsv), MP_ROM_PTR(&mp_set_led_hsv_obj) }, + { MP_ROM_QSTR(MP_QSTR_update_leds), MP_ROM_PTR(&mp_update_leds_obj) }, }; STATIC MP_DEFINE_CONST_DICT(mp_module_hardware_globals, mp_module_hardware_globals_table); diff --git a/python_payload/boot_button_demo.py b/python_payload/boot_button_demo.py deleted file mode 100644 index 2fe1f6bbef56d0f701d9728248ccfa9d66874b7e..0000000000000000000000000000000000000000 --- a/python_payload/boot_button_demo.py +++ /dev/null @@ -1,7 +0,0 @@ -from machine import Pin -from synth import tinysynth -p = Pin(0, Pin.IN) -s = tinysynth(440,1) -while True: - if(p.value() == 0): - s.start() diff --git a/python_payload/cap_touch_demo.py b/python_payload/cap_touch_demo.py index 40db5303a6b353f8e9f0ba49423ab3779c9b971b..8933591bebbb3d691721322dd78a166a813f65bd 100644 --- a/python_payload/cap_touch_demo.py +++ b/python_payload/cap_touch_demo.py @@ -1,5 +1,6 @@ from synth import tinysynth from hardware import * +import time set_global_volume_dB(0) synths = [] @@ -7,19 +8,41 @@ for i in range(5): synths += [tinysynth(440,1)] for synth in synths: - synth.decay(300) - synth.waveform(3) + synth.decay(100) + 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]] chord = chords[3] +chord_index = -1 + +def set_chord(i): + global chord_index + global chord + if(i != chord_index): + chord_index = i + for j in range(40): + hue = int(72*(i+0.5)) % 360 + set_led_hsv(j, hue, 1, 0.5) + chord = chords[i] + print("set chord " +str(i)) + +set_chord(3) + +def cap_touch_demo_start(delay): + global chord_index + global chord + while True: + update_leds() + for i in range(10): + if(get_captouch(i)): + if(i%2): + k = int((i-1)/2) + set_chord(k) + else: + k = int(i/2) + synths[k].tone(chord[k]) + synths[k].start() + print("synth " +str(k)) + time.sleep_ms(delay) -while True: - for i in range(10): - if(get_captouch(i)): - if(i%2): - chord = chords[int((i-1)/2)] - else: - i = int(i/2) - synths[i].tone(chord[i]) - synths[i].start()