樹莓派擴充套件板 sense HAT 的相關介面

只是一個id發表於2019-02-16

從官方給的 api 文件中硬翻的…

LED 模型

set_rotation 設定翻轉角度

這個函式可以設定 led 的旋轉角度

引數 型別 可選引數 描述
r Integer 0,90,180,270 0指的是樹莓派 HDMI 介面向下的方向
redraw Boolean TRUE,FALSE 預設為 TRUE

示例:

#!/usr/bin/python
import sys
import time
from sense_hat import SenseHat

X = (255, 0, 0)
O = (255, 255, 255)

question_mark = [
    O, O, O, X, X, O, O, O,
    O, O, X, O, O, X, O, O,
    O, O, O, O, O, X, O, O,
    O, O, O, O, X, O, O, O,
    O, O, O, X, O, O, O, O,
    O, O, O, X, O, O, O, O,
    O, O, O, O, O, O, O, O,
    O, O, O, X, O, O, O, O
]

sense = SenseHat()

sense.set_pixels(question_mark)

sense.set_pixel(0, 0, 255, 0, 0)
sense.set_pixel(0, 7, 0, 255, 0)
sense.set_pixel(7, 0, 0, 0, 255)
sense.set_pixel(7, 7, 255, 0, 255)

def close_light():
    black = [ [0,0,0] ] * 64
    sense.set_pixels(black)

try:
    while True:
        for r in [0, 90, 180, 270]:
            sense.set_rotation(r)
            time.sleep(0.3)
except KeyboardInterrupt:
    close_light()
    print "Good bye"

set_pixels 批量設定畫素點

改變64顆 led 的顯示顏色

引數 型別 可選引數 描述
pixel_list List [[R, G, B] * 64] 需要提供 list 長度為64的二維陣列, (r,g,b)為三原色的色值

示例參考上一個示例

get_pixels 獲取當前畫素點陣列

返回型別 描述
List 將當前的 led 屏上顯示的影像轉換成list

示例:

#!/usr/bin/python
from sense_hat import SenseHat


X = (255, 0, 0)
O = (0, 0, 0)
question_mark = [
    O, X, O, O, O, O, X, O,
    O, O, X, O, O, X, O, O,
    O, X, X, X, X, X, X, O,
    X, X, O, X, X, O, X, X,
    X, X, X, X, X, X, X, X,
    X, X, X, X, X, X, X, X,
    O, X, O, O, O, O, X, O,
    X, O, O, O, O, O, O, X
]

sense = SenseHat()

sense.set_pixels(question_mark)
out_list = sense.get_pixels()
print out_list

提示:之所以有這個函式是因為傳入set_pixels的畫素值有時會發生變化,sense HAT 是將每個畫素指定為
8 位數 (0-255) 但是如果傳入 led 的 frameBuffer 中的時候,顏色的位數會轉成 RGB565(5位紅色,6位綠色和5位藍色)
執行轉換的時候可以看到二進位制轉換時發生的精度損失
get_pixels 就是顯示畫素在緩衝區內結束時的值

set_pixel 設定單點畫素顏色

通過 x-y 座標系來定位畫素位置,以 HDMI 介面面向的位置為

引數 型別 可選引數 描述
x Integer 0-7 0為左 7為右
y Integer 0-7 0為上 7為下
當只有三個引數的時候
pixel Tuple / List 0-255 (r, g, b) 數值
當有五個引數的時候
r Integer 0-255
g Integer 0-255
b Integer 0-255

示例:

from sense_hat import SenseHat

sense = SenseHat()

# examples using (x, y, r, g, b)
sense.set_pixel(0, 0, 255, 0, 0)
sense.set_pixel(0, 7, 0, 255, 0)
sense.set_pixel(7, 0, 0, 0, 255)
sense.set_pixel(7, 7, 255, 0, 255)

red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)

# examples using (x, y, pixel)
sense.set_pixel(0, 0, red)
sense.set_pixel(0, 0, green)
sense.set_pixel(0, 0, blue)

get_pixel 獲取指定位置的顏色

get_pixels 不過是單體版的

引數 型別 可選引數 描述
x Integer 0-7 0為左 7為右
y Integer 0-7 0為上 7為下
返回型別 描述
List [R,G,B] 組成的陣列

示例:

from sense_hat import SenseHat

sense = SenseHat()
top_left_pixel = sense.get_pixel(0, 0)

load_image 載入影像到矩陣中

載入一個影像檔案,將其轉換為RGB格式,並在LED矩陣上顯示。影像的大小必須是8×8畫素。

引數 型別 可選引數 描述
file_path String ... 有效的圖片路徑
redraw Boolean TRUE/FALSE 是否重繪已載入的影像檔案在LED矩陣上。預設值為True

示例:

from sense_hat import SenseHat

sense = SenseHat()
sense.load_image("space_invader.png")
返回型別 描述
List [[R,G,B] * 64] 組成的陣列
from sense_hat import SenseHat

sense = SenseHat()
invader_pixels = sense.load_image("space_invader.png", redraw=False)

clear 讓 led 屏變成純色,預設是關閉

引數 型別 可選引數 描述
當只有一個引數的時候
pixel Tuple / List 0-255 (r, g, b) 數值,預設為[0,0,0]
當有三個引數的時候
r Integer 0-255
g Integer 0-255
b Integer 0-255

示例:

from sense_hat import SenseHat
from time import sleep

sense = SenseHat()

red = (255, 0, 0)

sense.clear()  # no arguments defaults to off
sleep(1)
sense.clear(red)  # passing in an RGB tuple
sleep(1)
sense.clear(255, 255, 255)  # passing in r, g and b values of a colour

show_message 螢幕顯示單個文字

就是街頭廣告燈的那種 led 滾屏啦!

引數 型別 可選引數 描述
text_string String ... 將要滾屏的字母
scroll_speed Float 任意浮點數 滾屏速度,預設 0.1
text_colour List [R,G,B]] 文字顏色,預設[255,255,255]
back_colour List [R,G,B]] 背景顏色,預設[0,0,0]

示例:

from sense_hat import SenseHat

sense = SenseHat()
sense.show_message("One small step for Pi!", text_colour=[255, 0, 0])

show_letter 單屏顯示字母

引數 型別 可選引數 描述
s String ... 將要顯示的字母
text_colour List [R,G,B]] 文字顏色,預設[255,255,255]
back_colour List [R,G,B]] 背景顏色,預設[0,0,0]

示例:

#!/usr/bin/python
import time
from sense_hat import SenseHat

sense = SenseHat()

letters = "ABCDEFGHIJKLMNOPQRSTUVWSYZ"


for i in letters:
    sense.show_letter(str(i))
    time.sleep(1)

low_light 調低亮度

如果覺得亮度有點刺眼的話可以開低亮度模式

import time
from sense_hat import SenseHat

sense = SenseHat()
sense.clear(255, 255, 255)
sense.low_light = True
time.sleep(2)
sense.low_light = False
#!/usr/bin/python
import time
from sense_hat import SenseHat

sense = SenseHat()
sense.clear(255, 127, 0)

sense.set_pixels(question_mark)

print(sense.gamma)
time.sleep(2)

old = sense.gamma
sense.gamma = old[::-1]
print(sense.gamma)
time.sleep(2)


sense.low_light = True
print(sense.gamma)
time.sleep(2)

sense.low_light = False

gamma

For advanced users. Most users will just need the low_light Boolean property above. The Sense HAT python API uses 8 bit (0 to 255) colours for R, G, B. When these are written to the Linux frame buffer they`re bit shifted into RGB 5 6 5. The driver then converts them to RGB 5 5 5 before it passes them over to the ATTiny88 AVR for writing to the LEDs.
The gamma property allows you to specify a gamma lookup table for the final 5 bits of colour used. The lookup table is a list of 32 numbers that must be between 0 and 31. The value of the incoming 5 bit colour is used to index the lookup table and the value found at that position is then written to the LEDs.

對於高階使用者。大多數使用者只需要上面的low_light布林屬性。這個感覺帽python API使用8位(0到255)的顏色為R,G,b。當這些被寫入Linux框架緩衝區時,它們被位轉換為RGB 5 6 5。然後,驅動程式將它們轉換為RGB 5 5 5,然後將其傳遞給ATTiny88 AVR以寫入led。
gamma屬性允許您為使用的最後5位顏色指定一個伽馬查詢表。查詢表是32個數字的列表,它們必須在0到31之間。傳入的5位顏色的值用於索引查詢表,然後將該位置上發現的值寫入led。
—來自有道詞典,因為暫時不知道用在哪裡

型別 可選引數 描述
List 長度為32的元組或列表,包含0到31之間的整數 最後的5位顏色的查詢表

示例:

import time
from sense_hat import SenseHat

sense = SenseHat()
sense.clear(255, 127, 0)

print(sense.gamma)
time.sleep(2)

sense.gamma = sense.gamma[::-1]
print(sense.gamma)
time.sleep(2)

sense.low_light = True
print(sense.gamma)
time.sleep(2)

sense.low_light = False

gamma_reset

一個函式將gamma查詢表重置為預設值,理想情況下,如果您已經對它進行了處理,並希望將它恢復到預設狀態。
示例:

import time
from sense_hat import SenseHat

sense = SenseHat()
sense.clear(255, 127, 0)
time.sleep(2)
sense.gamma = [0] * 32  # Will turn the LED matrix off
time.sleep(2)
sense.gamma_reset()

環境感應器

get_humidity 溼度

返回型別 描述
Float 溼度的百分數

示例:

#!/usr/bin/python
from sense_hat import SenseHat

sense = SenseHat()
humidity = sense.get_humidity()
print("Humidity: %s %%rH" % humidity)  #Humidity: 13.8048038483 %rH

# 同樣效果
print(sense.humidity)   #14.9011135101

get_temperature 溫度

返回值也是浮點數
示例:

#!/usr/bin/python
from sense_hat import SenseHat

sense = SenseHat()
temp = sense.get_temperature()
print("Temperature: %s C" % temp)   # Temperature: 33.0 C

# alternatives
print(sense.temp)       # 33.0
print(sense.temperature)    # 33.0

get_temperature_from_humidity 溫度

從溼度感測器獲取當前溫度(攝氏度)。
示例:

from sense_hat import SenseHat

sense = SenseHat()
temp = sense.get_temperature_from_humidity()
print("Temperature: %s C" % temp)

get_temperature_from_pressure 溫度

從壓力感測器獲取溫度

from sense_hat import SenseHat

sense = SenseHat()
temp = sense.get_temperature_from_pressure()
print("Temperature: %s C" % temp)

get_pressure 壓力

獲取壓力引數

ps: 1Bar=0.1MPa=1000mba=1000hpa=100*7.5mmhg=75mmhg=1個大氣壓

返回型別 描述
Float 單位為Millibars

示例:

from sense_hat import SenseHat

sense = SenseHat()
pressure = sense.get_pressure()
print("Pressure: %s Millibars" % pressure)  #Pressure: 1024.56738281 Millibars

# 同理
print(sense.pressure)   # 1024.56738281

IMU Sensor 慣性測量單元

IMU(inertial measurement unit)感測器是三個感測器的組合,每個感測器分別有x、y和z軸。由於這個原因,它被認為是一個9自由度的感測器。

  • 陀螺儀(Gyroscope)
  • 加速度計(Accelerometer)
  • 指南針(Magnetometer)

這個API允許你在任何組合中使用這些感測器來測量方向或單獨的感測器。

set_imu_config

支援或禁用陀螺儀、加速度計和/或磁強計

引數 型別 可選引數 描述
compass_enabled Boolean TRUE,FALSE 是否啟用指南針
gyro_enabled Boolean TRUE,FALSE 是否啟用陀螺儀
accel_enabled Boolean TRUE,FALSE 是否啟用加速度計

示例:

from sense_hat import SenseHat

sense = SenseHat()
sense.set_imu_config(False, True, False)  # 只開啟陀螺儀

get_orientation_radians

獲取當前方向弧度,依據飛行器軸引數的 pitch, roll 和 yaw.
理解傳說中的roll、yaw、pitch
尤拉角

返回型別 描述
Dictionary 由俯仰角pitch,偏航角yaw,翻滾角roll組成的字典key 值,value 為軸弧度

示例:

from sense_hat import SenseHat

sense = SenseHat()
orientation_rad = sense.get_orientation_radians()
print("p: {pitch}, r: {roll}, y: {yaw}".format(**orientation_rad)) # p: 0.0906969159842, r: -0.218863099813, y: 2.87161874771

# alternatives
print(sense.orientation_radians) # {`yaw`: 2.933598041534424, `roll`: -0.20759552717208862, `pitch`: 0.09733205288648605}

get_orientation_degrees

以俯仰、翻滾和偏航的飛機主軸得到當前的方向。

返回型別 描述
Dictionary 由俯仰角pitch,偏航角yaw,翻滾角roll組成的字典key 值,value 為軸角度

示例:

from sense_hat import SenseHat

sense = SenseHat()
orientation = sense.get_orientation_degrees()
print("p: {pitch}, r: {roll}, y: {yaw}".format(**orientation)) # p: 359.368855623, r: 359.958133745, y: 24.4292643968

get_orientation

作用同get_orientation_degrees

from sense_hat import SenseHat

sense = SenseHat()
orientation = sense.get_orientation()

print(sense.orientation) # {`yaw`: 20.334569404489745, `roll`: 0.02406978340326997, `pitch`: 359.2895215347403}

get_compass

呼叫羅盤時會預先呼叫set_imu_config禁止掉重力計和加速度計的功能

from sense_hat import SenseHat

sense = SenseHat()
north = sense.get_compass()
print("North: %s" % north) # North: 351.031626941

# alternatives
print(sense.compass) # 351.031626941

get_compass_raw

獲取原始x、y和z軸的磁強計資料。

返回型別 描述
Dictionary 字典物件索引的字串x,y和z。表示磁場強度的值浮動軸的microteslas(µT)。
from sense_hat import SenseHat

sense = SenseHat()
raw = sense.get_compass_raw()
print("x: {x}, y: {y}, z: {z}".format(**raw)) # x: 3.14855718613, y: 0.269534498453, z: -0.743863344193

# alternatives
print(sense.compass_raw) # {`y`: 0.4851621091365814, `x`: 5.667402744293213, `z`: -1.338953971862793}

get_gyroscope

呼叫set_imu_config來禁用磁強計和加速計,然後只從陀螺儀獲取當前方向。

返回型別 描述
Dictionary 由俯仰角pitch,偏航角yaw,翻滾角roll組成的字典key 值,value 為軸角度
from sense_hat import SenseHat

sense = SenseHat()
gyro_only = sense.get_gyroscope()
print("p: {pitch}, r: {roll}, y: {yaw}".format(**gyro_only))

# alternatives
print(sense.gyro) # {`yaw`: 0.0604013305118731, `roll`: 359.9494321175156, `pitch`: 359.9567423509234}
print(sense.gyroscope) # {`yaw`: 0.0604013305118731, `roll`: 359.9494321175156, `pitch`: 359.9567423509234}

get_gyroscope_raw

獲取原始x、y和z軸的陀螺儀資料。

返回型別 描述
Dictionary 一個由字串x、y和z索引的字典物件。這些值是按每秒弧度表示軸的旋轉強度的浮點數。

from sense_hat import SenseHat

sense = SenseHat()
raw = sense.get_gyroscope_raw()
print("x: {x}, y: {y}, z: {z}".format(**raw))

# alternatives
print(sense.gyro_raw)
print(sense.gyroscope_raw)

# x: 1.03765261173, y: 2.46352291107, z: 0.185390725732
# {`y`: 1.5728815793991089, `x`: 0.34309887886047363, `z`: 0.2984008193016052}
# {`y`: 0.8343454599380493, `x`: 0.163504496216774, `z`: 0.4767734408378601}

get_accelerometer

呼叫set_imu_config來禁用磁力儀和陀螺儀,然後從加速度計得到當前的方向。

返回型別 描述
Dictionary 由俯仰角pitch,偏航角yaw,翻滾角roll組成的字典key 值,value 為軸角度

from sense_hat import SenseHat

sense = SenseHat()
accel_only = sense.get_accelerometer()
print("p: {pitch}, r: {roll}, y: {yaw}".format(**accel_only))

# alternatives
print(sense.accel)
print(sense.accelerometer)

# p: 3.76471788135, r: 10.0814548376, y: 0.0
# {`yaw`: 4.5454772552392335e-07, `roll`: 10.082596332952239, `pitch`: 3.7639588765826475}
# {`yaw`: 4.5454772552392335e-07, `roll`: 10.082596332952239, `pitch`: 3.7639588765826475}

get_accelerometer_raw

獲取原始x、y和z軸加速度計資料。

返回型別 描述
Dictionary 一個由字串x、y和z索引的字典物件。這些值代表了在Gs中軸的加速度強度。
from sense_hat import SenseHat

sense = SenseHat()
raw = sense.get_accelerometer_raw()
print("x: {x}, y: {y}, z: {z}".format(**raw))

# alternatives
print(sense.accel_raw)
print(sense.accelerometer_raw)

# x: -0.0634367614985, y: 0.172625526786, z: 0.974787354469
# {`y`: 0.1738394945859909, `x`: -0.06516461074352264, `z`: 0.9757621884346008}
# {`y`: 0.17286831140518188, `x`: -0.06565827876329422, `z`: 0.9735689163208008}

Joystick 操縱桿

操縱事件

描述操縱桿事件的元組。包含三個命名引數:

  • 時間戳—事件發生的時間,作為秒數(與內建時間函式相同的格式)
  • 方向-操縱桿移動的方向,作為一個字串(“向上”,“向下”,“左”,“右”,“中間”)
  • 動作—發生的動作,作為一個字串(“按壓”,“釋放”,“持有”)

這個tuple型別被一些joystick方法使用,要麼作為返回型別,要麼是引數的型別。

wait_for_event

在發生joystick事件之前阻止執行,然後返回一個表示發生的事件的InputEvent

from sense_hat import SenseHat
from time import sleep

sense = SenseHat()
event = sense.stick.wait_for_event()
print("The joystick was {} {}".format(event.action, event.direction))
sleep(0.1)
event = sense.stick.wait_for_event()
print("The joystick was {} {}".format(event.action, event.direction))

在上面的例子中,如果你將操縱桿簡單地推到一個單一的方向,你就會看到兩個事件輸出:一個被壓的動作和一個釋放的動作。可選的emptybuffer可以用於在等待新事件之前重新整理任何未決事件。試試下面的指令碼,看看有什麼不同:

from sense_hat import SenseHat
from time import sleep

sense = SenseHat()
event = sense.stick.wait_for_event()
print("The joystick was {} {}".format(event.action, event.direction))
sleep(0.1)
event = sense.stick.wait_for_event(emptybuffer=True)
print("The joystick was {} {}".format(event.action, event.direction))

get_events

返回自最後一次呼叫get_eventswait_for_event之後發生的所有事件的InputEvent tuple的列表。

from sense_hat import SenseHat

sense = SenseHat()
while True:
    for event in sense.stick.get_events():
        print("The joystick was {} {}".format(event.action, event.direction))

direction_up, direction_left, direction_right, direction_down, direction_middle, direction_any

這些屬性可以被分配一個函式,當操縱桿按在相關的方向(或者在direction_any的任何方向上)時,它就會被呼叫。分配的函式要麼不接受引數,要麼必須接受一個引數,該引數將傳遞給相關的InputEvent

from sense_hat import SenseHat, ACTION_PRESSED, ACTION_HELD, ACTION_RELEASED
from signal import pause

x = 3
y = 3
sense = SenseHat()

def clamp(value, min_value=0, max_value=7):
    return min(max_value, max(min_value, value))

def pushed_up(event):
    global y
    if event.action != ACTION_RELEASED:
        y = clamp(y - 1)

def pushed_down(event):
    global y
    if event.action != ACTION_RELEASED:
        y = clamp(y + 1)

def pushed_left(event):
    global x
    if event.action != ACTION_RELEASED:
        x = clamp(x - 1)

def pushed_right(event):
    global x
    if event.action != ACTION_RELEASED:
        x = clamp(x + 1)

def refresh():
    sense.clear()
    sense.set_pixel(x, y, 255, 255, 255)

sense.stick.direction_up = pushed_up
sense.stick.direction_down = pushed_down
sense.stick.direction_left = pushed_left
sense.stick.direction_right = pushed_right
sense.stick.direction_any = refresh
refresh()
pause()

相關資料

部落格原文
api 原文
樹莓派+senseHAT 的一個入門專案
來自官方的 astro-pi 簡介

相關文章