Skip to content

Commit 9f2c6f2

Browse files
committed
Add MaTouch 1024x600 support and improve demos
1 parent f939d0f commit 9f2c6f2

4 files changed

Lines changed: 172 additions & 115 deletions

File tree

demo.py

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,34 @@
3333
import time
3434
import busio
3535
import board
36-
#import adafruit_focaltouch
36+
try:
37+
import adafruit_focaltouch
38+
except:
39+
pass
3740
import displayio
3841
import dotclockframebuffer
3942
import framebufferio
4043
import terminalio
4144
from rainbowio import colorwheel
4245
import adafruit_imageload
43-
from pydos_ui import Pydos_ui
46+
try:
47+
from pydos_ui import Pydos_ui
48+
except:
49+
Pydos_ui = None
4450

4551

46-
#SCL_pin = board.IO41 # set to a pin that you want to use for SCL
47-
#SDA_pin = board.IO42 # set to a pin that you want to use for SDA
52+
if 'ts' in dir(Pydos_ui):
53+
ft = Pydos_ui.ts
54+
else:
4855

49-
#IRQ_pin = board.IO40 # select a pin to connect to the display's interrupt pin ("IRQ") - not used in this code
56+
SCL_pin = board.SCL # set to a pin that you want to use for SCL
57+
SDA_pin = board.SDA # set to a pin that you want to use for SDA
5058

59+
IRQ_pin = board.TOUCH_INT # select a pin to connect to the display's interrupt pin ("IRQ") - not used in this code
5160

52-
#i2c = busio.I2C(SCL_pin, SDA_pin)
53-
54-
55-
#ft = adafruit_focaltouch.Adafruit_FocalTouch(i2c, debug=False)
56-
ft = Pydos_ui.ts
57-
58-
displayio.release_displays()
61+
i2c = busio.I2C(SCL_pin, SDA_pin)
5962

63+
ft = adafruit_focaltouch.Adafruit_FocalTouch(i2c, debug=False)
6064

6165
# load the background "HACKtablet" image
6266

@@ -66,7 +70,6 @@
6670
pixel_shader=hack_palette,
6771
)
6872

69-
7073
# setup the display
7174

7275
# create the list of 16 datapins (RGB565)
@@ -76,34 +79,37 @@
7679
# happens/ It may cause glitching depending upon what
7780
# settings you use.
7881

79-
print('Creating DotClockFrameBuffer.')
80-
81-
82-
#fb=dotclockdisplay.DotClockFramebuffer(
83-
# width=800, height=480,
84-
# hsync=board.IO47, vsync=board.IO48,
85-
# de=board.IO45,
86-
# pclock=board.IO21,
87-
# data_pins=datapin_list,
88-
# pclock_frequency= 24*1000*1000, # 24000000 # 24 MHz
89-
# hsync_back_porch = 100, # 100
90-
# hsync_front_porch = 40, # 40
91-
# hsync_pulse_width = 5, # 5
92-
# vsync_back_porch = 25, # 25
93-
# vsync_front_porch = 10, # 10
94-
# vsync_pulse_width = 1, # 1
95-
# pclock_active_neg = 1, # 1
96-
# bounce_buffer_size_px = 1000 * 15, # 15000
97-
# )
98-
99-
fb=dotclockframebuffer.DotClockFramebuffer(**board.TFT_PINS,**board.TFT_TIMINGS)
100-
101-
102-
print('Creating DotClockFrameBuffer Display.')
103-
104-
display=framebufferio.FramebufferDisplay(fb)
105-
# force_refresh=True,
106-
# )
82+
if 'display' in dir(Pydos_ui):
83+
display = Pydos_ui.display
84+
else:
85+
print('Creating DotClockFrameBuffer.')
86+
87+
#fb=dotclockdisplay.DotClockFramebuffer(
88+
# width=800, height=480,
89+
# hsync=board.IO47, vsync=board.IO48,
90+
# de=board.IO45,
91+
# pclock=board.IO21,
92+
# data_pins=datapin_list,
93+
# pclock_frequency= 24*1000*1000, # 24000000 # 24 MHz
94+
# hsync_back_porch = 100, # 100
95+
# hsync_front_porch = 40, # 40
96+
# hsync_pulse_width = 5, # 5
97+
# vsync_back_porch = 25, # 25
98+
# vsync_front_porch = 10, # 10
99+
# vsync_pulse_width = 1, # 1
100+
# pclock_active_neg = 1, # 1
101+
# bounce_buffer_size_px = 1000 * 15, # 15000
102+
# )
103+
104+
displayio.release_displays()
105+
106+
fb=dotclockframebuffer.DotClockFramebuffer(**board.TFT_PINS,**board.TFT_TIMINGS)
107+
108+
print('Creating DotClockFrameBuffer Display.')
109+
110+
display=framebufferio.FramebufferDisplay(fb)
111+
# force_refresh=True,
112+
# )
107113

108114
display.root_group = None
109115
display.refresh()

lib/pydos_ui_virt.py

Lines changed: 69 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from sys import stdin,stdout,implementation
22
from os import getenv
3+
from storage import getmount
34

45
import board
56
import displayio
@@ -70,16 +71,27 @@ def __init__(self):
7071
displayio.release_displays()
7172

7273
if 'TFT_PINS' in dir(board):
73-
disp_bus=dotclockframebuffer.DotClockFramebuffer(**board.TFT_PINS,**board.TFT_TIMINGS)
74-
self._display=framebufferio.FramebufferDisplay(disp_bus)
74+
sWdth = getenv('PYDOS_TS_WIDTH')
75+
if sWdth == None:
76+
if board.board_id == "makerfabs_tft7":
77+
sWdth = input("What is the resolution Width of the touch screen? (1024/800/...): ")
78+
else:
79+
sWdth = board.TFT_TIMINGS['width']
80+
self.updateTOML("PYDOS_TS_WIDTH",str(sWdth))
81+
82+
if sWdth == 1024 and "TFT_TIMINGS1024" in dir(board):
83+
disp_bus=dotclockframebuffer.DotClockFramebuffer(**board.TFT_PINS,**board.TFT_TIMINGS1024)
84+
else:
85+
disp_bus=dotclockframebuffer.DotClockFramebuffer(**board.TFT_PINS,**board.TFT_TIMINGS)
86+
self.display=framebufferio.FramebufferDisplay(disp_bus)
7587
else:
7688
if 'SPI' in dir(board):
7789
spi = board.SPI()
7890
else:
7991
spi = busio.SPI(clock=board.SCK,MOSI=board.MOSI,MISO=board.MISO)
8092
disp_bus=displayio.FourWire(spi,command=board.D10,chip_select=board.D9, \
8193
reset=board.D6)
82-
self._display=adafruit_ili9341.ILI9341(disp_bus,width=320,height=240)
94+
self.display=adafruit_ili9341.ILI9341(disp_bus,width=320,height=240)
8395
self._swapYdir = True # TSC2007
8496

8597
ts_calib = getenv('PYDOS_TS_CALIB')
@@ -93,14 +105,14 @@ def __init__(self):
93105
self._calibXadj = self._ts_calib[0] - 1
94106
self._calibYfact = (self._ts_calib[3]-self._ts_calib[1]+1)/600
95107
self._calibYadj = self._ts_calib[1] - 1
96-
self._calibKBfact = (self._ts_calib[3]-self._ts_calib[1]+1)/self._display.height
108+
self._calibKBfact = (self._ts_calib[3]-self._ts_calib[1]+1)/self.display.height
97109
self._calibKBadj = self._ts_calib[1] - 1
98-
scrCalibX = self._display.width/1024
99-
scrCalibY = self._display.height/600
110+
scrCalibX = self.display.width/1024
111+
scrCalibY = self.display.height/600
100112

101-
self._kbd_row = self._display.height - round(scrCalibY*330)
113+
self._kbd_row = self.display.height - round(scrCalibY*330)
102114
keyboard_bitmap,keyboard_palette = adafruit_imageload.load \
103-
("/lib/keyboard"+str(self._display.width)+".bmp", \
115+
("/lib/keyboard"+str(self.display.width)+".bmp", \
104116
bitmap=displayio.Bitmap,palette=displayio.Palette)
105117
htile=displayio.TileGrid(keyboard_bitmap,pixel_shader=keyboard_palette)
106118
htile.x=round(scrCalibX*20)
@@ -114,7 +126,7 @@ def __init__(self):
114126
color = 0xFFFFFF
115127
self._keyedTxt = label.Label(font, text="", color=color)
116128
self._keyedTxt.x = round(scrCalibX*20)
117-
self._keyedTxt.y = self._display.height - round(scrCalibY*355)
129+
self._keyedTxt.y = self.display.height - round(scrCalibY*355)
118130
self._keyedTxt.scale = 2
119131
self._kbd_group.append(self._keyedTxt)
120132

@@ -129,32 +141,22 @@ def __init__(self):
129141
self._capsIndicator.scale = 2
130142
self._kbd_group.append(self._capsIndicator)
131143

132-
#self._row1Keys = [665,615,565,515,465,415,365,315,265,215,165,115,68,-99999]
133-
#self._row1Keys = [600,555,510,465,420,375,330,285,240,195,150,105,60,-99999]
134144
self._row1Keys = [843,780,718,655,593,530,467,404,342,279,216,154,92,-99999]
135145
self._row1Keys = [self._calibX(x) for x in self._row1Keys]
136146
self._row1Letters = ['\x08','=','-','0','9','8','7','6','5','4','3','2','1','`']
137147
self._row1Uppers = ['\x08','+','_',')','(','*','&','^','%','$','#','@','!','~']
138-
#self._row2Keys = [695,645,595,545,495,445,395,345,295,245,195,145,95,-99999]
139-
#self._row2Keys = [615,570,525,485,440,395,350,305,260,215,175,130,80,-99999]
140148
self._row2Keys = [880,818,755,692,629,567,504,441,378,316,253,190,127,-99999]
141149
self._row2Keys = [self._calibX(x) for x in self._row2Keys]
142150
self._row2Letters = ['\\',']','[','p','o','i','u','y','t','r','e','w','q','\x09']
143151
self._row2Uppers = ['|','}','{']
144-
#self._row3Keys = [650,600,550,500,450,400,350,300,250,200,150,100,-99999]
145-
#self._row3Keys = [590,545,500,455,410,365,320,275,230,185,140,95,-99999]
146152
self._row3Keys = [825,763,700,637,574,511,448,385,323,260,197,134,-99999]
147153
self._row3Keys = [self._calibX(x) for x in self._row3Keys]
148154
self._row3Letters = ['\n',"'",';','l','k','j','h','g','f','d','s',"a",'C']
149155
self._row3Uppers = ['\n','"',':']
150-
#self._row4Keys = [635,585,535,485,435,385,335,285,235,185,135,-99999]
151-
#self._row4Keys = [565,520,475,430,385,340,295,250,205,160,115,-99999]
152156
self._row4Keys = [790,727,664,602,539,476,413,351,288,225,162,-99999]
153157
self._row4Keys = [self._calibX(x) for x in self._row4Keys]
154158
self._row4Letters = ['S','/','.',',','m','n','b','v','c','x','z','S']
155159
self._row4Uppers = ['S','?','>','<']
156-
#self._row5Keys = [710,520,220,125,-99999]
157-
#self._row5Keys = [640,460,200,110,-99999]
158160
self._row5Keys = [880,650,280,155,-99999]
159161
self._row5Keys = [self._calibX(x) for x in self._row5Keys]
160162
self._row5Letters = ['X','',' ','','\x1b']
@@ -163,6 +165,40 @@ def __init__(self):
163165
_calibY = lambda self,y: round(y*self._calibYfact) + self._calibYadj
164166
_calibKB = lambda self,y: round(y*self._calibKBfact) + self._calibKBadj
165167

168+
def updateTOML(self,tomlvar,tomlval):
169+
if getmount('/').readonly:
170+
print("***READONLY*** filesystem")
171+
print(tomlvar+" not set to",tomlval)
172+
else:
173+
envline = {}
174+
defaults = True
175+
try:
176+
envfile = open('/settings.toml')
177+
except:
178+
defaults = False
179+
180+
if defaults:
181+
for line in envfile:
182+
try:
183+
envline[line.split('=')[0].strip()] = line.split('=')[1].strip()
184+
except:
185+
pass
186+
envfile.close()
187+
188+
try:
189+
intval = (type(int(tomlval)) == int)
190+
except:
191+
intval = False
192+
193+
with open('/settings.toml','w') as envfile:
194+
for param in envline:
195+
if param != tomlvar:
196+
envfile.write(param+"="+envline.get(param,"")+"\n")
197+
if intval:
198+
envfile.write(tomlvar+'='+str(tomlval))
199+
else:
200+
envfile.write(tomlvar+'="'+tomlval+'"')
201+
166202
def calibrate(self):
167203

168204
self._ts_calib = []
@@ -198,10 +234,10 @@ def calibrate(self):
198234
calib_scr.append(block)
199235
calib_scr.append(calibCount)
200236

201-
self._display.root_group = calib_scr
237+
self.display.root_group = calib_scr
202238

203-
smallest_X = self._display.width
204-
smallest_Y = self._display.height
239+
smallest_X = self.display.width
240+
smallest_Y = self.display.height
205241
largest_X = 1
206242
largest_Y = 1
207243

@@ -222,8 +258,8 @@ def calibrate(self):
222258
#smallest_X -= 5
223259
#smallest_Y -= 5
224260

225-
block.x = self._display.width - 10
226-
block.y = self._display.height - 10
261+
block.x = self.display.width - 10
262+
block.y = self.display.height - 10
227263

228264
count = 5
229265
calibCount.text=str(count)
@@ -246,28 +282,9 @@ def calibrate(self):
246282
#largest_X += 5
247283
#largest_Y += 5
248284

249-
envline = {}
250-
defaults = True
251-
try:
252-
envfile = open('/settings.toml')
253-
except:
254-
defaults = False
255-
256-
if defaults:
257-
for line in envfile:
258-
try:
259-
envline[line.split('=')[0].strip()] = line.split('=')[1].strip()
260-
except:
261-
pass
262-
envfile.close()
263-
264-
with open('/settings.toml','w') as envfile:
265-
for param in envline:
266-
if param != 'PYDOS_TS_CALIB':
267-
envfile.write(param+"="+envline.get(param,"")+"\n")
268-
envfile.write('PYDOS_TS_CALIB="(%s,%s,%s,%s)"'%(smallest_X,smallest_Y,largest_X,largest_Y))
285+
self.updateTOML('PYDOS_TS_CALIB',"(%s,%s,%s,%s)"%(smallest_X,smallest_Y,largest_X,largest_Y))
269286

270-
self._display.root_group=displayio.CIRCUITPYTHON_TERMINAL
287+
self.display.root_group=displayio.CIRCUITPYTHON_TERMINAL
271288
print("Screen Calibrated: (%s,%s) (%s,%s)" % (smallest_X,smallest_Y,largest_X,largest_Y))
272289
return (smallest_X,smallest_Y,largest_X,largest_Y)
273290

@@ -294,7 +311,7 @@ def read_keyboard(self,num):
294311
if not self._touched:
295312
self._touched = self.virt_touched()
296313
if self._touched:
297-
if self.touches[0]['x'] > self._calibX(self._display.width*.925) and self.touches[0]['y'] < self._calibY(85):
314+
if self.touches[0]['x'] > self._calibX(self.display.width*.925) and self.touches[0]['y'] < self._calibY(85):
298315
retVal = '\n'
299316
else:
300317
retVal = self.read_virtKeyboard(num)
@@ -306,15 +323,14 @@ def read_keyboard(self,num):
306323
return retVal
307324

308325
def get_screensize(self):
309-
#return (round(self._display.height*.04),round(self._display.width*.0817))
310326
return (
311-
round(self._display.height/(terminalio.FONT.bitmap.height*displayio.CIRCUITPYTHON_TERMINAL.scale))-1,
312-
round(self._display.width/((terminalio.FONT.bitmap.width/95)*displayio.CIRCUITPYTHON_TERMINAL.scale))-1
327+
round(self.display.height/(terminalio.FONT.bitmap.height*displayio.CIRCUITPYTHON_TERMINAL.scale))-1,
328+
round(self.display.width/((terminalio.FONT.bitmap.width/95)*displayio.CIRCUITPYTHON_TERMINAL.scale))-2
313329
)
314330

315331
def _identifyLocation(self,xloc,yloc):
316332
kbd_row = self._calibKB(self._kbd_row)
317-
if xloc > self._calibX(self._display.width*.925) and yloc < self._calibY(85):
333+
if xloc > self._calibX(self.display.width*.925) and yloc < self._calibY(85):
318334
retKey = "\n"
319335
elif yloc < kbd_row+self._calibY(11):
320336
retKey = ""
@@ -395,7 +411,7 @@ def virt_touched(self):
395411
return False
396412

397413
def read_virtKeyboard(self,num=0):
398-
self._display.root_group=self._kbd_group
414+
self.display.root_group=self._kbd_group
399415

400416
while self.virt_touched():
401417
pass
@@ -439,7 +455,7 @@ def read_virtKeyboard(self,num=0):
439455
#while len(self._kbd_group) > 1:
440456
# self._kbd_group.pop()
441457
self._keyedTxt.text = ""
442-
self._display.root_group = displayio.CIRCUITPYTHON_TERMINAL
458+
self.display.root_group = displayio.CIRCUITPYTHON_TERMINAL
443459
return keyString
444460

445461
Pydos_ui = PyDOS_UI()
@@ -469,7 +485,7 @@ def input(disp_text=None):
469485
if done:
470486
break
471487
elif Pydos_ui.virt_touched():
472-
if Pydos_ui.touches[0]['x'] > Pydos_ui._calibX(Pydos_ui._display.width*.925) and \
488+
if Pydos_ui.touches[0]['x'] > Pydos_ui._calibX(Pydos_ui.display.width*.925) and \
473489
Pydos_ui.touches[0]['y'] < Pydos_ui._calibY(85):
474490
keys = ''
475491
else:

0 commit comments

Comments
 (0)