本教學譯自Makeblock官方教學文件,原文點[ 我] 本教學文件以windows作業系統環境為範例,Mac使用者可以參考結合實際情況進行操作。 快速教程:如果您已安裝了Python環境和第三方編輯器,請跳至第三步。或在您的編輯器配置的Python環境當中安裝pyserial和cyberpi程式庫即可實現童芯派第三方編輯器的程式設計。 pip install pyserial pip install cyberpi 8 ]5 F$ f1 Z5 T* C2 r3 u F
1.安裝Python環境- 進入Python官方網站下載Python環境包,請根據自己的作業系統進行選擇。建議下載3.6-3.8的版本進行使用。如果下載最新的Python版本,可能帶來無法正常使用的問題。比如Python3.9以上的版本目前是不支援Windows 7作業系統的。) f% w0 g2 H3 ^- A( |+ X/ r
Python Windows環境不同版本下載頁面:Windows Python下載 Python Mac OS 環境不同版本下載頁面: Mac OS Python下載
. Z$ G* q4 ~# C% w- 按兩下安裝包進行安裝,注意勾選下方的Add Python 3.6 to PATH,點擊Install Now進行安裝,也可以點擊Customize installation將Python環境安裝至指定目錄下。& m0 O1 ]6 l. @: p) U/ ?* }+ ]
2.下載安裝並配置協力廠商Python編輯器(以Visual Studio Code為例)提示:不同的編輯器在操作步驟上有所差異。同時建議在編輯器當中配置安裝在系統中的Python環境。 - 下載 VSCODE,請根據自己的作業系統進行選擇。 VSCODE下載地址
- 點擊下載下來的安裝包進行安裝。(根據提示進行安裝即可)
# G& g- W/ l2 f' ?! z+ Q. k4 ^+ w# p
- 點擊桌面上Visual Studio Code的圖示運行程式。
- 在軟體介面左側找到下圖紅色方框當中的圖示並點擊。6 M8 e3 n9 S O: z
( u: E0 |( k: t0 N
- 在彈出的介面的搜索框當中,搜索Chinese (Traditional) Language Pack for Visual Studio Code,並點擊進行安裝。重啟後可以實現Visual Studio Code介面切換為中文。注意該擴展外掛程式發佈者為Microsoft。(如您習慣在英文介面進行程式設計,可以跳過此步驟)
( N( ? g3 L7 c9 _# m
, L% d6 O! d' c4 V) S e+ U, U7 S( j- 繼續搜索Python,安裝Visual Studio Code中的Python編輯環境。注意發佈者為Microsoft。
7 q7 M6 V% d& p* d) r+ `' v% P7 @
8 s2 ]6 T( ] F2 ]$ b- 在Visual Studio Code編輯器當中配置前面安裝的Python編輯環境。
- 新建資料夾並新建py尾碼的檔就可以對基礎的Python程式編寫。. L3 T+ `8 y5 X4 `$ L7 }
2 S* a2 n$ N7 V( r3.童芯派第三方編輯器程式設計準備! f w; t. C; e
- windows環境下,按下鍵盤上的win+r鍵,輸入cmd並按下Enter,然後會彈出cmd視窗。0 h# _' [+ y S0 q5 Y8 H& |
: f; l. Y+ P. N6 o/ o' {+ y
- 安裝pyserial程式庫,pyserial程式庫可以確保童芯派通過USB線或藍牙Dongle與第三方編輯器建立連接。
1 L, W- c$ `4 f2 p& f# ]$ @0 s
在cmd視窗中輸入pip install pyserial 並按下Enter,等待安裝完畢。
/ E+ f1 Q m8 e- 安裝cyberpi程式庫,cyberpi程式庫是用於對童芯派進行線上程式設計的Python程式庫。% |8 o/ K, R( f" W2 y
在cmd視窗中輸入pip install cyberpi 並按下Enter,等待程式庫安裝完畢即可。 0 |2 h4 @6 U; ~0 l! p4 F
) F3 L$ s: h/ L! w ]9 d- 完成安裝後,您就可以在配置了Python環境的編輯器當中進行程式編寫了。
/ V. U: } \1 Z
5 I2 c: f: q0 \( ?4.注意事項 在第三方編輯器當中目前僅支持對童芯派進行線上模式程式設計。 * j* W& e* p2 O' y9 \, U
5.一些程式設計案例
9 e) o5 R; r5 k, D' J8 c童芯派滑鼠 需要安裝pynput模組,參照上面的步驟pip install pynput
' E9 w9 a( R7 r功能介紹:. n& o! Q' Z1 Z0 @
利用童芯派的陀螺儀模組和按鍵結合pynput模組的滑鼠控制功能,實現滑鼠的功能。8 m' O7 b7 ?/ i
通過陀螺儀控制滑鼠移動。通過按鍵實現滑鼠按鍵功能。) q# z$ l* s* ~
結合藍牙模組可以實現無線滑鼠的功能。
: D8 ?" S. [/ }$ W( L
+ p( O/ i/ s% J7 M. Mfrom pynput.mouse import Button,Controller" A& t" m; {# Z% L4 y( M+ p
import cyberpi
- j/ u& U' U6 u0 Eimport time. g. Z6 k8 p* {
& V; a6 B3 J1 Y, i# K+ D7 P) M
mouse = Controller() C7 k0 V: i2 D1 I6 l
. k4 p5 U- `# t1 i% m* xwhile True:4 A0 L3 }! p9 J! k
if cyberpi.is_tiltback():. D/ G! q/ Z3 a. T, h0 ?1 o( @
mouse.move(-3, 0); S8 h3 q2 u9 K. X4 p
print(mouse.position)
' a k' n* D8 S/ ^, T$ z if cyberpi.is_tiltforward():0 O- w) L: e9 Q. G8 L0 `3 j, K
mouse.move(3, 0)
W% V. M8 s, X2 a4 ^ if cyberpi.is_tiltleft():2 N/ |) L+ r2 m# h; X& A
mouse.move(0, -3)" J6 s+ g: K6 H0 J# S
if cyberpi.is_tiltright():% s7 Y7 B) @5 e' Z! a+ X) @2 ]
mouse.move(0, 3)
7 u+ C) c6 ^! ]5 B if cyberpi.controller.is_press("b"):& E, {. M; }4 r6 M6 f7 k3 h' O
mouse.press(Button.left)
" V! Q$ e o1 J; e' H mouse.release(Button.left)
! h: E$ g/ C: c& e, U$ H# E mouse.press(Button.left)+ ~! \9 M) T5 q/ z- C# E
mouse.release(Button.left)3 Y1 P' E4 H" ?4 Q2 u. h
if cyberpi.controller.is_press("a"):( v6 b& `+ o6 w# @4 _& N* ~
mouse.press(Button.right)
2 n9 c6 _6 r/ i' c* i% I) e8 n2 | mouse.release(Button.right)
; B! x! p L2 W: K1 L time.sleep(0.01)
3 o+ d2 s3 \# g* M4 d% U
6 N) }& b( c' K8 ]! K6 K. }
8 u/ T; ]* D$ a7 V- a/ e語音打字器
5 y6 k6 L5 ~+ c6 P( Z! b5 {9 R" ?功能介紹:
9 c1 v" \ l! ~( V. d- Q( ^利用童芯派的語音辨識功能結合pynput模組的鍵盤控制功能。
% }" a5 w4 m. w將語音辨識的結果通過pynput在文檔中打出來。' U) H4 j" W: u0 u2 k2 O8 k
該程式目前只能支援英文。
6 p3 v( u7 o5 C" \& N# X: c% ~1 @5 Q% u2 O, ?! S
import cyberpi3 V( g4 p. ~# Z7 n
from pynput.keyboard import Key,Controller
+ s( X8 E1 t' Z' ^0 timport time
9 M5 b" N" D9 v9 |* m; F# p: M' }# G 9 ?, `. a3 X, `" D
keyboard = Controller()
& \* ], w9 B ^% B8 Hcyberpi.console.clear()6 I# @0 b: N. Q) z0 g; v1 p* \
cyberpi.led.on(0, 0, 0)
# ~# {+ D) q$ G7 V+ a% y3 N7 [cyberpi.set_recognition_url()6 Y) q V, L( J
cyberpi.cloud.setkey("請輸入雲服務授權碼") # 通過慧程式設計帳號可以獲得- V( k' ~- }8 ]+ l
cyberpi.wifi.connect("WIFI名稱", "WIFI密碼")0 w- H# A) V8 R+ N, U6 J
while not cyberpi.wifi.is_connect(): f H) f. g/ J& D( t
pass7 w8 W' ^+ Y! D6 [, O
cyberpi.led.on(0,0,255)7 D+ s/ d1 R8 a
cyberpi.console.println("WIFI已連接")( c4 P7 I7 B2 }1 Z# E2 C. g
cyberpi.console.println("--------------")9 F* Y& J( y6 |; a
cyberpi.console.println("按下A鍵開始語音辨識")
7 e' I [0 C+ B, t* }% _: Cwhile True:& X9 F( k u9 s' B$ X
if cyberpi.controller.is_press('a'):
4 b- ?) o. {; z/ ?) } keyboard.press(Key.space)0 h; U/ L' x6 D" o
cyberpi.console.clear()
: [; _- J: i( }& ?% s: g, Q, R cyberpi.led.on(100, 0, 0)' x& \' O/ w4 m3 C7 W+ N
cyberpi.console.println("開始語音辨識")
0 |+ i2 Q! I! R4 O$ q) { cyberpi.audio.play("switch")$ A1 |$ \7 i* D& G
cyberpi.console.println("--------------")$ m1 R4 _* A5 W$ {6 F
cyberpi.cloud.listen("english", 2)
! b6 b0 ^" \( E' U4 M# x( I2 ]: x; @ cyberpi.led.on(0, 0, 0)$ F0 {" ?+ g# R- A
say = cyberpi.cloud.listen_result()& W/ }: h/ z' R# n- ~0 c$ J
cyberpi.console.println(say): I( t: T3 A" C, }, ?
cyberpi.console.println("--------------")6 {# M: T4 b5 x2 q, \
for i in say:+ y# Y$ D4 J0 c' \0 d
if i == '':
1 ]8 Z2 P# P0 \0 C9 U6 Q keyboard.press(' ')
3 ~! y( V+ C( w0 i& m else:
F6 H/ m# t/ I( n keyboard.press(str(i))
" y: M8 a H# P; d R, N time.sleep(0.03)2 B$ j9 O( @; o# K9 l7 a2 X$ T
L. Z8 V: x0 v3 V+ S' _
matplotlib音量柱狀圖 i4 K! V6 h% O6 W* ?. k; o! D& n
import cyberpi
% Y5 N- Z3 t* _9 A0 nimport time& }4 e. k" V1 \+ a
from matplotlib import pyplot as plt
; S8 \# y( C" f ]6 ]' ~" i
! e0 T" k9 H) \/ i" D# g4 H1 rplt.rcParams['font.sans-serif'] =['Microsoft YaHei']
+ D5 t! E, P9 J! e# p! ]plt.ion()+ ?6 v) Z8 Y5 f; H( X) E3 c
% c) R) G5 E- ]$ C6 w4 zwhile True:
1 o2 T( t. ^8 Z% U3 |& d G, N loud = cyberpi.get_loudness()& P. I: I1 j; y. y
plt.clf()8 R+ ?0 A1 H# c0 i
plt.xlabel("數據名稱")" \4 z$ r1 \6 J9 @! \8 L3 e
plt.ylabel("音量讀值")% h! W# d* D3 t7 ~' ?1 d' `. L
y_locator = plt.MultipleLocator(5)5 I7 A+ D, D1 ?; ~" E
ax=plt.gca(), @) T! p3 }% G- f, T* a
ax.yaxis.set_major_locator(y_locator)
2 i4 B/ p) A) l5 z( g, p0 h plt.ylim(0,100)
" U. v5 u7 @ c' o0 B plt.bar('音量', loud, align='center',label=f'溫度{loud}')
) b# C" T+ l8 Y L plt.title('童芯派聲音感測器讀值')
0 Q: Z; j9 l+ N/ c plt.legend()
" ~6 S- f9 } T& b, y plt.show()4 f! `/ L' K4 R
plt.pause(0.01): X7 A- V5 a" O4 z/ L0 g/ p
4 a; Y8 A2 C1 I/ N3 q3 Z) o9 p+ [5 u
matplotlib 多圖表顯示
6 I5 S' J6 k9 l q! f7 ?8 c" c5 I0 q% Z" ?) H# p7 x
from matplotlib import pyplot as plt
2 [- v+ k- B3 T- C( D3 k+ gimport time9 O! | d% d0 F+ i) M2 H. R) G* E
import cyberpi
: X: A+ w9 r% b5 G5 F1 t
' Z* Y4 I) n( Q5 D7 r' V$ Kloud_list = []
+ {; c( o- y* I9 ` Lbri_list = []
1 ^6 B7 q" o2 Lplt.rcParams['font.sans-serif'] =['Microsoft YaHei'] # 載入中文字體,這裡載入的是電腦的自帶的微軟雅黑字體1 C# j9 H- v- L# ]: S6 J0 W" q. |
plt.ion()
+ W* F. n$ J5 [8 w8 rplt.figure(figsize=(10,10))" y7 |5 H; u+ O5 j- H
plt.figure(1)
: n. F9 D; X: }- y9 @x_locator = plt.MultipleLocator(5)
/ G) q" T! i5 D3 D4 hy_locator = plt.MultipleLocator(5)
/ N; D# Y) ^; Awhile True:+ W$ c' I* j6 a2 k6 u
loud = cyberpi.get_loudness()+ K* o8 P" @- j9 h
bri = cyberpi.get_bri()& x' V) g4 Z; W! J8 p. W1 H
loud_list.append(loud)
7 {8 |, }" d) K2 f" J( s; ^1 Y bri_list.append(bri)
; W) z$ ^; ~; _ battery = cyberpi.get_battery()
: k2 @$ {) c$ v# V) K size = [battery, 100-battery] _9 u1 t$ ~! c
status = [f'剩餘電量:{battery}%', f'已用電量:{100-battery}%']' ~ n$ w( T3 }. _1 u) ~) t4 `+ n7 J
ax1 = plt.subplot(221)
# c# b3 A( d* G* g' s3 a5 ^ plt.title('光線折線圖')
4 v9 B1 ^# c% Y) {" f ax1.plot(bri_list)
& P1 I+ ` [: i) S ax2 = plt.subplot(222)
$ B$ K8 d- I4 M& O3 @, J0 h7 f plt.title('聲音柱狀圖')9 }, W' ]6 D0 b8 V6 C( y% D
ax2.xaxis.set_major_locator(x_locator)2 C. F/ H& [. `0 ~
ax2.yaxis.set_major_locator(y_locator)
$ J- Z, g, X; k/ F" M: |( M plt.ylim(0,100)& n% ^* F& E# o d! _% J0 y2 g6 ~- i
ax2.bar('sound', loud)
/ V$ h% L8 s0 n' j ax3 = plt.subplot(223)1 l) H# ?8 @+ l( q/ h
ax3.xaxis.set_major_locator(x_locator)7 ~1 o% J9 C) d+ ]" f
ax3.yaxis.set_major_locator(y_locator)
+ }6 z. q& @2 r8 F; Z plt.xlim(0,100)
) U0 `" t& b- t plt.ylim(0,100)
" o2 y$ a# D4 E plt.title('聲音、音量散點圖')
% m/ ^" d2 n& `& \; a# N1 p2 j ax3.scatter(loud_list,bri_list)
) z* M2 a7 }$ X9 E9 J# c ax4 = plt.subplot(224)
7 M# Y$ d. s9 ?8 u, L* J9 e ax4.pie(size, labels = status, radius=1,wedgeprops = {'width': 0.3,'edgecolor': 'w'})
- |0 N: u: c# B% L plt.title('童芯派電量')
1 t1 }2 r% Z& N plt.pause(0.2)
, q: f& t$ d2 c$ l1 k8 o plt.clf()4 h$ W0 `; V+ L2 @' ~- X
if cyberpi.controller.is_press('a'):# {3 K T- N$ A1 J( A
break" j) e2 D" k ]& {
if len(bri_list) > 500:' P5 j8 e9 _& ?. w
bri_list = []' X: V, ^& @. ^
loud_list = []
; c, a1 b, [0 O1 ~& s; M* O8 q1 a- b: ?0 t: b
|