Touch Screen Library: Unterschied zwischen den Versionen

Aus ProjectWiki
Wechseln zu:Navigation, Suche
K
K
Zeile 1: Zeile 1:
 
==Overview==
 
==Overview==
Based on the Appnote [http://www.atmel.com/Images/doc8091.pdf AVR341 - Four and five wire Resistive Touch Screen Controller] by Atmel, this library handles everything needed to use a 4-wire resistive touchscreen in Bascom.
+
Based on the Appnote [http://www.atmel.com/Images/doc8091.pdf AVR341 - Four and five-wire Touch Screen Controller] by Atmel, this library handles everything needed to use a 4-wire resistive touchscreen in Bascom.
  
 
Features:
 
Features:
Zeile 14: Zeile 14:
  
 
===Options===
 
===Options===
<pre>' width of the underlying screen [pixels]
+
Options and settings need to be set before the $include-statement, every setting has a default value that will be used if not specified otherwise.
  Const Touch_screen_width = 240
+
(in the list below the default values are shown)
  
' height of the underlying screen [pixels]
+
Width of the underlying screen in pixels:
  Const Touch_screen_height = 320
+
<pre>Const Touch_screen_width = 240</pre>
  
' if the display supports rotation, then the touch coordinates need to be rotated too
+
Height of the underlying screen in pixels:
  Const Touch_enable_rotation = False
+
<pre>Const Touch_screen_height = 320</pre>
  
' measure the touch contact resistance [False: Off, 1: Method 1, 2: Method 2]
+
If the display supports rotation, then the touch coordinates need to be rotated too. To enable this function, set to true:
  Touch_read_resistance = False
+
<pre>Const Touch_enable_rotation = False</pre>
  
' if contact resistance measurement is enabled, the touch ITO layer resistances need to be known [ohms]
+
Measure the touch contact resistance:
  Const Touch_resistance_x = 200
+
* False: Off
  Const Touch_resistance_y = 600
+
* 1: Method 1 (needs one additional sample cycle)
 +
* 2: Method 2 (needs two additional sample cycles)
 +
<pre>Touch_read_resistance = False</pre>
  
' control port for touch screen
+
If the contact resistance measurement is enabled, the touch ITO layer resistances need to be known (in Ohms, measured between + and - contact of the unconnected touch layers, Y-resistance is only needed for Method 1, X for both):
  Touch_ctrl_port Alias Porta
+
<pre>Const Touch_resistance_x = 200
 +
Const Touch_resistance_y = 600</pre>
  
' control port pin connections
+
Control port for touch screen (standard I/O port):
  Const Touch_pin_xp = 0
+
<pre>Touch_ctrl_port Alias Porta</pre>
  Const Touch_pin_xn = 1
 
  Const Touch_pin_yp = 2
 
  Const Touch_pin_yn = 3
 
  
' adc port pin connections
+
Control port pin connections:
  Const Touch_pin_xn_adc = 4
+
<pre>Const Touch_pin_xp = 0
  Const Touch_pin_yn_adc = 5
+
Const Touch_pin_xn = 1
  Const Touch_pin_yp_adc = 6
+
Const Touch_pin_yp = 2
 +
Const Touch_pin_yn = 3</pre>
  
' measurements per sample
+
ADC port pin connections (XMega: Adca is used, AtMega: can be the same as the control port pins if control port is also the ADC port):
  Const Touch_samples = 5
+
<pre>Const Touch_pin_xn_adc = 4
 +
Const Touch_pin_yn_adc = 5
 +
Const Touch_pin_yp_adc = 6</pre>
  
' new coordinate samples [coordinates/sec]
+
Measurements per sample (The median value is taken as coordinate sample):
  Const Touch_updaterate = 100
+
<pre>Const Touch_samples = 5</pre>
  
' no. of samples to be sure the touch screen has been released
+
Coordinate update rate (coordinates generated per second)
  Const Touch_release_count = 5
+
<pre>Const Touch_updaterate = 100</pre>
  
' they bounce badly [sec]
+
No. of invalid coordinate samples to be sure the touch screen has been released
  Const Touch_time_debounce = 0.05
+
<pre>Const Touch_release_count = 5</pre>
  
' depends on RC time constant of touch screen, 0.2ms should work for most [sec]
+
Debounce time in seconds (touch screen tend to bounce badly, during this time, invalid samples are ignored and the state will not switch to released):
  Const Touch_time_switch_measurement = 0.0002
+
<pre>Const Touch_time_debounce = 0.05</pre>
  
' click/drag detection delta [pixels]
+
After changing the type of sample to be measured, the screen needs some time for the voltages to settle, this depends on RC time constant of touch screen, 0.2ms should work for most (in seconds):
  Const Touch_click_delta = 15
+
<pre>Const Touch_time_switch_measurement = 0.0002</pre>
  
' max speed [pixels/sec]
+
Click/Drag detection delta in pixels (Drag: first and actual coordinate delta above threshold, Click: first and last coordinate below threshold and not dragging, applied on the raw ADC values, may not be 100% pixel-correct):
  Const Touch_max_delta = 3000</pre>
+
<pre>Const Touch_click_delta = 15</pre>
 +
 
 +
Maximum speed in pixels/sec (delta between last and actual coordinates, also applied on the raw ADC values, same as for Click/Drag detection):
 +
<pre>Const Touch_max_delta = 3000</pre>
  
  
Zeile 81: Zeile 87:
  
 
Display rotation:
 
Display rotation:
<pre>' Rotation = 0: Portrait
+
* 0:
'          = 1: Landscape
+
* 1: 90°
'          = 2: Portrait 180°
+
* 2: 180°
'          = 3: Landscape 180°
+
* 3: 270°
Dim Touch_rotation As Byte</pre>
+
<pre>Dim Touch_rotation As Byte</pre>
  
 
Sub routines:
 
Sub routines:
<pre>Sub Touch_init()
+
<pre>Sub Touch_init()</pre>
Sub Touch_calibrate()
+
Initializes the hardware modules (Timer, ADC, Pin Interrupt) and enters standby mode
Sub Touch_load_calibration()
+
 
Sub Touch_calculate_calibration()
+
<pre>Sub Touch_calibrate()</pre>
Sub Touch_reset_calibration()
+
Starts the calibration procedure, calculates the coefficients and stores the calibration values in EEPROM
Function Touch_getsample() As Byte</pre>
+
 
 +
<pre>Sub Touch_load_calibration()</pre>
 +
Loads the calibration values from EEPROM. If they are not set, the calibration procedure is started. This sub routine is called from Touch_init, no need to call again after init.
 +
 
 +
<pre>Sub Touch_reset_calibration()</pre>
 +
Resets the stored calibration values
 +
 
 +
<pre>Function Touch_getsample() As Byte</pre>
 +
Returns True (1) if new valid coordinate data is available, otherwise returns False (0). The coordinate data is transformed into screen coordinates if calibration values are available. The coordinate variables are updated.
  
 +
<pre>Function Touch_click() As Byte</pre>
 +
Returns True (1) if the touch screen has been clicked (delta between first and last point below threshold and touch screen released). The coordinate variables are updated.
  
 
===Other Display Types===
 
===Other Display Types===
  
  
===Change used Timer-, ADC-Hardware-Module===
+
===Change Timer-, ADC-Hardware-Module and Pin Interrupt===
  
  

Version vom 18. November 2014, 03:57 Uhr

Overview

Based on the Appnote AVR341 - Four and five-wire Touch Screen Controller by Atmel, this library handles everything needed to use a 4-wire resistive touchscreen in Bascom.

Features:

  • Interrupt-controlled sampling
  • Touch contact pressure measurement
  • Adjustable sampling rate
  • Screen coordinate calibration (stored in EEPROM)
  • Screen rotation
  • Click/Drag detection
  • AtXMega, AtMega (untested)

How to use

Options

Options and settings need to be set before the $include-statement, every setting has a default value that will be used if not specified otherwise. (in the list below the default values are shown)

Width of the underlying screen in pixels:

Const Touch_screen_width = 240

Height of the underlying screen in pixels:

Const Touch_screen_height = 320

If the display supports rotation, then the touch coordinates need to be rotated too. To enable this function, set to true:

Const Touch_enable_rotation = False

Measure the touch contact resistance:

  • False: Off
  • 1: Method 1 (needs one additional sample cycle)
  • 2: Method 2 (needs two additional sample cycles)
Touch_read_resistance = False

If the contact resistance measurement is enabled, the touch ITO layer resistances need to be known (in Ohms, measured between + and - contact of the unconnected touch layers, Y-resistance is only needed for Method 1, X for both):

Const Touch_resistance_x = 200
Const Touch_resistance_y = 600

Control port for touch screen (standard I/O port):

Touch_ctrl_port Alias Porta

Control port pin connections:

Const Touch_pin_xp = 0
Const Touch_pin_xn = 1
Const Touch_pin_yp = 2
Const Touch_pin_yn = 3

ADC port pin connections (XMega: Adca is used, AtMega: can be the same as the control port pins if control port is also the ADC port):

Const Touch_pin_xn_adc = 4
Const Touch_pin_yn_adc = 5
Const Touch_pin_yp_adc = 6

Measurements per sample (The median value is taken as coordinate sample):

Const Touch_samples = 5

Coordinate update rate (coordinates generated per second)

Const Touch_updaterate = 100

No. of invalid coordinate samples to be sure the touch screen has been released

Const Touch_release_count = 5

Debounce time in seconds (touch screen tend to bounce badly, during this time, invalid samples are ignored and the state will not switch to released):

Const Touch_time_debounce = 0.05

After changing the type of sample to be measured, the screen needs some time for the voltages to settle, this depends on RC time constant of touch screen, 0.2ms should work for most (in seconds):

Const Touch_time_switch_measurement = 0.0002

Click/Drag detection delta in pixels (Drag: first and actual coordinate delta above threshold, Click: first and last coordinate below threshold and not dragging, applied on the raw ADC values, may not be 100% pixel-correct):

Const Touch_click_delta = 15

Maximum speed in pixels/sec (delta between last and actual coordinates, also applied on the raw ADC values, same as for Click/Drag detection):

Const Touch_max_delta = 3000


API

Status Flags:

Dim Touch_newsample As Byte
Dim Touch_calibrated As Bit
Dim Touch_touched As Bit
Dim Touch_clicked As Byte
Dim Touch_dragging As Bit

Coordinates:

Dim Touch_x As Word , Touch_y As Word
#if Touch_measure_resistance >= 1
   Dim Touch_r As Word
#endif

Display rotation:

  • 0: 0°
  • 1: 90°
  • 2: 180°
  • 3: 270°
Dim Touch_rotation As Byte

Sub routines:

Sub Touch_init()

Initializes the hardware modules (Timer, ADC, Pin Interrupt) and enters standby mode

Sub Touch_calibrate()

Starts the calibration procedure, calculates the coefficients and stores the calibration values in EEPROM

Sub Touch_load_calibration()

Loads the calibration values from EEPROM. If they are not set, the calibration procedure is started. This sub routine is called from Touch_init, no need to call again after init.

Sub Touch_reset_calibration()

Resets the stored calibration values

Function Touch_getsample() As Byte

Returns True (1) if new valid coordinate data is available, otherwise returns False (0). The coordinate data is transformed into screen coordinates if calibration values are available. The coordinate variables are updated.

Function Touch_click() As Byte

Returns True (1) if the touch screen has been clicked (delta between first and last point below threshold and touch screen released). The coordinate variables are updated.

Other Display Types

Change Timer-, ADC-Hardware-Module and Pin Interrupt

Sample

Download