4559 views|28 replies

3386

Posts

0

Resources
The OP
 

MicroPython Hands-on (38) - Controlling the Touch Screen [Copy link]

 
 

1. Touch screen technology
The touch screen is structurally composed of an inductive liquid crystal display device, which can receive signals from the touch head or other touch actions. When the inductive display receives the touch signal, the entire touch device will execute different instructions according to the pre-written program to realize the user's touch intention. This technology replaces the traditional mechanical button device, and with the picture of the liquid crystal display, it can obtain a very vivid picture and operation enjoyment, which is welcomed by more and more people.

Touch screen technology first appeared in some industrial and commercial equipment, such as POS terminals, elevator buttons, etc. Touch screen technology facilitates human-computer interaction to a great extent, and the touch screen itself is very durable. These characteristics have led to a large application and development of touch screen technology. With touch screen technology, users can solve the complicated operation problems in the past by simply clicking the corresponding touch pattern with their fingers, which greatly facilitates users. The launch of iPhone mobile phones in recent years has stimulated the development of touch screen related industries, and touch screen technology has been applied to different products. With the rapid development of technologies such as mobile Internet and cloud computing, people's demand and requirements for touch screen technology are also increasing. It is believed that touch screen technology will appear more and more in different electronic products. In addition, touch screens also have great development space in the field of automotive electronics and retail industry. According to relevant authoritative surveys, by 2012, the touch screen market for automotive electronics and retail industry will reach 2 billion US dollars, accounting for 20% of the overall touch screen industry market. At the same time, in the PC industry, due to the continuous development of Microsoft operating systems, touch screen technology will also occupy a very important role. In addition, in the medical field, public facilities field and other aspects, touch screen control technology will be further popularized and has excellent market prospects.

China's touch screen consumer market has great potential, especially in the digital electronic product market. However, in the touch screen industry, China's touch screen industry is mainly concentrated in the middle and lower reaches of the industrial chain. At present, Shenbei New District in Shenyang, Liaoning Province has also established a mobile phone manufacturing center, which has attracted mobile phone touch screen manufacturers including Chenxun Technology. On the one hand, it reflects the powerful market of mobile phone touch screens, and on the other hand, it reflects that the technical content of my country's touch screen industry is relatively low, and it is mainly based on processing and OEM. However, in the touch screen industry chain, the touch screen driver chip is the core, which determines the quality of touch screen products. Major chip design companies around the world are also committed to the research and development of high-precision, low-power touch screen driver chips.

 
 

3386

Posts

0

Resources
2
 

2. Resistive touch screen
It is a sensor, which is basically a structure of film plus glass. The adjacent sides of the film and glass are coated with ITO (nano indium tin oxide) coating. ITO has good conductivity and transparency. When the touch operation is performed, the ITO on the lower layer of the film will contact the ITO on the upper layer of the glass, and the corresponding electrical signal will be transmitted through the sensor, sent to the processor through the conversion circuit, and converted into the X and Y values on the screen through calculation, and the action of clicking will be completed and displayed on the screen. It converts the physical position of the touch point (X, Y) in the rectangular area into a voltage representing the X coordinate and the Y coordinate. Many LCD modules use resistive touch screens, which can use four, five, seven or eight wires to generate the screen bias voltage and read back the voltage of the touch point.

 
 
 

3386

Posts

0

Resources
3
 

3. Working principle of resistive touch screen
The operation and control of screen content is mainly realized through the principle of pressure sensing. The body of this touch screen is a multi-layer composite film that matches the display surface very well. The first layer is a glass or organic glass bottom layer, the second layer is a partition layer, and the third layer is a polyresin surface layer. The surface is also coated with a transparent conductive layer, and then covered with a hardened, smooth and scratch-resistant plastic layer. The conductive layer and glass layer sensors on the surface of the polyresin surface are separated by many tiny partitions. The current passes through the surface layer. When the surface layer is lightly touched and pressed down, it contacts the bottom layer. The controller reads the corresponding current from the four corners at the same time and calculates the distance of the finger position. This touch screen uses two highly transparent conductive layers to form a touch screen, and the distance between the two layers is only 2.5 microns. When a finger touches the screen, the two conductive layers that are normally insulated from each other come into contact at the touch point. Because one of the conductive layers is connected to the 5V uniform voltage field in the Y-axis direction, the voltage of the detection layer changes from zero to non-zero. After the controller detects this connection, it performs A/D conversion and compares the obtained voltage value with 5V to obtain the Y-axis coordinate of the touch point. Similarly, the X-axis coordinate is obtained. This is the most basic principle common to all resistive technology touch screens.

The touch screen consists of two transparent layers stacked one on top of the other. Four-wire and eight-wire touch screens consist of two layers of transparent resistive material with the same surface resistance. Five-wire and seven-wire touch screens consist of a resistive layer and a conductive layer, usually separated by an elastic material. When the pressure on the touch screen surface (such as by pressing with a stylus or finger) is large enough, contact is made between the top and bottom layers. All resistive touch screens use the principle of a voltage divider to generate voltages representing the X and Y coordinates. As shown in Figure 3, the voltage divider is implemented by connecting two resistors in series. The top resistor (R1) is connected to a positive reference voltage (VREF), and the bottom resistor (R2) is grounded. The voltage measured at the junction of the two resistors is proportional to the resistance of the bottom resistor. To measure a coordinate in a specific direction on a resistive touch screen, one of the resistive layers needs to be biased: one side of it is connected to VREF and the other side is grounded. At the same time, the unbiased layer is connected to the high impedance input of an ADC. When the pressure on the touch screen is large enough to cause contact between the two layers, the resistive surface is separated into two resistors. Their resistance is proportional to the distance from the touch point to the bias edge. The resistance between the touch point and the ground edge is equivalent to the lower resistor in the voltage divider. Therefore, the voltage measured on the unbiased layer is proportional to the distance from the touch point to the ground edge. The transparent ITO conductive film coated inside the resistive touch screen has process requirements. The coating should not be too thick, otherwise it will not only reduce the transmittance, but also form an internal reflection layer, reducing the clarity; the coating should not be too thin, otherwise it will easily break. During use, since the working accuracy of the touch screen depends on the precision of the resistor network, if a certain resistor network fails, the touch screen here will fail: the surface of the touch screen is often touched, and the thin layer of transparent ITO conductive film on the surface will have fine cracks, which will also cause touch failure; the outer layer of the transparent ITO conductive film is made of plastic material and has no protective layer, so the safety is poor. However, from a structural point of view, the resistive touch screen is a relatively closed system. Therefore, compared with other touch screens, it is not affected by external pollutants, such as dust, water vapor, oil, etc., and is suitable for occasions where gloves are worn or direct touch is not allowed. Therefore, it can work normally in harsh environments and is suitable for aviation airborne display systems.

 
 
 

3386

Posts

0

Resources
4
 

4. Advantages and Disadvantages of Resistive Touch Screens
(1) Advantages
The advantages of resistive touch screens are that the screen and control system are relatively cheap, the response sensitivity is very good, and whether it is a four-wire resistive touch screen or a five-wire resistive touch screen, they are a kind of working environment completely isolated from the outside world, not afraid of dust and water vapor, and can adapt to various harsh environments. It can be touched by any object and has good stability. The advantages of resistive touch screens can be classified as follows:
a. The resistive touch screen has high accuracy, which can be down to the pixel level, and the maximum applicable resolution can reach 4096x4096.
b. The screen is not affected by dust, water vapor and oil, and can be used in low or high temperature environments.
c. The resistive touch screen uses pressure sensing, which can be touched by any object, even with gloves, and can be used for handwriting recognition.
d. Due to mature technology and low threshold, the cost of resistive touch screens is relatively cheap.

(2) Disadvantages
The disadvantage is that the outer film of the resistive touch screen is easily scratched, making the touch screen unusable. The multi-layer structure will cause a large light loss. For handheld devices, it is usually necessary to increase the backlight source to compensate for the poor light transmittance, but this will also increase battery consumption. The disadvantages of resistive touch screens can be classified as follows:
a. Resistive touch screens can be designed for multi-point touch, but when two points are pressed at the same time, the pressure on the screen becomes unbalanced, resulting in touch errors, so the degree of multi-point touch is difficult to achieve.
b. Resistive touch screens are more likely to be damaged by scratches, etc.

 
 
 

3386

Posts

0

Resources
5
 

5. MixPY_ Touch screen function library

The touchscreen module contains basic touchscreen reading operations (import touchscreen as ts).

(1) Initialize the touch screen

ts.init(i2c=None, cal=None)

Initialize the touch screen

Parameters:
i2c: Touch screen that supports I2C communication, pass in the I2C instance object, this parameter may be renamed or cancelled later
cal: calibration data, a tuple of 7 integer values, which can be obtained through the touchscreen.calibrate() function

Returns:
None

(2) Calibrate the screen

ts.calibrate()

Calibrate the screen so that the screen display and touch screen pixels can correspond

Parameters:
None

Return:
Returns a tuple of 7 integer values, which can be saved to the file system or flash and passed in during initialization so that you don't have to calibrate every time

(3) Obtaining touch data

ts.read()

Read the current screen status and the coordinates of the pressed point

Parameters:
None

Returns:
a tuple of 3 integer values (status, x, y). Note that this value will always maintain the previous status
status: status, the values are touchscreen.STATUS_PRESS, touchscreen.STATUS_MOVE, touchscreen.STATUS_RELEASE
x: x-axis coordinate
y: y-axis coordinate

(4) System default touchscreen constants:

touchscreen.STATUS\_PRESS

The screen is pressed, the first value of the tuple returned by the read() function

touchscreen.STATUS\_MOVE

The screen is pressed and moved, that is, pressed and moved, the first value of the tuple returned by the read() function

touchscreen.STATUS\_RELEASE

The screen is no longer held down, that is, there is no click, and the read() function returns the first value of the tuple.

 
 
 

3386

Posts

0

Resources
6
 

6. Experiment 1: Touch to light up the red screen

#MicroPython动手做(38)——控制触摸屏
#实验之一:触摸点亮红色屏幕

import touchscreen as ts
import time
import mixno
import lcd
from machine import I2C


lcd.init(freq=15000000,color=0)
i2c = I2C(I2C.I2C0, freq=400000, scl=30, sda=31)
ts.init(i2c)
LED_G=mixno.pin(6,mixno.GPIO.OUT)
while True:
    if ts.read()[0] == 2:
        lcd.clear(248)
        time.sleep_ms(300)
    else:
        lcd.clear(0)

 
 
 

3386

Posts

0

Resources
7
 

 
 
 

3386

Posts

0

Resources
8
 

MixPY Experimental Graphics Programming

 
 
 

3386

Posts

0

Resources
9
 

7. Experiment 2: Manually calibrate the touch screen

The red light starts calibration, and the green light ends after five points of calibration.

#MicroPython动手做(38)——控制触摸屏
#实验之二:手动校准触摸屏幕

import touchscreen as ts
import mixno
import lcd
from machine import I2C


lcd.init(freq=15000000,color=0)
i2c = I2C(I2C.I2C0, freq=400000, scl=30, sda=31)
LED_R=mixno.pin(7,mixno.GPIO.OUT)
ts.init(i2c)
ts.calibrate()
LED_G=mixno.pin(6,mixno.GPIO.OUT)

 
 
 

3386

Posts

0

Resources
10
 

 
 
 

3386

Posts

0

Resources
11
 

MixPY Experimental Graphics Programming

 
 
 

3386

Posts

0

Resources
12
 

8. Experiment 3: Touch the green block to switch the green LED light

#MicroPython动手做(38)——控制触摸屏
#实验之三:触摸绿色块开关绿色LED灯

import touchscreen as ts
import time
import mixno
import lcd
import image
from machine import I2C


i2c = I2C(I2C.I2C0, freq=400000, scl=30, sda=31)
lcd.init(freq=15000000,color=0)
ts.init(i2c)
LED_G=mixno.pin(6,mixno.GPIO.OUT)
image = image.Image()
while True:
    image = image.draw_rectangle([100,80,120,80],57351,1,1)
    if ts.read()[0] == 3:
        if 100 < ts.read()[1] < 220 and 80 < ts.read()[2] < 160:
            LED_G.value(0)
            time.sleep_ms(300)
    else:
        LED_G.value(1)
    lcd.display(image)

 
 
 

3386

Posts

0

Resources
13
 

Experiment 3 scenario diagram

 
 
 

3386

Posts

0

Resources
14
 

MixPY Experimental Graphics Programming

 
 
 

3386

Posts

0

Resources
15
 

9. Experiment 4: Red and blue touch blocks control red and blue LED lights

The red and blue color buttons turn on the corresponding color LED lights.

#MicroPython动手做(38)——控制触摸屏
#实验之四:红蓝触摸块控制红蓝色LED灯

import touchscreen as ts
import mixno
import lcd
import image
from machine import I2C


i2c = I2C(I2C.I2C0, freq=400000, scl=30, sda=31)
lcd.init(freq=15000000,color=0)
ts.init(i2c)
LED_R=mixno.pin(7,mixno.GPIO.OUT)
LED_B=mixno.pin(8,mixno.GPIO.OUT)
image = image.Image()
image = image.draw_rectangle([50,80,60,80],248,1,1)
image = image.draw_rectangle([190,80,60,80],7936,1,1)
while True:
    if 50 < ts.read()[1] < 110 and 80 < ts.read()[2] < 160:
        LED_R.value(0)
    else:
        LED_R.value(1)
    if 190 < ts.read()[1] < 250 and 80 < ts.read()[2] < 160:
        LED_B.value(0)
    else:
        LED_B.value(1)
    lcd.display(image)

 
 
 

3386

Posts

0

Resources
16
 

Schematic diagram of the scene in Experiment 4

 
 
 

3386

Posts

0

Resources
17
 

MixPY Experiment 4 Graphics Programming

 
 
 

3386

Posts

0

Resources
18
 

10. Experiment 5: Touch the red line

#MicroPython动手做(38)——控制触摸屏
#实验之五:触摸画红线

import touchscreen as ts
import mixno
import lcd
import image
from machine import I2C


lcd.init(freq=15000000,color=0x0000)
i2c = I2C(I2C.I2C0, freq=400000, scl=30, sda=31)
ts.init(i2c)
lcd.clear(0x0000)
img = image.Image()
status_last = ts.STATUS_IDLE
x_last = 0
y_last = 0
draw = False
LED_R=mixno.pin(7,mixno.GPIO.OUT)
while True:
    status = ts.read()[0]
    x = ts.read()[1]
    y = ts.read()[2]
    if draw:
        img = img.draw_line([x_last,y_last,x,y],(255,0,0),1)
    if status_last != status:
        if status == ts.STATUS_MOVE or status == ts.STATUS_PRESS:
            draw = True
        else:
            draw = False
        status_last = status
    lcd.display(img)
    x_last = x
    y_last = y

 
 
 

3386

Posts

0

Resources
19
 

Schematic diagram of the scene in Experiment 5

 
 
 

3386

Posts

0

Resources
20
 

MixPY Experiment 5: Graphical Programming

 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews

Room 1530, Zhongguancun MOOC Times Building, Block B, 18 Zhongguancun Street, Haidian District, Beijing 100190, China Tel:(010)82350740 Postcode:100190

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list