Skip to content

Exiting from an OP #442

@Robecq

Description

@Robecq

I am trying to understand how you exit out of an OP and turn off the function that was running in it.
I can enter and exit the OP, but the function I ran in it still runs. I feel I am missing something simple but cant see it and would love some help-
I am using the tt-Go t-dsiplay example and am loading one of the tft_espi graphics tests from the menu. It runs as it should except I cannot exit from it.

#include "menu_manager.h"


// TFT and button objects
TFT_eSPI tft = TFT_eSPI();  // Define the TFT object
Button2 btnUp(BTN_UP);
Button2 btnDwn(BTN_DWN);
bool invertFieldKeys = false;//global fields keys inversion flag (for code driven single input)

/////
bool tft_output(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t* bitmap) {
 // Stop further decoding as image is running off bottom of screen
 if (y >= tft.height()) return 0;
 // This function will clip the image block rendering automatically at the TFT boundaries
 tft.pushImage(x, y, w, h, bitmap);
 // Return 1 to decode next block
 return 1;
}

const colorDef<uint16_t> colors[] MEMMODE = {
 { { (uint16_t)Black, (uint16_t)Black },
   { (uint16_t)Black, (uint16_t)DarkerBlue, (uint16_t)DarkerBlue } },  //bgColor
 { { (uint16_t)Gray, (uint16_t)Gray },
   { (uint16_t)White, (uint16_t)White, (uint16_t)White } },  //fgColor
 { { (uint16_t)White, (uint16_t)Black },
   { (uint16_t)Yellow, (uint16_t)Yellow, (uint16_t)Red } },  //valColor
 { { (uint16_t)White, (uint16_t)Black },
   { (uint16_t)White, (uint16_t)Yellow, (uint16_t)Yellow } },  //unitColor
 { { (uint16_t)White, (uint16_t)White },
   { (uint16_t)Black, (uint16_t)Blue, (uint16_t)White } },  //cursorColor
 { { (uint16_t)White, (uint16_t)Yellow },
   { (uint16_t)DarkerRed, (uint16_t)White, (uint16_t)White } },  //titleColor
};

//////// pages for the menu
////
result doAlert(eventMask e, prompt& item);
int test = 55;

// Setting PWM properties, do not change this!
const int pwmFreq = 5000;
const int pwmResolution = 8;
const int pwmLedChannelTFT = 0;

int ledBacklight = 80;  // Initial TFT backlight intensity on a scale of 0 to 255. Initial value is 80.

int ledCtrl = LOW;  // Initial value for external connected led

result myLedOn() {
 ledCtrl = HIGH;
 return proceed;
}
result myLedOff() {
 ledCtrl = LOW;
 return proceed;
}


TOGGLE(ledCtrl, setBuzz, "Buzzer: ", doNothing, noEvent, noStyle,  //,doExit,enterEvent,noStyle
      VALUE("On", HIGH, doNothing, noEvent),
      VALUE("Off", LOW, doNothing, noEvent));

//int selVal = 1;
TOGGLE(selVal, rotateMenu, "Rotate Screen: ", doNothing, noEvent, noStyle,
    //  VALUE("Zero", 0, updateScreenRotation, enterEvent),
      VALUE("A", 1, updateScreenRotation, enterEvent),
     // VALUE("Two", 2, updateScreenRotation, enterEvent),
      VALUE("B", 3, updateScreenRotation, enterEvent));

/*//int selVal = 1;
void updateScreenRotation() {
   switch (selVal) {
       case 0:
           tft.setRotation(0);
           break;
       case 1:
           tft.setRotation(1);
           break;
       case 2:
           tft.setRotation(2);
           break;
       case 3:
           tft.setRotation(3);
           break;
   }
}*/

int chooseTest = -1;
CHOOSE(chooseTest, chooseMenu, "Choose", doNothing, noEvent, noStyle,
      VALUE("First", 1, doNothing, noEvent),
      VALUE("Second", 2, doNothing, noEvent),
      VALUE("Third", 3, doNothing, noEvent),
      VALUE("Last", -1, doNothing, noEvent));


//////
MENU(subMenu, "Settings", doNothing, noEvent, noStyle,
    OP("Battery", doNothing, noEvent),
    SUBMENU(rotateMenu),
    SUBMENU(setBuzz),
    FIELD(ledBacklight, "Backlight: ", "", 0, 255, 10, 5, doNothing, noEvent, wrapStyle),  // Menu option to set the intensity of the backlight of the screen.
    OP("RawData", doNothing, noEvent),

    EXIT("<Back"));



MENU(mainMenu, "Main menu", doNothing, noEvent, wrapStyle,
    OP("OP_Template", OP_Template_Loop, enterEvent), // Operation1Loop is the name of the function to run
    OP("Graphics Test PDQ", TFT_graphicstest_PDQ_Loop, enterEvent),
    OP("OPeration3", doNothing, noEvent),

    SUBMENU(subMenu),
    SUBMENU(chooseMenu),
    //OP("Alert test",doAlert,enterEvent),
    EXIT("<Back"));

#define MAX_DEPTH 5

serialIn serial(Serial);

//MENU_INPUTS(in,&serial);its single, no need to `chainStream`

//define serial output device
idx_t serialTops[MAX_DEPTH] = { 0 };
serialOut outSerial(Serial, serialTops);

#define GFX_WIDTH 240
#define GFX_HEIGHT 135
#define fontW 12
#define fontH 24  // 18

constMEM panel panels[] MEMMODE = { { 0, 0, GFX_WIDTH / fontW, GFX_HEIGHT / fontH } };
navNode* nodes[sizeof(panels) / sizeof(panel)];  //navNodes to store navigation status
panelsList pList(panels, nodes, 1);              //a list of panels and nodes
idx_t eSpiTops[MAX_DEPTH] = { 0 };
TFT_eSPIOut eSpiOut(tft, colors, eSpiTops, pList, fontW, fontH + 1);
menuOut* constMEM outputs[] MEMMODE = { &outSerial, &eSpiOut };  //list of output devices
outputsList out(outputs, sizeof(outputs) / sizeof(menuOut*));    //outputs list controller

NAVROOT(nav, mainMenu, MAX_DEPTH, serial, out);

unsigned long idleTimestamp = millis();

//when menu is suspended
result idle(menuOut& o, idleEvent e) {
 if (e == idling) {
   // Show the idle message once
   int xpos = tft.width() / 2;  // Half the screen width
   tft.fillScreen(Black);

   tft.setTextSize(1);
   tft.setTextColor(Yellow, Black);
   tft.setTextWrap(false);
   tft.setTextDatum(MC_DATUM);
   tft.drawString("IDLE", xpos, 50);
   int getFontHeight = tft.fontHeight();

   tft.setTextSize(1);
   tft.setTextColor(White, Black);
   tft.setTextDatum(MC_DATUM);
   tft.drawString("Long press a button", xpos, 90);
   tft.drawString("to exit", xpos, 110);
 }
 return proceed;
}


void button_init() {
 btnUp.setLongClickHandler([](Button2& b) {
   // exit/go back
   unsigned int time = b.wasPressedFor();
   if (time >= 550) {
     nav.doNav(escCmd);
   }
 });

 btnUp.setClickHandler([](Button2& b) {
   // Up
   unsigned int time = b.wasPressedFor();
   if (time >= 50) {
     nav.doNav(downCmd);  // It's called downCmd because it decreases the index of an array. Visually that would mean the selector goes upwards.
   }
 });

 btnDwn.setLongClickHandler([](Button2& b) {
   // select/enter
   unsigned int time = b.wasPressedFor();
   if (time >= 550) {
     nav.doNav(enterCmd);
   }
 });

 btnDwn.setClickHandler([](Button2& b) {
   // Down
   unsigned int time = b.wasPressedFor();
   if (time >= 50) {
     nav.doNav(upCmd);  // It's called upCmd because it increases the index of an array. Visually that would mean the selector goes downwards.
   }
 });
}

void button_loop() {
 // Check for button presses
 btnUp.loop();
 btnDwn.loop();
}

void listDir() {
 File root = LittleFS.open("/");
 File file = root.openNextFile();
 while (file) {
   Serial.print("File: ");
   Serial.println(file.name());
   file = root.openNextFile();
 }
}


// Menu setup function
void menuSetup() {

 nav.idleTask = idle;  //point a function to be used when menu is suspended
 mainMenu[2].disable();
 //outGfx.usePreview=true;//reserve one panel for preview?
 //nav.showTitle=false;//show menu title?

 //SPI.begin(); // Leave this commented or else there will be nothing shown on the screen.

 // Initialise LittleFS
 if (!LittleFS.begin()) {
   Serial.println("LittleFS initialisation failed!");
   while (1) yield();  // Stay here twiddling thumbs waiting
 }
 listDir();
 Serial.println("\r\nInitialisation done.");

 tft.init();  // Initialize the screen



 ledcSetup(pwmLedChannelTFT, pwmFreq, pwmResolution);
 ledcAttachPin(TFT_BL, pwmLedChannelTFT);
 ledcWrite(pwmLedChannelTFT, ledBacklight);  // Set default backlight intensity

// updateScreenRotation();  // Set initial rotation based on selVal - doesnt work

 tft.setRotation(1);  // Rotate display a quarter clockwise

 tft.setTextFont(4);  // Set to built-in font number 4




 tft.setTextSize(1);
 tft.setTextWrap(false);
 tft.fillScreen(Black);

 Serial.print("Showing bootlogo... ");
 tft.setSwapBytes(true);  // We need to swap the colour bytes (endianess)

 // The jpeg image can be scaled by a factor of 1, 2, 4, or 8
 TJpgDec.setJpgScale(1);
 // The decoder must be given the exact name of the rendering function above
 TJpgDec.setCallback(tft_output);
 uint16_t w = 0, h = 0;
 TJpgDec.getFsJpgSize(&w, &h, "/BootLogo.jpg", LittleFS);  // Note name preceded with "/"
 TJpgDec.drawFsJpg(0, 0, "/BootLogo.jpg", LittleFS);
 delay(2000);



 //delay(1000);
 Serial.println("DONE");

 Serial.print("Initialize buttons... ");
 button_init();
 delay(300);
 Serial.println("DONE");

 Serial.print("Initialize battery... ");

 // batteryInit();
 batterySetup();

 delay(300);
 Serial.println("DONE");

 Serial.print("Initialize external led... ");
 if (LED_CTRL > 0) {
   pinMode(LED_CTRL, OUTPUT);
   digitalWrite(LED_CTRL, ledCtrl);
   delay(500);
   Serial.println("DONE");
 } else {
   delay(500);
   Serial.println("NOT SET UP");
 }
 delay(2000);  // A little bit more delay so that you will be able to see the bootlogo.

 Serial.println("- READY -");

 tft.fillScreen(Black);  // Clear the screen to be ready to draw the menu

 //// Operations Setups/////
 TFT_graphicstest_PDQ_Setup();
}

void menuLoop() {

 nav.poll();  //this device only draws when needed - what does this do?
 // Set blacklight intenisty of screen
 ledcWrite(pwmLedChannelTFT, ledBacklight);
 batteryLoop();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions