Hi.
Got a music player project where I am using bitmaps to make an icon menu.
I'm using a button library to handle button stuff, it seems to work if I make a simple code with just button inputs.
I'm sure I'm missing something with the loops, something I haven't considered.
The buttons works and it does reach the end of the if loop and switch case statements, but it is sporadic behaviour. Can't get it to go both left and right, sometimes freezes. I can't understand what it wrong. I'm using Seeeduino Xiao, pins are connected through button to ground. I'm posting part of the code that doesn't work, it handles only the main menu. This is to avoid overwhelming readers, I hope this will be enough, but if I missed including something let me know.
Got a music player project where I am using bitmaps to make an icon menu.
I'm using a button library to handle button stuff, it seems to work if I make a simple code with just button inputs.
I'm sure I'm missing something with the loops, something I haven't considered.
The buttons works and it does reach the end of the if loop and switch case statements, but it is sporadic behaviour. Can't get it to go both left and right, sometimes freezes. I can't understand what it wrong. I'm using Seeeduino Xiao, pins are connected through button to ground. I'm posting part of the code that doesn't work, it handles only the main menu. This is to avoid overwhelming readers, I hope this will be enough, but if I missed including something let me know.
Code:
void loop() { //frame Coords: 0,0 , 32,0 , 64,0 , 96,0 32 widht 21 length
//add menuState 0 (music player default)
//menuState 1, main menu
if (menuState == 1) { //main menu, does not handle any commands to DFplayer
u8g2.clearBuffer(); //this clears the buffer entirely, add the main menu bitmap, draws a frame around the first item then pushes it to screen.
u8g2.drawXBMP(0, 0, 128, 32, mainMenu);
//u8g2.drawFrame(0, 0, 32, 21);
u8g2.sendBuffer();
Serial.println("draw menu"); //debug
//cursorPos = 0;
Serial.println(cursorPos); //debug
switch (cursorPos) { //each case instance handles removal and insertion of applicable frame
case 0: //cursor is folders
menuFrameClear(); //makes all frames transparent in buffer
u8g2.drawFrame(0, 0, 32, 21); //add correct frame, push to screen
u8g2.sendBuffer();
Serial.println("frame drawn"); //debug
while (!btnMenu.pressed() && !btnLeft.pressed() && !btnRight.pressed()){ //required to make code pause until input is received
if (btnMenu.pressed()) { //if menu button is pressed, enter folder menu
menuState = 2; //this changes the menu state. Program should then exit the parent if loop and proceed to next if loop (next menu). This rarely happens.
cursorPos = 0; //menuState 2 handles removal of previous menu. Cursor remains in 0 position.
Serial.println("menu case 0"); //debug
}
if (btnRight.pressed()) {
cursorPos = 1; //move cursor to right
Serial.println("right case 0"); //debug
}
if (btnLeft.pressed()) {
cursorPos = 3; //loop around to the left
Serial.println("left case 0"); //debug
}
}
case 1: //cursor is at play mode
menuFrameClear(); //remove all frames, add new one
u8g2.drawFrame(32, 0, 32, 21);
u8g2.sendBuffer();
while (!btnMenu.pressed() && !btnLeft.pressed() && !btnRight.pressed()){
if (btnMenu.pressed()) {
menuState = 3; //set menu to play mode, state 3 menu handles delete of previous screen
cursorPos = 0;
Serial.println("menu case 1");
}
if (btnRight.pressed()) {
cursorPos = 2; //send cursor to the right
Serial.println("right case 1");
}
if (btnLeft.pressed()) {
cursorPos = 0; //send cursor to the Left
Serial.println("left case 1");
}
}
case 2:
menuFrameClear(); //remove all frames add new one
u8g2.drawFrame(64, 0, 32, 21);
u8g2.sendBuffer();
while (!btnMenu.pressed() && !btnLeft.pressed() && !btnRight.pressed()){
if (btnMenu.pressed()) {
menuState = 4; //set menu to EQ mode, state 4 menu handles delete of previous screen
cursorPos = 0;
Serial.println("menu case 2");
}
if (btnRight.pressed()) {
cursorPos = 3; //send cursor to the right
Serial.println("right case 2");
}
if (btnLeft.pressed()) {
cursorPos = 1; //send cursor to the Left
Serial.println("left case 2");
}
}
case 3:
menuFrameClear(); //remove all frames
u8g2.drawFrame(96, 0, 32, 21);
u8g2.sendBuffer();
while (!btnMenu.pressed() && !btnLeft.pressed() && !btnRight.pressed()){
if (btnMenu.pressed()) {
menuState = 5; //set menu to music mode, state 5 menu handles delete of previous screen
cursorPos = 0;
Serial.println("menu case 3");
}
if (btnRight.pressed()) {
cursorPos = 0; //send cursor to the right
Serial.println("right case 3");
}
if (btnLeft.pressed()) {
cursorPos = 2; //send cursor to the Left
Serial.println("left case 3");
}
}
}
}