最近想测试一下买的触摸屏是否可以正常使用,正巧从箱底翻出了吃灰多年的pyboard(其实是使用STM32F407的国产版,不过没区别啦),配合VSCode + Pymakr,很容易就驱动起来了。
贴个测试图和代码:
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)
离线
赞!MPY玩的人越来越多了
离线
商用还是很少啊, 基本都是diy,自己的那一套也在吃灰中,哈哈哈 看来需要找出来玩玩了
离线
最便宜的MicroPython板子可能就是ESP8266了。跑Javascript也可以。
离线
厉害了,,点赞
离线