本教學譯自Makeblock官方教學文件,原文點[ 我] 本教學文件以windows作業系統環境為範例,Mac使用者可以參考結合實際情況進行操作。 快速教程:如果您已安裝了Python環境和第三方編輯器,請跳至第三步。或在您的編輯器配置的Python環境當中安裝pyserial和cyberpi程式庫即可實現童芯派第三方編輯器的程式設計。 pip install pyserial pip install cyberpi
# C! s q _* b1.安裝Python環境- 進入Python官方網站下載Python環境包,請根據自己的作業系統進行選擇。建議下載3.6-3.8的版本進行使用。如果下載最新的Python版本,可能帶來無法正常使用的問題。比如Python3.9以上的版本目前是不支援Windows 7作業系統的。% J4 R% E6 L* H$ a
Python Windows環境不同版本下載頁面:Windows Python下載 Python Mac OS 環境不同版本下載頁面: Mac OS Python下載 0 V. H2 P4 z+ I4 g0 X# L. r8 V
- 按兩下安裝包進行安裝,注意勾選下方的Add Python 3.6 to PATH,點擊Install Now進行安裝,也可以點擊Customize installation將Python環境安裝至指定目錄下。
- k. \: y5 U7 O% `* s( }0 r 2.下載安裝並配置協力廠商Python編輯器(以Visual Studio Code為例)提示:不同的編輯器在操作步驟上有所差異。同時建議在編輯器當中配置安裝在系統中的Python環境。 - 下載 VSCODE,請根據自己的作業系統進行選擇。 VSCODE下載地址
- 點擊下載下來的安裝包進行安裝。(根據提示進行安裝即可)
8 D( p' y9 \7 a
- 點擊桌面上Visual Studio Code的圖示運行程式。
- 在軟體介面左側找到下圖紅色方框當中的圖示並點擊。4 ]) g+ V, i; s- Q7 @) J' |
9 g7 T+ q0 b/ O% @7 W
- 在彈出的介面的搜索框當中,搜索Chinese (Traditional) Language Pack for Visual Studio Code,並點擊進行安裝。重啟後可以實現Visual Studio Code介面切換為中文。注意該擴展外掛程式發佈者為Microsoft。(如您習慣在英文介面進行程式設計,可以跳過此步驟)
/ b5 ~' K, y( N, }" j
/ Q% v H' I0 y$ Y- 繼續搜索Python,安裝Visual Studio Code中的Python編輯環境。注意發佈者為Microsoft。
& E( d( g* Q; M4 J. n3 ?
$ B3 {9 n* q2 N- H- x: L- 在Visual Studio Code編輯器當中配置前面安裝的Python編輯環境。
- 新建資料夾並新建py尾碼的檔就可以對基礎的Python程式編寫。' S7 ^) Y* H: m! x+ v9 D
+ e; t; v. E5 Y4 Q6 i
3.童芯派第三方編輯器程式設計準備! {- `/ z$ i; c8 h6 D# v
- windows環境下,按下鍵盤上的win+r鍵,輸入cmd並按下Enter,然後會彈出cmd視窗。; m4 c4 O" J7 |; S' n- |6 \
, `$ x7 ]( m' d1 l- 安裝pyserial程式庫,pyserial程式庫可以確保童芯派通過USB線或藍牙Dongle與第三方編輯器建立連接。
) k% m* d( K- B2 [
在cmd視窗中輸入pip install pyserial 並按下Enter,等待安裝完畢。
4 x: e2 R; _: {/ w- 安裝cyberpi程式庫,cyberpi程式庫是用於對童芯派進行線上程式設計的Python程式庫。* _+ U' _$ O R( j/ A
在cmd視窗中輸入pip install cyberpi 並按下Enter,等待程式庫安裝完畢即可。
+ C7 R3 v' A- b/ B" W0 V* j2 P1 v& T1 g1 Z
- 完成安裝後,您就可以在配置了Python環境的編輯器當中進行程式編寫了。
( m0 U6 [" ~% t! M
9 m6 ?" o2 l$ s4 x1 @/ S" o- [7 n4.注意事項 在第三方編輯器當中目前僅支持對童芯派進行線上模式程式設計。 & i5 [( s, m; ?+ J3 e0 X; J
5.一些程式設計案例! }/ |+ h" J( t: `* g" y- r
童芯派滑鼠 需要安裝pynput模組,參照上面的步驟pip install pynput / E" W8 l8 H/ u2 c0 O4 i* ^
功能介紹:
7 M8 v' h- A: {) e7 K B, n利用童芯派的陀螺儀模組和按鍵結合pynput模組的滑鼠控制功能,實現滑鼠的功能。
) r0 y4 |" F" r- Q [" c通過陀螺儀控制滑鼠移動。通過按鍵實現滑鼠按鍵功能。
4 Q- Q8 ^# ?& O7 ? ^結合藍牙模組可以實現無線滑鼠的功能。
+ X H/ }* Y L. m- U# y, ]; E& @/ P! U. K1 f2 m4 Y
from pynput.mouse import Button,Controller
4 c" X* w) C( d% C$ A/ `& Y; S/ Oimport cyberpi
$ u& E! |7 e1 l) _" u* H) Ximport time; ` V+ p3 Z7 g8 T3 G/ i* @
8 E( j& A3 ^* g/ |mouse = Controller() d/ Y" t8 c- l6 {, m7 Y% p
& L! W! K6 t5 ^- f! ?% Mwhile True:4 ~9 @3 E7 k9 Y+ J: x
if cyberpi.is_tiltback():
0 t: m3 L. u3 t6 A* P w( L mouse.move(-3, 0)' H' Z% A1 Y/ K' Z4 a
print(mouse.position)
1 _4 t" z7 V" z( y! v" `% D if cyberpi.is_tiltforward():4 l, p9 }1 I1 q5 g1 X" ^
mouse.move(3, 0)
/ |) F* I: R7 @% Q if cyberpi.is_tiltleft():2 H9 d, y2 X: g/ T
mouse.move(0, -3)5 B) V+ W- U- k6 Y' D: e: L- R
if cyberpi.is_tiltright():
7 R# y6 X2 J3 R mouse.move(0, 3)
' U" O4 K" [3 i# g, @8 W, R if cyberpi.controller.is_press("b"):
, `; ?! ^8 R9 E8 r9 \ mouse.press(Button.left)
; S! p% M" L) Z1 Z5 y9 B mouse.release(Button.left)
9 k+ _5 S# F5 Q" X mouse.press(Button.left)2 e% C) }9 E& p* j0 y R: c6 u9 p
mouse.release(Button.left)9 D9 g3 j! `) s+ v$ H) W2 ?2 d5 g- y+ x
if cyberpi.controller.is_press("a"):' c' @: {5 y' Q: |2 l3 [# s
mouse.press(Button.right)2 D- `5 w( {6 k& |
mouse.release(Button.right)2 Q1 t! z+ T) X8 M$ S6 O! b
time.sleep(0.01)
5 C3 v5 g; v) z9 L1 J, m
& m4 Z) k, [& ?( \& Q* p3 |% ~# C0 G3 g& Q6 C2 O0 m; e- {1 j
語音打字器 , Y* B9 k2 ^, f' T7 a( o. V& Z9 @4 _. d
功能介紹:
5 Z% Q9 q) h3 S4 ]( d0 k* O; u( w( J' |利用童芯派的語音辨識功能結合pynput模組的鍵盤控制功能。" b$ u2 h& ^/ |# X! K% A7 N2 F* }
將語音辨識的結果通過pynput在文檔中打出來。
; G9 G: x; w/ T& K4 T$ p1 O4 x該程式目前只能支援英文。! ^- A( J4 }& ]: D$ |
- Z7 Y* t% Z: _# Z1 w1 Limport cyberpi6 s' Q' ?: q* l* [0 v. c1 j
from pynput.keyboard import Key,Controller
3 i6 f/ S. C/ I6 i- K4 Y1 t$ s, _import time
# z4 c6 r8 a# U$ G
' n1 `# }% h' L, `6 j" u' Wkeyboard = Controller()
; J8 D. d' w: ^( ~+ c# u% v6 `cyberpi.console.clear()+ d8 w, Y& O; y; l$ r5 _7 A A' U
cyberpi.led.on(0, 0, 0)
+ z, w, y' p5 A' E& v, h2 Xcyberpi.set_recognition_url()
/ G$ a6 X$ Q" \- ^8 \5 Ecyberpi.cloud.setkey("請輸入雲服務授權碼") # 通過慧程式設計帳號可以獲得' G2 s; R& S* Z/ [* i# T: c0 S
cyberpi.wifi.connect("WIFI名稱", "WIFI密碼")
. O7 R* M- e% f3 Twhile not cyberpi.wifi.is_connect():- Y5 b7 h" h7 q7 h/ ]/ x1 v
pass
) m& L" }2 j( v: Gcyberpi.led.on(0,0,255)7 @. @. T. a* I( Q; @. @
cyberpi.console.println("WIFI已連接")
8 Z/ `3 U: c* Bcyberpi.console.println("--------------")9 I8 S; k c0 _3 @/ r w# C( R u/ C# i
cyberpi.console.println("按下A鍵開始語音辨識")
2 C7 {. L. A. T% a% v1 N! M8 }7 twhile True:# A# r, e- \7 p4 w
if cyberpi.controller.is_press('a'):9 c/ n* f7 ?0 e/ u5 o
keyboard.press(Key.space)6 D) L9 s) Z) H# A6 f( ?
cyberpi.console.clear()- c$ q/ T1 }; I: M
cyberpi.led.on(100, 0, 0)
; P3 G- J7 h) l% X$ g. I cyberpi.console.println("開始語音辨識")
3 v% {' m$ I- k+ i7 f cyberpi.audio.play("switch")! ~) [9 w- A- s* ~( t/ v$ q
cyberpi.console.println("--------------")
8 u* C( N/ o6 |. q cyberpi.cloud.listen("english", 2)# j0 D: g; X: i7 _% z) `
cyberpi.led.on(0, 0, 0)
; l1 O3 a. q4 ] ~' ~ say = cyberpi.cloud.listen_result()2 D0 r1 F% D/ p, H% M" Q& [
cyberpi.console.println(say)
, j" P9 t& D! P1 L2 _' ^ cyberpi.console.println("--------------")
& x7 r* W4 I5 Y8 C* I for i in say:
) z1 l# _# a3 P* W! ] if i == '':
3 p" _/ m; O: |+ ], v d keyboard.press(' ') F$ o- X8 g# |& v. R" P- b$ u
else:) @4 I" }" m" }; h+ G: ~, p( ?
keyboard.press(str(i))
7 V$ |) `. x6 q time.sleep(0.03)+ O# i5 \1 k' d8 k0 ]1 d' q
: m& c! K; P1 u* q' ^: s1 `' Cmatplotlib音量柱狀圖
/ y0 z0 ~1 V/ D( I* I0 `3 n: b, `import cyberpi t* W1 S9 s5 _: e- h% l* J4 F
import time
" ?1 [6 a& r& B/ Q* M& v3 Jfrom matplotlib import pyplot as plt; o- z- o! Z+ s2 p) k7 }& h
* h6 A8 |+ }( d8 Lplt.rcParams['font.sans-serif'] =['Microsoft YaHei']2 ~7 p. f# ]/ n8 y: D8 F! Q
plt.ion()" F% v! s7 q7 f$ `
* D- J5 l: y; M. zwhile True:' P! }0 i2 t+ G2 W) {4 I3 K
loud = cyberpi.get_loudness()
9 w5 n# H! x& ^/ A plt.clf()
- H ]0 `/ h; g4 U4 B# t! l+ v7 H plt.xlabel("數據名稱")3 y) W# A F# V( J& q+ ~; `% ]$ b
plt.ylabel("音量讀值")
$ g# x- S/ B# h- m9 x' `6 \+ g y_locator = plt.MultipleLocator(5)% h( Y2 i( ~* Z" x( y
ax=plt.gca()
9 A9 E% z0 Z' _5 S ax.yaxis.set_major_locator(y_locator)* W4 C S _! T
plt.ylim(0,100)
! B7 v- X' p' H8 |# o! T plt.bar('音量', loud, align='center',label=f'溫度{loud}')
* @' |) A5 U6 f) f% F plt.title('童芯派聲音感測器讀值')
( j! J/ D; F* I9 E plt.legend()
& C( q3 [9 q/ \( b+ [ ` Y3 ? plt.show(): s2 o# j/ d3 z( }! B0 c
plt.pause(0.01)
3 i1 Q& ^4 R+ _
% k6 J8 y1 K5 ~5 [) i" C- ]1 x3 A+ `3 A9 s! e+ K3 M
matplotlib 多圖表顯示
2 {6 C5 q. r! W' g O6 `" T0 o8 G8 E3 Q$ p( y& Z/ m# l/ y
from matplotlib import pyplot as plt/ H1 X: }3 c* h) U+ C4 V: n8 B8 ]
import time
3 d1 z! L$ k4 [: O% q" ~3 kimport cyberpi
U1 o- P8 v% F2 f0 M8 B Y . J+ Q4 {9 {% I$ I7 V" u
loud_list = []
5 `1 V8 [# V! S4 Ybri_list = []+ x& q% y) @# j
plt.rcParams['font.sans-serif'] =['Microsoft YaHei'] # 載入中文字體,這裡載入的是電腦的自帶的微軟雅黑字體
' `+ t. c0 d" _. h/ iplt.ion()& o& R* {0 a5 D5 m
plt.figure(figsize=(10,10))
) `2 e* ? p* N8 P9 t# g) V3 Wplt.figure(1); Y) b6 @4 l K9 l6 t
x_locator = plt.MultipleLocator(5)
2 k/ C' J V/ zy_locator = plt.MultipleLocator(5)
6 x, y3 ?* [4 x8 Y0 Mwhile True:
+ I' ~. J* Z' i9 Y2 q) p" d loud = cyberpi.get_loudness()
; I$ b" Y7 f1 g6 M$ P& r% R& H% _ bri = cyberpi.get_bri()
# O0 f, I3 A$ Q0 Q2 P% d8 V$ r loud_list.append(loud)9 G6 O7 `4 p* U* ~- E
bri_list.append(bri)" a/ A! L6 P3 |! E8 M% ?9 \
battery = cyberpi.get_battery()
' T) H3 T- K" ^. e size = [battery, 100-battery]* a3 Q- g3 E/ W6 [0 E* m3 v3 z# ~
status = [f'剩餘電量:{battery}%', f'已用電量:{100-battery}%']
- X4 \# H% X* N5 U: F# {0 ^ ax1 = plt.subplot(221)
0 U, G# [& G; P6 \- {, J8 R plt.title('光線折線圖')8 A" Y7 @5 }" O, v) u( k1 ~* x9 y
ax1.plot(bri_list), ], j' l1 {4 }
ax2 = plt.subplot(222)
6 _! Q6 D4 ?: r. W% T plt.title('聲音柱狀圖')* D7 p# a* b; K
ax2.xaxis.set_major_locator(x_locator)- B' |+ i. P' T% ~; a( s
ax2.yaxis.set_major_locator(y_locator)
7 M1 Z; ?% S) x plt.ylim(0,100)
. t) U+ N1 W8 ?4 x ax2.bar('sound', loud)4 V1 W* v& X0 {7 C- d4 u* X" T
ax3 = plt.subplot(223)# R7 b$ Z4 ~5 W# j
ax3.xaxis.set_major_locator(x_locator)5 w3 Q' B. \* o; G
ax3.yaxis.set_major_locator(y_locator)
8 q) L- Q2 D. H4 V9 Z7 Y plt.xlim(0,100) a% k% c8 j( `, O" u
plt.ylim(0,100)& L. V4 l( X( R
plt.title('聲音、音量散點圖')# y& ^6 X6 z8 w& Q. b( C3 }, f& b8 i
ax3.scatter(loud_list,bri_list)
" }) U5 c6 m' ?0 R' J8 S7 Y ax4 = plt.subplot(224)
+ L! R6 b- K2 u2 \- y8 Y. Q0 q9 V ax4.pie(size, labels = status, radius=1,wedgeprops = {'width': 0.3,'edgecolor': 'w'})% x4 @5 z: q5 p3 }0 X
plt.title('童芯派電量')
/ o" ~1 T5 L% d; ~ plt.pause(0.2)- A' F2 V' ?$ o; i! V# l0 G% }
plt.clf()0 Z0 q) s) j0 H% o
if cyberpi.controller.is_press('a'):, G' l" [+ t4 k. S; S" ^: U
break
/ L5 }9 T- O& C+ J7 Y, T( l if len(bri_list) > 500:, s; K8 l" I* X: _& r
bri_list = []8 t9 T, J0 r1 r0 a, q. x
loud_list = []
2 s% e) I& c2 i& Q$ @2 N J" V- A. r# T
|