Direction Pad - movement

This is a discussion on Direction Pad - movement within the C Programming forums, part of the General Programming Boards category; I am writing a menu system for an LCD screen. The screen has 6 buttons, Up, Down, Left, Right, Select, ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    37

    Direction Pad - movement

    I am writing a menu system for an LCD screen. The screen has 6 buttons, Up, Down, Left, Right, Select, Cancel.

    I have written the following code to scroll up and down through the menu. Here is my code

    Code:
    void position_cursor(void)
    {
        cursor[0] = 0x10; // 0x10 : cursor select character
        clear_cursor[0] = 0x20; //clear cursor character
       // Up Arrow Pressed
       if((data4 == 0x07) && (cursorY != 0)){ //0x07 : Up arrow release cursorY: int psoition of //cursor
                 write_cursor(cursorY, clear_cursor);
                 write_cursor(--cursorY, cursor);
       }
       
       // Down Arrow Pressed
       else if((data4 == 0x08) && (cursorY != 3)) { // 0x08: Down arrow release
                 write_cursor(cursorY, clear_cursor);
                 write_cursor(++cursorY, cursor);
       }
    
    }
    cursorY is initially set to 0. Cursor is in position 0.

    Scenario.
    Press Down: Moves cursor to position #1. Clears cursor from position #0 . Perfect
    Press Down: Nothing happens
    Press Up: Clears cursor. Does not re-write cursor
    Press Down: Re-writes cursor to position #1
    Press Down: Nothing happens
    Press Up: Clears cursor. Does not re-write cursor.
    Press Down: Re-writes cursor to position #1

    The last 2 button presses can only be executed from then on and continuously.
    Nothing happens if a button is not pressed, as should be. The previous only happens when the buttons are pressed.

    Does anyone know of an effecient algorithm to implement this?

    Any advice or suggestions would be greatly appreciated.

    Thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sorry but I can't see anything directly, immediately, wrong with your code as such.

    Some "style" comments: This is not WRONG as such, but...
    change code like this
    Code:
                 write_cursor(++cursorY, cursor);
    to something like this:
    Code:
                 ++cursorY;
                 write_cursor(cursorY, cursor);
    (Obviously likewise for the cursor up code)

    That way, the increment/decrement is much more obvious, and if you ever change write_cursor to be a macro or some such, it won't affect the update of the actual position. It also helps if you ever try to debug the code - you can check the value before you call the function, rather than having the function call and increment as one line which you can't (triivally) separate into different steps.

    Following that a step further, you can do:
    Code:
        oldCursorY = cursorY;
        ... check if cursor key pressed; update cursorY....
        if (oldCursorY != cursorY)
        {
          write_cursor(oldCursorY, clear_cursor);
          write_cursor(cursorY, cursor);
        }
    This would make smaller code, and easier to follow/debug/understand.


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mouse Maze Problem
    By Furbiesandbeans in forum C++ Programming
    Replies: 13
    Last Post: 04-28-2008, 04:20 PM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. natural leg movement
    By DavidP in forum Game Programming
    Replies: 32
    Last Post: 01-11-2004, 08:01 AM
  4. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 12:54 AM
  5. Classic problem doesn't accept direction
    By cheesehead in forum C++ Programming
    Replies: 5
    Last Post: 11-13-2001, 05:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21