本教學譯自Makeblock官方教學文件,原文點[ 我] 本教學文件以windows作業系統環境為範例,Mac使用者可以參考結合實際情況進行操作。 快速教程:如果您已安裝了Python環境和第三方編輯器,請跳至第三步。或在您的編輯器配置的Python環境當中安裝pyserial和cyberpi程式庫即可實現童芯派第三方編輯器的程式設計。 pip install pyserial pip install cyberpi 4 x* ^: p2 o0 _# J9 K# @
1.安裝Python環境- 進入Python官方網站下載Python環境包,請根據自己的作業系統進行選擇。建議下載3.6-3.8的版本進行使用。如果下載最新的Python版本,可能帶來無法正常使用的問題。比如Python3.9以上的版本目前是不支援Windows 7作業系統的。
1 h# Q- @/ \% l0 t6 |$ j
Python Windows環境不同版本下載頁面:Windows Python下載 Python Mac OS 環境不同版本下載頁面: Mac OS Python下載
7 N7 h1 J J) Q- X$ n- 按兩下安裝包進行安裝,注意勾選下方的Add Python 3.6 to PATH,點擊Install Now進行安裝,也可以點擊Customize installation將Python環境安裝至指定目錄下。/ [! K4 Z) W# u. V
2.下載安裝並配置協力廠商Python編輯器(以Visual Studio Code為例)提示:不同的編輯器在操作步驟上有所差異。同時建議在編輯器當中配置安裝在系統中的Python環境。 - 下載 VSCODE,請根據自己的作業系統進行選擇。 VSCODE下載地址
- 點擊下載下來的安裝包進行安裝。(根據提示進行安裝即可)
$ s5 A- g# ^4 y8 l* e7 }
- 點擊桌面上Visual Studio Code的圖示運行程式。
- 在軟體介面左側找到下圖紅色方框當中的圖示並點擊。
) t0 z) y0 {' C- C. l: u8 Q9 ^ ; P# Y; [! ~: ?
- 在彈出的介面的搜索框當中,搜索Chinese (Traditional) Language Pack for Visual Studio Code,並點擊進行安裝。重啟後可以實現Visual Studio Code介面切換為中文。注意該擴展外掛程式發佈者為Microsoft。(如您習慣在英文介面進行程式設計,可以跳過此步驟)" v. ?. j, Q/ q& j5 m8 ~% _ H
8 ] A0 g [9 ~$ U$ g$ M
- 繼續搜索Python,安裝Visual Studio Code中的Python編輯環境。注意發佈者為Microsoft。
% Y1 D- l4 N5 P+ ]% D1 D3 N
9 L: r& h& f- d$ l! p6 E8 N8 m- 在Visual Studio Code編輯器當中配置前面安裝的Python編輯環境。
- 新建資料夾並新建py尾碼的檔就可以對基礎的Python程式編寫。# P4 p8 S, m; g/ C ?1 h
j1 {- {$ U! H3.童芯派第三方編輯器程式設計準備
0 l$ D; f1 k4 u1 F. `/ P& S! g- windows環境下,按下鍵盤上的win+r鍵,輸入cmd並按下Enter,然後會彈出cmd視窗。. M& N( ^, @( P
4 V* h" @0 L( b) v5 l2 ^7 I2 f
- 安裝pyserial程式庫,pyserial程式庫可以確保童芯派通過USB線或藍牙Dongle與第三方編輯器建立連接。
& Z( s2 x# z% x, Z
在cmd視窗中輸入pip install pyserial 並按下Enter,等待安裝完畢。
& g' P7 z4 f" K+ ]- 安裝cyberpi程式庫,cyberpi程式庫是用於對童芯派進行線上程式設計的Python程式庫。
: E2 e, @' }. V) q z
在cmd視窗中輸入pip install cyberpi 並按下Enter,等待程式庫安裝完畢即可。 ( ^+ B) j1 _6 I) i
L7 A& ~6 l) B% S# C: n% K j
- 完成安裝後,您就可以在配置了Python環境的編輯器當中進行程式編寫了。5 r6 N* U( P g. N% O0 Z% q
9 @7 ^( q3 i" o0 V3 ]% s7 R4.注意事項 在第三方編輯器當中目前僅支持對童芯派進行線上模式程式設計。
; j3 @% S" e6 [5.一些程式設計案例
# T9 @8 e% d+ R4 M' M# n5 \童芯派滑鼠 需要安裝pynput模組,參照上面的步驟pip install pynput
# J3 A1 N% }7 Y9 m功能介紹:) j- |( \( t( l+ g& [& ?
利用童芯派的陀螺儀模組和按鍵結合pynput模組的滑鼠控制功能,實現滑鼠的功能。1 m- C- v! W. Y0 F% Z
通過陀螺儀控制滑鼠移動。通過按鍵實現滑鼠按鍵功能。1 T3 s( ?6 U7 k. d+ o
結合藍牙模組可以實現無線滑鼠的功能。
; a( E' Y4 } n6 q: q
: j: X& C% K2 m. N/ Cfrom pynput.mouse import Button,Controller1 G' [) M3 H0 v5 I% e
import cyberpi
" ]1 @: }2 e+ O5 {import time
9 F- k: \9 ?6 G" n/ P
7 j+ p P% y1 I/ q2 L: umouse = Controller()
- t2 F1 e1 a& O9 R
$ ?; A7 Y: ?& S/ V2 U' Qwhile True:$ K, Y, G) O/ g* U; ?
if cyberpi.is_tiltback():
' Y5 w) B2 h# H9 w mouse.move(-3, 0), w3 w- E4 v4 v' O. t* c
print(mouse.position)
9 ^4 R3 f/ h) J if cyberpi.is_tiltforward():( x! O$ N2 f# o7 ]4 a1 l
mouse.move(3, 0)
4 d+ \, W6 r p0 i if cyberpi.is_tiltleft():9 M) N4 S3 q/ f9 G- i2 G5 @3 i {+ y
mouse.move(0, -3)
9 V9 j' r! Y, v5 X if cyberpi.is_tiltright():' Q* @; P' a/ v) g
mouse.move(0, 3)+ D3 l7 v! z z
if cyberpi.controller.is_press("b"):2 O3 J4 H1 l4 Z9 o) t
mouse.press(Button.left)
( n3 s9 M( I& W' Z- u mouse.release(Button.left)( R& ?# j1 Z3 ^2 c# t
mouse.press(Button.left)5 Z! |! T$ W, y3 ]( ?0 f
mouse.release(Button.left)
, v1 @& F+ i2 K. O; c7 L if cyberpi.controller.is_press("a"):
; H, g; e; o3 l0 ]- X( R5 B+ P mouse.press(Button.right) {2 e0 j) J! E6 k! Q
mouse.release(Button.right)
3 f' L; V: _6 w! ^ time.sleep(0.01)7 \6 @8 ?' R8 ] |
5 k2 g# w( X, X4 C
" h7 ~, M0 h& D! R5 ^0 m8 _
語音打字器
+ E2 r5 q+ V I# P- S2 l0 {( D功能介紹:0 f. {( _8 z8 p& a, N3 |3 j
利用童芯派的語音辨識功能結合pynput模組的鍵盤控制功能。5 l: O) e* F8 Z
將語音辨識的結果通過pynput在文檔中打出來。3 P) q# G2 x" _, O
該程式目前只能支援英文。
- u; d- h2 S1 m
& }" _1 {! r" b% @ a2 |2 Timport cyberpi! Y; \9 { l; [- v. u7 B
from pynput.keyboard import Key,Controller
h5 i9 @! l0 L! M! ^import time: e7 _2 G9 m G: p! F7 |
) V% i- F2 p0 A2 x/ t# skeyboard = Controller()
0 _. Y* s7 h& i) x4 }0 V' G3 ?cyberpi.console.clear()
1 E* H3 m6 Z/ l2 L, e& y0 n7 w; ^$ P6 Rcyberpi.led.on(0, 0, 0)
! q7 ]2 c# N5 n3 Bcyberpi.set_recognition_url()
: q9 M6 F3 u" N8 ] ^4 \2 S1 w. zcyberpi.cloud.setkey("請輸入雲服務授權碼") # 通過慧程式設計帳號可以獲得
- @4 s+ N, |1 \) R ^cyberpi.wifi.connect("WIFI名稱", "WIFI密碼")0 ?0 B* x9 e) a1 }
while not cyberpi.wifi.is_connect():( C1 l2 Q# a7 o5 C3 U$ ?
pass
/ C; P# W! y( @6 n' R0 d- t' _) @) |cyberpi.led.on(0,0,255), }8 S- W; C: [% ?5 f, E5 `
cyberpi.console.println("WIFI已連接")
, o% r! @) ]( R+ N/ ]* bcyberpi.console.println("--------------")
( P6 ?# T3 F9 h& [4 Ocyberpi.console.println("按下A鍵開始語音辨識")2 _3 M* n% c2 @5 F
while True:
) e9 Z5 K/ T7 b2 {, G8 u8 a if cyberpi.controller.is_press('a'):
# j2 H: s8 C! O N4 z keyboard.press(Key.space)
! O2 d, ]. ?5 G U7 @5 [ cyberpi.console.clear()8 c! }3 x/ t# {1 q
cyberpi.led.on(100, 0, 0)* X1 C8 t! L5 @, t& P% t
cyberpi.console.println("開始語音辨識")
: H# z, p1 {/ A. ~ cyberpi.audio.play("switch")
, v v5 M# i: T4 @* B8 t1 L! R* I: P cyberpi.console.println("--------------")
# c* i4 z/ y' }: `7 L$ @) c0 v" _ cyberpi.cloud.listen("english", 2)5 |, J5 z- @* C ?7 O
cyberpi.led.on(0, 0, 0)
3 h$ K, a; K, f ~, g/ L& V3 X: _ say = cyberpi.cloud.listen_result()& I- i4 E, U2 Y i
cyberpi.console.println(say)
7 n4 s# ^. P$ _" q# `# e' D cyberpi.console.println("--------------")
1 V" d6 ~* S5 L, U for i in say:6 C! i# L1 N+ f( }& w
if i == '':- c; R. _* B( k- i$ E, Q& `2 U- @
keyboard.press(' ')4 r7 ^) j: A, q; O0 z
else:
3 V* ?6 I, M; j* R `8 ?2 a keyboard.press(str(i))
) b; a6 N7 j! U time.sleep(0.03)" {5 ^- }: t% z% Y
" n8 h* f( j* b' s7 j
matplotlib音量柱狀圖
! K# J# I8 `! B4 qimport cyberpi& T) s% F4 q; b2 F
import time0 P2 Q8 t' B n; I
from matplotlib import pyplot as plt0 s7 D8 l: s. w& E7 ]5 ?8 x( m
* @7 j2 Y# Y9 b# t% ]& ]
plt.rcParams['font.sans-serif'] =['Microsoft YaHei']
+ V4 u$ i! B" i* U- e. E( Cplt.ion()! X: c# _/ F- ]+ f G
6 b( z7 v; K, }+ X/ D
while True:
2 n* w( ?2 S6 V# N9 O loud = cyberpi.get_loudness()
9 A$ Z& O+ g( i plt.clf()
7 Y; y% b+ K) L1 i, o1 I& m plt.xlabel("數據名稱")# @- x* e7 Q3 T5 M
plt.ylabel("音量讀值")
/ H: j. o9 D/ V y_locator = plt.MultipleLocator(5)7 A) S$ f3 T P/ E' J. B
ax=plt.gca()
: @: d: N5 [7 i ax.yaxis.set_major_locator(y_locator)4 A2 n; B/ D/ C( W+ u9 P7 Y, ^2 \
plt.ylim(0,100)
/ `& u% U3 W- |- c# U* f+ J plt.bar('音量', loud, align='center',label=f'溫度{loud}')
2 B) f2 V) {$ u, o plt.title('童芯派聲音感測器讀值')9 m' y9 x) }4 Q0 a3 k, r
plt.legend()
( g8 [$ L# C" A plt.show()
4 M. ~ G: j2 K% ]" k K/ q& A plt.pause(0.01)
3 F9 C: q4 c* x: _; R3 d# b5 ]2 K+ U
3 p/ x ^5 W" r
matplotlib 多圖表顯示
" b- w/ X/ @( _4 V& p7 t. |: }- _1 o
from matplotlib import pyplot as plt
& d5 D. }+ v; l6 Himport time
+ x2 z: f/ D6 X! y7 q4 w% v2 kimport cyberpi5 Q+ m6 E% a! E
" o1 W; K7 E$ l/ S$ j' g
loud_list = []
+ A& w, J/ J' K, C1 _& z( jbri_list = []3 f9 B/ y7 G& v, _' N
plt.rcParams['font.sans-serif'] =['Microsoft YaHei'] # 載入中文字體,這裡載入的是電腦的自帶的微軟雅黑字體
, U7 c" P& g% F. \plt.ion()9 c9 K) ?" R* T8 N/ m8 T' o
plt.figure(figsize=(10,10))
k5 j! m# [; [6 `+ I$ k2 rplt.figure(1)
2 L) Y6 j# t7 ^) X Ax_locator = plt.MultipleLocator(5)3 R# ?0 o0 H2 |, i/ V5 V; g
y_locator = plt.MultipleLocator(5)
, r7 `0 a/ a$ K6 j4 f7 Z* Uwhile True:8 u- m- i" t2 u
loud = cyberpi.get_loudness()
( P& J* V2 \1 Y# ~) t1 L' ?0 h bri = cyberpi.get_bri()
# [# S- g& S! H/ s& v4 j loud_list.append(loud)" E3 Q* v3 U( H* S% a' ]
bri_list.append(bri)
* A7 ]$ e( w. N* _" [ battery = cyberpi.get_battery()$ e1 T6 ]: y5 h# ?8 y
size = [battery, 100-battery]" v( n) {. H4 g/ x3 J
status = [f'剩餘電量:{battery}%', f'已用電量:{100-battery}%']/ {2 O P+ K; r# a
ax1 = plt.subplot(221)
: [/ \& @# d0 ~1 E5 v: b+ h: w) G plt.title('光線折線圖')% q0 u9 w+ z: B- U' r& {
ax1.plot(bri_list)
( ^& r! B& _- c+ D2 S& h& T( Y ax2 = plt.subplot(222)" s5 s0 I/ ^- \3 O9 \& _9 _
plt.title('聲音柱狀圖')1 K: A3 @) M+ _, w' \2 r
ax2.xaxis.set_major_locator(x_locator)
% A& j2 _9 l8 H0 f: S' q& P ax2.yaxis.set_major_locator(y_locator)
9 }- k2 Y: {; Y2 ~2 M1 h plt.ylim(0,100)
, y ?: _$ d, } ax2.bar('sound', loud)
( ?4 z6 o, i) p- A ax3 = plt.subplot(223)
+ ~) k: @5 {( C2 P2 W# _0 h4 q; F ax3.xaxis.set_major_locator(x_locator)0 ?1 ? c/ J7 |8 v3 B+ V: r w* h
ax3.yaxis.set_major_locator(y_locator)
3 m" \ m8 z/ I# b @ plt.xlim(0,100)
) ~# X8 s7 _2 \; o- R9 U plt.ylim(0,100)0 G# ?+ Z1 {0 ]
plt.title('聲音、音量散點圖')% X+ T! x M; _5 R; K
ax3.scatter(loud_list,bri_list)
8 d- q( A' H9 U* r9 W2 k* @ ax4 = plt.subplot(224)
1 Z: y8 O: L5 a5 x) \ ax4.pie(size, labels = status, radius=1,wedgeprops = {'width': 0.3,'edgecolor': 'w'})
' B. u7 U: n% ?! y8 n. h5 G plt.title('童芯派電量')
- y! j, @$ \1 P4 c) W% Z @8 @0 `0 | plt.pause(0.2)6 r0 w, r0 G1 Y5 c# `
plt.clf()6 T. N; C8 _6 @1 w2 a* q1 ^
if cyberpi.controller.is_press('a'):
- h. \* @5 C7 V3 W break
! x8 u. {3 ]8 k/ i3 d' K if len(bri_list) > 500:: Y8 {, e8 o0 G4 ?/ ^, [1 |
bri_list = []
k, K6 k$ [% p E! Y5 [ loud_list = []
, ~/ g' k: N* I3 X" s& Q" _- c- f# }* x5 }7 R* N0 W
|