本教學譯自Makeblock官方教學文件,原文點[ 我] 本教學文件以windows作業系統環境為範例,Mac使用者可以參考結合實際情況進行操作。 快速教程:如果您已安裝了Python環境和第三方編輯器,請跳至第三步。或在您的編輯器配置的Python環境當中安裝pyserial和cyberpi程式庫即可實現童芯派第三方編輯器的程式設計。 pip install pyserial pip install cyberpi
1 r: F( }, N0 |$ y- L1.安裝Python環境- 進入Python官方網站下載Python環境包,請根據自己的作業系統進行選擇。建議下載3.6-3.8的版本進行使用。如果下載最新的Python版本,可能帶來無法正常使用的問題。比如Python3.9以上的版本目前是不支援Windows 7作業系統的。$ `. M7 y4 Y+ b6 v3 X, N7 g9 g
Python Windows環境不同版本下載頁面:Windows Python下載 Python Mac OS 環境不同版本下載頁面: Mac OS Python下載
% X3 d7 S X& ]3 [$ j- 按兩下安裝包進行安裝,注意勾選下方的Add Python 3.6 to PATH,點擊Install Now進行安裝,也可以點擊Customize installation將Python環境安裝至指定目錄下。
0 L1 Q7 m" b: V8 A$ U8 m, f 2.下載安裝並配置協力廠商Python編輯器(以Visual Studio Code為例)提示:不同的編輯器在操作步驟上有所差異。同時建議在編輯器當中配置安裝在系統中的Python環境。 - 下載 VSCODE,請根據自己的作業系統進行選擇。 VSCODE下載地址
- 點擊下載下來的安裝包進行安裝。(根據提示進行安裝即可)
' `5 u; ?/ X+ }9 }( Q
- 點擊桌面上Visual Studio Code的圖示運行程式。
- 在軟體介面左側找到下圖紅色方框當中的圖示並點擊。' Z m" o0 U& H( H. ~. a
1 c5 `: J7 t+ X) N4 x, B
- 在彈出的介面的搜索框當中,搜索Chinese (Traditional) Language Pack for Visual Studio Code,並點擊進行安裝。重啟後可以實現Visual Studio Code介面切換為中文。注意該擴展外掛程式發佈者為Microsoft。(如您習慣在英文介面進行程式設計,可以跳過此步驟)
) T& K7 M2 @. J6 g' [. l
& |% x- h6 \6 d. o: u- 繼續搜索Python,安裝Visual Studio Code中的Python編輯環境。注意發佈者為Microsoft。
" }& H. f1 n, S% Y) w1 ~! m ( M. z N( V* R; }
- 在Visual Studio Code編輯器當中配置前面安裝的Python編輯環境。
- 新建資料夾並新建py尾碼的檔就可以對基礎的Python程式編寫。5 M7 Z4 D9 l: Q+ t) K6 u
# \, j6 D. E! b
3.童芯派第三方編輯器程式設計準備# p4 S, `3 r8 I( Q
- windows環境下,按下鍵盤上的win+r鍵,輸入cmd並按下Enter,然後會彈出cmd視窗。& u) N1 C+ I2 E; [3 n
A$ o. \- J" G8 V: y
- 安裝pyserial程式庫,pyserial程式庫可以確保童芯派通過USB線或藍牙Dongle與第三方編輯器建立連接。
0 g3 D5 A7 h5 `6 j3 T% ~, R
在cmd視窗中輸入pip install pyserial 並按下Enter,等待安裝完畢。 , R9 j; V2 K6 p) t+ u
- 安裝cyberpi程式庫,cyberpi程式庫是用於對童芯派進行線上程式設計的Python程式庫。! Z( o" u! T7 r/ Z! O. d' G
在cmd視窗中輸入pip install cyberpi 並按下Enter,等待程式庫安裝完畢即可。 6 C7 K9 i, i: {+ D! V7 Y- f
1 f* U. y7 c n- 完成安裝後,您就可以在配置了Python環境的編輯器當中進行程式編寫了。
9 F- I* f1 F$ ?! }5 r1 K 6 p4 u6 B) z7 Q. Q
4.注意事項 在第三方編輯器當中目前僅支持對童芯派進行線上模式程式設計。 $ D$ l: w, s, Y/ N/ ^
5.一些程式設計案例' h ?8 g$ @5 A
童芯派滑鼠 需要安裝pynput模組,參照上面的步驟pip install pynput * F8 c+ V& y: k
功能介紹:% j2 j/ V+ @. g: e
利用童芯派的陀螺儀模組和按鍵結合pynput模組的滑鼠控制功能,實現滑鼠的功能。
: Z' I3 j4 g6 _9 K! d+ V" f通過陀螺儀控制滑鼠移動。通過按鍵實現滑鼠按鍵功能。
, r, w a- O4 _( T* A. O% p5 l) T' ~結合藍牙模組可以實現無線滑鼠的功能。! g, x- u: Y; l. H
$ z; |9 u0 o: |0 c" l) _" t
from pynput.mouse import Button,Controller
9 _9 `4 e# a7 n0 u; q* J3 K. I. Ximport cyberpi; F+ C3 S4 |( D* m* a1 F# K/ {
import time
3 W$ Q! x1 G2 y @4 N8 p
& I: b- P0 i/ a! R$ hmouse = Controller()* Q1 l. h9 V3 J# I7 ~
( ~: e5 e" F- N E2 Z' kwhile True: I) c- B6 V1 y2 p
if cyberpi.is_tiltback():
- [7 Y3 y e0 ]2 B R" K mouse.move(-3, 0)
1 L9 U" p4 e8 ? print(mouse.position)
6 Q- A/ Q# V+ t9 a' d |% } if cyberpi.is_tiltforward():
) _+ `9 W0 ~6 z) |/ o7 { mouse.move(3, 0) |8 m3 Q+ K: Z2 ?% P2 A
if cyberpi.is_tiltleft():
/ j1 N# D. G4 q" ~ mouse.move(0, -3)( B. j0 N, f' i& j/ V' F
if cyberpi.is_tiltright():" b \ b% V9 {- @ `
mouse.move(0, 3). H% {$ P# K) w; p# E0 E8 F
if cyberpi.controller.is_press("b"):- }) n) g+ x) p3 }/ i
mouse.press(Button.left)
& e N+ H9 r( c mouse.release(Button.left)5 c. D1 H. P& W+ H) A
mouse.press(Button.left)
& }1 P# S3 o* c4 Z- p" G mouse.release(Button.left)' B- z8 g( O" B0 d
if cyberpi.controller.is_press("a"):+ A% Y3 D2 ~4 q) p0 K7 r7 L
mouse.press(Button.right)
0 i; r2 M5 M# ^! ^ mouse.release(Button.right)
1 }' ?- b2 I. q+ X( r- d time.sleep(0.01)
V3 k1 R: U& N; X, h5 V8 ~3 O. I! k1 c; n6 N' ?3 n" m
6 \/ w6 t! R: ?
語音打字器 ! S! z( k* F5 F+ J9 U2 R
功能介紹:
# d$ H7 X5 U! S0 \/ d! Q6 V利用童芯派的語音辨識功能結合pynput模組的鍵盤控制功能。
* L: x7 c0 W! V. \0 N將語音辨識的結果通過pynput在文檔中打出來。( C7 E+ H/ g' [, U' e/ g
該程式目前只能支援英文。1 U4 O3 K* v s# {8 H2 ~
0 ]7 q6 p8 ^! @6 b
import cyberpi* M. o! W/ v2 M* v: I
from pynput.keyboard import Key,Controller
T% t; N6 Q6 ]0 q8 timport time9 C& y8 g Q$ v# _) J1 ?- e
& E! c8 ^0 K' E/ Q
keyboard = Controller()* a9 R( D( `3 e2 ~
cyberpi.console.clear()
1 L: `1 E5 D. v/ mcyberpi.led.on(0, 0, 0)& y; k' S3 _% u; b
cyberpi.set_recognition_url()" q) V9 N" K' l& Q9 k7 c
cyberpi.cloud.setkey("請輸入雲服務授權碼") # 通過慧程式設計帳號可以獲得
: [/ |+ O$ l: Y4 @9 m. L: Tcyberpi.wifi.connect("WIFI名稱", "WIFI密碼")
4 ~( I. V9 h, V( Y* v3 @# q6 Pwhile not cyberpi.wifi.is_connect():
: j C5 N& E" J0 y pass/ h, h+ [7 @: ~/ r9 d9 V& R6 W- K* L
cyberpi.led.on(0,0,255)
- c$ k2 x- S& m' l: g7 M2 icyberpi.console.println("WIFI已連接")
8 A W# `! j! }& h7 R; ^( Lcyberpi.console.println("--------------")
6 I7 S+ L6 n1 G$ F8 \1 ^7 `cyberpi.console.println("按下A鍵開始語音辨識")
5 T# L+ U$ V9 U9 C lwhile True:+ r* p* `# D1 [
if cyberpi.controller.is_press('a'):7 N' F$ B3 c! P+ J! i* J
keyboard.press(Key.space)9 o. E c3 V8 L m, X& L6 z: G
cyberpi.console.clear()
3 {: }+ w! K( F' y {5 U, j$ w+ W- T cyberpi.led.on(100, 0, 0)
, C! d- {5 p4 }' u( A" } cyberpi.console.println("開始語音辨識")
1 x- D5 z1 R/ D x4 l cyberpi.audio.play("switch"). c: ^! r7 z& b3 u U m
cyberpi.console.println("--------------")7 _1 v3 q3 H' s9 m7 x. |
cyberpi.cloud.listen("english", 2)
3 {. c$ L' S5 h' r$ v) B cyberpi.led.on(0, 0, 0)
, s- L! F4 \ F) s( `( Z say = cyberpi.cloud.listen_result()
5 q$ g- Q- X r1 l4 ? cyberpi.console.println(say)1 C# K/ Q: M0 y/ e2 ?" K
cyberpi.console.println("--------------")
- M% G" o3 Y1 e0 ` for i in say:
; U) L) W2 }7 q if i == '':# O7 d6 `. L [! f) A& k" a
keyboard.press(' ')
* z- Z: ~ X% J. S else:" u) U2 S6 y3 X- D1 h
keyboard.press(str(i))7 [, O" h7 i* h- X
time.sleep(0.03)
( x2 W; z# r; [: }
3 p- N$ Y3 Z4 umatplotlib音量柱狀圖 : o) E+ H$ U g0 \
import cyberpi
# L, d, `8 S7 `import time
2 z+ G: s5 s9 i) dfrom matplotlib import pyplot as plt
. g: p( F: g* ~. o
6 Q$ b+ [9 S' Q' Cplt.rcParams['font.sans-serif'] =['Microsoft YaHei']
' Q* c4 C- m0 Dplt.ion()8 ^& W) v& b; `3 j% I6 y
' k3 C9 |$ n' w! U2 N- I7 ^0 ?% Z0 M
while True:
: L3 r* ^3 w- T/ O loud = cyberpi.get_loudness()+ @! s1 y' v4 C0 h
plt.clf()4 ]& x [; |5 E
plt.xlabel("數據名稱") ]) h0 |# V+ j7 u4 u- X5 ?
plt.ylabel("音量讀值")
7 x p/ V3 t2 v3 n6 s; b y_locator = plt.MultipleLocator(5)( {0 ], i+ }& W6 F4 T
ax=plt.gca()
1 t% }7 D- i7 Y# F1 { ax.yaxis.set_major_locator(y_locator)$ U5 {. D( y3 x$ Y& l0 y
plt.ylim(0,100)6 V$ f+ P2 ~4 F! X
plt.bar('音量', loud, align='center',label=f'溫度{loud}')
. C# M" v* [8 O+ k8 g+ `. F plt.title('童芯派聲音感測器讀值')
9 k. O9 N6 s+ }2 }9 ~( N+ i plt.legend()
- D* c( K! r9 B# x2 z. A; Z2 r7 G plt.show()3 C/ P5 |+ t+ o
plt.pause(0.01)
1 z: b0 D% ?' ?7 Q7 L% P0 M
1 n4 n, M( K9 I) h6 f
7 }1 s/ I) [0 O0 M% l& b+ A7 Imatplotlib 多圖表顯示4 S7 T6 x) w& @, e" g2 s
7 ?1 m2 D( l9 t, _7 M
from matplotlib import pyplot as plt J6 F& Z2 x; W% c
import time
+ o, {0 @ C9 vimport cyberpi
6 i+ N+ c; m# c! b, O. g3 Q) Q8 z : Y+ t! C3 P+ ]# i
loud_list = []2 l) I, @6 X$ q x
bri_list = [] a8 S( e$ q" {# J# d
plt.rcParams['font.sans-serif'] =['Microsoft YaHei'] # 載入中文字體,這裡載入的是電腦的自帶的微軟雅黑字體& f& o8 }; ~% M+ V7 u
plt.ion()( O3 _+ C# V5 k9 w# c1 [
plt.figure(figsize=(10,10))
7 L9 c8 k5 X2 zplt.figure(1)( A& o$ q% t2 J) R# T5 a8 h* D
x_locator = plt.MultipleLocator(5)/ W, Y C4 H# u; @5 R$ U" N
y_locator = plt.MultipleLocator(5): R J' u4 Q5 V- J7 t, ?
while True:+ ?7 y% F- ]0 A6 j" F$ m8 Q. r
loud = cyberpi.get_loudness()4 X+ Z; n. c# ~$ t, a: N
bri = cyberpi.get_bri()6 ?6 p+ R. d, k2 w6 a/ u
loud_list.append(loud)6 Q! {1 d; w. u, V. { g4 f
bri_list.append(bri) ]2 V7 C/ U7 ?( V3 n) {0 B! F
battery = cyberpi.get_battery()4 T7 ?: ~7 D _* l% F
size = [battery, 100-battery]
1 g3 `+ v# f) U! U3 K8 N status = [f'剩餘電量:{battery}%', f'已用電量:{100-battery}%']1 Z2 b- a" ~- d: Y& y; |# Z
ax1 = plt.subplot(221)8 ?' d( M. U6 O& K1 Y" c! B. n
plt.title('光線折線圖')
4 V* i. j! \& C6 A, ?7 l9 q; W ax1.plot(bri_list)
* S: c5 v7 `6 b( J ax2 = plt.subplot(222)
% [( [. d& r/ C, n0 g8 a* m plt.title('聲音柱狀圖')
# i, d$ L# |+ ^$ }+ [% g& | ax2.xaxis.set_major_locator(x_locator)
( j) V3 J, k& V" n/ c% t ax2.yaxis.set_major_locator(y_locator)9 ]8 }+ _. b7 |- T
plt.ylim(0,100)
8 ^4 E9 Y0 _# A. J ax2.bar('sound', loud)
+ J2 P( {3 j$ N- M ax3 = plt.subplot(223), ]9 w5 M3 O* I& Q# r
ax3.xaxis.set_major_locator(x_locator)
/ d& o+ V9 a8 [0 u* a! Y4 T8 X ax3.yaxis.set_major_locator(y_locator)' @! ?/ T; W4 M1 ], k# U
plt.xlim(0,100)
% X# G$ K/ t. |4 I' W: U plt.ylim(0,100)$ s$ {$ t7 ^% [- w7 r
plt.title('聲音、音量散點圖')
7 J# J0 ?) N/ k/ E ax3.scatter(loud_list,bri_list)
$ [# L* n/ [' F) R! B( @ ax4 = plt.subplot(224). H( w `/ l- }, O4 a( h& H
ax4.pie(size, labels = status, radius=1,wedgeprops = {'width': 0.3,'edgecolor': 'w'})- R. @4 q4 d/ X6 d6 Z! A
plt.title('童芯派電量')- [$ M, j* E# i2 r
plt.pause(0.2)
+ s9 L, w4 m6 \' Y8 F3 e plt.clf(); I. V& G, k7 n" c3 q" y5 K# @$ c6 J/ x
if cyberpi.controller.is_press('a'):
4 K) q% k, R Q- r3 I break
1 |0 B: d; W2 @, ^ if len(bri_list) > 500:
7 I0 c- U: f& z/ e* {. ~. @ bri_list = []9 O+ y& p. [" ]# N
loud_list = []+ w- e. d1 ^2 l( C7 g) {" t3 B& ?9 @
" S8 {1 ]# z8 U( d8 H2 C
|