Alors pour l'ami Yann, comme promis voilà le code du script python qui permet d'utiliser 2 switchs ON OFF pour balayer les 4 possibilités de rotation d'écran :
1 switch à brancher entre GPIO17 (pin réelle 11) et une masse
1 switch à brancher entre GPIO27 (pin réelle 13) et une masse
Le premier switch contrôle la rotation 0-90°, le deuxième flip l'écran à 180°
#!/usr/bin/env python
# GPIOScreenRotate.py
# Import
import RPi.GPIO as GPIO
import os
import sys
# bypass write protection
os.system("mount /boot -o remount,rw")
# command line option to block reboot auto mode (for tests and shutdown exec)
# add N to the command line
# usage example : GPIOScreenRotate.py N
rebootMode = True
if len(sys.argv)>1:
if sys.argv[1]=="N":
rebootMode = False
print "Reboot mode disabled"
# Var init
rotationExists = False
currentRotationValue=0
newRotationValue=0
# to force the default rotation of screen (in case of particular setup)
defaultRotationValue=0
# to store default value
# add 'display_default_rotate=value' value [0,1,2,3] in /boot/config.txt
with open("/boot/config.txt","r") as f:
lines = f.readlines()
for line in lines:
if line.startswith("display_default_rotate="):
defaultRotationValue=int(line.split("=")[1])
break
print "Default rotation=",defaultRotationValue
# GPIO init with BCM pin values
GPIO.setmode(GPIO.BCM)
rotatePin=17
flipPin=27
# GPIO init alternative mode with board pin values
#GPIO.setmode(GPIO.BOARD)
#rotatePin=11
#flipPin=13
# GPIO setup : wire each switch between GPIO pin and Ground
GPIO.setup(rotatePin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(flipPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Get the current rotation
with open("/boot/config.txt","r") as f:
lines = f.readlines()
for line in lines:
if line.startswith("display_rotate="):
currentRotationValue = int(line.split("=")[1])
rotationExists = True
print "Current rotation found in /boot/config.txt :", currentRotationValue
break
# if line not exists in /boot/config.txt
# write the default value line at eof
if not rotationExists:
with open("/boot/config.txt","a") as f:
f.write("display_rotate=0")
print "Add display_rotate=0 to /boot/config.txt eof", currentRotationValue
# Get the GPIO rotation
# Get inverse value of switchs (because of PULL UP internal resistor) : ON=0 / OFF=1
newRotationValue = defaultRotationValue + (GPIO.input(rotatePin)+1)%2 + 2*((GPIO.input(flipPin)+1)%2)
newRotationValue = newRotationValue%4
#print "New rotation=", newRotationValue
# Cleaning GPIO changes
GPIO.cleanup()
# Compare current and new rotation value
# change /boot/config.txt if values are different
if currentRotationValue != newRotationValue:
rotationString = "display_rotate="+str(newRotationValue)+"\n"
# the display_rotate parameter is already in /boot/config.txt
if rotationExists:
with open("/boot/config.txt","r") as f:
lines = f.readlines()
with open("/boot/config.txt","w") as f:
for line in lines:
if line.startswith("display_rotate="):
line = rotationString
f.write(line)
# the display_rotate parameter is not in /boot/config.txt
else:
with open("/boot/config.txt","a") as f:
f.write(rotationString)
print "Rotation changed to ", newRotationValue
# reboot system
if rebootMode:
print "System must reboot"
os.system("shutdown -r now")
else:
# reactive write protection
os.system("mount /boot -o remount,ro")
print "No rotation change needed"
Ce code marche sur RAsPiCade (hors le mount/remount pas testé car inutile), il doit être lancé avec les droits
root (mon dieu je parle Linux maintenant
)
J'ai essayé de commenter au mieux dans la langue de l'auteur de théâtre bien connu outre manche
Oui j'ai changé mon avatar, celui là est plus adapté