本教學譯自Makeblock官方教學文件,原文點[ 我] 本教學文件以windows作業系統環境為範例,Mac使用者可以參考結合實際情況進行操作。 快速教程:如果您已安裝了Python環境和第三方編輯器,請跳至第三步。或在您的編輯器配置的Python環境當中安裝pyserial和cyberpi程式庫即可實現童芯派第三方編輯器的程式設計。 pip install pyserial pip install cyberpi
2 L/ T+ M& Z) ^* ?1 M7 ]2 p1.安裝Python環境- 進入Python官方網站下載Python環境包,請根據自己的作業系統進行選擇。建議下載3.6-3.8的版本進行使用。如果下載最新的Python版本,可能帶來無法正常使用的問題。比如Python3.9以上的版本目前是不支援Windows 7作業系統的。' t/ i2 H5 y$ Z
Python Windows環境不同版本下載頁面:Windows Python下載 Python Mac OS 環境不同版本下載頁面: Mac OS Python下載
; V6 R/ N& N& l$ P1 a- 按兩下安裝包進行安裝,注意勾選下方的Add Python 3.6 to PATH,點擊Install Now進行安裝,也可以點擊Customize installation將Python環境安裝至指定目錄下。
3 F+ q8 s# F3 u1 W% w7 q 2.下載安裝並配置協力廠商Python編輯器(以Visual Studio Code為例)提示:不同的編輯器在操作步驟上有所差異。同時建議在編輯器當中配置安裝在系統中的Python環境。 - 下載 VSCODE,請根據自己的作業系統進行選擇。 VSCODE下載地址
- 點擊下載下來的安裝包進行安裝。(根據提示進行安裝即可)
4 u/ w. d' H# P: V
- 點擊桌面上Visual Studio Code的圖示運行程式。
- 在軟體介面左側找到下圖紅色方框當中的圖示並點擊。- W' u/ g! s4 l7 H
$ ]) Z4 G! w3 `0 ?' f, B
- 在彈出的介面的搜索框當中,搜索Chinese (Traditional) Language Pack for Visual Studio Code,並點擊進行安裝。重啟後可以實現Visual Studio Code介面切換為中文。注意該擴展外掛程式發佈者為Microsoft。(如您習慣在英文介面進行程式設計,可以跳過此步驟)
2 B r1 G- j! m! Q& t6 E 0 w& k5 c# \4 ?, B
- 繼續搜索Python,安裝Visual Studio Code中的Python編輯環境。注意發佈者為Microsoft。3 ?$ @1 q3 K( J9 M
' N' W& x9 R2 z
- 在Visual Studio Code編輯器當中配置前面安裝的Python編輯環境。
- 新建資料夾並新建py尾碼的檔就可以對基礎的Python程式編寫。( D% G- M+ p2 C3 n* `% y
0 X) [! J# W& f4 Z( o! K3 a
3.童芯派第三方編輯器程式設計準備
8 Y, q& J3 K) s# G: q- windows環境下,按下鍵盤上的win+r鍵,輸入cmd並按下Enter,然後會彈出cmd視窗。+ B& Y1 o' F* S4 p4 h
( E/ x& [4 x/ j; y- ^, A
- 安裝pyserial程式庫,pyserial程式庫可以確保童芯派通過USB線或藍牙Dongle與第三方編輯器建立連接。7 V* J+ U/ b( w. T" f) w
在cmd視窗中輸入pip install pyserial 並按下Enter,等待安裝完畢。 . I- e0 a! P2 C9 i1 ^8 m, x
- 安裝cyberpi程式庫,cyberpi程式庫是用於對童芯派進行線上程式設計的Python程式庫。
* x" W; E# ]# h8 ~; }5 q& N# ]
在cmd視窗中輸入pip install cyberpi 並按下Enter,等待程式庫安裝完畢即可。 - S) K: |: m% ?( d
/ ~6 L* b7 y1 z. ^0 T8 O4 L/ d% \
- 完成安裝後,您就可以在配置了Python環境的編輯器當中進行程式編寫了。, M: ~* |& `/ R5 G0 C
- ^5 D# c) Z. w: t7 n
4.注意事項 在第三方編輯器當中目前僅支持對童芯派進行線上模式程式設計。
0 V9 H, `2 \6 K& t$ ?* M j5.一些程式設計案例& C2 @1 U9 p. L( x; w" Z0 J
童芯派滑鼠 需要安裝pynput模組,參照上面的步驟pip install pynput 1 k, ~9 e) ~% g2 J5 B9 B: _
功能介紹:
6 y* C/ ?. u+ Q利用童芯派的陀螺儀模組和按鍵結合pynput模組的滑鼠控制功能,實現滑鼠的功能。- ~1 K6 g1 h& s0 R, p- I( b. h) t
通過陀螺儀控制滑鼠移動。通過按鍵實現滑鼠按鍵功能。1 t+ F. P6 O9 l3 S4 [3 s6 }. V
結合藍牙模組可以實現無線滑鼠的功能。, f. P# E' T* p a& B- U0 t. k
( l: Z& N0 S" C+ q( Z5 F, l( _( ?
from pynput.mouse import Button,Controller5 w$ [! G' U3 r3 i4 y* b6 ^
import cyberpi) H6 n6 Z; L W) a8 @: z) b
import time
6 M/ I7 [ ?4 f 0 x( O1 @$ R" M; @9 O. a
mouse = Controller()
3 y: c7 D0 o; I / @# \. U, D! T, y
while True:
6 ~/ x. m* T$ X& s9 Q! ? if cyberpi.is_tiltback():
t8 v% @* Z7 n3 n+ K mouse.move(-3, 0)
. X/ x; n1 ?# Q5 y$ Y print(mouse.position)# Q; Q2 ~) S$ R* e/ w
if cyberpi.is_tiltforward():
& |- f+ l; c1 {; W mouse.move(3, 0)
% F+ v! T& j4 T6 d" ]# M if cyberpi.is_tiltleft():
+ [: }+ d+ k9 `# q, _9 w0 L2 N mouse.move(0, -3)
, Y( E0 d- q8 \ if cyberpi.is_tiltright():
2 Q( t- |) D6 k mouse.move(0, 3)
[' {/ {0 Z; j if cyberpi.controller.is_press("b"):
/ W7 I. |. D" }( E' \$ ^ mouse.press(Button.left)
& u" t& s. C; v# P& K mouse.release(Button.left)6 Q0 g* {9 a' D' v. ?3 `
mouse.press(Button.left)
, {# V& p+ ^( K; f mouse.release(Button.left)' G1 Y- W8 p3 |, N( J
if cyberpi.controller.is_press("a"):/ n5 E2 W) E! c/ j0 Q8 Q% f% q5 k
mouse.press(Button.right)9 O$ v3 m7 r. z7 W5 u" K) |
mouse.release(Button.right)
8 J/ ]2 Q- ]7 B9 j* ] _5 W: p% s time.sleep(0.01)- E3 o6 k, @ Z2 O8 t% D
) I( o9 p* [9 \# w) M( a- m
( c) L/ p6 N9 x1 ?) [" d
語音打字器 + [- c' n; l1 ?( a
功能介紹:& n' ^) _6 X" A5 L- [* \9 p1 n+ L
利用童芯派的語音辨識功能結合pynput模組的鍵盤控制功能。
) ?) F0 L% R8 I1 Q2 S( a% t8 D' O將語音辨識的結果通過pynput在文檔中打出來。
3 i( l7 Q% D. ~0 d2 O# R. e該程式目前只能支援英文。4 x: a, y7 x- `+ X7 H
]( m% t7 V J7 bimport cyberpi$ P, ^: Q. |0 f9 o5 m; O2 H" m T+ u
from pynput.keyboard import Key,Controller7 a; z) E B) F; x9 [0 N6 x4 v
import time( d; R! a8 Q, U% y
, a6 P' L) W w( z( {3 G& a
keyboard = Controller()
8 j) D$ a! |% ]/ g _7 mcyberpi.console.clear()
7 \. p5 d$ q$ J$ A" y# Z& h( f, lcyberpi.led.on(0, 0, 0)
2 _4 U' H: j/ r' Bcyberpi.set_recognition_url() i0 T' [: i) n0 I) q8 y
cyberpi.cloud.setkey("請輸入雲服務授權碼") # 通過慧程式設計帳號可以獲得
. J3 e9 K2 O* {$ ?: g8 r. lcyberpi.wifi.connect("WIFI名稱", "WIFI密碼")7 ~ K( W4 T. g- u" G& j! M! H
while not cyberpi.wifi.is_connect():# f3 A$ A: x1 B$ D! S
pass3 i8 s" G) c6 Z- E( K, r+ y
cyberpi.led.on(0,0,255)
* X5 p7 r4 d! s1 N O* Bcyberpi.console.println("WIFI已連接")6 G' o1 L {" E+ J- {& v* G
cyberpi.console.println("--------------")
e& \8 K$ n" Q; k% i5 ~: n8 Y5 f4 j4 w5 b' Fcyberpi.console.println("按下A鍵開始語音辨識")
% A, j9 P: t7 I) g) E3 c8 swhile True:6 g7 @8 f& Q/ R* M( d2 w( F
if cyberpi.controller.is_press('a'):3 T4 [% M: Y4 M
keyboard.press(Key.space): E; x+ i" C9 `: m. K+ u! I: c4 ~
cyberpi.console.clear()
; \( u. q6 Z' m. ~ cyberpi.led.on(100, 0, 0)& U; _ R, U/ k0 D
cyberpi.console.println("開始語音辨識")9 k9 b7 a' L+ j0 E
cyberpi.audio.play("switch")
- q- h) _# |( N7 ] cyberpi.console.println("--------------")0 y1 g. ?2 R ]8 k* A# h' I
cyberpi.cloud.listen("english", 2)! B$ `, h8 D5 k/ h5 Q
cyberpi.led.on(0, 0, 0)
8 `& I2 E2 O* a. e say = cyberpi.cloud.listen_result()
" ~& p7 y# A, J# _ cyberpi.console.println(say): J! F$ Z" U7 X
cyberpi.console.println("--------------")
* n% @7 Z6 V3 Y for i in say:
, {; z# Z7 x2 h. V, d3 @ if i == '':
, [ g, C' \$ Q9 ^9 i keyboard.press(' ')) i T% I C+ L( `% ~
else:
J+ p7 N+ T8 B1 k" E* o3 b8 Q( u keyboard.press(str(i))* G$ N( D( b) ^: ~0 X- T1 _ i% T
time.sleep(0.03)
) Q9 o" b% N e% u# T3 k$ ^" Z* e4 i# Z9 E( s! U
matplotlib音量柱狀圖
: C4 e7 G& y6 ~: G# h- Q. Kimport cyberpi
3 `$ V$ m# Y8 D. a+ Nimport time
' w. h2 O& k7 |2 x5 ~+ B* Nfrom matplotlib import pyplot as plt5 Q5 S3 V% Q, d0 y' ^& |; {4 R
' n) y0 Y# L; {0 Fplt.rcParams['font.sans-serif'] =['Microsoft YaHei']
; Z8 W) O- l1 u' n# `: Cplt.ion()- U+ ]& ]- B" I5 }) Z
; ~/ c+ l! R9 Z# hwhile True:* Z* ^1 c" G* }0 m; x; r d) a1 d
loud = cyberpi.get_loudness()5 Z6 K( H% h3 j6 a3 R
plt.clf()" `3 @/ V; @; \+ D, X
plt.xlabel("數據名稱")
# k* X) v7 y/ s5 Y7 B1 [ a; T plt.ylabel("音量讀值")
: j* T7 O! t) s' J4 A; j y_locator = plt.MultipleLocator(5)% I( `. e& R8 P0 L9 i
ax=plt.gca(). H* Z3 t5 T$ m) w" f
ax.yaxis.set_major_locator(y_locator)
" e8 I! J* s* K% l plt.ylim(0,100)4 `/ O9 w" g& e5 s; |
plt.bar('音量', loud, align='center',label=f'溫度{loud}')" j2 S" L7 ]& I
plt.title('童芯派聲音感測器讀值')6 ?3 [' v9 M$ t
plt.legend()6 P$ D( P& E' g8 W+ h
plt.show()
: i# ^- L& q% T% ^ plt.pause(0.01)/ ]* G+ V3 m( M6 _8 j5 T8 ^+ R
3 k* B0 o7 V" u' i) e5 ~
3 Y7 G ^/ m b1 ?2 w3 [matplotlib 多圖表顯示: h: O' N* @5 a2 i; I1 t: _
* g( r" `# L. `7 E" _; E# q/ }from matplotlib import pyplot as plt8 f9 w5 z. N! g( @
import time+ J6 s6 B# [3 B0 F+ O* D
import cyberpi, c( C# m6 T( P
2 `6 ^6 s8 `/ I# Zloud_list = []/ O9 [7 Z4 G) E6 q: G. d; H4 F. p
bri_list = []
4 D) T$ o. J3 M' G2 j7 e7 a* j2 qplt.rcParams['font.sans-serif'] =['Microsoft YaHei'] # 載入中文字體,這裡載入的是電腦的自帶的微軟雅黑字體
, g8 ?) S9 T$ h3 t/ g G2 w3 V$ Xplt.ion()0 s o' `1 R8 h& c. I8 z
plt.figure(figsize=(10,10))
$ }& ^4 g) ^( aplt.figure(1)6 w! @2 k% a: B$ i# U4 V' G
x_locator = plt.MultipleLocator(5)# W6 S) B3 e# B' A' e$ h: ?% C
y_locator = plt.MultipleLocator(5)
1 u; X- M {, j. f. Twhile True:
6 W, u! j% H# o0 E loud = cyberpi.get_loudness()
! z" D2 l( Y( _" ?8 f6 ?6 h5 k) j bri = cyberpi.get_bri()& g) C) b% p( s
loud_list.append(loud)8 o8 g, M1 ~3 V
bri_list.append(bri)
; n" r4 c7 a% j battery = cyberpi.get_battery()
3 X& ]$ R1 a+ [% L% [& L4 Z, ? size = [battery, 100-battery]
! U! ^2 x+ |; Z* _ status = [f'剩餘電量:{battery}%', f'已用電量:{100-battery}%']
- [6 N) ]0 m/ T8 X/ Z: Y) G ax1 = plt.subplot(221), X8 R7 N2 h: z( e1 E4 r& _
plt.title('光線折線圖')
2 ?$ a. Z2 M5 X; z# l ax1.plot(bri_list)
1 L% f# }8 C( U5 f; n ax2 = plt.subplot(222)) y4 [* y, s+ U- T9 h, G* [ P
plt.title('聲音柱狀圖')
" w5 q- |4 p8 \/ M0 q! `4 s# j% N% [ ax2.xaxis.set_major_locator(x_locator)
. z2 s, C7 Y2 E" c+ M: J ax2.yaxis.set_major_locator(y_locator)
. I7 p! B% l" {: Y/ t- @: c plt.ylim(0,100)
c ~- K! k |9 R ax2.bar('sound', loud)
- } o; u' x8 ^, S, l ax3 = plt.subplot(223)
# B4 \* h! f% U; j# B& ?( Y( Q ax3.xaxis.set_major_locator(x_locator)
/ f0 Z" [" K' P I# j/ E$ d ax3.yaxis.set_major_locator(y_locator)$ x3 m5 `2 E- v M
plt.xlim(0,100)
5 m5 |1 ~9 x! ]* h: n9 r, S plt.ylim(0,100)' A, r& z! [6 P4 N( P. I6 V
plt.title('聲音、音量散點圖')0 _0 a. \' ]! }$ `
ax3.scatter(loud_list,bri_list)
0 K3 k. E. t$ w* U; ?" C1 z4 y& T( f ax4 = plt.subplot(224)
. d1 F' e- w* q2 R ax4.pie(size, labels = status, radius=1,wedgeprops = {'width': 0.3,'edgecolor': 'w'}): ^4 i& L! w: B+ h; o3 S8 F' W, i7 L1 J
plt.title('童芯派電量')
( V, K: D' Q$ h' U; Q& q plt.pause(0.2)
- O S/ n0 l/ K* y. S$ b0 c- \+ ?+ g" } plt.clf()( v8 H8 T; B E4 W
if cyberpi.controller.is_press('a'):
7 i! O" A1 M3 D0 ` break+ \. d" p) Z) ^7 O0 X' G9 G
if len(bri_list) > 500:
* a, f/ }( Z' {& B( y3 e. n* D bri_list = []
+ ]6 O" ~% I3 s loud_list = [], B4 V7 b* x/ E( N) W
4 |" @& s5 `' A' j |