@@ -39,14 +39,14 @@ def __init__(self):
3939 #else:
4040 # IRQ_PIN = None
4141
42- self ._calibrated = False
4342 self .i2c = busio .I2C (SCL_pin , SDA_pin )
4443 if RES_pin is not None :
4544 self .ts = Touch_Screen (self .i2c , RES_pin , debug = False )
4645 else :
4746 self .ts = Touch_Screen (self .i2c , debug = False )
4847 self .touches = []
49-
48+ self ._touched = False
49+
5050 self .SHIFTED = False
5151 self .CAPLOCK = False
5252
@@ -55,62 +55,84 @@ def __init__(self):
5555 fb = dotclockframebuffer .DotClockFramebuffer (** board .TFT_PINS ,** board .TFT_TIMINGS )
5656 self ._display = framebufferio .FramebufferDisplay (fb )
5757
58- self ._kbd_row = self ._display .height - 260
59- keyboard_bitmap ,keyboard_palette = adafruit_imageload .load ("/lib/keyboard.bmp" ,bitmap = displayio .Bitmap ,palette = displayio .Palette )
58+ ts_calib = getenv ('PYDOS_TS_CALIB' )
59+ try :
60+ ts_calib = eval (ts_calib )
61+ except :
62+ ts_calib = self .calibrate ()
63+ if len (ts_calib ) != 4 :
64+ ts_calib = self .calibrate ()
65+ self ._calibXfact = (ts_calib [2 ]- ts_calib [0 ]+ 1 )/ 1024
66+ self ._calibXadj = ts_calib [0 ] - 1
67+ self ._calibYfact = (ts_calib [3 ]- ts_calib [1 ]+ 1 )/ 600
68+ self ._calibYadj = ts_calib [1 ] - 1
69+ self ._calibKBfact = (ts_calib [3 ]- ts_calib [1 ]+ 1 )/ self ._display .height
70+ self ._calibKBadj = ts_calib [1 ] - 1
71+ scrCalibX = self ._display .width / 1024
72+ scrCalibY = self ._display .height / 600
73+
74+ self ._kbd_row = self ._display .height - round (scrCalibY * 330 )
75+ keyboard_bitmap ,keyboard_palette = adafruit_imageload .load \
76+ ("/lib/keyboard" + str (self ._display .width )+ ".bmp" , \
77+ bitmap = displayio .Bitmap ,palette = displayio .Palette )
6078 htile = displayio .TileGrid (keyboard_bitmap ,pixel_shader = keyboard_palette )
61- htile .x = 15
79+ htile .x = round ( scrCalibX * 20 )
6280 htile .y = self ._kbd_row
6381 self ._kbd_group = displayio .Group ()
6482 self ._kbd_group .append (htile )
6583
6684 font = terminalio .FONT
6785 color = 0xFFFFFF
6886 self ._keyedTxt = label .Label (font , text = "" , color = color )
69- self ._keyedTxt .x = 15
70- self ._keyedTxt .y = self ._display .height - 280
87+ self ._keyedTxt .x = round ( scrCalibX * 20 )
88+ self ._keyedTxt .y = self ._display .height - round ( scrCalibY * 355 )
7189 self ._keyedTxt .scale = 2
7290 self ._kbd_group .append (self ._keyedTxt )
7391
7492 self ._shftIndicator = label .Label (font ,text = "" ,color = 0x00FF00 )
75- self ._shftIndicator .x = 15
76- self ._shftIndicator .y = 15
93+ self ._shftIndicator .x = round ( scrCalibX * 20 )
94+ self ._shftIndicator .y = round ( scrCalibY * 20 )
7795 self ._shftIndicator .scale = 2
7896 self ._kbd_group .append (self ._shftIndicator )
7997 self ._capsIndicator = label .Label (font ,text = "" ,color = 0x00FF00 )
80- self ._capsIndicator .x = 90
81- self ._capsIndicator .y = 15
98+ self ._capsIndicator .x = round ( scrCalibX * 105 )
99+ self ._capsIndicator .y = round ( scrCalibY * 20 )
82100 self ._capsIndicator .scale = 2
83101 self ._kbd_group .append (self ._capsIndicator )
84102
85- self ._touched = False
86-
87- self ._row1Keys = [665 ,615 ,565 ,515 ,465 ,415 ,365 ,315 ,265 ,215 ,165 ,115 ,68 ,- 99999 ]
103+ #self._row1Keys = [665,615,565,515,465,415,365,315,265,215,165,115,68,-99999]
104+ #self._row1Keys = [600,555,510,465,420,375,330,285,240,195,150,105,60,0,-99999]
105+ self ._row1Keys = [843 ,780 ,718 ,655 ,593 ,530 ,467 ,404 ,342 ,279 ,216 ,154 ,92 ,0 ,- 99999 ]
106+ self ._row1Keys = [self ._calibX (x ) for x in self ._row1Keys ]
88107 self ._row1Letters = ['\x08 ' ,'=' ,'-' ,'0' ,'9' ,'8' ,'7' ,'6' ,'5' ,'4' ,'3' ,'2' ,'1' ,'`' ]
89108 self ._row1Uppers = ['\x08 ' ,'+' ,'_' ,')' ,'(' ,'*' ,'&' ,'^' ,'%' ,'$' ,'#' ,'@' ,'!' ,'~' ]
90- self ._row2Keys = [695 ,645 ,595 ,545 ,495 ,445 ,395 ,345 ,295 ,245 ,195 ,145 ,95 ,- 99999 ]
109+ #self._row2Keys = [695,645,595,545,495,445,395,345,295,245,195,145,95,-99999]
110+ #self._row2Keys = [615,570,525,485,440,395,350,305,260,215,175,130,80,-99999]
111+ self ._row2Keys = [880 ,818 ,755 ,692 ,629 ,567 ,504 ,441 ,378 ,316 ,253 ,190 ,127 ,- 99999 ]
112+ self ._row2Keys = [self ._calibX (x ) for x in self ._row2Keys ]
91113 self ._row2Letters = ['\\ ' ,']' ,'[' ,'p' ,'o' ,'i' ,'u' ,'y' ,'t' ,'r' ,'e' ,'w' ,'q' ,'\x09 ' ]
92114 self ._row2Uppers = ['|' ,'}' ,'{' ]
93- self ._row3Keys = [650 ,600 ,550 ,500 ,450 ,400 ,350 ,300 ,250 ,200 ,150 ,100 ,- 99999 ]
115+ #self._row3Keys = [650,600,550,500,450,400,350,300,250,200,150,100,-99999]
116+ #self._row3Keys = [590,545,500,455,410,365,320,275,230,185,140,95,-99999]
117+ self ._row3Keys = [825 ,763 ,700 ,637 ,574 ,511 ,448 ,385 ,323 ,260 ,197 ,134 ,- 99999 ]
118+ self ._row3Keys = [self ._calibX (x ) for x in self ._row3Keys ]
94119 self ._row3Letters = ['\n ' ,"'" ,';' ,'l' ,'k' ,'j' ,'h' ,'g' ,'f' ,'d' ,'s' ,"a" ,'C' ]
95120 self ._row3Uppers = ['\n ' ,'"' ,':' ]
96- self ._row4Keys = [635 ,585 ,535 ,485 ,435 ,385 ,335 ,285 ,235 ,185 ,135 ,- 99999 ]
121+ #self._row4Keys = [635,585,535,485,435,385,335,285,235,185,135,-99999]
122+ #self._row4Keys = [565,520,475,430,385,340,295,250,205,160,115,-99999]
123+ self ._row4Keys = [790 ,727 ,664 ,602 ,539 ,476 ,413 ,351 ,288 ,225 ,162 ,- 99999 ]
124+ self ._row4Keys = [self ._calibX (x ) for x in self ._row4Keys ]
97125 self ._row4Letters = ['S' ,'/' ,'.' ,',' ,'m' ,'n' ,'b' ,'v' ,'c' ,'x' ,'z' ,'S' ]
98126 self ._row4Uppers = ['S' ,'?' ,'>' ,'<' ]
99- self ._row5Keys = [710 ,520 ,220 ,125 ,- 99999 ]
127+ #self._row5Keys = [710,520,220,125,-99999]
128+ #self._row5Keys = [640,460,200,110,-99999]
129+ self ._row5Keys = [880 ,650 ,280 ,155 ,- 99999 ]
130+ self ._row5Keys = [self ._calibX (x ) for x in self ._row5Keys ]
100131 self ._row5Letters = ['X' ,'' ,' ' ,'' ,'\x1b ' ]
101132
102- ts_calib = getenv ('PYDOS_TS_CALIB' )
103- try :
104- ts_calib = eval (ts_calib )
105- self ._calibrated = True
106- except :
107- ts_calib = self .calibrate ()
108- if len (ts_calib ) != 4 :
109- ts_calib = self .calibrate ()
110- self ._calibXfact = self ._display .width / (ts_calib [2 ]- ts_calib [0 ]+ 1 )
111- self ._calibXadj = ts_calib [0 ]
112- self ._calibYfact = self ._display .height / (ts_calib [3 ]- ts_calib [1 ]+ 1 )
113- self ._calibYadj = ts_calib [1 ]
133+ _calibX = lambda self ,x : round (x * self ._calibXfact ) + self ._calibXadj
134+ _calibY = lambda self ,y : round (y * self ._calibYfact ) + self ._calibYadj
135+ _calibKB = lambda self ,y : round (y * self ._calibKBfact ) + self ._calibKBadj
114136
115137 def calibrate (self ):
116138
@@ -211,7 +233,6 @@ def calibrate(self):
211233 envfile .write ('PYDOS_TS_CALIB="(%s,%s,%s,%s)"' % (smallest_X ,smallest_Y ,largest_X ,largest_Y ))
212234
213235 self ._display .root_group = displayio .CIRCUITPYTHON_TERMINAL
214- self ._calibrated = True
215236 print ("Screen Calibrated: (%s,%s) (%s,%s)" % (smallest_X ,smallest_Y ,largest_X ,largest_Y ))
216237 return (smallest_X ,smallest_Y ,largest_X ,largest_Y )
217238
@@ -251,15 +272,16 @@ def get_screensize(self):
251272 return (round (self ._display .height * .04 ),round (self ._display .width * .0817 ))
252273
253274 def _identifyLocation (self ,xloc ,yloc ):
254- if yloc < self ._kbd_row + 11 :
275+ kbd_row = self ._calibKB (self ._kbd_row )
276+ if yloc < kbd_row + self ._calibY (11 ):
255277 retKey = ""
256- elif yloc > self ._kbd_row + 203 : # 435
278+ elif yloc > kbd_row + self ._calibY ( 255 ) : # 435
257279 retKey = self ._row5Letters [next (a [0 ] for a in enumerate (self ._row5Keys ) if a [1 ]<= xloc )]
258- elif yloc >= self ._kbd_row + 148 : # 390
280+ elif yloc >= kbd_row + self ._calibY ( 197 ) : # 390
259281 retKey = self ._row4Letters [next (a [0 ] for a in enumerate (self ._row4Keys ) if a [1 ]<= xloc )]
260- elif yloc >= self ._kbd_row + 103 : # 345
282+ elif yloc >= kbd_row + self ._calibY ( 132 ) : # 345
261283 retKey = self ._row3Letters [next (a [0 ] for a in enumerate (self ._row3Keys ) if a [1 ]<= xloc )]
262- elif yloc >= self ._kbd_row + 57 : # 300
284+ elif yloc >= kbd_row + self ._calibY ( 75 ) : # 300
263285 retKey = self ._row2Letters [next (a [0 ] for a in enumerate (self ._row2Keys ) if a [1 ]<= xloc )]
264286 else : # 255
265287 retKey = self ._row1Letters [next (a [0 ] for a in enumerate (self ._row1Keys ) if a [1 ]<= xloc )]
@@ -302,11 +324,6 @@ def virt_touched(self):
302324 if self .ts .touched :
303325 self .touches = self .ts .touches
304326 if self .touches != []:
305- if self ._calibrated :
306- for point in self .touches :
307- point ['x' ] = round (point ['x' ]* self ._calibXfact ) - self ._calibXadj
308- point ['y' ] = round (point ['y' ]* self ._calibYfact ) - self ._calibYadj
309-
310327 return True
311328
312329 if not self ._touched :
0 commit comments