Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 16:53 02 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Trying to implement swipe!

Author Message
Bizzie
Senior Member

Joined: 06/07/2014
Location: Australia
Posts: 192
Posted: 04:02pm 05 Jan 2016
Copy link to clipboard 
Print this post

Hi all,

I would like to be able to change screens by swiping side ways on the display.

Here is my setup -

> Micromite Plus MMBasic Ver 4.7 Beta 36
Copyright 2011-2015 Geof>
> option list
OPTION BAUDRATE 115200
OPTION LCDPANEL ILI9341, LANDSCAPE, 21, 22, 23
OPTION TOUCH 18, 14
OPTION SDCARD 49
> list

Option explicit

Dim Last = 0

GUI INTERRUPT PenDown,PenUp



Sub PenDown
Print "Down Touch(X),Touch(LASTX) => ";Touch(X),Touch(LASTX)
Last = Touch(LASTX)
End Sub

Sub PenUp
Print "Touch(LASTX), Last => ";Touch(LASTX),Last
End Sub

Do

Loop

> run
Down Touch(X),Touch(LASTX) => 43 -1
Up Touch(X),Touch(LASTX), Last => -1 -1
Down Touch(X),Touch(LASTX) => 42 -1
Up Touch(X),Touch(LASTX), Last => -1 -1
Down Touch(X),Touch(LASTX) => 164 -1
Up Touch(X),Touch(LASTX), Last => -1 -1
>

I do not seem to be able to pick up a true value for LASTX (or Last)!

What do I not understand?

Rob

Rob White
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 04:24pm 06 Jan 2016
Copy link to clipboard 
Print this post

TOUCH(LASTX) does seem to be faulty (Beta 37)

Any use of the touch screen will be difficult to master due to the inconsistent nature of our touching methods.

This code seems to do a fairly good job of detecting 'swipes'
It might conflict with any other code such as GUI buttons.

' TassyJim
'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
OPTION EXPLICIT
DIM Last = 0, Last1 = 0
GUI INTERRUPT PenDown ' ,PenUp

DO

LOOP
END

SUB PenDown
Last = TOUCH(X)
IF Last > -1 THEN
GUI INTERRUPT 0 ' make sure we don't call again until finished processing
PRINT "Down Touch(X) => ";Last
SETTICK 100, swipe ' check touch position 100 ms later
ENDIF
END SUB

SUB swipe
LOCAL n
SETTICK 0, swipe ' make sure we don't call again until finished processing
FOR n = 1 TO 100 ' keep trying for a positive touch
Last1 = TOUCH(X)
IF Last1 > -1 THEN EXIT FOR
NEXT n

IF Last1 > -1 THEN
IF (Last1 - Last) < -5 THEN ' moved at least 5 pixels
PRINT "Swipe left ";Last1;" < ";Last
ELSEIF (Last1 - Last) > 5 THEN
PRINT "Swipe right ";Last1;" > ";Last
ENDIF
ENDIF
GUI INTERRUPT PenDown ' turn the touch interrupt back on
END SUB



A button might not look as fancy but it will be more reliable.

JimEdited by TassyJim 2016-01-08
VK7JH
MMedit
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9610
Posted: 04:42pm 06 Jan 2016
Copy link to clipboard 
Print this post

No offence at all intended, but if you need swipe, perhaps you should be writing code for Android devices instead.

The ARM CPU/GPU thing has the juice to be able to do this kind of thing, whereas the PIC32 does not, really.

I use buttons to navigate between menus(just as Jim suggests) - see my thread about numberboxes erasing background text to see how I have one of my menu screens setup.

Simple, but effective.

Not to discourage you though - I will be watching this thread to see if you(or others) come up with something that works.
Smoke makes things work. When the smoke gets out, it stops!
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 06:10pm 06 Jan 2016
Copy link to clipboard 
Print this post


As JFK would put it:

“We choose to go with the swipe in this decade and do the other things, not because they are easy, but because they are hard."

Jim


VK7JH
MMedit
 
Bizzie
Senior Member

Joined: 06/07/2014
Location: Australia
Posts: 192
Posted: 06:18pm 06 Jan 2016
Copy link to clipboard 
Print this post

I deliberated whether to use the word swipe or not.

I was only intending to pick up whether movement was from left to right or vice a versa and then display the next or previous page. No scrolling of image - yes not enough horse power!


Rob White
 
Bizzie
Senior Member

Joined: 06/07/2014
Location: Australia
Posts: 192
Posted: 12:16pm 09 Jan 2016
Copy link to clipboard 
Print this post

Here is an implementation of TassyJim's code above. Which pages through 8 pages using swipe left or right.

' Example of a GUI setup using horizontal swiping to change pages (screens)
' Written by Rob White with help from TasyJim
'
' This is an example of presenting various pages to the user. No variables
' are actually set.

Option EXPLICIT
GUI DEFAULT HIDDEN
GUI INTERRUPT guiAction

Const MaxWhls = 6
Const MaxPages = 8
Const CtrlColor = RGB(Yellow)
Const BckColor = RGB(Black)
Const CtrlTextColor = RGB(White)

Dim CurrPage As integer = 1
Dim Last As integer = 0
Dim Last1 As integer = 0

CLS
Font 1,2

' NOTE controls for each screen must be ordered (as far as #ref is concerned)
' I believe it could be done better with arrays BUT that would mean getting
' Geoff to change the way GUI SHOW works. - To take an array of #ref numbers
' instead of an optional number of references. The UBOUND(array,dia) would
' also be handy.

' Controls that are on the screen all the time or for more than 1 screen
Const gui0Go = 1
Const gui0Max = 1
GUI BUTTON gui0Go, "GO",265,185,50,50,CtrlColor,RGB(Green)

' Controls for page 1 - Reset counts & Save interval
Const pg1Frm = 10
Const pg1ResetY = 11
Const pg1SaveInt = 12
Const pg1Cap1 = 13
Const pg1Cap2 = 14
Const pg1Max = 14

GUI FRAME pg1Frm, "Setup", 0,13,MM.HRes,MM.VRes,CtrlColor
GUI CAPTION pg1Cap1, "Zero counts",10,60,LT,CtrlTextColor,BckColor
GUI CHECKBOX pg1ResetY, "", 250,50,40,CtrlColor

GUI CAPTION pg1Cap2, "Save freq.",10,130,LT,CtrlTextColor,BckColor
GUI SPINBOX pg1SaveInt, 160,120,150,40,CtrlColor,BckColor,1,1,120

' Controls for page 2 - Set date
Const pg2Frm = 20
Const pg2Yr = 21
Const pg2Mth = 22
Const pg2Day = 23
Const pg2Cap1 = 24
Const pg2Cap2 = 25
Const pg2Cap3 = 26
Const pg2Max = 26

GUI FRAME pg2Frm, "Date", 0,13,MM.HRes,MM.VRes,CtrlColor

GUI CAPTION pg2Cap1, "Day",10,30,LT,CtrlTextColor,BckColor
GUI SPINBOX pg2Day, 150,40,140,40,CtrlColor,BckColor,1,1,31

GUI CAPTION pg2Cap2, "Month",10,90,LT,CtrlTextColor,BckColor
GUI SPINBOX pg2Mth, 150,90,140,40,CtrlColor,BckColor,1,1,12

GUI CAPTION pg2Cap3, "Year",10,140,LT,CtrlTextColor,BckColor
GUI SPINBOX pg2Yr, 150,140,140,40,CtrlColor,BckColor,1,15,30

' Controls for page 3 - Set time
Const pg3Frm = 30
Const pg3Hour = 31
Const pg3Min = 32
Const pg3Cap1 = 33
Const pg3Cap2 = 34
Const pg3Max = 34

GUI FRAME pg3Frm, "Time", 0,13,MM.HRes,MM.VRes,CtrlColor

GUI CAPTION pg3Cap1, "Hour",10,30,LT,CtrlTextColor,BckColor
GUI SPINBOX pg3Hour, 150,40,140,40,CtrlColor,BckColor,1,0,23

GUI CAPTION pg3Cap2, "Minute",10,130,LT,CtrlTextColor,BckColor
GUI SPINBOX pg3Min, 150,130,140,40,CtrlColor,BckColor,1,0,59



' Controls for page 4 - Units

Const pg4Frm = 40
Const pg4Counts = 41
Const pg4Revs = 42
Const pg4Distance = 43
Const pg4Cap1 = 44
Const pg4Cap2 = 45
Const pg4Cap3 = 46
Const pg4Max = 46

GUI FRAME pg4Frm, "Units", 0,13,MM.HRes,MM.VRes,CtrlColor

GUI CAPTION pg4Cap1, "Counts",10,40,LT,CtrlTextColor,BckColor
GUI RADIO pg4Counts, "", 200,40,40,CtrlColor

GUI CAPTION pg4Cap2, "Revs",10,90,LT,CtrlTextColor,BckColor
GUI RADIO pg4Revs, "", 200,90,40,CtrlColor

GUI CAPTION pg4Cap3, "Distance",10,140,LT,CtrlTextColor,BckColor
GUI RADIO pg4Distance, "m", 200,140,40,CtrlColor

' Controls for page 5 - Colours
Const pg5Red = 50
Const pg5Green = 51
Const pg5Blue = 52
Const pg5Wheel = 53
Const pg5Frm = 54
Const pg5Cap1 = 55
Const pg5Cap2 = 56
Const pg5Cap3 = 57
Const pg5Cap4 = 58
Const pg5Max = 58

GUI FRAME pg5Frm, "Graph colours", 0,13,MM.HRes,MM.VRes,CtrlColor

GUI CAPTION pg5Cap1, "Red",10,40,LT,CtrlTextColor,BckColor
GUI SPINBOX pg5Red, 100,40,140,40,CtrlColor,BckColor,5,0,255

GUI CAPTION pg5Cap2, "Blue",10,90,LT,CtrlTextColor,BckColor
GUI SPINBOX pg5Blue, 100,90,140,40,CtrlColor,BckColor,5,0,255

GUI CAPTION pg5Cap3, "Green",10,140,LT,CtrlTextColor,BckColor
GUI SPINBOX pg5Green, 100,140,140,40,CtrlColor,BckColor,5,0,255

GUI CAPTION pg5Cap4, "Wheel",10,190,LT,CtrlTextColor,BckColor
GUI SPINBOX pg5Wheel, 100,190,140,40,CtrlColor,BckColor,1,1,MaxWhls



' Controls for page 6 - Totals graph
Const pg6Frm = 60
Const pg6Max = 60

GUI FRAME pg6Frm, "Totals graph", 0,13,MM.HRes,MM.VRes,CtrlColor

' Controls for page 7 - 24 hour Graph
Const pg7Frm = 70
Const pg7Max = 70

GUI FRAME pg7Frm, "24 Hour graph", 0,13,MM.HRes,MM.VRes,CtrlColor

' Controls for page 8 - Stop
Const pg8Frm = 80
Const pg8stop = 81 ' Stop button - close down
Const pg8Max = 81

GUI FRAME pg8Frm, "Exit program", 0,13,MM.HRes,MM.VRes,CtrlColor
GUI BUTTON pg8stop,"Stop",(MM.HRes/2)-25,(MM.VRes/2)-25,50,50,CtrlColor,BckColor


'''''''''''''''''''''''' MAIN LINE ''''''''''''''''''''''''''''''''''''''''
Do


Loop


' Swipe code by TassyJim
'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Sub guiAction
Last = Touch(X)
If Last > -1 Then
GUI INTERRUPT 0 ' make sure we don't call again until finished processing
'Print "Down Touch(X) => ";Last
SetTick 100, swipe ' check touch position 100 ms later
' 100 ms delay seems to work OK but should experiment with this.
EndIf
End Sub

Sub swipe
Local n
SetTick 0, swipe ' make sure we don't call again until finished processing
For n = 1 To 100 ' keep trying for a positive touch
Last1 = Touch(X)
If Last1 > -1 Then Exit For
Next n

If Last1 > -1 Then
If (Last1 - Last) < -5 Then ' moved at least 5 pixels
'Print "Swipe left ";Last1;" < ";Last
CurrPage = CurrPage -1
If CurrPage < 1 Then Currpage = MaxPages
ElseIf (Last1 - Last) > 5 Then
'Print "Swipe right ";Last1;" > ";Last
CurrPage = CurrPage + 1
If CurrPage > MaxPages Then Currpage = 1
EndIf
EndIf

''''''''''' Code below added by me to redraw pages
Print "CurrPage = ";CurrPage
GUI HIDE ALL
' Show Ctrls which appear on all screens
For n = 1 To gui0Max
GUI SHOW n
Next

' Now show Ctrls specific to a page.
Select Case CurrPage
Case 1
For n = 10 To pg1Max
GUI SHOW n
Next
Case 2
For n = 20 To pg2Max
GUI SHOW n
Next
Case 3
For n = 30 To pg3Max
GUI SHOW n
Next
Case 4
For n = 40 To pg4Max
GUI SHOW n
Next
Case 5
For n = 50 To pg5Max
GUI SHOW n
Next
Case 6
For n = 60 To pg6Max
GUI SHOW n
Next
Case 7
For n = 70 To pg7Max
GUI SHOW n
Next
Case 8
For n = 80 To pg8Max
GUI SHOW n
Next

End Select
GUI INTERRUPT guiAction ' turn the touch interrupt back on
End Sub



Rob White
 
centrex

Guru

Joined: 13/11/2011
Location: Australia
Posts: 320
Posted: 01:53pm 09 Jan 2016
Copy link to clipboard 
Print this post

Bizzie
What display are you demonstrating this on, all works with some position mods on a 2.8 ILI9341 lcd.

cliff
Cliff
 
Bizzie
Senior Member

Joined: 06/07/2014
Location: Australia
Posts: 192
Posted: 02:03pm 09 Jan 2016
Copy link to clipboard 
Print this post

Sorry I should of stated that my setup is :-

Micromite Plus MMBasic Ver 4.7 Beta 37
Copyright 2011-2015 Geof>
> option list
OPTION LCDPANEL ILI9341, LANDSCAPE, 21, 22, 23
OPTION TOUCH 18, 14
OPTION SDCARD 49
>

It is essential that you use Beta 3.7 - see
http://www.thebackshed.com/forum/forum_posts.asp?TID=8237&PN=1
Rob White
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025