10.4 How to rotate the screen orientation

The RPi-Spark screen support the four directions 0, 90, 180, 270. This way you can display content in different directions depending on the application.

In this section we use gravity sensors to implement an automatic rotate screen app.

def _getScreenDirection(self, accel):   
    if  ((accel["x"] > 0 and accel["x"] <= 5) and
        (accel["y"] > 5 and accel["y"] <= 10)):
        return 0

    if  ((accel["x"] >= -10 and accel["x"] <= -5 ) and
        (accel["y"] >= -5 and accel["y"] < 5)):
        return 90

    if  ((accel["x"] > -5 and accel["x"] <= 5 ) and
        (accel["y"] >= -10 and accel["y"] <= -5 )):
        return 180

    if  ((accel["x"] > 5 and accel["x"] <= 10 ) and
        (accel["y"] > -5 and accel["y"] <= 5 )):
        return 270

    return 0

Next we implement the data output display function of the horizontal and vertical screens respectively _scrPortrait and _scrLandscape .

Called based on the detection result of the device direction:

scrDir = self._getScreenDirection(nAccelData)
self.RPiSpark.Screen.rotateDirection( scrDir )

In this way, the display direction of the screen can be dynamically changed, then the appropriate screen output layout can be selected according to the direction.

Here is the complete code:

from JMRPiFoundations.Skeleton.RPiSparkModule import RPiSparkModule
from JMRPiFoundations.Utiles.DataFilters import LowFilter3Axis
from PIL import ImageFont
from time import sleep

class HelloWorld(RPiSparkModule):

    def _getScreenDirection(self, accel):
        if  ((accel["x"] > 0 and accel["x"] <= 5) and
            (accel["y"] > 5 and accel["y"] <= 10)):
            return 0

        if  ((accel["x"] >= -10 and accel["x"] <= -5 ) and
            (accel["y"] >= -5 and accel["y"] < 5)):
            return 90

        if  ((accel["x"] > -5 and accel["x"] <= 5 ) and
            (accel["y"] >= -10 and accel["y"] <= -5 )):
            return 180

        if  ((accel["x"] > 5 and accel["x"] <= 10 ) and
            (accel["y"] > -5 and accel["y"] <= 5 )):
            return 270
        return 0

    def _scrPortrait(self):
        self.RPiSpark.Screen.Canvas.rectangle( (0, 0, 127, 63), 0, 1 )
        self.RPiSpark.Screen.Canvas.rectangle( (2, 2, 125, 61), 0, 1 )        
        # Show "Hello World !" and current temperature of RPi-Spark on screen of RPi-Spark
        text = "Hello World !\nTemp: {:6.2f}".format(self.RPiSpark.Attitude.getTemp())
        self.RPiSpark.Screen.write(text, xy=(0,16), spacing=10, screenCenter=True)
        # Draw a line
        self.RPiSpark.Screen.Canvas.line( (24, 32, 98, 32), 1, 2 )

    def _scrLandscape(self):
        # Show "Hello World !" on screen of RPi-Spark
        self.RPiSpark.Screen.Canvas.rectangle( (0, 0, 63, 127), 0, 1 )
        self.RPiSpark.Screen.Canvas.rectangle( (2, 2, 61, 125), 0, 1 )
        # Show "Hello World !" and current temperature of RPi-Spark on screen of RPi-Spark
        text = "Hello\nWorld !\nTemp:\n{:6.2f}".format(self.RPiSpark.Attitude.getTemp())
        self.RPiSpark.Screen.write(text, xy=(0,16), spacing=10, screenCenter=True)
        # Draw a line
        self.RPiSpark.Screen.Canvas.line( (4, 32, 59, 32), 1, 2 )

    def setup(self):
        # Create a PIL.ImageFont object instance
        self.DEF_FONT = ImageFont.load_default()
        # Open thermometer
        self.RPiSpark.Attitude.openWith(temp=True, accel=True, gyro=False)
        self.initKeyButtons("QUERY")

    def run(self):

        accelLFP = LowFilter3Axis(30.0, 1.5)
        while True:
            # Check exit key status ( JOY_UP + SW_A )
            if self.readExitButtonStatus():
                break;

            # Get Accel data from attitude sensor
            accelData = self.RPiSpark.Attitude.getAccelData()
            nAccelData = accelLFP.addSampleValue(accelData["x"], accelData["y"], accelData["z"])            
            scrDir = self._getScreenDirection(nAccelData)
            self.RPiSpark.Screen.rotateDirection(scrDir)

            if scrDir in [0, 180]: self._scrPortrait()
            if scrDir in [90, 270]: self._scrLandscape()
            self.RPiSpark.Screen.refresh()

Save as ScrRotate_10_4.py and execute the following command in the terminal:

$> rspk ScrRotate_10_4.py -f

At this time you can try to change the orientation of the device, you will find that the screen layout will also change.


 

Was this article helpful?

YES     |     NO

 

Enjoying the project? Spotted a mistake? Any opinions on the website? Let us know!