II.3.3. Writing Button Event Handler
Button’s event handler should be written if any change of the button’s state/value should trigger some task. For example, if we have to indicate the change by LED indicator or to compute some intermediate parameter value. To write the handler, we have to define the handler function void onButtonChange(int buttonIndex) inside the effectModule descendant class. Here is the example code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
#include <blackstomp.h> //effect module class definition class gainDoubler:public effectModule { public: void init(); void deInit(); void onButtonChange(int buttonIndex); void process(float* inLeft, float* inRight, float* outLeft, float* outRight, int sampleCount); }; //effect module class implementation void gainDoubler::init() { //select the appropriate device by uncommenting one of the following two lines: //setDeviceType(DT_ESP32_A1S_AC101); setDeviceType(DT_ESP32_A1S_ES8388); //define your effect name name = "GAIN DOUBLER"; //define the input mode (IM_LR or IM_LMIC) inputMode = IM_LR; //setting up the buttons //setup the first button as toggle button button[0].mode = BM_TOGGLE; //enable encoder port for buttons encoderMode = EM_BUTTONS; //setup the second button as tap tempo button button[1].mode = BM_TAPTEMPO; button[1].min = 50; button[1].max = 2000; //setup the third button as temporary button button[2].mode = BM_MOMENTARY; } void gainDoubler::deInit() { //do all the necessary deinitialization here } void gainDoubler::onButtonChange(int buttonIndex) { switch(buttonIndex) { case 0: //main button state has changed { if(button[0].value) //if effect is activated { analogBypass(false); mainLed->turnOn(); } else //if effect is bypassed { analogBypass(true); mainLed->turnOff(); } break; } case 1: //the button[1] state has changed { auxLed->blink(10,button[1].value-10,1,0,0); break; } case 2: //the button[0] state has changed { //do something here break; } } } void gainDoubler::process(float* inLeft, float* inRight, float* outLeft, float* outRight, int sampleCount) { for(int i=0;i<sampleCount;i++) { outLeft[i] = 2 * inLeft[i]; outRight[i] = 2 * inRight[i]; } } //Arduino core setup //declare an instance of your effect module gainDoubler myPedal; void setup() { blackstompSetup(&myPedal); } //Arduino core loop void loop() { } |
Now you can see in the codes that handler function definition has been added to the gainDoubler class at line #9 and the implementation has been added at line #43.