本教學譯自Makeblock官方教學文件,原文點[ 我] 本教學文件以windows作業系統環境為範例,Mac使用者可以參考結合實際情況進行操作。 快速教程:如果您已安裝了Python環境和第三方編輯器,請跳至第三步。或在您的編輯器配置的Python環境當中安裝pyserial和cyberpi程式庫即可實現童芯派第三方編輯器的程式設計。 pip install pyserial pip install cyberpi & g1 [! Y) m& b3 v' B8 C
1.安裝Python環境- 進入Python官方網站下載Python環境包,請根據自己的作業系統進行選擇。建議下載3.6-3.8的版本進行使用。如果下載最新的Python版本,可能帶來無法正常使用的問題。比如Python3.9以上的版本目前是不支援Windows 7作業系統的。
6 ?2 v% I" J& q# X0 W: E
Python Windows環境不同版本下載頁面:Windows Python下載 Python Mac OS 環境不同版本下載頁面: Mac OS Python下載 . _1 W- H8 ^: ~8 C( M' _$ V
- 按兩下安裝包進行安裝,注意勾選下方的Add Python 3.6 to PATH,點擊Install Now進行安裝,也可以點擊Customize installation將Python環境安裝至指定目錄下。
1 S) X l/ ?4 z- p4 o" N" b 2.下載安裝並配置協力廠商Python編輯器(以Visual Studio Code為例)提示:不同的編輯器在操作步驟上有所差異。同時建議在編輯器當中配置安裝在系統中的Python環境。 - 下載 VSCODE,請根據自己的作業系統進行選擇。 VSCODE下載地址
- 點擊下載下來的安裝包進行安裝。(根據提示進行安裝即可)1 P6 s. z# y- d2 l
- 點擊桌面上Visual Studio Code的圖示運行程式。
- 在軟體介面左側找到下圖紅色方框當中的圖示並點擊。
9 w: L3 E7 i9 z; s4 I4 z' ]+ Y" L" w
, u' t8 i: l2 k" z" w1 y- 在彈出的介面的搜索框當中,搜索Chinese (Traditional) Language Pack for Visual Studio Code,並點擊進行安裝。重啟後可以實現Visual Studio Code介面切換為中文。注意該擴展外掛程式發佈者為Microsoft。(如您習慣在英文介面進行程式設計,可以跳過此步驟)
$ ~; H. C( C8 K9 x' w5 F. w # O& @; u& H6 ~% C' D- I8 C9 ~ z+ h/ g
- 繼續搜索Python,安裝Visual Studio Code中的Python編輯環境。注意發佈者為Microsoft。3 a% [: S' k! s4 c; V& G/ `. [
- W5 t' W" R1 z: @: t( a2 F
- 在Visual Studio Code編輯器當中配置前面安裝的Python編輯環境。
- 新建資料夾並新建py尾碼的檔就可以對基礎的Python程式編寫。' D( H* p4 |! n1 V
0 s! S$ V; `) S# T6 G. d3.童芯派第三方編輯器程式設計準備( ~: w/ q% Q! w7 H
- windows環境下,按下鍵盤上的win+r鍵,輸入cmd並按下Enter,然後會彈出cmd視窗。4 d6 T7 N4 q. x* E+ C1 ?- D6 p
( g0 c" P- m) ?
- 安裝pyserial程式庫,pyserial程式庫可以確保童芯派通過USB線或藍牙Dongle與第三方編輯器建立連接。5 S% g" v+ Z7 ?( u/ Y2 c/ v
在cmd視窗中輸入pip install pyserial 並按下Enter,等待安裝完畢。 k; F+ G( h- M' g' ?& r2 j) H
- 安裝cyberpi程式庫,cyberpi程式庫是用於對童芯派進行線上程式設計的Python程式庫。" n% q- I/ X6 k- `' E8 ~
在cmd視窗中輸入pip install cyberpi 並按下Enter,等待程式庫安裝完畢即可。 $ \# M4 d7 h7 T2 [% R% I
4 X$ [5 x: s& F1 y. R' U& w- 完成安裝後,您就可以在配置了Python環境的編輯器當中進行程式編寫了。; F6 A# A, G) [
4 O8 D5 g5 t2 T; @7 {- {% X6 ]3 {
4.注意事項 在第三方編輯器當中目前僅支持對童芯派進行線上模式程式設計。 4 j5 q- R) L$ O* _1 H% M, j
5.一些程式設計案例+ k0 _8 m: q! i! C
童芯派滑鼠 需要安裝pynput模組,參照上面的步驟pip install pynput 3 b7 a4 I% K( W ?. J6 C
功能介紹:
( D* N# p* } [/ F利用童芯派的陀螺儀模組和按鍵結合pynput模組的滑鼠控制功能,實現滑鼠的功能。
0 u$ }/ a$ i; L! G8 T8 X通過陀螺儀控制滑鼠移動。通過按鍵實現滑鼠按鍵功能。$ o0 K9 U) ?7 {: q: q
結合藍牙模組可以實現無線滑鼠的功能。
8 u& N% ?; j: T: U5 N7 f; q) C i/ D$ H7 g- `+ [! \
from pynput.mouse import Button,Controller
) Y; u) j. [! O% Y/ J; {% m4 G1 Nimport cyberpi! J8 q0 E* ^4 `. ?- ~' u M! T
import time. w. ^7 A3 x' S: ?3 E% K7 D" X* J
5 `' t& S# m, g3 vmouse = Controller(); E+ F8 B+ L2 i& `
/ g$ M) x. d: o" ?9 mwhile True:4 x. Z7 n/ v: R3 O8 c
if cyberpi.is_tiltback():
3 G' ^9 j7 L% W1 u1 Q. P6 }' T mouse.move(-3, 0)& I0 |2 Z: I6 c+ B9 ~! w
print(mouse.position)! ]+ P9 q: E7 h" H* U( q
if cyberpi.is_tiltforward():6 {! M. Y' s6 |" H, z, _
mouse.move(3, 0)" a, y. ?) a, A( ~- [
if cyberpi.is_tiltleft():
t2 ?. |- Q3 S- p, A8 ] mouse.move(0, -3)
2 L; R% k7 O9 |7 u if cyberpi.is_tiltright():
m2 S& k @# D: X0 U, p9 }4 v mouse.move(0, 3): b( Y8 x. w4 u
if cyberpi.controller.is_press("b"):" H- L0 u* w5 C: g8 _: m4 _
mouse.press(Button.left)
- K* c3 d& P1 L& ]% S; ]. Y mouse.release(Button.left)
+ c6 @ c& T" V* @: P mouse.press(Button.left)
( m' D: k* p9 A B: G: N mouse.release(Button.left)
! {# r8 [' o) c' D if cyberpi.controller.is_press("a"):
! O' J4 ~" |% S2 g7 E$ N' G mouse.press(Button.right)" X" u3 n6 G# x
mouse.release(Button.right), N1 A7 k& v( g/ R$ s" P3 O
time.sleep(0.01)
' S5 y6 p. n3 }$ _ n& l/ |1 l! v i" A9 s; V7 }; a) Z& s
! U4 Q7 J; ^8 z0 `. E# S# M語音打字器
, A- ~5 o/ W" Q9 X9 q8 E功能介紹:
0 _5 E3 s0 ~; a8 L利用童芯派的語音辨識功能結合pynput模組的鍵盤控制功能。
0 M* A' y; F% e* K將語音辨識的結果通過pynput在文檔中打出來。
o% @/ X1 ?& f3 G4 p# j( Y: }該程式目前只能支援英文。
/ D& {# h1 W1 T# @+ U& ~& E0 `" _' K* i0 h" K9 O
import cyberpi* Y7 n% S2 }; M- g. D
from pynput.keyboard import Key,Controller+ e9 N4 ~( K& b G+ ?0 c
import time( c9 ^& D9 a/ A2 T' H
5 @7 Y( M- z6 d3 I# a4 ]keyboard = Controller()
/ i" j4 |7 o# m- T7 Ocyberpi.console.clear()$ o- R- g# x4 g
cyberpi.led.on(0, 0, 0). W4 ~7 R6 l2 E
cyberpi.set_recognition_url()
- B- j0 N+ q- w$ Y/ Vcyberpi.cloud.setkey("請輸入雲服務授權碼") # 通過慧程式設計帳號可以獲得
/ `0 z$ Z- n# P. z5 Icyberpi.wifi.connect("WIFI名稱", "WIFI密碼") l! G% \6 g% g1 f. u% r, D8 |/ D
while not cyberpi.wifi.is_connect():5 K9 H6 T6 q: }4 ^5 a, B& v
pass4 K4 h3 e+ H x* f8 Z5 \/ [
cyberpi.led.on(0,0,255)% g, e- U2 T; i6 G' E
cyberpi.console.println("WIFI已連接")
0 {1 E6 D2 N5 Ccyberpi.console.println("--------------")' C6 w% ^: j5 v7 ]# W; U, T3 b
cyberpi.console.println("按下A鍵開始語音辨識")0 J& o* w' Z: V! I6 N, I
while True:
0 `- J4 e" W! a, R/ ] if cyberpi.controller.is_press('a'):$ u* e: ?+ }$ T5 _ B" V8 m" X G
keyboard.press(Key.space)9 }% v% S/ d6 {7 }+ M6 n
cyberpi.console.clear()8 e! u4 a, w) ~) j; X0 _
cyberpi.led.on(100, 0, 0)( y# b8 z9 X5 L' f# m' l& G4 G7 F
cyberpi.console.println("開始語音辨識")
3 b( [. L" A3 N1 ~3 m# u cyberpi.audio.play("switch")
7 `4 C+ c* A8 ?0 y2 n6 [ cyberpi.console.println("--------------")' ^! h' a; k6 M- @! B
cyberpi.cloud.listen("english", 2). f. J1 g( [% t
cyberpi.led.on(0, 0, 0)
' h$ I8 G% x! v say = cyberpi.cloud.listen_result()* c# W9 e- ^: G& K+ S6 A$ g
cyberpi.console.println(say)
0 V1 M' w) ^9 f cyberpi.console.println("--------------")
* y3 m0 S3 t/ z, |# w: z4 }8 R for i in say:3 @; o& x) W+ o& C
if i == '':3 k" U, n. p* s$ H
keyboard.press(' ')/ r* M$ \7 k b
else: b% z& b w+ A: _8 }! f: }
keyboard.press(str(i))
5 F+ |4 q2 K& |3 T" U time.sleep(0.03)
+ i& v: ]- G; }2 Q6 k8 B
7 Z( I6 n3 f: `5 ?$ W* mmatplotlib音量柱狀圖 ; o6 {& S' m* k. s$ v$ q" C) i- v L. ~
import cyberpi- r* ~7 ?! c% @2 R) q# {
import time; _/ q1 X' Z3 f+ j% s& p7 M, q- S
from matplotlib import pyplot as plt
; i X; Z3 h1 j$ ~3 y
* F3 K* F! k& l# p& B1 wplt.rcParams['font.sans-serif'] =['Microsoft YaHei']
) c9 m/ |9 e( S) V4 q. Dplt.ion()0 a7 D. x; @+ P% A9 o7 @9 O
" j; u* Z$ X- w9 P( v9 Nwhile True:
4 W! k2 ?& Y, q. r" j loud = cyberpi.get_loudness()% N' i, |# ^( X
plt.clf()
9 q2 L) h1 ?- b; F. F/ T plt.xlabel("數據名稱")
. S% u6 b [ T; p; [ plt.ylabel("音量讀值")
# V0 x8 }7 s& t7 Y2 T& P' S y_locator = plt.MultipleLocator(5)
" M8 R+ T k# e a ax=plt.gca()
1 H1 B% A/ n/ ?7 Y* @ ax.yaxis.set_major_locator(y_locator)
/ A6 R: T- V& B6 q+ Q1 ` plt.ylim(0,100)
1 h' X R" S* B; q- R plt.bar('音量', loud, align='center',label=f'溫度{loud}')% A# l) u7 Y2 N6 J K
plt.title('童芯派聲音感測器讀值')* j8 T! B, m, w- E' F+ I* O
plt.legend()
0 n+ O# z# b+ o0 o2 h0 U6 \ plt.show()- y0 y- _4 m+ n- g
plt.pause(0.01)! w+ l! N9 Z6 Y5 ]
/ t! F1 Z+ {. a, Z
! d6 Y% T: i: gmatplotlib 多圖表顯示% x0 |) d4 ~+ J0 ~
: d: c1 Y, t/ D, f# yfrom matplotlib import pyplot as plt) b! c, G! ]2 J N+ i5 ~; y
import time6 B0 Q8 B4 U7 G& @% ^
import cyberpi2 f% I6 e* Y# f: Z) v
* g, q* ]. ~' q, C- sloud_list = []
9 K. j: Z3 v: Ebri_list = []. l" R7 @% H7 h3 P$ W* }
plt.rcParams['font.sans-serif'] =['Microsoft YaHei'] # 載入中文字體,這裡載入的是電腦的自帶的微軟雅黑字體! Z" }6 e2 h& x4 z" Z) A9 J
plt.ion()
; P6 X( x0 A. H% Splt.figure(figsize=(10,10))
& B/ _* H) G) d! @" m/ X5 Hplt.figure(1) B h1 m7 F2 Q1 j6 `9 R
x_locator = plt.MultipleLocator(5)
! S' L' o/ _6 ?# k2 yy_locator = plt.MultipleLocator(5)
2 P, l* ~* x: {: Qwhile True:" q8 D' ?) V& E. U3 N
loud = cyberpi.get_loudness(); R. w A: Q& ~: Y: M
bri = cyberpi.get_bri()
) J7 |1 C: f, U ~5 w% s loud_list.append(loud)3 [. E! z+ k* G( ], M' N' L$ q: R! [3 T
bri_list.append(bri)
; @7 [4 v+ b8 ^: a1 |8 T; w( u' \" N battery = cyberpi.get_battery(): S- M R. G, v7 u
size = [battery, 100-battery]/ ]' `4 k' `8 c6 P4 l1 s. ?+ `
status = [f'剩餘電量:{battery}%', f'已用電量:{100-battery}%']: u6 T, W9 @; f- q s
ax1 = plt.subplot(221): [* }' i' E; E$ U7 f; B; L# L/ B
plt.title('光線折線圖')% m6 U. \' s- s' G* v ^, W
ax1.plot(bri_list)- S$ }$ ?- t2 Y
ax2 = plt.subplot(222)) `& o' t, N$ F* ?4 P' V0 V3 N
plt.title('聲音柱狀圖')
3 R2 }+ O5 e: s1 I ax2.xaxis.set_major_locator(x_locator)& _- }+ L* {4 ~: o8 @1 R$ `
ax2.yaxis.set_major_locator(y_locator); |7 X* o0 l. f# t- i7 x
plt.ylim(0,100)
# J1 w5 C7 p, q9 o& n4 U9 _: [: P ax2.bar('sound', loud)7 Z$ V0 f$ |2 E, o, f- r
ax3 = plt.subplot(223)+ C0 y! W* k9 p! Z2 r' M4 `9 W1 ~
ax3.xaxis.set_major_locator(x_locator)! \4 D& m0 n; ]; O% R+ K
ax3.yaxis.set_major_locator(y_locator)
( w1 h% l/ r9 b& |! c plt.xlim(0,100)
- S& Y z9 `5 I- H6 u plt.ylim(0,100)* O$ Z8 s% C, N" T1 C) X
plt.title('聲音、音量散點圖')
2 R/ F- A, P) N w8 } ax3.scatter(loud_list,bri_list)0 C0 a d/ I2 U5 C2 E; C
ax4 = plt.subplot(224)$ ]) ?1 v7 q. ~6 ~
ax4.pie(size, labels = status, radius=1,wedgeprops = {'width': 0.3,'edgecolor': 'w'})" Y4 \2 E) d' k L! i/ Y
plt.title('童芯派電量')
7 ]' ~$ x6 x% {7 J3 I- o4 f plt.pause(0.2)
6 u/ U2 C- y; J) C, E plt.clf()
7 L2 M0 M. U I. d- `; p8 W- O- T: \ b if cyberpi.controller.is_press('a'):& y$ o4 e/ [. J5 _
break
& R( l* x1 V7 J. G3 _% p$ s+ I0 z7 u if len(bri_list) > 500:
7 m |# D) V9 P% L$ Z: x bri_list = []
2 c7 d# B+ }9 O) L1 b4 k0 W0 X loud_list = []- L+ f9 \2 F* h/ {* \2 l) W
# N4 \# o7 V% Y. I+ B5 u |