forked from adafruit/Adafruit-PiTFT-Helper
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adafruit-pitft-touch-cal
executable file
·347 lines (318 loc) · 11.2 KB
/
adafruit-pitft-touch-cal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/usr/bin/python
# Script to automatically update Raspberry Pi PiTFT touchscreen calibration
# based on the current rotation of the screen.
# Copyright (c) 2014 Adafruit Industries
# Author: Tony DiCola
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import argparse
import os
import subprocess
import sys
# Calibration configuration default values.
CAL_CONFIG = {}
# 2.8" resisitive touch calibration values.
CAL_CONFIG['28r'] = {}
CAL_CONFIG['28r']['pointercal'] = {}
CAL_CONFIG['28r']['pointercal']['0'] = '4315 -49 -889068 18 5873 -1043172 6553636'
CAL_CONFIG['28r']['pointercal']['90'] = '-30 -5902 22077792 4360 -105 -1038814 65536'
CAL_CONFIG['28r']['pointercal']['180'] = '-4228 73 16353030 -60 -5888 22004262 65536'
CAL_CONFIG['28r']['pointercal']['270'] = '-69 5859 -829540 -4306 3 16564590 6553636'
CAL_CONFIG['28r']['xorg'] = {}
CAL_CONFIG['28r']['xorg']['0'] = """
Section "InputClass"
Identifier "calibration"
MatchProduct "stmpe-ts"
Option "Calibration" "252 3861 180 3745"
Option "SwapAxes" "0"
EndSection
"""
CAL_CONFIG['28r']['xorg']['90'] = """
Section "InputClass"
Identifier "calibration"
MatchProduct "stmpe-ts"
Option "Calibration" "3807 174 244 3872"
Option "SwapAxes" "1"
EndSection
"""
CAL_CONFIG['28r']['xorg']['180'] = """
Section "InputClass"
Identifier "calibration"
MatchProduct "stmpe-ts"
Option "Calibration" "3868 264 3789 237"
Option "SwapAxes" "0"
EndSection
"""
CAL_CONFIG['28r']['xorg']['270'] = """
Section "InputClass"
Identifier "calibration"
MatchProduct "stmpe-ts"
Option "Calibration" "287 3739 3817 207"
Option "SwapAxes" "1"
EndSection
"""
# 2.8" capacitive touch calibration values.
CAL_CONFIG['28c'] = {}
CAL_CONFIG['28c']['pointercal'] = {}
CAL_CONFIG['28c']['pointercal']['0'] = '-65536 0 15728640 -320 -65536 20971520 65536'
CAL_CONFIG['28c']['pointercal']['90'] = '320 65536 0 -65536 0 15728640 65536'
CAL_CONFIG['28c']['pointercal']['180'] = '65536 0 -655360 0 65536 -655360 65536'
CAL_CONFIG['28c']['pointercal']['270'] = '0 -65536 20971520 65536 0 -65536 65536'
CAL_CONFIG['28c']['xorg'] = {}
CAL_CONFIG['28c']['xorg']['0'] = """
Section "InputClass"
Identifier "captouch"
MatchProduct "ft6x06_ts"
Option "SwapAxes" "0"
Option "InvertY" "1"
Option "InvertX" "1"
Option "Calibration" "0 240 0 320"
EndSection
"""
CAL_CONFIG['28c']['xorg']['90'] = """
Section "InputClass"
Identifier "captouch"
MatchProduct "ft6x06_ts"
Option "SwapAxes" "1"
Option "InvertY" "1"
Option "Calibration" "0 320 0 240"
EndSection
"""
CAL_CONFIG['28c']['xorg']['180'] = """
Section "InputClass"
Identifier "captouch"
MatchProduct "ft6x06_ts"
Option "SwapAxes" "0"
Option "InvertY" "0"
Option "Calibration" "0 240 0 320"
EndSection
"""
CAL_CONFIG['28c']['xorg']['270'] = """
Section "InputClass"
Identifier "captouch"
MatchProduct "ft6x06_ts"
Option "SwapAxes" "1"
Option "InvertY" "0"
Option "InvertX" "1"
Option "Calibration" "0 320 0 240"
EndSection
"""
# 3.5" resisitive touch calibration values.
CAL_CONFIG['35r'] = {}
CAL_CONFIG['35r']['pointercal'] = {}
CAL_CONFIG['35r']['pointercal']['0'] = '5835 56 -1810410 22 8426 -1062652 65536'
CAL_CONFIG['35r']['pointercal']['90'] = '-16 -8501 33169914 5735 45 -1425640 65536'
CAL_CONFIG['35r']['pointercal']['180'] = '-5853 8 22390770 -59 -8353 32810368 65536'
CAL_CONFIG['35r']['pointercal']['270'] = '-95 8395 -908648 -5849 164 22156762 65536'
CAL_CONFIG['35r']['xorg'] = {}
CAL_CONFIG['35r']['xorg']['0'] = """
Section "InputClass"
Identifier "calibration"
MatchProduct "stmpe-ts"
Option "Calibration" "291 3847 141 3889"
Option "SwapAxes" "0"
EndSection
"""
CAL_CONFIG['35r']['xorg']['90'] = """
Section "InputClass"
Identifier "calibration"
MatchProduct "stmpe-ts"
Option "Calibration" "150 3912 3843 255"
Option "SwapAxes" "1"
Option "InvertX" "1"
Option "InvertY" "1"
EndSection
"""
CAL_CONFIG['35r']['xorg']['180'] = """
Section "InputClass"
Identifier "calibration"
MatchProduct "stmpe-ts"
Option "Calibration" "291 3847 141 3889"
Option "SwapAxes" "0"
Option "InvertX" "1"
Option "InvertY" "1"
EndSection
"""
CAL_CONFIG['35r']['xorg']['270'] = """
Section "InputClass"
Identifier "calibration"
MatchProduct "stmpe-ts"
Option "Calibration" "150 3912 3843 255"
Option "SwapAxes" "1"
Option "InvertX" "0"
Option "InvertY" "0"
EndSection
"""
# Other configuration.
POINTERCAL_FILE = '/etc/pointercal'
XORGCAL_FILE = '/etc/X11/xorg.conf.d/99-calibration.conf'
ALLOWED_TYPES = CAL_CONFIG.keys()
ALLOWED_ROTATIONS = ['0', '90', '180', '270']
def read_file(filename):
"""Read specified file contents and return them, or None if file isn't
readable.
"""
try:
with open(filename, 'r') as infile:
return infile.read()
except IOError:
return None
def write_file(filename, data):
"""Write specified data to file. Returns True if data was written."""
try:
# Check if path to file exists. Create path if necessary.
directory = os.path.dirname(filename)
if not os.path.exists(directory):
os.makedirs(directory)
# Open file and write data.
with open(filename, 'w') as outfile:
outfile.write(data)
return True
except IOError, OSError:
return False
def determine_rotation():
"""Determine the rotation of the PiTFT screen by examining
/sys/class/graphics/fb1/rotate config.
"""
return read_file('/sys/class/graphics/fb1/rotate')
def determine_type():
"""Determine the type of display by examining loaded kernel modules.
"""
# Call lsmod to list kernel modules.
output = subprocess.check_output('lsmod')
# Parse out module names from lsmod response (grab first word of each line
# after the first line).
modules = map(lambda x: x.split()[0], output.splitlines()[1:])
# Check for display type based on loaded modules.
if 'stmpe_ts' in modules and 'fb_ili9340' in modules:
return '28r'
elif 'ft6x06_ts' in modules and 'fb_ili9340' in modules:
return '28c'
elif 'stmpe_ts' in modules and 'fb_hx8357d' in modules:
return '35r'
else:
return None
# Parse command line arguments.
parser = argparse.ArgumentParser(description='Automatically set the PiTFT touchscreen calibration for both /etc/pointercal and X.Org based on the current screen rotation.')
parser.add_argument('-t', '--type',
choices=ALLOWED_TYPES,
required=False,
dest='type',
help='set display type')
parser.add_argument('-r', '--rotation',
choices=ALLOWED_ROTATIONS,
required=False,
dest='rotation',
help='set calibration for specified screen rotation')
parser.add_argument('-f', '--force',
required=False,
action='store_const',
const=True,
default=False,
dest='force',
help='update calibration without prompting for confirmation')
args = parser.parse_args()
# Check that you're running as root.
if os.geteuid() != 0:
print 'Must be run as root so calibration files can be updated!'
print 'Try running with sudo, for example: sudo ./pitft_touch_cal.py'
sys.exit(1)
# Determine display type if not specified in parameters.
display_type = args.type
if display_type is None:
display_type = determine_type()
if display_type is None:
print 'Could not detect display type!'
print ''
print 'Make sure PiTFT software is configured and run again.'
print 'Alternatively, run with the --type parameter to'
print 'specify an explicit display type value.'
print ''
parser.print_help()
sys.exit(1)
# Check display type is allowed value.
if display_type not in ALLOWED_TYPES:
print 'Unsupported display type: {0}'.format(display_type)
parser.print_help()
sys.exit(1)
# Determine rotation if not specified in parameters.
rotation = args.rotation
if rotation is None:
rotation = determine_rotation()
if rotation is None:
# Error if rotation couldn't be determined.
print 'Could not detect screen rotation!'
print ''
print 'Make sure PiTFT software is configured and run again.'
print 'Alternatively, run with the --rotation parameter to'
print 'specify an explicit rotation value.'
print ''
parser.print_help()
sys.exit(1)
# Check rotation is allowed value.
rotation = rotation.strip()
if rotation not in ALLOWED_ROTATIONS:
print 'Unsupported rotation value: {0}'.format(rotation)
parser.print_help()
sys.exit(1)
print '---------------------------------'
print 'USING DISPLAY: {0}'.format(display_type)
print ''
print '---------------------------------'
print 'USING ROTATION: {0}'.format(rotation)
print ''
# Print current calibration values.
print '---------------------------------'
print 'CURRENT CONFIGURATION'
print ''
for cal_file in [POINTERCAL_FILE, XORGCAL_FILE]:
cal = read_file(cal_file)
if cal is None:
print 'Could not determine {0} configuration.'.format(cal_file)
else:
print 'Current {0} configuration:'.format(cal_file)
print cal.strip()
print ''
# Determine new calibration values.
new_pointercal = CAL_CONFIG[display_type]['pointercal'][rotation]
new_xorgcal = CAL_CONFIG[display_type]['xorg'][rotation]
# Print new calibration values.
print '---------------------------------'
print 'NEW CONFIGURATION'
print ''
for cal, filename in [(new_pointercal, POINTERCAL_FILE),
(new_xorgcal, XORGCAL_FILE)]:
print 'New {0} configuration:'.format(filename)
print cal.strip()
print ''
# Confirm calibration change with user.
if not args.force:
confirm = raw_input('Update current configuration to new configuration? [y/N]: ')
print '---------------------------------'
print ''
if confirm.lower() not in ['y', 'yes']:
print 'Exiting without updating configuration.'
sys.exit(0)
# Change calibration.
status = 0
for cal, filename in [(new_pointercal, POINTERCAL_FILE),
(new_xorgcal, XORGCAL_FILE)]:
if not write_file(filename, cal):
print 'Failed to update {0}'.format(filename)
status = 1
else:
print 'Updated {0}'.format(filename)
sys.exit(status)