本教學譯自Makeblock官方教學文件,原文點[ 我] 本教學文件以windows作業系統環境為範例,Mac使用者可以參考結合實際情況進行操作。 快速教程:如果您已安裝了Python環境和第三方編輯器,請跳至第三步。或在您的編輯器配置的Python環境當中安裝pyserial和cyberpi程式庫即可實現童芯派第三方編輯器的程式設計。 pip install pyserial pip install cyberpi # N2 b) j# x9 d" `6 J
1.安裝Python環境- 進入Python官方網站下載Python環境包,請根據自己的作業系統進行選擇。建議下載3.6-3.8的版本進行使用。如果下載最新的Python版本,可能帶來無法正常使用的問題。比如Python3.9以上的版本目前是不支援Windows 7作業系統的。$ L H5 }" N* \. Y7 O
Python Windows環境不同版本下載頁面:Windows Python下載 Python Mac OS 環境不同版本下載頁面: Mac OS Python下載 1 B% l% C6 Q! H
- 按兩下安裝包進行安裝,注意勾選下方的Add Python 3.6 to PATH,點擊Install Now進行安裝,也可以點擊Customize installation將Python環境安裝至指定目錄下。! S4 f1 q/ C1 H4 q
2.下載安裝並配置協力廠商Python編輯器(以Visual Studio Code為例)提示:不同的編輯器在操作步驟上有所差異。同時建議在編輯器當中配置安裝在系統中的Python環境。 - 下載 VSCODE,請根據自己的作業系統進行選擇。 VSCODE下載地址
- 點擊下載下來的安裝包進行安裝。(根據提示進行安裝即可)! y# l$ Q8 ?3 P
- 點擊桌面上Visual Studio Code的圖示運行程式。
- 在軟體介面左側找到下圖紅色方框當中的圖示並點擊。. ]% [1 Q/ O" ~6 ~% U4 L
. A- |0 H& c* G- e$ f9 F7 T
- 在彈出的介面的搜索框當中,搜索Chinese (Traditional) Language Pack for Visual Studio Code,並點擊進行安裝。重啟後可以實現Visual Studio Code介面切換為中文。注意該擴展外掛程式發佈者為Microsoft。(如您習慣在英文介面進行程式設計,可以跳過此步驟)
# z% J/ H6 e& Y1 X: f- ]
* ~0 W/ u; L, X& Q- 繼續搜索Python,安裝Visual Studio Code中的Python編輯環境。注意發佈者為Microsoft。! L% j/ H. k5 T
# S+ ?4 k( u7 \0 i
- 在Visual Studio Code編輯器當中配置前面安裝的Python編輯環境。
- 新建資料夾並新建py尾碼的檔就可以對基礎的Python程式編寫。
0 K; T- f7 V% T) r 1 \" l* M3 R; x! P+ K" ^# D: D
3.童芯派第三方編輯器程式設計準備
7 w3 ` ^4 n2 ~3 L1 j- windows環境下,按下鍵盤上的win+r鍵,輸入cmd並按下Enter,然後會彈出cmd視窗。( F/ H! _8 K: ?2 c
+ H! |) Z; R4 o# H5 M- 安裝pyserial程式庫,pyserial程式庫可以確保童芯派通過USB線或藍牙Dongle與第三方編輯器建立連接。8 j' M _) g! n) t
在cmd視窗中輸入pip install pyserial 並按下Enter,等待安裝完畢。
3 ~( ~* q7 _1 [5 y) `2 m- 安裝cyberpi程式庫,cyberpi程式庫是用於對童芯派進行線上程式設計的Python程式庫。" H2 Q2 {; D8 |) \( Q1 \5 j/ K
在cmd視窗中輸入pip install cyberpi 並按下Enter,等待程式庫安裝完畢即可。
8 T# B/ p+ d2 r! L9 c& w: ]; E; W5 i2 |" q
- 完成安裝後,您就可以在配置了Python環境的編輯器當中進行程式編寫了。
9 [0 s* S0 g! ^, ~4 [
) f/ C; ]4 w; k* b4.注意事項 在第三方編輯器當中目前僅支持對童芯派進行線上模式程式設計。 / N& z* Y* \ T# \* K9 F8 O! Z
5.一些程式設計案例
% s! F. ~+ ?0 u0 ^童芯派滑鼠 需要安裝pynput模組,參照上面的步驟pip install pynput - _ n) `1 U% V
功能介紹:. d0 J; K; b0 J
利用童芯派的陀螺儀模組和按鍵結合pynput模組的滑鼠控制功能,實現滑鼠的功能。6 q7 w6 i: N$ y# S& U) s7 j
通過陀螺儀控制滑鼠移動。通過按鍵實現滑鼠按鍵功能。. G4 r" Z" b. p+ N% g! ?( g
結合藍牙模組可以實現無線滑鼠的功能。
. t5 I& E5 x7 V0 X7 t" c [6 z% M1 [# @- T8 z; ?# P
from pynput.mouse import Button,Controller$ N& b. Y0 U' y/ ?+ r" n
import cyberpi
: V0 f9 Y3 m& V/ ? [$ ~import time! o8 z1 f# T, R. _" t7 D- D
" r- ^2 h# J+ f$ Pmouse = Controller()
" O3 w* |* o3 P. w$ E
2 J4 y9 o/ K+ r3 Ywhile True:
% n9 X2 l( g0 R if cyberpi.is_tiltback():+ Z, d4 l$ v: o+ R$ v
mouse.move(-3, 0)
- \6 W5 p" h0 m6 @" V" g7 a print(mouse.position)7 x4 i$ _8 S* C' c/ T; }
if cyberpi.is_tiltforward():
! M5 }4 I2 I5 c; _6 K9 @& b mouse.move(3, 0)
4 C- ^8 r; I/ n* W% \ if cyberpi.is_tiltleft():
+ N, Q2 {6 y4 P& T mouse.move(0, -3)
; K7 k& }* Z2 I& p0 ^- r ~ if cyberpi.is_tiltright():
' u% Y" v& }* d/ ~/ P9 {6 E5 P) J" Q mouse.move(0, 3)
# J2 x# S2 L5 W7 E if cyberpi.controller.is_press("b"):- r1 ]" ^( s9 ?3 u. Q
mouse.press(Button.left)1 h/ w: i0 v, {; f k" b V: X
mouse.release(Button.left)
# W8 l; _$ b/ q/ \5 a6 e% y' ~ mouse.press(Button.left)% m7 b2 e. P% _! `) f7 Z5 K
mouse.release(Button.left)* o& a! q0 `. W/ u
if cyberpi.controller.is_press("a"):
' F9 X3 S/ p4 s% M2 N mouse.press(Button.right)* Q8 } r! k" F( m+ A
mouse.release(Button.right)9 q+ R' n& ]9 r
time.sleep(0.01)
: J2 k7 @ w; G: h0 Y$ z2 b3 ^+ ]' g* H( p9 L$ u! N
# V8 b7 j$ s4 V2 S# @6 q; A語音打字器 8 Z/ X* {) I T7 Q
功能介紹:
+ n6 Q# X- [' |2 h2 ^& l利用童芯派的語音辨識功能結合pynput模組的鍵盤控制功能。
& Z1 X! r. U N7 V將語音辨識的結果通過pynput在文檔中打出來。
) \3 d7 H; j+ _該程式目前只能支援英文。
$ ^4 a) ~; @9 h: R/ ~9 X( s8 ~6 w6 _. e. o% h& N) @
import cyberpi
! X5 J% U- \7 Bfrom pynput.keyboard import Key,Controller
' C9 [% l- C, U" j2 e! n/ t" {import time- C: p9 s/ |* w3 J$ n6 x8 E6 x
7 ^' Z8 m5 q& }! e, d: o
keyboard = Controller()
! A8 w/ ^7 N8 _$ xcyberpi.console.clear()( B/ b2 H" y5 h8 X+ f( Z$ O
cyberpi.led.on(0, 0, 0)
. _" l6 b- I; s! v1 { Ocyberpi.set_recognition_url()6 L7 `4 O6 D' c( l
cyberpi.cloud.setkey("請輸入雲服務授權碼") # 通過慧程式設計帳號可以獲得. V. \! _7 V' l5 I& C- N6 g5 G
cyberpi.wifi.connect("WIFI名稱", "WIFI密碼")( _$ G; _" [# K, \* E3 `. K# D
while not cyberpi.wifi.is_connect():
" M! S: D# d2 e; R( X+ x pass: h$ Z6 ~( C$ o/ z8 \
cyberpi.led.on(0,0,255)
& y8 P6 t$ Q: jcyberpi.console.println("WIFI已連接")
1 ^/ f7 r; R- }$ C9 @cyberpi.console.println("--------------")
0 Y! [/ w8 C1 g1 |cyberpi.console.println("按下A鍵開始語音辨識")
n& D: j) {/ j% m! r9 Z6 T+ Pwhile True:
3 R& l7 F2 o3 P. p if cyberpi.controller.is_press('a'):
% L4 i& `9 e7 V! w! | keyboard.press(Key.space)
4 M! f7 C! R* b0 t cyberpi.console.clear()0 i- [, x0 {- H9 ~7 d) D. o
cyberpi.led.on(100, 0, 0)( g" x; R5 r6 J2 W+ U) }# H1 [) }
cyberpi.console.println("開始語音辨識")
. W5 J4 Q: u; E cyberpi.audio.play("switch") D1 J' V* [ A8 d
cyberpi.console.println("--------------")
+ e+ _% ]' g- B: ~: u; _' p" @ cyberpi.cloud.listen("english", 2)7 o+ g6 A1 R' Y Z/ O1 y" a
cyberpi.led.on(0, 0, 0)
; Z- D& e5 p! d9 z: m7 \ say = cyberpi.cloud.listen_result()( I* V# d- W% T1 M6 W
cyberpi.console.println(say). z% n7 E7 F$ G' H8 a( |
cyberpi.console.println("--------------")
$ [- D- |8 F; b, s& a- B for i in say:: N& o, b9 V$ N: Z3 J
if i == '':
/ ^( P- S: O* P } keyboard.press(' ')6 J7 H1 }( K: x) {4 C/ K
else:
2 A( N* G& ?, }5 s3 f8 A6 i keyboard.press(str(i))- K: a4 W/ }9 f
time.sleep(0.03)
8 K7 ]! T( K. V/ L) i9 x! Y- D0 m" a3 b& V( }
matplotlib音量柱狀圖 ' m. Y z6 q* m1 y
import cyberpi) R' f& O! ^( q" a
import time
* E9 I' C7 i4 H( {! _from matplotlib import pyplot as plt
S; U! V( C) i 8 [+ a. P/ e) x+ r' _' m( L
plt.rcParams['font.sans-serif'] =['Microsoft YaHei']
& T3 G; _6 K1 O wplt.ion()! Q. {4 Z1 J8 H f3 M: y: [0 R
' [+ @: }) U2 _5 S* r. |& p
while True:
0 I$ b* p; `, F' G( ] loud = cyberpi.get_loudness()
5 l8 W! T/ Y+ ]0 O plt.clf()/ v2 }% ~% R; M- t* d% u
plt.xlabel("數據名稱")
9 l1 `5 B9 ?; v plt.ylabel("音量讀值")2 B! j0 e' q- K1 Z& Z! Z4 V/ |8 w
y_locator = plt.MultipleLocator(5)
9 T& Q8 [: R4 N8 _# G$ q' j0 f ax=plt.gca()
+ Q" j) S6 q7 |0 } ax.yaxis.set_major_locator(y_locator)
6 b' u* g2 r$ H( @ plt.ylim(0,100)" h, y$ Q8 G0 `. I7 c
plt.bar('音量', loud, align='center',label=f'溫度{loud}')
/ V; C( E' W+ D. I plt.title('童芯派聲音感測器讀值')
& \# X6 d3 ?6 i% f( d5 E plt.legend()
) k5 V6 I) O' Q; p plt.show(); U: }* J% ^7 s3 Z# Z
plt.pause(0.01)
- j& Z8 Q$ ]! Q2 L5 z$ k2 H: b, J% O3 |" J
$ e& O. X) v4 V, Kmatplotlib 多圖表顯示
% c$ K) m; Q; R, h0 D5 a; J) C7 Z( e! ~
from matplotlib import pyplot as plt
- ]: p- ^4 `! x4 I e: V- |import time
8 `, Q {1 U ^4 I: g1 e3 ~* ^+ aimport cyberpi
( y2 s! z2 A/ M* _) l5 ]8 J & s. f! c# Y2 D+ o" @! m
loud_list = []0 x( p6 l5 _0 J- m" h/ h
bri_list = []7 U3 V. Z! \3 j w& }% d* b' G, g( K% F
plt.rcParams['font.sans-serif'] =['Microsoft YaHei'] # 載入中文字體,這裡載入的是電腦的自帶的微軟雅黑字體
: M: p3 X0 X; R2 Y7 kplt.ion()% n' \' N+ E, C$ t
plt.figure(figsize=(10,10))' k1 Q% F% N$ O' x8 }
plt.figure(1)! i$ m: B- \; }3 ]7 B* _
x_locator = plt.MultipleLocator(5)9 c" i c) R) P8 o
y_locator = plt.MultipleLocator(5)
6 X+ O! W& w2 U/ Jwhile True:; k1 o% X& o! b
loud = cyberpi.get_loudness()
/ c, o/ h3 s+ M: F9 t bri = cyberpi.get_bri()1 C, K3 m/ y) Z" n, |! L8 o4 E
loud_list.append(loud)
% k7 c; Q4 y; t bri_list.append(bri)
/ h, r1 } [% | battery = cyberpi.get_battery()
8 }; u+ O8 X4 k size = [battery, 100-battery]
5 K$ f1 F: E& p- S6 g5 B; x status = [f'剩餘電量:{battery}%', f'已用電量:{100-battery}%']
) g; k4 O& ?/ O8 N3 [0 O ax1 = plt.subplot(221)
( A) g" H- |& ?' o% V plt.title('光線折線圖')
+ P; I+ M+ P& J# V% U: H% u ax1.plot(bri_list)
e4 F, U, C/ ^+ p ax2 = plt.subplot(222)2 ?- ?0 X7 y n, I
plt.title('聲音柱狀圖'), Q* Q1 X/ [) j: z% o
ax2.xaxis.set_major_locator(x_locator)! Y% ^5 \& w6 ?2 o. x
ax2.yaxis.set_major_locator(y_locator)% V5 l+ P* U7 P+ g, y4 P
plt.ylim(0,100) W4 n. ~3 m# c. Y( I
ax2.bar('sound', loud)# S# @8 c$ y- J, Q" B
ax3 = plt.subplot(223)
: _' f% Y8 R; g' c% K1 I4 o ax3.xaxis.set_major_locator(x_locator)
4 [5 f" v0 F- s& e' ?& V ax3.yaxis.set_major_locator(y_locator)
. [: w/ T' S$ l1 G plt.xlim(0,100)
& |# U9 O3 Y4 C6 K plt.ylim(0,100)8 w7 y4 V0 i: r9 }% P$ Z) ]6 S
plt.title('聲音、音量散點圖')* A- Y6 s# y8 X5 j+ H
ax3.scatter(loud_list,bri_list)
+ f0 d5 P) o( h6 E# F ax4 = plt.subplot(224)' P* s' x& l# g! a
ax4.pie(size, labels = status, radius=1,wedgeprops = {'width': 0.3,'edgecolor': 'w'}): ^1 P8 j* [& Y5 z# T2 R+ ]
plt.title('童芯派電量')4 W. ~9 X) Z' u* ]3 h
plt.pause(0.2)
* j4 w% X- \! J, W& \6 z5 W plt.clf()
# Q0 n2 ]5 h9 @# R3 ^) t. y( L* ~5 s1 U; S* x if cyberpi.controller.is_press('a'):
) f/ L, y ^5 ]- @( p J7 T& t break2 r" S0 U- p( A6 j6 X- h
if len(bri_list) > 500:
; G6 A+ r4 f1 R4 e bri_list = [], J) Q6 d- O5 \) G5 m! T# I
loud_list = []* G* d" ]% u' I0 Z8 g$ ]+ {
! \. \# y$ i; V. G6 ]
|