About 4D Systems PTY LTD
4D Systems, Australia
Home Company Products 4DGL Developers Center Support forum Distributors News & Events Contact us App Notes
4DGL - Sharing our bytes to the world

MouseDraw.4DG


Codebase / PICASO-GFX / PICASO-GFX2 and 4DGL Display Modules / Generic Examples / Touchscreen Related

Simplified demo for using external pointing device.
Program must be loaded to FLASH
This demo will work with ALL Picaso GFX displays
Program must be loaded to FLASH, then open the
"4DGL uVGA Link" from the Workshop tool menu.

The demo works the same as the original DRAW.4DG touch example, but uses external pointing device.


About this code
Author: The 4D team
Uploaded on: 17/03/2011

Back to previous page

Pictures:

Previous Next

Source code:

#platform "uLCD-32032-P1_GFX2"
/*  4DGL Demo Application
    - Simplified Pointing Device Demo -
    -- PICASO Platform --
*/

// Simplified demo for using external pointing device
// Program must be loaded to FLASH
// This demo will work with ALL Picaso GFX displays
// Program must be loaded to FLASH, then open the
// "4DGL uVGA Link" from the Workshop tool menu.

//

#inherit "4DGL_16bitColours.fnc"

#IF EXISTS UVGA_II_GFX
    #constant SAFE_MX 0             // safe position for mouse screen copy
    #constant SAFE_MY 480           // off screen bottom left corner
    #constant SAFE_MWIDTH 12        // area size
    #constant SAFE_MHEIGHT 12
#ELSE
    // NB some display devices have extra GRAM, so mouse pointer could be hidden 'off screen'
    // Refer to individual device data sheets
    #constant SAFE_MX 0             // safe position for mouse screen copy
    #constant SAFE_MY 308           // bottom of screen bottom left corner
    #constant SAFE_MWIDTH 12        // area size
    #constant SAFE_MHEIGHT 12

#ENDIF

var combuf[100];

var touchGetX, touchGetY, touchState;

var B1State:=UP;

// initialize comms buffer for mouse
func mouseInit()
    com_Init(combuf,200,0);             // must use buffered comms
    setbaud(BAUD_19200);
endfunc

// turn mouse ON or OFF
// when turned on, pointer is shown, area under pointer is saved
// when turned off, area under pointer is restored
func mouse(var state)
    var private restore, oldx, oldy;
    var a, x, y;
    if(restore) gfx_ScreenCopyPaste(SAFE_MX, SAFE_MY, oldx, oldy, SAFE_MWIDTH, SAFE_MHEIGHT);         // restore old area if applicable
    if(state)
        x := touchGetX;
        y := touchGetY;

        // the storage area for the mouse pointer is 'off screen'
        gfx_ScreenCopyPaste(x, y, SAFE_MX, SAFE_MY, SAFE_MWIDTH, SAFE_MHEIGHT);         // the storage area for the mouse pointer is 'off screen', save new area
        a := gfx_OutlineColour(1);                                                                  // darkest colour possible so we get an outline
        gfx_TriangleFilled(x,y,x+5,y+10,x+10,y+5,WHITE);        // ptr, could use a Image Control entry
        gfx_OutlineColour(a);                                                                       // restore outline colour
        oldx := x;
        oldy := y;
        restore := 1;   // mouse now showing
    else
        restore := 0;    // mouse is hidden
    endif
endfunc

// wait for mouse/kbd byte
func mget()
    var c;
    while((c:=serin()) < 0);
    return c;
endfunc

func pollMouse()
    var c;
    if((c:=serin()) >= 0)
         // put any text in text window
        if(c<0x80 || c>0x82)
            // not used here,  capture text or proposed new driver commands
        else
            if (c==0x80)                                // pressed
                touchState := TOUCH_PRESSED;
                B1State := DOWN;
            else if (c==0x81)                           // released
                touchState := TOUCH_RELEASED;
                B1State := UP;
            else                                        // moved
                touchState := TOUCH_MOVING;
            endif
            touchGetX := (mget() + mget()<<8);
            touchGetY := (mget() + mget()<<8);

            if(B1State == UP)                           // if mouse button up, update mouse pointer
                mouse(ON);
            endif
        endif
    else
    endif
endfunc





func main()
    var firstx, firsty, x, y, state;

    mouseInit();                                                // initialize the mouse driver

    print("DRAW.....");


    mouse(ON);


    repeat
        pollMouse();
        state := touchState;                                    // look for any touch activity
        x := touchGetX;                                         // grab the x
        y := touchGetY;                                         // and the y coordinates of the touch
        if(state == TOUCH_PRESSED)                              // if there's a press
            mouse(OFF);                                         // hide mouse pointer
            firstx := x;
            firsty := y;
            //gfx_LineTo(x, y);                                   // draw a line from our previous spot
        endif

        if(state ==  TOUCH_RELEASED)                            // if there's a release;
            mouse(OFF);                                         // mouse off so it doesnt corrupt circle
            gfx_CircleFilled(x, y, 10, RED);                    // draw a red circle of radius=10
        endif

        if(state == TOUCH_MOVING)                               // if there's movement
            if(B1State == DOWN)
                //gfx_PutPixel(x, y, LIGHTGREEN);                   // we'll could draw a pixel
                gfx_Line(firstx, firsty, x, y, LIGHTGREEN);         // but lines are much better
                firstx := x;
                firsty := y;
            endif
        endif
    forever                                    // loop forever

endfunc








Copyright © 2007 4D Systems Pty Ltd, Sydney, Australia - All Rights Reserved | Terms and Conditions