本教學譯自Makeblock官方教學文件,原文點[ 我] 本教學文件以windows作業系統環境為範例,Mac使用者可以參考結合實際情況進行操作。 快速教程:如果您已安裝了Python環境和第三方編輯器,請跳至第三步。或在您的編輯器配置的Python環境當中安裝pyserial和cyberpi程式庫即可實現童芯派第三方編輯器的程式設計。 pip install pyserial pip install cyberpi 8 g- v# c$ {4 U4 K6 Y" P
1.安裝Python環境- 進入Python官方網站下載Python環境包,請根據自己的作業系統進行選擇。建議下載3.6-3.8的版本進行使用。如果下載最新的Python版本,可能帶來無法正常使用的問題。比如Python3.9以上的版本目前是不支援Windows 7作業系統的。
/ L2 J* R- d) t7 T8 _. f
Python Windows環境不同版本下載頁面:Windows Python下載 Python Mac OS 環境不同版本下載頁面: Mac OS Python下載 0 N2 g' s# E$ K
- 按兩下安裝包進行安裝,注意勾選下方的Add Python 3.6 to PATH,點擊Install Now進行安裝,也可以點擊Customize installation將Python環境安裝至指定目錄下。0 d6 R: N9 Q2 w) i# H$ {
2.下載安裝並配置協力廠商Python編輯器(以Visual Studio Code為例)提示:不同的編輯器在操作步驟上有所差異。同時建議在編輯器當中配置安裝在系統中的Python環境。 - 下載 VSCODE,請根據自己的作業系統進行選擇。 VSCODE下載地址
- 點擊下載下來的安裝包進行安裝。(根據提示進行安裝即可)
/ O3 M9 w6 Q' D Y7 F
- 點擊桌面上Visual Studio Code的圖示運行程式。
- 在軟體介面左側找到下圖紅色方框當中的圖示並點擊。
' J1 L, _) B0 R0 m. G. j - g4 x7 |/ _# a8 Z; ]7 w& \
- 在彈出的介面的搜索框當中,搜索Chinese (Traditional) Language Pack for Visual Studio Code,並點擊進行安裝。重啟後可以實現Visual Studio Code介面切換為中文。注意該擴展外掛程式發佈者為Microsoft。(如您習慣在英文介面進行程式設計,可以跳過此步驟)
0 p3 Q; P- S; C/ R z 6 Q& A0 j- |6 n& N( l* B
- 繼續搜索Python,安裝Visual Studio Code中的Python編輯環境。注意發佈者為Microsoft。7 S' F6 O: J; F
1 A1 \% m6 e- r ?4 {, O5 F1 p
- 在Visual Studio Code編輯器當中配置前面安裝的Python編輯環境。
- 新建資料夾並新建py尾碼的檔就可以對基礎的Python程式編寫。7 A% }- E; g% l- _3 X" z) i+ Y8 ]
$ @# ]0 }. }- e' K
3.童芯派第三方編輯器程式設計準備
0 g. @5 g# e6 E) Q) S- windows環境下,按下鍵盤上的win+r鍵,輸入cmd並按下Enter,然後會彈出cmd視窗。
4 U% b4 t8 H' z, Z0 D1 d ( @# d% ?) i9 L: g
- 安裝pyserial程式庫,pyserial程式庫可以確保童芯派通過USB線或藍牙Dongle與第三方編輯器建立連接。( Y% N& Q+ ?$ H9 C5 x0 G& F
在cmd視窗中輸入pip install pyserial 並按下Enter,等待安裝完畢。
9 p/ d L7 ?6 e( x0 y- 安裝cyberpi程式庫,cyberpi程式庫是用於對童芯派進行線上程式設計的Python程式庫。
9 l/ Q/ X) A* |& @" h
在cmd視窗中輸入pip install cyberpi 並按下Enter,等待程式庫安裝完畢即可。
& S$ U$ v) B% M- r- o
, H% p/ k5 a$ F# T4 Q. k- 完成安裝後,您就可以在配置了Python環境的編輯器當中進行程式編寫了。
% G: w$ h( `6 D* z. Z# W 9 D6 p6 z8 q% ?, e7 d
4.注意事項 在第三方編輯器當中目前僅支持對童芯派進行線上模式程式設計。 : s2 i6 p2 E* g& I- D$ a g
5.一些程式設計案例0 X! B- x3 O, S: ~ H1 ]# e; `
童芯派滑鼠 需要安裝pynput模組,參照上面的步驟pip install pynput 4 [% O. z# x9 }0 u7 r9 q8 k
功能介紹:1 @# n4 x; K( a% B
利用童芯派的陀螺儀模組和按鍵結合pynput模組的滑鼠控制功能,實現滑鼠的功能。% L8 b8 }8 c' M. U
通過陀螺儀控制滑鼠移動。通過按鍵實現滑鼠按鍵功能。
0 {3 p- X8 ?0 H% W$ r( p結合藍牙模組可以實現無線滑鼠的功能。% R1 }6 | P1 \: `5 G0 m
8 b9 L: {0 B% Y+ f
from pynput.mouse import Button,Controller
; R0 l8 _! l8 ~import cyberpi
, `4 U9 a4 {( C& m; C1 Cimport time" A# }4 L1 Y |8 J+ `9 ^. G
& P7 q3 Q8 J- S
mouse = Controller()% m0 }: _/ \) M7 J: s4 j w2 F& T% U
" D; D ]. r7 ]) }4 Z3 mwhile True:9 I m: N+ `$ N6 k. v) i! D' a
if cyberpi.is_tiltback():
/ |8 o" _% y. b6 T mouse.move(-3, 0)
: u7 W3 w" r# d) ?0 u print(mouse.position)
4 K# r9 I0 m T if cyberpi.is_tiltforward():
S- i$ U& K+ X9 S mouse.move(3, 0)* W9 |2 d/ ]; D+ e* v" G, ^* e8 V
if cyberpi.is_tiltleft():
: J+ ?! _+ Z0 Y, y' j% { mouse.move(0, -3)
9 D: }; Q# N; B$ C if cyberpi.is_tiltright():+ \- Q7 e, |6 I4 @9 {: }6 ]9 I
mouse.move(0, 3)5 s; I* t% d. q$ p3 L
if cyberpi.controller.is_press("b"):
3 I! o; R4 x& l2 y0 U mouse.press(Button.left)
" t* x: `7 O) |' ^ mouse.release(Button.left)* F1 Z/ w/ j5 y! u! i: P' T
mouse.press(Button.left)
9 `6 X" E, F* C6 G; k1 N0 b mouse.release(Button.left)
7 x' e6 }& X* a" g if cyberpi.controller.is_press("a"):% t* m* u' F! E4 g
mouse.press(Button.right) h0 l- \, n$ M
mouse.release(Button.right)
0 Z' t- Q. D6 j5 _( ?' r- r$ ~ time.sleep(0.01)5 y% y+ l( ~: a5 q4 @8 M
! e0 s4 ~* R$ j3 T) B, N; Z3 G/ M6 P2 i0 c9 _6 F( e5 q
語音打字器
4 N4 D7 r, t! C( G) M- ~6 B功能介紹:4 i* V. |9 X$ o& E( l+ ~4 l) S
利用童芯派的語音辨識功能結合pynput模組的鍵盤控制功能。* i: w% u/ R2 B2 s
將語音辨識的結果通過pynput在文檔中打出來。8 O* w1 t+ W. Y9 [. Q" ^
該程式目前只能支援英文。
* g7 P" z1 N6 }# P" M/ W# x% ~% w3 P) O* ^8 C
import cyberpi
2 O7 Y) e* |: P9 G6 L# \7 zfrom pynput.keyboard import Key,Controller6 r# x5 E2 e: F% }: S
import time6 u1 P9 @# W9 t
2 f- ~5 x1 D; m& T* g* }4 T
keyboard = Controller()
! b2 w9 ]! B( O$ I" Ocyberpi.console.clear(), w6 i7 H" v, [
cyberpi.led.on(0, 0, 0)
( }; B3 V* }0 T5 _cyberpi.set_recognition_url()
8 w; _2 j4 t& ?+ Xcyberpi.cloud.setkey("請輸入雲服務授權碼") # 通過慧程式設計帳號可以獲得7 K$ E& o' |: [4 t3 [5 p9 N
cyberpi.wifi.connect("WIFI名稱", "WIFI密碼")* q. X1 v% x' K$ i4 Y
while not cyberpi.wifi.is_connect():
k% U$ r% {4 W2 |2 a; ] pass# `% O. j' i5 d9 d5 S* M4 V
cyberpi.led.on(0,0,255)7 f: a* j. z" k: f
cyberpi.console.println("WIFI已連接")- q9 q K$ T1 f' f# u2 r& T
cyberpi.console.println("--------------"): r- V# D2 C$ ]3 T @% H1 c
cyberpi.console.println("按下A鍵開始語音辨識")( @+ B8 B: _0 s8 n3 D
while True:! M1 m0 C- o; S+ k
if cyberpi.controller.is_press('a'):" [; @( `, l1 K5 Y# a
keyboard.press(Key.space)
% s) _* x4 i/ E) ~7 P+ L2 y) ^; q cyberpi.console.clear()
* D- d6 N2 P: q4 }" Q cyberpi.led.on(100, 0, 0)
: h# g$ {; Y9 ?' q% F( x/ f+ P cyberpi.console.println("開始語音辨識")
5 c3 V0 Y* n4 O' z/ |. d/ j cyberpi.audio.play("switch")/ K9 v0 l! y6 F# q* w8 I& J
cyberpi.console.println("--------------")
' U* {7 X- f- Z* B" Q cyberpi.cloud.listen("english", 2)
/ d T/ K; J# F8 [2 H cyberpi.led.on(0, 0, 0)
3 E! X# K% i3 D Q; x say = cyberpi.cloud.listen_result()' p& }& W& x2 T+ C% X; j
cyberpi.console.println(say)
* B6 w+ h8 L& S! @) x" h' Z I. @ cyberpi.console.println("--------------")8 j9 {& F) I+ V: ~
for i in say:2 ?: `' b7 D3 R) x$ \2 V4 W
if i == '':* d# ?# u& L, {# Z5 _
keyboard.press(' ')
1 {3 V& y j9 h" O else:# L9 |, T( A) x
keyboard.press(str(i))3 t, R$ Z9 l2 x7 [% x' K
time.sleep(0.03)
7 w1 N: g- }* R& q# I8 c0 o
/ Q( u. G" ^+ F4 X4 t( Umatplotlib音量柱狀圖 ) ~+ s. v" _7 a$ A
import cyberpi
4 a4 t5 c6 ? ]9 Gimport time
& O0 {/ R) j" efrom matplotlib import pyplot as plt$ u2 ^6 ^; k3 c/ @$ V
6 H) q" ?1 `( }' b0 Nplt.rcParams['font.sans-serif'] =['Microsoft YaHei']
7 {9 [6 k( A9 Q, Fplt.ion()# ^' ^7 Q! N/ I2 C7 d3 u: ]; r( W2 {
7 h8 w. b* |; n9 t1 Y
while True:
" f8 h5 A" l8 r% Y8 W8 h1 w loud = cyberpi.get_loudness()/ e. Y' t* m4 P$ V4 R
plt.clf()
0 `+ P4 v2 P# L4 K5 R& B3 ~! ` plt.xlabel("數據名稱")$ o6 o' c# d7 [' \2 G7 B- ]
plt.ylabel("音量讀值")
5 s7 _: l+ t7 y; G' v y_locator = plt.MultipleLocator(5)3 i4 U. \& S( k2 S( }* L& q
ax=plt.gca()
7 L. A! ~* w P; s ax.yaxis.set_major_locator(y_locator)
4 `- A* \" @# m/ C: k plt.ylim(0,100)7 B' p' c6 G( d& |; h' Z; q/ k
plt.bar('音量', loud, align='center',label=f'溫度{loud}')8 ]* _5 u ?# j3 q
plt.title('童芯派聲音感測器讀值')& b5 a2 U w! i7 [2 h
plt.legend()6 B* b. V) Y3 u
plt.show()1 U7 f) y/ `% v0 V" M# k: X
plt.pause(0.01)
# b. K7 W: ~* n6 t& X' S/ D# q6 T& v' _# d8 c3 {' _0 j, O4 @
, \# I. q* R$ S6 }7 k- qmatplotlib 多圖表顯示/ a3 q1 Q. V1 e% [, V5 e
- e& ~" n; S' @* Q* _
from matplotlib import pyplot as plt5 J- S- g! Q$ H6 C- c+ q
import time
, m6 a2 q3 X& P5 U4 O! S( m; \import cyberpi9 ]! o- `( U& g( W# o
6 U8 _, k+ ]. zloud_list = [], @/ G0 h+ p* z7 H$ l0 v, X7 Z9 g
bri_list = [], i: Y N$ S* }% C( ~% P6 g
plt.rcParams['font.sans-serif'] =['Microsoft YaHei'] # 載入中文字體,這裡載入的是電腦的自帶的微軟雅黑字體
* L5 I+ J; J4 U6 t. R' V1 bplt.ion()
2 R+ _' g/ ^ e( a w2 uplt.figure(figsize=(10,10))* P5 }4 C v( V2 V! F1 z7 K" L
plt.figure(1)
3 d5 H; R9 r; ~5 _x_locator = plt.MultipleLocator(5)+ q E1 K$ I2 \# s3 q6 S
y_locator = plt.MultipleLocator(5)
; M5 h+ \5 n( f4 o! cwhile True:6 N' f& n O9 q- B9 n$ V
loud = cyberpi.get_loudness()
; c* t0 U- G$ q0 T/ E4 P bri = cyberpi.get_bri()
- y+ C* ~, V2 ?# G P- Y loud_list.append(loud)
4 d/ J* ]' r* g( t. Z2 i1 X bri_list.append(bri)9 i; B# P6 J8 p4 V% y/ p i
battery = cyberpi.get_battery()
* s. M! J/ A- F# K size = [battery, 100-battery]" @. P( ?# A% s& t f
status = [f'剩餘電量:{battery}%', f'已用電量:{100-battery}%']1 j/ F. h% W) I$ v4 D- u T" F
ax1 = plt.subplot(221)
8 p& t5 |; t% Z9 | plt.title('光線折線圖')
; ?4 d# t! P2 g$ t: Y7 l: D f ax1.plot(bri_list)
$ |; m& p8 \* N% ^9 c5 x ax2 = plt.subplot(222)+ j: }. Q( N% R7 w4 @% }
plt.title('聲音柱狀圖')) D1 g9 S( [ e' B
ax2.xaxis.set_major_locator(x_locator)! r% f8 o; i, {2 s' ]2 v' z" S
ax2.yaxis.set_major_locator(y_locator)! c U- N$ q, g, f$ ~$ J9 n
plt.ylim(0,100)
4 Q. L4 _/ \3 [, N3 A4 r8 H1 x8 v ax2.bar('sound', loud)
7 B5 _; E. v1 Q3 U ax3 = plt.subplot(223)
7 y N9 M/ `' r& k ax3.xaxis.set_major_locator(x_locator)7 B5 Z; c+ _* l
ax3.yaxis.set_major_locator(y_locator)# U! s' i: t9 v& ~3 ~
plt.xlim(0,100)
3 r+ V8 B) V0 B9 }- v. s7 w plt.ylim(0,100); y2 ^3 X7 |3 u1 X3 {3 x' N- J
plt.title('聲音、音量散點圖')% d" K) }7 a5 d. s* P% `7 O1 \4 Y
ax3.scatter(loud_list,bri_list)' ~# y" P( |, Z
ax4 = plt.subplot(224)9 l$ ]+ {* B: u- ^. x% c
ax4.pie(size, labels = status, radius=1,wedgeprops = {'width': 0.3,'edgecolor': 'w'})
: U4 N2 ]! F I/ @ plt.title('童芯派電量')
7 G' ?$ \) V5 m! D) n+ u plt.pause(0.2)# O: Y. ?+ r6 q
plt.clf()
: e ~) v- T; ]6 Y2 } Y. y if cyberpi.controller.is_press('a'):3 l, {* w2 E$ K& d) U
break
4 d% [7 `$ R5 r$ R) C if len(bri_list) > 500:
: y3 P% G" Q& ^) l: G% ?+ B7 R bri_list = []
0 |. P! [5 J) y. u, ?$ P& V loud_list = []5 C4 l# l' j, ]! K; c
* d" `$ C- ~2 R1 P+ z8 j4 V* Y |