您尚未登录。

楼主 #1 2019-12-30 22:29:14

metro
会员
注册时间: 2019-03-09
已发帖子: 442
积分: 486

尝鲜MicroPython,写一个GT911触摸屏的demo

最近想测试一下买的触摸屏是否可以正常使用,正巧从箱底翻出了吃灰多年的pyboard(其实是使用STM32F407的国产版,不过没区别啦),配合VSCode + Pymakr,很容易就驱动起来了。

贴个测试图和代码:
GT911.png

import struct
import time
from pyb import Pin, ExtInt, Switch
from machine import I2C
from micropython import schedule

import micropython
micropython.alloc_emergency_exception_buf(1000)

class GT911:
    def __init__(self, i2c, rst_label, int_label):
        self.i2c = i2c
        self.rst_pin = Pin(rst_label, Pin.OUT_PP)
        self.int_pin = Pin(int_label, Pin.OUT_PP)
        # Assert reset
        self.rst_pin.low()
        self.int_pin.low()
        time.sleep_ms(10)
        # Deassert reset
        self.rst_pin.high()
        # Wait until reset done
        time.sleep_ms(5)
        # Release INT pin for interrupt
        self.int_pin = Pin(int_label, Pin.PULL_UP)
        self.gt911_addr = 0x5D
    
    def read_reg(self, reg_addr, nbytes):
        return self.i2c.readfrom_mem(self.gt911_addr, reg_addr, nbytes, addrsize = 16)
    
    def write_reg(self, reg_addr, data):
        self.i2c.writeto_mem(self.gt911_addr, reg_addr, data, addrsize = 16)
    
    def get_touch_point(self, id):
        info = self.i2c.readfrom_mem(self.gt911_addr, 0x8148 + id * 0x8, 0x8, addrsize = 16)
        return tuple([struct.unpack('<h', info[0:2])[0], struct.unpack('<h', info[2:4])[0], struct.unpack('<h', info[4:6])[0], int(info[7])])
    
    def register_callback(self, callback):
        self.extint = ExtInt(self.int_pin, ExtInt.IRQ_FALLING, Pin.PULL_UP, callback)

gt911 = GT911(I2C(1, freq = 100000), 'X12', 'X11')

def gt911_isr(_):
    # Get status register
    sta_reg = gt911.read_reg(0x814E, 1)[0]
    if sta_reg & 0x80 == 0:
        return
    # Get number of touch points
    npoints = sta_reg & 0xF
    # Display touch points
    print('\r', end = '')
    for id in range(npoints):
        info = gt911.get_touch_point(id + 1)
        print('(%4d, %3d) ' % (info[0], info[1]), end = '')
    # Fill blank to flush display
    for id in range(5 - npoints):
        print('            ', end = '')
    # Clear interrupt
    gt911.write_reg(0x814E, bytearray([0]))

print('Press USR button to stop')

# Attach ISR
gt911.register_callback(lambda e: schedule(gt911_isr, e))

while Switch().value() == False:
    pass

# Detach ISR
gt911.register_callback(None)

离线

#2 2019-12-31 09:57:18

达克罗德
会员
注册时间: 2018-04-10
已发帖子: 1,134
积分: 1086.5

Re: 尝鲜MicroPython,写一个GT911触摸屏的demo

赞!MPY玩的人越来越多了

离线

#3 2020-06-15 08:49:19

shawnzhang
会员
注册时间: 2020-06-15
已发帖子: 3
积分: 3

Re: 尝鲜MicroPython,写一个GT911触摸屏的demo

商用还是很少啊,   基本都是diy,自己的那一套也在吃灰中,哈哈哈  看来需要找出来玩玩了

离线

#4 2020-06-16 11:12:34

iamseer
会员
注册时间: 2020-06-06
已发帖子: 68
积分: 45.5

Re: 尝鲜MicroPython,写一个GT911触摸屏的demo

最便宜的MicroPython板子可能就是ESP8266了。跑Javascript也可以。

离线

#5 2020-06-16 16:22:29

XIVN1987
会员
注册时间: 2019-08-30
已发帖子: 237
积分: 298.5

Re: 尝鲜MicroPython,写一个GT911触摸屏的demo

厉害了,,点赞

离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn