Tuesday, June 25, 2024

Sense Hat Cheatsheet (for Raspberry Pi)

from sense_hat import SenseHat

sense = SenseHat()

# -------------robotera.blogspot.com---------------------------

# 8x8 LED matrix

sense.clear()

sense.clear(r, g, b)

sense.show_letter("J")

sense.show_letter("L", red)

sense.show_message("Hello world")   # default scroll_speed = 0.1 sec

sense.show_message("Hello world", 0.3)   # 2nd argument scroll_speed; also set rotation to 270 degree

sense.show_message("Keep the arrow pointing up", scroll_speed=0.05, text_colour=[100,100,100])

sense.show_message(message, scroll_speed=0.05)

sense.show_message(message)    # optional to set scroll_speed=0.05, text_colour=[r,g,b], back_colour=[r,g,b]

sense.set_pixel(x,y, (r,g,b))       # index 0-7

sense.set_pixels(array_8x8_colours)

sense.set_rotation({angle:0|90|180|270})    # for LED matrix

sense.flip_h()      # horizontal mirroring

sense.flip_v()      # vertical mirroring

# ambient

sense.get_humidity()                # percentage, eg.  41.35004..

sense.get_pressure()                # millibars, eg. 1013.40380859

sense.get_temperature()             # celcius, eg. 33.5409..

sense.get_temperature_from_humidity()   # same as get_temperature()

sense.get_temperature_from_pressure()

# detect gravity

sense.get_accelerometer_raw() # return dictionary of x, y, z  # unit G

# gyroscope

sense.get_orientation()      # return dictionary of pitch, roll, yaw

# joystick opt 1) get events

event = sense.stick.get_events()

    event.direction     # string: {up | down | left | right | middle}

    event.action        # string: {pressed | released}

# joystick opt 2) register stick callbacks

    sense.stick.direction_up = fcn_up

    sense.stick.direction_down = fcn_dn

    sense.stick.direction_left = fcn_l

    sense.stick.direction_right = fcn_r

    sense.stick.direction_middle = fcn_m

# colour sensor (version 2 only)

    sense.color.gain = 60

    sense.color.integration_cycles = 64

    c = sense.colour.colour        # return tuple of (r,g,b,clear)

    sense.colour.integration_time

# -------------robotera.blogspot.com---------------------------

from time import sleep

sleep(0.5)      # half second

from random import randint

num = randint(0,10)     # inclusive [0,10]

from random import choice

card = choice(['A', 'K', 'Q', 'J'])

from random import uniform

fnum = uniform(0,10)    # exclusive [0.0, 10.0)

# built-in

f = round(f, 1)

s = str(5)

n = int('8')


Labels: ,