A short example of using subtree for stuff.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

347 lines
11 KiB

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