1717# along with this program. If not, see <http://www.gnu.org/licenses/>.
1818#######################################################################
1919
20+ import argparse
2021import os
2122import subprocess
2223
2324import lib_setup
2425import lib_system
2526
2627class display_config (object ):
27- def __init__ (self ):
28+ def __init__ (self , activate = None ):
2829 self .CONFIG_FILE = '/boot/firmware/lbb-display.txt'
2930 self .UDEV_FILE = '/etc/udev/hwdb.d/61-ads7846-touch.hwdb'
3031
@@ -36,6 +37,8 @@ def __init__(self):
3637 self .conf_TOUCH_MATRIX_X = self .__setup .get_val ('conf_TOUCH_MATRIX_X' )
3738 self .conf_TOUCH_MATRIX_Y = self .__setup .get_val ('conf_TOUCH_MATRIX_Y' )
3839
40+ self .conf_DISP = activate if activate in ['display' , 'screen' , '0' ] else self .__setup .get_val ('conf_DISP' )
41+
3942 def setup_display (self ):
4043 if not os .path .isfile ('/usr/sbin/lightdm' ):
4144 return (False )
@@ -46,24 +49,25 @@ def setup_display(self):
4649 def __write_config_txt (self ):
4750 rpi = lib_system .get_pi_model (number_only = True )
4851
49- # define driver
50- match self .conf_SCREEN_DRIVER :
51- case 'piscreen' :
52- DRIVER = f'dtoverlay=piscreen,speed={ self .conf_SCREEN_SPEED } ,rotate={ self .conf_SCREEN_ROTATE } '
53- case 'waveshare35a' :
54- DRIVER = f'dtoverlay=waveshare35a,speed={ self .conf_SCREEN_SPEED } ,rotate={ self .conf_SCREEN_ROTATE } '
55- case 'mipi-dbi' :
56- DRIVER = f'''dtoverlay=mipi-dbi,spi0-0,ili9486
57- dtparam=speed={ self .conf_SCREEN_SPEED }
58- dtparam=rotate={ self .conf_SCREEN_ROTATE }
59- dtparam=reset-gpio=25
60- dtparam=dc-gpio=24'''
61-
62- # Raspberry Pi 4 specific settings
63- Pi4 = '' if rpi >= 5 else 'hdmi_force_hotplug=1'
64-
65- # assemble include for config.txt
66- CONFIG = f"""# Display settings
52+ if self .conf_DISP == 'screen' :
53+ # define driver
54+ match self .conf_SCREEN_DRIVER :
55+ case 'piscreen' :
56+ DRIVER = f'dtoverlay=piscreen,speed={ self .conf_SCREEN_SPEED } ,rotate={ self .conf_SCREEN_ROTATE } '
57+ case 'waveshare35a' :
58+ DRIVER = f'dtoverlay=waveshare35a,speed={ self .conf_SCREEN_SPEED } ,rotate={ self .conf_SCREEN_ROTATE } '
59+ case 'mipi-dbi' :
60+ DRIVER = f'''dtoverlay=mipi-dbi,spi0-0,ili9486
61+ dtparam=speed={ self .conf_SCREEN_SPEED }
62+ dtparam=rotate={ self .conf_SCREEN_ROTATE }
63+ dtparam=reset-gpio=25
64+ dtparam=dc-gpio=24'''
65+
66+ # Raspberry Pi 4 specific settings
67+ Pi4 = '' if rpi >= 5 else 'hdmi_force_hotplug=1'
68+
69+ # assemble include for config.txt
70+ CONFIG = f"""# Display settings
6771
6872 # Enable SPI bus
6973 dtparam=spi=on
@@ -75,25 +79,48 @@ def __write_config_txt(self):
7579 { DRIVER }
7680
7781 dtparam=drm=on
78- """
82+ """
83+ else :
84+ CONFIG = ''
85+
7986 with open (self .CONFIG_FILE , 'w' ) as config_file :
8087 config_file .write (CONFIG )
8188
8289 def __write_touch_udev (self ):
8390 # create and activate self.UDEV_FILE
84- CONFIG = f"""evdev:name:ADS7846 Touchscreen*:*
91+ if self .conf_DISP == 'screen' :
92+ CONFIG = f"""evdev:name:ADS7846 Touchscreen*:*
8593 LIBINPUT_MODEL_PRESSURE_PAD=1
8694 LIBINPUT_ATTR_PRESSURE_RANGE=10:255
8795 LIBINPUT_ATTR_TOUCH_SIZE_RANGE=1:1
8896 LIBINPUT_CALIBRATION_MATRIX={ self .conf_TOUCH_MATRIX_X } { self .conf_TOUCH_MATRIX_Y } 0 0 1
89- """
97+ """
98+ else :
99+ CONFIG = ''
100+
90101 with open (self .UDEV_FILE , 'w' ) as config_file :
91102 config_file .write (CONFIG )
92103
93104 subprocess .run (['sudo' , 'systemd-hwdb' , 'update' ])
94105 subprocess .run (['sudo' , 'udevadm' , 'trigger' , '-s' , 'input' ])
95106
107+ def parse_args () -> argparse .Namespace :
108+ parser = argparse .ArgumentParser (
109+ description = "Write config files for SPI touchscreens"
110+ )
111+
112+ activates = ['display' , 'screen' , '0' ]
113+ parser .add_argument (
114+ "--activate" ,
115+ choices = activates ,
116+ default = "" ,
117+ help = f'One of { activates } ' ,
118+ )
119+
120+ return parser .parse_args ()
121+
96122if __name__ == "__main__" :
97- display_config ().setup_display ()
123+ args = parse_args ()
124+ display_config (activate = args .activate ).setup_display ()
98125
99126
0 commit comments