MicroPython 如此簡單系列: PS2 無線遙控器 (Gamepad)

操控模式

PS2 鍵值配置

此表的鍵值由 MyKitPS2 函式庫取得, 可自行定義!

函式庫 MyKitPS2

測試範例

ESP32 的 GPIO 腳位

範例一: 讀取 PS2 鍵值

from MyKitPS2 import myPS2
ps2 = myPS2(26, 27, 14, 12)
while True:
k, v = ps2.read()
if v >= 0: # v >= 0, 找到按鍵中已定義的鍵值
print('Analog:{}, {}={}'.format(ps2.is_analog, k, v))

範例二: 啟動與結束的判斷

from MyKitPS2 import myPS2
ps2 = myPS2(26, 27, 14, 12)
while True:
k, v = ps2.read()
if v >= 0 and ps2.is_started: # 判斷是否有按下 START 啟動的控制
print('Analog:{}, {}={}'.format(ps2.is_analog, k, v))
if k == 'CROSS': # 按 CROSS 離開
break

範例三: 自訂按鍵值

pad_dict = {'QUIT': 57343, 'GREEN': 63487}   
print('\nNEW Gamepad:', ps2.gamepad(pad_dict))

如此而已, 就是這麼簡單, 希望能對你/妳有了一點點小幫助 !

--

--

Maker & Freelancer

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store