August 17, 2016

Automatic orientation of Sense HAT display

In this post I will show you how to automatically detect the position of the Raspberry PI using the Sense HAT accelerometer sensor and rotate the display accordingly.

The auto_rotate_display function in the script below is doing all the logic. It's reading sensors data to detect the orientation and rotating the Sense HAT display by calling the set_rotation function.

import time
from sense_hat import SenseHat

MSG_COLOR = [200,0,160]
BKG_COLOR = [0,0,0]
SCROLL_SPEED = 0.06
MESSAGE = "Can't loose my head"


def auto_rotate_display():
  # read sensors data to detect orientation
  x = round(sense.get_accelerometer_raw()['x'], 0)
  y = round(sense.get_accelerometer_raw()['y'], 0)

  rot = 0
  if x == -1:
    rot=90
  elif y == -1:
    rot=180
  elif x == 1:
    rot=270

  # rotate the display according to the orientation
  print ("Current orientation x=%s y=%s  rotating display by %s degrees" % (x, y, rot))
  sense.set_rotation(rot)


sense = SenseHat()

while True:
  auto_rotate_display()
  sense.show_message(MESSAGE, scroll_speed=SCROLL_SPEED, text_colour=MSG_COLOR, back_colour=BKG_COLOR)
  time.sleep(1)


1 comment:

  1. Look at the way my associate Wesley Virgin's tale launches in this shocking and controversial VIDEO.

    Wesley was in the military-and soon after leaving-he discovered hidden, "SELF MIND CONTROL" tactics that the government and others used to obtain anything they want.

    As it turns out, these are the EXACT same secrets tons of celebrities (especially those who "come out of nowhere") and top business people used to become rich and successful.

    You probably know that you utilize only 10% of your brain.

    Mostly, that's because most of your brain's power is UNTAPPED.

    Maybe that thought has even taken place INSIDE your own head... as it did in my good friend Wesley Virgin's head 7 years ago, while riding a non-registered, beat-up trash bucket of a car without a driver's license and with $3.20 in his pocket.

    "I'm very fed up with living payroll to payroll! When will I finally succeed?"

    You've taken part in those questions, right?

    Your success story is waiting to happen. Go and take a leap of faith in YOURSELF.

    WATCH WESLEY SPEAK NOW

    ReplyDelete

Note: Only a member of this blog may post a comment.