Skip to content

Commit

Permalink
touch pad setup
Browse files Browse the repository at this point in the history
  • Loading branch information
marchingband committed Jun 7, 2021
1 parent 1c6c0fb commit 9553968
Show file tree
Hide file tree
Showing 21 changed files with 476 additions and 281 deletions.
21 changes: 19 additions & 2 deletions src/WVR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,42 @@

WVR::WVR()
{
// this->autoConfigPins = true;
this->wifiIsOn = get_wifi_is_on();
// this->mute = false;
// this->globalVolume = 127;
}

void WVR::begin()
{
wvr_init();
}

void WVR::play(uint8_t voice, uint8_t note, uint8_t velocity)
{
play_wav(voice,note,velocity);
}

void WVR::stop(uint8_t voice, uint8_t note)
{
stop_wav(voice,note);
}
void WVR::serverPause()

void WVR::wifiOff()
{
server_pause();
this->wifiIsOn = get_wifi_is_on();
}
void WVR::serverResume()

void WVR::wifiOn()
{
server_resume();
this->wifiIsOn = get_wifi_is_on();
}

void WVR::toggleWifi()
{
this->wifiIsOn ? wifiOff() : wifiOn();
// wifiIsOn = !wifiIsOn;
this->wifiIsOn = get_wifi_is_on();
}
12 changes: 9 additions & 3 deletions src/WVR.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@
class WVR {
public:
WVR();
void begin();
void begin(void);
void play(uint8_t voice, uint8_t note, uint8_t velocity);
void stop(uint8_t voice, uint8_t note);
void serverPause();
void serverResume();
void wifiOff(void);
void wifiOn(void);
void toggleWifi(void);
// int globalVolume;
// bool mute;
// bool autoConfigPins;
bool wifiIsOn;

};

#endif
109 changes: 103 additions & 6 deletions src/button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,40 @@
#include "button.h"
#include "wvr_pins.h"
#include "ws_log.h"
#include "file_system.h"
#include "wav_player.h"
#include "midi_in.h"
#include "WVR.h"

#define TOUCH_THRESH_NO_USE (0)
#define TOUCH_THRESH_PERCENT (80)
#define TOUCHPAD_FILTER_TOUCH_PERIOD (10)

xQueueHandle gpio_queue_handle;
xTaskHandle gpio_task_handle;


static void gpioTask(void* x) {
button_event_t *event;
uint32_t touch_reg = 0;
for(;;) {
if(xQueueReceive(gpio_queue_handle, &event, portMAX_DELAY)) {
event->button->handleChange(event->val);
log_i("gpio task pin:%d val:%d",event->pin, event->val);
if(event->button->touch != 1)
{
// digital
event->button->handleChange(event->val);
}
else
{
// touch
touch_reg = ( touch_reg | (uint32_t)event->val ); // cache the new parts of this event
if((touch_reg >> gpioNumToTPNum(event->pin)) & 0x01)
{
event->button->handleChange(0); // 0 to invert the edge
touch_reg &= ~(1UL << gpioNumToTPNum(event->pin)); // clear that bit from the cache but leave the rest for the other touch interrupts
}
}
}
};
}
Expand All @@ -24,13 +49,40 @@ void IRAM_ATTR isr(void *e){
xQueueSendFromISR(gpio_queue_handle, &event, NULL);
}

void IRAM_ATTR touch_isr(void *e){
// isr_log_i("touch isr");
button_event_t *event = (button_event_t*)e;
uint32_t pad_intr = touch_pad_get_status();
touch_pad_clear_status();
// send the whole register to the queue so the other pad interrupts can read it even though its bee cleared
event->val = pad_intr;
xQueueSendFromISR(gpio_queue_handle, &event, NULL);
}

Button::Button(int pin, int mode, int dbnc){
this->pin = pin;
this->mode = mode;
this->dbnc = dbnc;
this->last = 0;
this->touch = false;
this->handlePress = NULL;
this->handleRelease = NULL;
event.pin = pin;
event.button = this;
// gpio_reset_pin(gpioNumToGpioNum_T(pin));
}

Button::Button(int pin, int mode, int dbnc, bool touch){
this->pin = pin;
this->mode = mode;
this->dbnc = dbnc;
this->last = 0;
this->touch = true;
this->handlePress = NULL;
this->handleRelease = NULL;
event.pin = pin;
event.button = this;
// gpio_reset_pin(gpioNumToGpioNum_T(pin));
}

Button::~Button(){
Expand All @@ -40,7 +92,16 @@ Button::~Button(){

void Button::onPress(void(*handlePress)()){
this->handlePress = handlePress;
attachInterruptArg(pin, isr, (void*)&event, CHANGE);
if(this->touch != 1)
{
// digital read mode
attachInterruptArg(pin, isr, (void*)&event, CHANGE);
}
else
{
// capacitive touch mode
init_touch_pad(pin, (void*)&event);
}
}

void Button::onRelease(void(*handleRelease)()){
Expand All @@ -50,6 +111,7 @@ void Button::onRelease(void(*handleRelease)()){

void Button::handleChange(int val){
// if EDGE_NONE then ignore
log_i("pin:%d val:%d",pin,val);
if(mode != RISING && mode != FALLING) return;
int now = millis();
if((now - last) > dbnc){
Expand All @@ -58,18 +120,53 @@ void Button::handleChange(int val){
(val==0 && mode == FALLING) ||
(val==1 && mode == RISING)
){
if(handlePress){
if(handlePress != NULL)
{
handlePress();
}
} else {
if(handleRelease){
}
else
{
if(handleRelease != NULL)
{
handleRelease();
}
}
}
}

static bool touch_initialized = false;

void init_touch(void)
{
log_i("*");
touch_pad_init();
touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
touch_pad_set_voltage(TOUCH_HVOLT_2V7, TOUCH_LVOLT_0V5, TOUCH_HVOLT_ATTEN_0V5);
// touch_pad_filter_start(TOUCHPAD_FILTER_TOUCH_PERIOD);
// touch_initialized = true;
}

void init_touch_pad(int pin, void *event)
{
log_i("*");
if(!touch_initialized)
{
init_touch();
}
uint16_t touch_value;
touch_pad_config( gpioNumToTPNum(pin), TOUCH_THRESH_NO_USE);
touch_pad_filter_start(TOUCHPAD_FILTER_TOUCH_PERIOD);
touch_initialized = true;

touch_pad_read_filtered( gpioNumToTPNum(pin), &touch_value);
ESP_ERROR_CHECK(touch_pad_set_thresh( gpioNumToTPNum(pin), touch_value * 2 / 3));
log_i("touch setup TPNUM:%d touchValue:%d",gpioNumToTPNum(pin),touch_value);
touch_pad_isr_register(touch_isr, (void*)event);
touch_pad_intr_enable();
}

void button_init(){
gpio_queue_handle = xQueueCreate(10, sizeof(button_event_t));
xTaskCreate(gpioTask, "gpio_task", 2048, NULL, 3, &gpio_task_handle);
}
}
6 changes: 5 additions & 1 deletion src/button.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@ class Button {
int dbnc;
int last;
int mode;
bool touch;
void (*handlePress)();
void (*handleRelease)();
button_event_t event;
Button(int pin, int mode, int dbnc);
Button(int pin, int mode, int dbnc, bool touch);
~Button();
void onPress(void(*handlePress)());
void onRelease(void(*handlePress)());
void handleChange(int val);
};

void button_init();
void button_init(void);
void init_touch(void);
void init_touch_pad(int pin, void *event);

#endif
15 changes: 10 additions & 5 deletions src/dev_board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ extern "C" void encoder_init(void);
void rgb_init(void);
extern "C" void pot_init(void);

Button b1(D5,FALLING, 50);
Button b2(D6,FALLING, 50);
Button b3(D7,FALLING, 50);
Button s1(D3,FALLING, 50);
Button s2(D4,FALLING, 50);
// Button b1(D5,FALLING, 50);
// Button b2(D6,FALLING, 50);
// Button b3(D7,FALLING, 50);
// Button s1(D3,FALLING, 50);
// Button s2(D4,FALLING, 50);

void logB1(){
wlog_i("1");
Expand Down Expand Up @@ -45,6 +45,11 @@ void logSw2Down(){
}

void dev_board_init(){
Button b1(D5,FALLING, 50);
Button b2(D6,FALLING, 50);
Button b3(D7,FALLING, 50);
Button s1(D3,FALLING, 50);
Button s2(D4,FALLING, 50);

button_init();
encoder_init();
Expand Down
20 changes: 19 additions & 1 deletion src/file_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,8 @@ void add_wav_to_file_system(char *name,int voice,int note,size_t start_block,siz
voice_data[note].start_block = start_block;
voice_data[note].length = size;
voice_data[note].empty = 0;
memcpy(voice_data[note].name,name,24);
bzero(voice_data[note].name,24);
strcpy(voice_data[note].name,name);
ESP_ERROR_CHECK(emmc_write(voice_data,voice_start_block,BLOCKS_PER_VOICE));
read_wav_lut_from_disk();
// read_rack_lut_from_disk();
Expand Down Expand Up @@ -1142,6 +1143,23 @@ void updatePinConfig(cJSON *config){
cJSON_Delete(json);
}

void log_pin_config(void)
{
for(int i=0;i<14;i++)
{
log_i("pin %d action:%d edge:%d gpio:%d note:%d touch:%d velocity:%d dbnc:%d",
i,
pin_config_lut[i].action,
pin_config_lut[i].edge,
pin_config_lut[i].gpio_num,
pin_config_lut[i].note,
pin_config_lut[i].touch,
pin_config_lut[i].velocity,
pin_config_lut[i].debounce
);
}
}

void updateMetadata(cJSON *config){
cJSON *json = cJSON_Parse(config);
metadata.global_volume = cJSON_GetObjectItemCaseSensitive(json, "globalVolume")->valueint;
Expand Down
1 change: 1 addition & 0 deletions src/file_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ size_t search_directory(struct wav_lu_t *_data, size_t num_data_entries, size_t
void current_bank_up(void);
void current_bank_down(void);
struct metadata_t *get_metadata(void);
void log_pin_config(void);

#ifdef __cplusplus
}
Expand Down
Loading

0 comments on commit 9553968

Please sign in to comment.