This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Possible to have states?

I'm trying to implement states for a dynamic menu that's being drawn on my LCD. From my understanding, in the main block, we need an infinite loop to run commands. I am trying to call a drawMenuBars() function, which also contains another while(!nextScreen) loop that will not break until nextScreen goes high.

For some reason my LCD screen goes all funky as if a whole bunch of hex codes are being sent. I can only assume this is because there are 2 while loops. I want to stay in the drawMenuBars function until next is pressed, but it seems like I can't have more than one infinite loop. Can someone tell me how to get around this issue so I can implement states pending where a user is?

  • how can anybody help you when you refer to vague functions like 'drawMenuBars' without any explanation? please post code snippets and provide more details.

  • I was hoping for someone to just tell me whether I could have an infinite loop in my main block as well as an infinite loop in a function to maintain a state. If you insist to see my code here it is.

    main.c

    /*******************************************************************************
    **   Main Function  main()
    *******************************************************************************/
    int main (void)  {
            int boot = 0;
    //      int menuState = 0;      // Used to determine which menu bar is highlighted
      /* Initialize interfaces */
      ledInit();
      buttonInit();
      i2cInit();
    
      /* Main Loop */
      while(1){
                    if(boot == 0) {
                            drawBootScreen();
                            boot = 1;
                    }
                    if(nextButtonPressed())
                            drawMenuScreen();
            }
    }
    

    drawMenuScreen()

    // Draws the menu bars after boot screen
    void drawMenuScreen(void) {
            u08 clear[2] = {0xfe,0x58};
            int nextPos = 0;        // Next menu bar position
            int menuState = 0;      // Tells which menu is highlighted
            u08* text = "hi";
            u08 reset[2] = {0xfe,0x48};
            int next = 0;   // Boolean that tells if next is pressed
    //      int menu = 0;   // Menu tracker to determine which bar to highlight
            i2cMasterSendNI(TW_MR_DATA_ACK, 0x02, clear);
            drawTitleBar();
            while(!next) {
    /*              while(menuState==0) {
                            nextPos = drawMenuBox(0,10,"Start New Workout",1);
                            if(downButtonPressed()) {
                                    nextPos = drawMenuBox(0,10,"Start New Workout",0);
                                    menuState = 1;
                            }
                            if(nextButtonPressed())
                                    next = 1;
                    }
    */
                    nextPos = drawMenuBox(0,10,"Start New Workout",1);
                    nextPos = drawMenuBox(0,nextPos,"Resume Workout",0);
                    nextPos = drawMenuBox(0,nextPos,"Connect to PC",0);
                    nextPos = drawMenuBox(0,nextPos,"Settings",0);
                    nextPos = drawMenuBox(0,nextPos,"History",0);
                    if(downButtonPressed())
                            next =1;
            }
            i2cMasterSendNI(TW_MR_DATA_ACK,0x02,clear);
    }
    

    drawMenuBox function

    // Draw menu box with top left corner at (x,y) w or w/o shading
    int drawMenuBox(int x, int y, char* text, int shading) {
            u08 x1 = intToHex(x);
            u08 y_box       = intToHex(y);
            u08 y_text = intToHex(y+2);
            u08 y2 = intToHex(y + MENU_BAR_HEIGHT);
            int nextYcoord = y + MENU_BAR_HEIGHT;
            u08 len = intToHex(strlen(text));
            u08 cmd1[7] = {0xfe,0x78,0x01,0x00,0x00,0xfe,0x13};     // Draw box
            u08 cmd2[4] = {0xfe,0x79,0x00,0x00};    // Move cursor to (x,y)
            u08* cmd3 = (u08*)text;
            u08 cmd4[2] = {0xfe,0x48};
            if(shading == 1) {
                    cmd1[2] = 0x01;
                    cmd1[3] = x1;
                    cmd1[4] = y_box;
                    cmd1[6] = y2;
                    cmd2[2] = x1;
                    cmd2[3] = y_text;
            }
            else {
                    cmd1[2] = 0x00;
                    cmd1[3] = x1;
                    cmd1[4] = y_box;
                    cmd1[6] = y2;
                    cmd2[2] = x1;
                    cmd2[3] = y_text;
            }
            i2cMasterSendNI(TW_MR_DATA_ACK, 0x7, cmd1);
            i2cMasterSendNI(0x50, 0x4, cmd2);
            i2cMasterSendNI(TW_MR_DATA_ACK, len, cmd3);
            i2cMasterSendNI(TW_MR_DATA_ACK, 0x02, cmd4);
            return nextYcoord;
    }
    

    drawTitleBar function

    // Draws the title bar displaying battery life(***Not yet implemented***) and cFit header
    void drawTitleBar(void) {
            u08 cmd1[6] = {0xfe,0x6c,0x00,0x09,0xfe,0x09};  // Draw line across y=9
            u08 cmd2[4] = {0xfe,0x79,0x70,0x00};    // Move cursor to (0,124)
            u08* cmd3 = "cFit";   // Write to screen
            u08 cmd4[7] = {0xfe,0x78,0x01,0xe3,0x01,0xed,0x05};     // Draw battery box
            u08 cmd5[7] = {0xfe,0x78,0x01,0xed,0x02,0xef,0x04};     // Draw battery tip
            i2cMasterSendNI(TW_MR_DATA_ACK, 0x06, cmd1);
            i2cMasterSendNI(TW_MR_DATA_ACK, 0x04, cmd2);
            i2cMasterSendNI(TW_MR_DATA_ACK, 0x04, cmd3);
            i2cMasterSendNI(TW_MR_DATA_ACK, 0x07, cmd4);
            i2cMasterSendNI(TW_MR_DATA_ACK, 0x07, cmd5);
    }
    

    I have tried running one command in the drawMenuScreen() loop. If drawTitleBar() is inside the loop it works. If I call a drawMenuBar the command is called multiple times but it is not drawing in the right place. I have a feeling it has something to do with the LCD more than the ARM7's functionality, and that one of my commands causes the x,y coordinates to change.

  • N/m I just needed to put a delay after a button signal goes high because the clock cycle is so fast.