本教學譯自Makeblock官方教學文件,原文點[ 我] 本教學文件以windows作業系統環境為範例,Mac使用者可以參考結合實際情況進行操作。 快速教程:如果您已安裝了Python環境和第三方編輯器,請跳至第三步。或在您的編輯器配置的Python環境當中安裝pyserial和cyberpi程式庫即可實現童芯派第三方編輯器的程式設計。 pip install pyserial pip install cyberpi 3 }! W0 n/ Y( N6 {( K* y4 ^/ W
1.安裝Python環境- 進入Python官方網站下載Python環境包,請根據自己的作業系統進行選擇。建議下載3.6-3.8的版本進行使用。如果下載最新的Python版本,可能帶來無法正常使用的問題。比如Python3.9以上的版本目前是不支援Windows 7作業系統的。
: b6 m& n3 y1 W
Python Windows環境不同版本下載頁面:Windows Python下載 Python Mac OS 環境不同版本下載頁面: Mac OS Python下載 * t3 S0 D0 J5 Y: k1 ?
- 按兩下安裝包進行安裝,注意勾選下方的Add Python 3.6 to PATH,點擊Install Now進行安裝,也可以點擊Customize installation將Python環境安裝至指定目錄下。6 n4 H; R! p3 B; r, a0 S6 u
2.下載安裝並配置協力廠商Python編輯器(以Visual Studio Code為例)提示:不同的編輯器在操作步驟上有所差異。同時建議在編輯器當中配置安裝在系統中的Python環境。 - 下載 VSCODE,請根據自己的作業系統進行選擇。 VSCODE下載地址
- 點擊下載下來的安裝包進行安裝。(根據提示進行安裝即可)
& r7 E' i7 x: A3 f; G6 Y/ ~+ S7 J
- 點擊桌面上Visual Studio Code的圖示運行程式。
- 在軟體介面左側找到下圖紅色方框當中的圖示並點擊。
4 E" [& W# [6 T: |* I( I
$ s6 N2 |) J' s0 h6 o- 在彈出的介面的搜索框當中,搜索Chinese (Traditional) Language Pack for Visual Studio Code,並點擊進行安裝。重啟後可以實現Visual Studio Code介面切換為中文。注意該擴展外掛程式發佈者為Microsoft。(如您習慣在英文介面進行程式設計,可以跳過此步驟)- I K0 s8 K! j! F& L
! H6 E+ `3 d: D$ `& m. d( |/ `- 繼續搜索Python,安裝Visual Studio Code中的Python編輯環境。注意發佈者為Microsoft。
* s& |/ _' [0 g % F& y4 W/ {$ I; A8 w) b F
- 在Visual Studio Code編輯器當中配置前面安裝的Python編輯環境。
- 新建資料夾並新建py尾碼的檔就可以對基礎的Python程式編寫。
8 u: P7 c0 }- l; Z3 s G
5 E: a; b9 r7 X6 Q" v I3.童芯派第三方編輯器程式設計準備
0 ]0 B2 I u$ H0 }8 f& v- windows環境下,按下鍵盤上的win+r鍵,輸入cmd並按下Enter,然後會彈出cmd視窗。
; c c3 n C7 Z* ^8 Q# H
+ k2 t; J9 S- Q: A; {- 安裝pyserial程式庫,pyserial程式庫可以確保童芯派通過USB線或藍牙Dongle與第三方編輯器建立連接。
% X3 Z* x/ e& Z6 {
在cmd視窗中輸入pip install pyserial 並按下Enter,等待安裝完畢。 3 P, i: m# s+ ]& w9 z
- 安裝cyberpi程式庫,cyberpi程式庫是用於對童芯派進行線上程式設計的Python程式庫。, Z. I9 f$ _, |1 S9 Q
在cmd視窗中輸入pip install cyberpi 並按下Enter,等待程式庫安裝完畢即可。
5 G0 Q6 _0 a7 R5 s! P
# v3 Y+ a7 {" V7 q. s- 完成安裝後,您就可以在配置了Python環境的編輯器當中進行程式編寫了。* f7 ?. ]9 s# ^. E: J: {' c
2 h% Y& v$ _: \/ K9 x- \: g
4.注意事項 在第三方編輯器當中目前僅支持對童芯派進行線上模式程式設計。
: I8 l* L' ^) d b& W5.一些程式設計案例
5 t* p. x/ N. s3 F4 d2 P+ C童芯派滑鼠 需要安裝pynput模組,參照上面的步驟pip install pynput ' v1 A& q b" `: a7 l1 |3 U) x
功能介紹:# E( W# }$ s# }+ g# S, x
利用童芯派的陀螺儀模組和按鍵結合pynput模組的滑鼠控制功能,實現滑鼠的功能。
, D6 z* \1 l* N! y6 ]( h通過陀螺儀控制滑鼠移動。通過按鍵實現滑鼠按鍵功能。
" V( k# ?9 X2 l1 H! h9 P結合藍牙模組可以實現無線滑鼠的功能。
. Q% e6 f4 U% N; Q4 g" a5 F
" A0 Q' _, r: s0 Y# |5 ^; |- F' Z9 Lfrom pynput.mouse import Button,Controller
% s- h6 r D& W7 R: Y0 `3 qimport cyberpi! C; G' a# [, r# T/ S) g N
import time
6 b% j( |" d! |7 y7 c 8 r% I! y' J$ ?( _+ `& [/ r! [
mouse = Controller()/ d& Y. a/ [4 n R+ S
3 G: e/ b8 \& M9 }1 s, @
while True:! c+ n# x, h7 [
if cyberpi.is_tiltback():' }& u" N6 @1 y! b0 X
mouse.move(-3, 0)
' C: h2 ]+ S3 v print(mouse.position)
q" j' v% o7 q2 S) ~0 B2 T if cyberpi.is_tiltforward():
: _' g4 ]$ D0 Y% Q3 k: s$ A mouse.move(3, 0)9 W; g! y5 e# w- T
if cyberpi.is_tiltleft():7 Z6 I5 \! G& M8 ^% p
mouse.move(0, -3)
& v5 \( |7 j/ {5 i* l if cyberpi.is_tiltright():
! b( C- a& `$ N4 i mouse.move(0, 3)
9 n; U# l0 i' j if cyberpi.controller.is_press("b"):) L e1 @5 _. {; I3 ~
mouse.press(Button.left)
& q+ E( s9 j) f8 m# c mouse.release(Button.left)
) J$ t$ k6 \9 W mouse.press(Button.left)
# f1 y4 j# `$ L3 n mouse.release(Button.left)( @2 K5 u3 T6 U& m8 u1 `
if cyberpi.controller.is_press("a"):
' q. h& n+ D- W- n& f) D4 g ~ mouse.press(Button.right)) ~( v6 Y& X& I+ t# B
mouse.release(Button.right)& k5 i" X3 ^+ t+ T$ v
time.sleep(0.01)
7 a, Y2 Z/ h1 g2 _$ Y- h' b) M' b6 E" J) ^9 U [
9 I( t U3 m( y) s
語音打字器 # P6 y1 V& t/ P9 ]
功能介紹:
, K. v4 b/ T4 L. e利用童芯派的語音辨識功能結合pynput模組的鍵盤控制功能。3 q# B d( ?: n3 o: h! d$ x
將語音辨識的結果通過pynput在文檔中打出來。
- A- |+ n5 _+ g( T# D" o: J該程式目前只能支援英文。& ^- S+ z$ a* T+ D# V0 [
4 V$ F+ w0 j3 S5 a8 g- eimport cyberpi; j; k0 `7 J- p% X$ i$ ]. [( T& x
from pynput.keyboard import Key,Controller
+ J' _4 @0 s2 _7 \import time
; K3 b+ U, L) F/ ^0 q# Y! \ " u% H1 T) Y# o
keyboard = Controller()
& i* c# d5 X* G/ w2 Lcyberpi.console.clear()
5 a# W0 }9 K, e8 f4 p5 ecyberpi.led.on(0, 0, 0), a) W8 c/ l8 T0 J
cyberpi.set_recognition_url()" Z( U1 C1 f6 @) j8 K
cyberpi.cloud.setkey("請輸入雲服務授權碼") # 通過慧程式設計帳號可以獲得
6 u/ _- v# J: ^8 bcyberpi.wifi.connect("WIFI名稱", "WIFI密碼")5 t7 @9 w1 K7 U
while not cyberpi.wifi.is_connect():
) ^7 m# V0 ~4 }2 v& } pass; t) b: i5 n! d- a1 e, a5 F
cyberpi.led.on(0,0,255)
& V* q" f' P, x. X, W1 ~: K. `cyberpi.console.println("WIFI已連接")
+ x& v8 {* |# a! Mcyberpi.console.println("--------------")
1 S4 E) T+ ^6 P, L9 F. w7 j- D+ \cyberpi.console.println("按下A鍵開始語音辨識")
1 S' }' y# P: O' X5 N/ J( Jwhile True:
0 j6 V0 o* Z$ L" ?! m if cyberpi.controller.is_press('a'):" n& p* S6 S6 e7 c0 f a5 d
keyboard.press(Key.space)+ W/ x2 R$ [! \6 O$ o# A
cyberpi.console.clear()8 t: n) z6 d5 ?" Z& c- f
cyberpi.led.on(100, 0, 0)8 u# K) U- O; n/ f7 \7 f
cyberpi.console.println("開始語音辨識")
% w/ I- p! u4 Z/ u3 n1 G, b! k cyberpi.audio.play("switch")
# D. y- I% }- C# A cyberpi.console.println("--------------")" [8 N- s. @- c
cyberpi.cloud.listen("english", 2) N+ i( t- w3 R' V7 v% {/ f
cyberpi.led.on(0, 0, 0). i$ }/ B, E: d* J
say = cyberpi.cloud.listen_result()9 E3 l. P9 z2 O0 t; d
cyberpi.console.println(say)
* F# j$ _, U' @ cyberpi.console.println("--------------")
8 f. w" Y8 w' U& I for i in say:
( w- ^, \* l* d: ~8 _ if i == '':) W5 C+ |* t4 {& S; R4 L' z' r
keyboard.press(' ')
6 d) L& J4 y# c1 \5 O+ Z% c- ?/ a else:0 |! Z9 \0 ~2 [8 o# b a
keyboard.press(str(i))
5 n# C2 l5 f4 R- {0 k8 ]/ M/ r7 x time.sleep(0.03)
2 q5 U, m! v# g; y; a
5 i% S# B+ Z$ R0 r! tmatplotlib音量柱狀圖 / Y+ j/ |) Z* a9 l
import cyberpi
' O1 a8 ~ n: @( u2 Vimport time- o: ?" ^; I! o |
from matplotlib import pyplot as plt
8 A# j1 m- L& u" @& d7 j& S- i$ F # i: Z9 X2 w& Q/ W/ R0 U1 ]0 e; W
plt.rcParams['font.sans-serif'] =['Microsoft YaHei']! N2 ]5 } c k4 x0 Z9 k8 C7 @( N$ C
plt.ion()
# O3 @+ F3 {3 H5 L9 Z1 x& ~6 O9 W! X$ E
. o, U3 k9 y: p8 nwhile True:8 d* A" H8 x3 E# s
loud = cyberpi.get_loudness()$ U* ~) |, |. w! y7 C E6 W& Z) w# _
plt.clf()
" Y6 | b. t& A plt.xlabel("數據名稱")
* }9 n9 a; @* f- o, g9 m W* C plt.ylabel("音量讀值")" n; ]" s6 o+ C
y_locator = plt.MultipleLocator(5)
3 O8 k; A* @$ B! T! A# c( ~" u ax=plt.gca()- N! {3 T# i' P& K+ ?6 ~/ W
ax.yaxis.set_major_locator(y_locator)
; k) g! r% z1 L0 H+ Z2 ] plt.ylim(0,100)
( H2 ^: S# K3 u* \5 M F& O. i# _0 i plt.bar('音量', loud, align='center',label=f'溫度{loud}')( A# ~- H" K0 O2 y5 w
plt.title('童芯派聲音感測器讀值')2 A- v2 ?! [+ M" R1 d+ W
plt.legend()
; q$ `/ C; |- w plt.show()( S% d8 m' T% ~6 k
plt.pause(0.01)
: m9 Z% x6 f2 L/ O1 C7 `6 B
( D+ `7 y4 ]# w+ x. d" W6 T7 N* h' n" C; \+ X) q2 t/ u6 N
matplotlib 多圖表顯示
7 a7 ?( x, C4 `5 c0 ?% L7 b0 z3 L2 A+ ?/ A s' w3 | n# O/ o* c
from matplotlib import pyplot as plt
+ a/ m; G1 K) m, ~import time
+ M; Z. W( o4 u" T% `import cyberpi6 f3 g8 q3 M& T7 C# e3 C9 {% J
: A# r0 m3 l4 V/ \& q T7 U* iloud_list = []% {7 `% ^3 F- V+ z I" k
bri_list = []
+ B1 m4 R d7 i( Q" z1 Z9 i0 ~9 Vplt.rcParams['font.sans-serif'] =['Microsoft YaHei'] # 載入中文字體,這裡載入的是電腦的自帶的微軟雅黑字體5 O! ?8 u: u9 A q. y8 N
plt.ion()
/ J' j0 F# p- I2 {plt.figure(figsize=(10,10)); A9 g* @4 n/ V
plt.figure(1)+ R# y" f! l. e+ r8 ^
x_locator = plt.MultipleLocator(5)
! s+ l' A6 S) P u/ Q/ b9 zy_locator = plt.MultipleLocator(5); ^; V8 c7 J# }
while True:
; R0 l: f" Z0 o& Y loud = cyberpi.get_loudness()2 D( E* R3 X5 t
bri = cyberpi.get_bri()# t p4 _( c* l( ^1 H4 {. k
loud_list.append(loud)
! J' G5 ]% @, f# x [0 F bri_list.append(bri)4 o( E+ ^/ W) Z8 o w' a
battery = cyberpi.get_battery()- @8 Z) D; f1 M& t8 h8 w; r1 P
size = [battery, 100-battery]
* S4 _ F2 S4 p status = [f'剩餘電量:{battery}%', f'已用電量:{100-battery}%']
! I& k/ H0 u: a8 c$ i2 ?% f, y- l ax1 = plt.subplot(221)
- q5 O# R0 V* G* R1 J plt.title('光線折線圖')
3 m: R0 V7 O6 h( |% t7 G: ~ ax1.plot(bri_list)7 k& I; t: c T. V
ax2 = plt.subplot(222)9 N5 f# K- Q) e# }5 x# r2 s- b3 q
plt.title('聲音柱狀圖')
" A$ v2 M3 w6 Q3 R% X" h; Y: h ax2.xaxis.set_major_locator(x_locator)5 ?" ?( c4 t/ I; u6 k
ax2.yaxis.set_major_locator(y_locator)
, A0 B3 d {# n! ^6 _* n plt.ylim(0,100)- ^9 _4 Z8 v1 D% ?7 B
ax2.bar('sound', loud)
- k2 Z( J3 s+ N# ^% T2 K ax3 = plt.subplot(223)% y8 k! |6 T- d7 K
ax3.xaxis.set_major_locator(x_locator): s" p6 v: f$ E; `
ax3.yaxis.set_major_locator(y_locator)
; u: L0 p+ J& I }/ H5 \ plt.xlim(0,100)1 Q# M/ X) U6 P5 y9 ^" L' D7 C, Q- z
plt.ylim(0,100)
_- s4 G# X1 o plt.title('聲音、音量散點圖')
, o+ w* {* ]7 }0 J. P! k ax3.scatter(loud_list,bri_list). A, h3 z$ b) E1 ^, E3 V" F& T
ax4 = plt.subplot(224)0 V' q9 R0 p9 z) |( _1 g
ax4.pie(size, labels = status, radius=1,wedgeprops = {'width': 0.3,'edgecolor': 'w'})
& t- Q3 n' M5 t0 b4 o plt.title('童芯派電量')$ i/ s% ^" ~/ ?. m t
plt.pause(0.2)
3 a2 G9 J5 i1 C( E# ^ R plt.clf()* s/ R7 ~4 J% J4 C7 n$ b9 h* Z& ]# e
if cyberpi.controller.is_press('a'):, a. K# s P6 _& X4 o K9 Z9 t
break
' f. T8 _1 {" F1 T5 F1 i if len(bri_list) > 500:
3 z3 X; c2 I4 ?+ D. C# G9 d bri_list = []$ F* V6 c. D& p, C" b$ ? k) {$ W
loud_list = []
: p7 s6 Y/ M, ]4 z2 d7 `2 M
- j8 q8 d% [5 @$ j |