// Loop ---------------------------------------------------
void loop()
{
readMidi();
if (tft.asyncUpdateActive() == false)
{
draw_Peak();
readEncoder();
readTouchscreen();
check_Symbols();
checkScreensaver();
}
if (updateScreen_Timer >= 51) // 51ms
{
tft.updateScreenAsync(false);
updateScreen_Timer = 0;
}
}
// Touchscreen query -------------------------------------------
void readTouchscreen()
{
if (touchReadTimer >= TouchRead_interval)
{
touchReadTimer = 0;
read_touch_buttons();
}
}
// read touch buttons ------------------------------------------
void read_touch_buttons()
{
// touchbutton_ID -1 = TouchScreen was not pressed
// touchbutton_ID 0 = non touchbutton array is pressed
// touchbutton_ID >0 = touchbutton array is pressed
// touchbutton_ID -2 = touchbutton array hold and move
int touchbutton_ID = -1;
static int old_touchbutton_ID = -1;
if (ts.touched())
{
TS_Point p = ts.getPoint();
p.x = map(p.x, 0, 240, 240, 0);
p.y = map(p.y, 0, 320, 320, 0);
int y = tft.height() - p.x;
int x = p.y;
if (old_touchbutton_ID == -1)
{
touchbutton_ID = get_touchbutton_ID(y, x, pageNo); // 0 non button pressed
}
if (touchbutton_ID > 0)
{
if (touchbutton_ID != old_touchbutton_ID)
{
old_touchbutton_ID = touchbutton_ID;
touchFunctionHandler(touchbutton_ID, x, y, pageNo);
}
}
else if (touchbutton_ID != old_touchbutton_ID)
{
// clear button state
old_touchbutton_ID = touchbutton_ID;
clear_touchbuttons_state(pageNo);
}
Reset_screensaver();
}
else
{
touchbutton_ID = -1;
old_touchbutton_ID = -1;
}
}
// get touch_buuton ID -----------------------------------------
int get_touchbutton_ID(int y, int x, uint8_t pageNo)
{
int touchbutton_ID = 0; // non touchbutton
// menu page 1
if (pageNo == Program_Menu)
{
for (uint8_t index = 0; index < 8; index++)
{
uint16_t x1 = TouchKey_page1[index][0];
uint16_t x2 = TouchKey_page1[index][1];
uint16_t key_width = TouchKey_page1[index][2];
uint16_t key_height = TouchKey_page1[index][3];
if ((x > x1) && (x < (x1 + key_width)))
{
if ((y > x2) && (y < (x2 + key_height)))
{
if (TK_state_P1[index] == false)
{
TK_state_P1[index] = true;
touchbutton_ID = index + 1;
}
}
}
}
}
// menu page 2
else if (pageNo == Osc_Menu)
{
for (uint8_t index = 0; index < 8; index++)
{
uint16_t x1 = TouchKey_page2[index][0];
uint16_t x2 = TouchKey_page2[index][1];
uint16_t key_width = TouchKey_page2[index][2];
uint16_t key_height = TouchKey_page2[index][3];
if ((x > x1) && (x < (x1 + key_width)))
{
if ((y > x2) && (y < (x2 + key_height)))
{
if (TK_state_P2[index] == false)
{
TK_state_P2[index] = true;
touchbutton_ID = index + 1;
}
}
}
}
}
// menu page 3 // Mixer menu
else if (pageNo == Mixer_menu)
{
for (uint8_t index = 0; index < 9; index++) // index = 9 touchbutton
{
uint16_t x1 = TouchKey_page3[index][0];
uint16_t x2 = TouchKey_page3[index][1];
uint16_t key_width = TouchKey_page3[index][2];
uint16_t key_height = TouchKey_page3[index][3];
if ((x > x1) && (x < (x1 + key_width)))
{
if ((y > x2) && (y < (x2 + key_height)))
{
if (TK_state_P3[index] == false)
{
TK_state_P3[index] = true;
touchbutton_ID = index + 1;
}
}
}
}
}
// menu page 4
else if (pageNo == Envelope_menu)
{
for (uint8_t index = 0; index < 8; index++)
{
uint16_t x1 = TouchKey_page4[index][0];
uint16_t x2 = TouchKey_page4[index][1];
uint16_t key_width = TouchKey_page4[index][2];
uint16_t key_height = TouchKey_page4[index][3];
if ((x > x1) && (x < (x1 + key_width)))
{
if ((y > x2) && (y < (x2 + key_height)))
{
if (TK_state_P4[index] == false)
{
TK_state_P4[index] = true;
touchbutton_ID = index + 1;
}
}
}
}
}
return touchbutton_ID;
}
// touch button function handler -------------------------------
void touchFunctionHandler(int touchbutton_ID, uint16_t xpos, uint16_t ypos, int pageNo)
{
switch (pageNo)
{
case Program_Menu:
handle_touchscreen_menu_1(touchbutton_ID);
break;
case Osc_Menu:
handle_touchscreen_menu_2(touchbutton_ID);
break;
case Mixer_menu:
handle_touchscreen_menu_3(touchbutton_ID, xpos, ypos);
break;
case Envelope_menu:
handle_touchscreen_menu_4(touchbutton_ID);
break;
default:
break;
}
}
// handle touchscreen Menu page 3 Mixer ------------------------------
FLASHMEM void handle_touchscreen_menu_3(int touchbutton_ID, uint16_t xpos, uint16_t ypos)
{
// menu forward button
if (touchbutton_ID == 4)
{
pageNo++;
if (pageNo >= 3)
{
pageNo = 3;
}
if (old_pageNo != pageNo)
{
old_pageNo = pageNo;
draw_menu_page(pageNo);
}
}
// menu back button
else if (touchbutton_ID == 1)
{
pageNo--;
if (pageNo <= 0)
{
pageNo = 0;
}
if (old_pageNo != pageNo)
{
old_pageNo = pageNo;
draw_menu_page(pageNo);
}
}
// Top menu 1 Programs
else if (touchbutton_ID == 5)
{
pageNo = 0;
draw_menu_page(pageNo);
}
// Top menu 2 osc1-3
else if (touchbutton_ID == 6)
{
pageNo = 1;
draw_menu_page(pageNo);
}
// Top menu 3 Filter
else if (touchbutton_ID == 7)
{
pageNo = 2;
draw_menu_page(pageNo);
}
// Top menu 4 Envelope
else if (touchbutton_ID == 8)
{
pageNo = 3;
draw_menu_page(pageNo);
}
// Osc1 Mixer
else if (touchbutton_ID == 9)
{
//Serial.print("Ypos: "); Serial.println(ypos);
//Serial.print("oscMix: "); Serial.println(oscMix[0]);
if (ypos >= 195)
{
ypos = 195;
}
else if (ypos <= 41)
{
ypos = 41;
}
static uint8_t old_val = 0;
oscMix[0] = 127 - ((ypos - 41) * 0.82);
if (oscMix[0] != old_val)
{
old_val = oscMix[0];
float Oscgain = 0.00787 * oscMix[0];
osc1GainMix(Oscgain);
uint8_t level = 0.93 * oscMix[0];
if (level <= 1)
{
level = 1;
}
tft.fillRect(20, 70, 20, 120, ILI9341_DARKERGREEN);
tft.fillRect(42, 70, 20, 120, ILI9341_DARKERGREEN);
tft.fillRect(20, 70 + (120 - level), 20, level, ILI9341_DARKGREEN);
tft.fillRect(42, 70 + (120 - level), 20, level, ILI9341_DARKGREEN);
tft.drawFastHLine(20, 70 + (120 - level), 20, ILI9341_YELLOW);
tft.drawFastHLine(20, 70 + (119 - level), 20, ILI9341_YELLOW);
tft.drawFastHLine(42, 70 + (120 - level), 20, ILI9341_YELLOW);
tft.drawFastHLine(42, 70 + (119 - level), 20, ILI9341_YELLOW);
}
}
}
// clear touchhandler state ------------------------------------
void clear_touchbuttons_state(int pageNo)
{
switch (pageNo)
{
case 0:
for (uint8_t index = 0; index < 8; index++)
{
if (TK_state_P1[index] == true)
{
TK_state_P1[index] = false;
}
}
break;
case 1:
for (uint8_t index = 0; index < 8; index++)
{
if (TK_state_P2[index] == true)
{
TK_state_P2[index] = false;
}
}
break;
case 2:
for (uint8_t index = 0; index < 13; index++)
{
if (TK_state_P3[index] == true)
{
TK_state_P3[index] = false;
}
}
break;
case 3:
for (uint8_t index = 0; index < 8; index++)
{
if (TK_state_P4[index] == true)
{
TK_state_P4[index] = false;
}
}
break;
default:
break;
}
}