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

uVGAIIdraw.4dg


Codebase / PICASO-GFX / PICASO-GFX2 and 4DGL Display Modules / Device Specific Examples

Simple demo showing existing touch screen functions integrated with external pointing device on uVGAII.
Program must be loaded to FLASH, then open the "4DGL uVGA Link"
from the Workshop tool menu.
About this code
Author: The 4D team
Uploaded on: 17/03/2011

Back to previous page

Source code:

#platform "uVGA-II_GFX2"
/*  4DGL Demo Application
    - Pointing device Demo -
    -- PICASO Platform --
*/

// Simple demo showing existing touch screen functions integrated with
// external pointing device on uVGAII.
// Program must be loaded to FLASH, then open the "4DGL uVGA Link"
// from the Workshop tool menu.



#inherit "4DGL_16bitColours.fnc"

#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

var combuf[100];
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 := touch_Get(TOUCH_GETX);
        y := touch_Get(TOUCH_GETY);
        // 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
                pokeW(TOUCH_DRIVE, TOUCH_PRESSED);
                B1State := DOWN;
            else if (c==0x81)                           // released
                pokeW(TOUCH_DRIVE, TOUCH_RELEASED);
                 B1State := UP;
            else                                        // moved
                pokeW(TOUCH_DRIVE, TOUCH_MOVING);
            endif
            pokeW(TOUCH_RAW_X ,(mget() + mget()<<8));
            pokeW(TOUCH_RAW_Y ,(mget() + mget()<<8));

            if(B1State == UP)              // if mouse button up, and we're not tracking sliders
                mouse(ON);
            endif
        endif
    endif
endfunc





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

    mouseInit();                                                // initialize the mouse driver

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


    mouse(ON);


    repeat
        pollMouse();
        state := touch_Get(TOUCH_STATUS);                       // look for any touch activity
        x := touch_Get(TOUCH_GETX);                             // grab the x
        y := touch_Get(TOUCH_GETY);                             // and the y coordinates of the touch
        if(state == TOUCH_PRESSED)                              // if there's a press
            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);
            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)
                mouse(OFF);
                //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