;Download this code into your VM-1 QVGA starter kit, ; then type 'Run', or hit F10. ;****** Calibration: ****** ;Type 'force_cal' at the command line to force a calibration of the ;touchscreen at next startup. ;In VenomIDE, you can simply put your cursor on the next line and hit F4: ; force_cal ;Define some constants: #DEFINE XSZ 320 ;x length pixels #DEFINE YSZ 240 ;y height pixels ;Init procedure: called by the default startup procedure. ;In this case, initialises the graphics LCD and associated objects. TO init MAKE bias_control Digital(22) ;controls power to lcd bias cct. bias_control.On ;switch on fet/power to display MAKE glcd GraphicsLCD (3 XSZ YSZ) WAIT 50 ;Power sequencing glcd.On ; --"-- win := glcd.Window (0 0 XSZ YSZ 0);full screen window MAKE touch TouchScreen(0 1 144) MAKE kpd Keypad(touch) kbuff := kpd.InputBuffer(6,10) ;'Keypad input buffer' - facilitates reading keys from keypad. MAKE eeprom SafeData(0 1 162) ;to hold touchscreen calibration data, and other non-volatile data if you like. END ;Main procedure: called by the default startup procedure. ;Runs different demos - see comments. TO main START EVERY 100 glcd.Update ;task to update display check_cal_touch ;select which of these demos you wish to run by un-commenting it ; and downloading the procedure again. line_to ;DEMO #1 ;virtual_keypad;DEMO #2 END ;***************** SIMPLE TOUCH TEST ************************** TO line_to LOCAL starting := TRUE PRINT TO win , FONT 1 , "You can draw curves on the panel." ,CR, FONT 0 ,"[See procedure 'main' to change demo]" EVERY 50 [ IF touch.Asserted ;Touch detected... [ IF starting [ ;There is no previous line to append to, so start ; a new line by drawing a point here. win.Line(touch.Value(0),touch.Value(1),touch.Value(0),touch.Value(1)) starting := FALSE ] ELSE ;Draw a line from the last line's end to the new touch. win.Line(touch.Value(0),touch.Value(1)) ] ELSE starting := TRUE ; No touch detected, so start new line next time. ] END ;***************** END SIMPLE TOUCH TEST ************************** ;***************** VIRTUAL KEYPAD ************************** ;This set of procedures shows you how to create a virtual keypad on ;the touchscreen and read keypresses from it. TO virtual_keypad LOCAL offset PRINT TO win, FONT 1 , "Virtual keypad",CR ;Define some virtual keys, and put boxes round them to show ;where they are on the panel. touch.Key ;[remove any pre-existing virtual keys on the touchscreen.] REPEAT 4 [ offset := INDEX0*70 virtual_key(50+offset, 120, 80+offset, 150, INDEX0) ] PRINT TO win, HOME, FONT 1 , "Virtual keypad",CR, FONT 0 EVERY 40 [ kpd.Update ;scan the keypad for the benefit of the keypad input buffer. SELECT CASE kbuff.Key ;Act on which key is pressed (or do nothing). CASE 0 PRINT TO win, "key 0 " CASE 1 PRINT TO win, "key 1 " CASE 2 PRINT TO win, "key 2 " CASE 3 PRINT TO win, "key 3 " ] END ;Create a single virtual key, with a box to show it's position on the display, ; and a number to label it. TO virtual_key(x1,y1,x2,y2, key_num) touch.Key(x1,y1,x2,y2) PRINT TO win, FONT 1, GOTOXY(x1+12,y1+8), key_num:1 win.Box(x1,y1,x2,y2,1) END ;***************** END VIRTUAL KEYPAD ************************** ;***************** TOUCHSCREEN CALIBRATION ************************** #DEFINE TWOBYTES 16 ; The data size of touchsreen calibration constants. TO check_cal_touch ;If checksum ok read calibration values ;Else re-calibrate touchscreen IF eeprom.Checksum(0, 7) = eeprom.(8, TWOBYTES) ;Checksum OK? [ ;Yes: load the calibration into the touchscreen object. REPEAT 4 touch.Conversion(INDEX0) := eeprom.(INDEX0*2, TWOBYTES) ] ELSE ; Recalibrate. [ calibrate_touch ;store calibration values REPEAT 4 eeprom.(INDEX0*2, TWOBYTES) := touch.Conversion(INDEX0) ;reset checksum eeprom.(8 TWOBYTES) := eeprom.Checksum(0 7) ] END #DEFINE POSA 50 ;Offset of cross. #DEFINE SZ 5 ;Size of cross arms TO calibrate_touch DO [ PRINT TO win, CLS, "Touchscreen Calibration. Please touch the crosses:" ;Draw cross on screen at 50,50, from bottom left win.Line(POSA-SZ, POSA, POSA+SZ, POSA) ;Horiz line win.Line(POSA, POSA-SZ, POSA, POSA+SZ) ;Vert line ;Store reading touch.Conversion(1, POSA, POSA) PRINT BEEP PRINT TO win, CLS, "Touchscreen Calibration. Please touch the crosses:" ;Draw cross on screen at 50,50 from top right win.Line(XSZ-POSA-SZ, YSZ-POSA, XSZ-POSA+SZ, YSZ-POSA) win.Line(XSZ-POSA, YSZ-POSA-SZ, XSZ-POSA, YSZ-POSA+SZ) ;Store reading touch.Conversion(2, XSZ-POSA, YSZ-POSA) PRINT BEEP ] UNTIL check_calibration END TO check_calibration LOCAL ky PRINT TO win, CLS touch.Key ;Remove any exisitng keys from the touchscreen object. ;draw a confirmation key. touch.Key(298, 0, 318, 20) ;key 0 win.Box(298, 0, 318, 20, 1) PRINT TO win, FONT 0, RIGHT, GOTOXY(298-2, 5), "Confirm calibration:" ;make a key that is the rest of the display. touch.Key(0, 0, 320, 240) ;key 1 - the whole display! ky := kpd.Get IF ky = 1 PRINT TO win, CLS, "Calibration failed - please try again" ELSE PRINT TO win, CLS, "Calibration successful" WAIT 2000 PRINT TO win, CLS RETURN ky = 0 ;return true if was key zero, END ;Call this from the command line to force a calibration. TO force_cal MAKE eeprom SafeData(0 1 162) eeprom.(8 TWOBYTES) := NOT eeprom.(8 TWOBYTES) ;upset the checksum. PRINT "Calibration forced at next startup",CR END ;***************** END TOUCHSCREEN CALIBRATION **************************