Add touch-screen rotation support, binary versions for MicroPython 1.24, and recipe to make themImproved clock/time and file date handling

pull/34/head
Chris 5 months ago
parent 36fe87e28b
commit 4a1a9e8669

@ -0,0 +1,19 @@
# Define variables
MPY_CROSS = ../micropython/mpy-cross/build/mpy-cross
SRC_DIR = .
OUT_DIR = .
SRC = $(wildcard $(SRC_DIR)/*.py)
OUT = $(patsubst $(SRC_DIR)/%.py, $(OUT_DIR)/%.mpy, $(SRC))
# Default target
# all: $(OUT) $(OUT_DIR)/sh.txt
all: $(OUT)
# Rule to create .mpy from .py
$(OUT_DIR)/%.mpy: $(SRC_DIR)/%.py
$(MPY_CROSS) $< -o $@
# Phony targets
.PHONY: all

Binary file not shown.

@ -1074,3 +1074,25 @@ class Display(object):
self.spi.write(data)
self.spi.unlock()
self.cs.value = True
"""
from ili9341 import Display, color565
from machine import Pin, SPI, ADC, PWM, SDCard, SoftSPI
from xpt2046 import Touch
hspi = SPI(1, baudrate=40000000, sck=Pin(14), mosi=Pin(13))
display = Display(hspi, dc=Pin(2), cs=Pin(15), rst=Pin(0), width=320, height=240, rotation=270)
tft_bl = Pin(21, Pin.OUT)
tft_bl.value(1)
display.fill_rectangle(0, 0, 320, 240, 1234)
display.draw_circle(120,160,50,0)
display.draw_ellipse(125,165,90,20,9999)
display.draw_hline(20,40,100,8888)
from xglcd_font import XglcdFont
robotron = XglcdFont('fonts/EspressoDolce18x24.c', 18, 24 )
display.draw_text(100,160,"Hello World",robotron,2016,1234)
display.draw_text8x8(160,100,"Hello World",0,1234)
display.fill_circle(10,10,10,0b0000011111100000)
"""

Binary file not shown.

Binary file not shown.

@ -168,3 +168,19 @@ class XglcdFont(object):
# Add length of letter and spacing
length += self.letters[offset] + spacing
return length
"""
display = Display(hspi, dc=Pin(2), cs=Pin(15), rst=Pin(0), width=320, height=240, rotation=270)
tft_bl = Pin(21, Pin.OUT)
tft_bl.value(1)
display.fill_rectangle(0, 0, 320, 240, 1234)
display.draw_circle(120,160,50,0)
display.draw_ellipse(125,165,90,20,9999)
display.draw_hline(20,40,100,8888)
from xglcd_font import XglcdFont
robotron = XglcdFont('fonts/EspressoDolce18x24.c', 18, 24 )
display.draw_text(100,160,"Hello World",robotron,2016,1234)
display.draw_text8x8(160,100,"Hello World",0,1234)
"""

Binary file not shown.

@ -17,7 +17,7 @@ class Touch(object):
def __init__(self, spi, cs, int_pin=None, int_handler=None,
width=240, height=320,
x_min=100, x_max=1962, y_min=100, y_max=1900):
x_min=100, x_max=1962, y_min=100, y_max=1900, rotation=0):
"""Initialize touch screen controller.
Args:
@ -44,6 +44,7 @@ class Touch(object):
self.x_max = x_max
self.y_min = y_min
self.y_max = y_max
self.rotation = rotation
self.last_touch=0
self.x_multiplier = width / (x_max - x_min)
self.x_add = x_min * -self.x_multiplier
@ -58,6 +59,23 @@ class Touch(object):
int_pin.irq(trigger=int_pin.IRQ_FALLING | int_pin.IRQ_RISING,
handler=self.int_press)
def rot(self,x,y,rotation=None): # convert touch locations to possibly-rotated screen locations.
if rotation is None:
rotation=self.rotation
x,y = self.normalize(x,y)
if rotation == 0:
return x, self.height - y - 1
elif rotation == 90:
return self.height - y - 1, self.width - x - 1
elif rotation == 180:
return self.width - x - 1, y
elif rotation == 270:
return y , x
def get_touch(self):
"""Take multiple samples to get accurate touch reading."""
timeout = 2 # set timeout to 2 seconds
@ -107,7 +125,7 @@ class Touch(object):
y = int(self.y_multiplier * y + self.y_add)
return x, y
def raw_touch(self,xyonly=False):
def raw_touch(self,xyonly=False,nozero=True):
"""Read raw X,Y touch values.
Returns:
@ -128,12 +146,13 @@ class Touch(object):
b = self.send_command(self.GET_BATTERY)
a = self.send_command(self.GET_AUX) # chip aux ADC input
p = x * (z1 - z2) / z1 if z1>0 else None # touch-pressure is a formula based on x
p2 = x/4096 * (z2/z1-1) if z1>0 else None # touch-pressure is a formula based on x
d = None
if p is not None:
now=time.ticks_ms()
d = time.ticks_diff(now, self.last_touch) # timediff since last (for debounce)
self.last_touch=now
return (p, x, y, z1, z2, t0, t1, b, a, d)
return (p, x, y, z1, z2, t0, t1, b, a, d, p2) + tuple(self.normalize(x,y) + tuple(self.rot(x,y))) # cnd
def send_command(self, command):
"""Write command to XT2046 (MicroPython).
@ -149,3 +168,20 @@ class Touch(object):
self.cs(1)
return (self.rx_buf[1] << 4) | (self.rx_buf[2] >> 4)
""" Examples
import time
from xpt2046 import Touch
from machine import Pin, SPI, ADC, PWM, SDCard, SoftSPI
sspi = SoftSPI(baudrate=500000, sck=Pin(25), mosi=Pin(32), miso=Pin(39))
touch = Touch(sspi, cs=Pin(33)) # a, int_pin=Pin(36), int_handler=self._touch_handler)
while True:
time.sleep(0.1)
t = touch.raw_touch() # (p, x, y, z1, z2, t0, t1, b, a, d)
if t[-1] is not None:
print(t)
"""

Loading…
Cancel
Save