本教學譯自Makeblock官方教學文件,原文點[ 我] 本教學文件以windows作業系統環境為範例,Mac使用者可以參考結合實際情況進行操作。 快速教程:如果您已安裝了Python環境和第三方編輯器,請跳至第三步。或在您的編輯器配置的Python環境當中安裝pyserial和cyberpi程式庫即可實現童芯派第三方編輯器的程式設計。 pip install pyserial pip install cyberpi
/ @0 R# W- P* d2 n/ e8 x1.安裝Python環境- 進入Python官方網站下載Python環境包,請根據自己的作業系統進行選擇。建議下載3.6-3.8的版本進行使用。如果下載最新的Python版本,可能帶來無法正常使用的問題。比如Python3.9以上的版本目前是不支援Windows 7作業系統的。( D! o+ u2 d3 x( O. h# p, L; _
Python Windows環境不同版本下載頁面:Windows Python下載 Python Mac OS 環境不同版本下載頁面: Mac OS Python下載 / g7 ?6 P4 d. p) _' W: ]6 A* U
- 按兩下安裝包進行安裝,注意勾選下方的Add Python 3.6 to PATH,點擊Install Now進行安裝,也可以點擊Customize installation將Python環境安裝至指定目錄下。
( `( D1 l8 H+ D6 m7 L/ `. ]; K& m 2.下載安裝並配置協力廠商Python編輯器(以Visual Studio Code為例)提示:不同的編輯器在操作步驟上有所差異。同時建議在編輯器當中配置安裝在系統中的Python環境。 - 下載 VSCODE,請根據自己的作業系統進行選擇。 VSCODE下載地址
- 點擊下載下來的安裝包進行安裝。(根據提示進行安裝即可)) d5 T+ r1 y" A* h
- 點擊桌面上Visual Studio Code的圖示運行程式。
- 在軟體介面左側找到下圖紅色方框當中的圖示並點擊。
2 }0 }7 M& ]( H% H' _ 4 C3 Y9 t7 Y# g E" U9 }% f0 V
- 在彈出的介面的搜索框當中,搜索Chinese (Traditional) Language Pack for Visual Studio Code,並點擊進行安裝。重啟後可以實現Visual Studio Code介面切換為中文。注意該擴展外掛程式發佈者為Microsoft。(如您習慣在英文介面進行程式設計,可以跳過此步驟)
9 ?( X; D$ `, w, n% o + M) j/ `! Q" `- X$ x2 e7 [$ o+ ]' `
- 繼續搜索Python,安裝Visual Studio Code中的Python編輯環境。注意發佈者為Microsoft。
! e s9 K4 `' d
! P1 v+ ^$ F, I) S+ ~0 I- 在Visual Studio Code編輯器當中配置前面安裝的Python編輯環境。
- 新建資料夾並新建py尾碼的檔就可以對基礎的Python程式編寫。1 g5 f" y" g0 d/ r, O: F, |0 c8 i
6 g- r: U% C. [2 N+ c+ l @; q
3.童芯派第三方編輯器程式設計準備" P& \. D) E6 G& K' M9 v
- windows環境下,按下鍵盤上的win+r鍵,輸入cmd並按下Enter,然後會彈出cmd視窗。
8 x% p% B9 l) m, W' `4 U, t7 o
' n9 y" {' d* y! n- 安裝pyserial程式庫,pyserial程式庫可以確保童芯派通過USB線或藍牙Dongle與第三方編輯器建立連接。, N: u8 M9 v7 a' _0 ~0 l5 `
在cmd視窗中輸入pip install pyserial 並按下Enter,等待安裝完畢。
5 f2 d. F0 x2 i3 e2 D0 r3 S- T- 安裝cyberpi程式庫,cyberpi程式庫是用於對童芯派進行線上程式設計的Python程式庫。
+ ~5 ^% p+ l W4 p/ K, \) i
在cmd視窗中輸入pip install cyberpi 並按下Enter,等待程式庫安裝完畢即可。
' X6 v! } i* i7 z$ @6 Y9 i% S9 i$ D3 a& u+ i& }! H
- 完成安裝後,您就可以在配置了Python環境的編輯器當中進行程式編寫了。. I2 m6 u' t, \0 O* C* k: Z
], G. M: M2 h2 _) Q( i4.注意事項 在第三方編輯器當中目前僅支持對童芯派進行線上模式程式設計。 6 Y2 ^3 ], j' t3 m0 I9 c$ y
5.一些程式設計案例
/ _$ n4 B( S" J* o9 R3 |% @& L7 U童芯派滑鼠 需要安裝pynput模組,參照上面的步驟pip install pynput ) Z' b! s+ N2 g
功能介紹:# j1 m- @3 G4 O
利用童芯派的陀螺儀模組和按鍵結合pynput模組的滑鼠控制功能,實現滑鼠的功能。
8 ^. D$ {& [ z. @通過陀螺儀控制滑鼠移動。通過按鍵實現滑鼠按鍵功能。) g( W& u8 O9 T* e ^
結合藍牙模組可以實現無線滑鼠的功能。
2 j& h/ q |. C* y8 n, }2 u1 E2 x7 ]$ X1 ^3 U7 f0 f6 P2 s
from pynput.mouse import Button,Controller. J$ X4 M+ B/ q& \# m; D+ }
import cyberpi( s1 ]7 O; a5 J
import time
, P4 _4 ^" N3 ?( s5 U, E
3 U2 \, s" o' r- | y6 D8 q7 W3 ~$ l! [mouse = Controller()* d3 V( w4 y9 J2 R( }
; g1 J2 w# f# Q. F0 z0 t$ Kwhile True:0 B0 C3 W) E. c5 o0 Y
if cyberpi.is_tiltback():4 i/ I3 s) O' q3 d% x
mouse.move(-3, 0)- c3 G+ L @% m; G) j9 V" h2 h
print(mouse.position)
( W. |4 d9 [- A if cyberpi.is_tiltforward():4 h0 w: ~0 o# A. w0 L- `1 x
mouse.move(3, 0)
( A* L; R" z, w% `5 {$ ^ if cyberpi.is_tiltleft():. `0 y% }; X9 h& l( h* v; j
mouse.move(0, -3)5 U, V T$ X# F! Y
if cyberpi.is_tiltright():0 J x# f- W+ p% D0 i2 I, O8 p
mouse.move(0, 3)3 ?5 z6 Z5 s! U9 d
if cyberpi.controller.is_press("b"):
' x8 x7 C y: q mouse.press(Button.left)/ ?( E* K6 l+ y$ h' W3 Q
mouse.release(Button.left)6 Q" `+ B* _: ^" J+ R; g
mouse.press(Button.left)
- K; M/ @" V7 }3 ^$ F mouse.release(Button.left)
: `* } ~3 K. `2 y if cyberpi.controller.is_press("a"):6 j: ~ @1 O7 u4 h
mouse.press(Button.right)
' ^+ g. ?+ l$ V, E% c- T. v mouse.release(Button.right)
2 ?4 y- I _9 K; [" \7 ^ time.sleep(0.01)
- b# X6 `6 x0 t/ h) y$ G, F, _2 f* s2 e: ~
# h0 j( {5 z2 {4 C
語音打字器
- p. n# d: e8 @$ U0 S功能介紹:
- A' ? r0 c6 w利用童芯派的語音辨識功能結合pynput模組的鍵盤控制功能。/ Q, `/ q/ b8 R& s6 M0 c- D& j- L
將語音辨識的結果通過pynput在文檔中打出來。
' d9 ~' e+ Q, o l0 F該程式目前只能支援英文。
5 F1 t3 W0 A0 j3 H! [& N
: I+ g5 u$ g2 `import cyberpi" u% o' U" ]9 h4 p
from pynput.keyboard import Key,Controller
" G6 n( Y! a- \- b4 N6 H7 ^+ @import time
1 X' H8 Q6 k2 t, ] - ?3 q% ^& e, j6 k
keyboard = Controller()
- U2 S7 ?+ V: [2 tcyberpi.console.clear()+ [ u6 b2 [- P" G
cyberpi.led.on(0, 0, 0)+ V3 Z* e# j4 I) H
cyberpi.set_recognition_url()
" X5 N: N) ^, P6 z1 \% W$ Icyberpi.cloud.setkey("請輸入雲服務授權碼") # 通過慧程式設計帳號可以獲得
4 i5 ^" j7 [, U4 d1 l ~cyberpi.wifi.connect("WIFI名稱", "WIFI密碼")
- b& t3 L* w1 |5 ^/ {5 K& d7 W Fwhile not cyberpi.wifi.is_connect():+ L5 X2 b# F, z$ f% J% o: k
pass4 X5 p: j$ }" h3 a; V2 K9 ~# b f
cyberpi.led.on(0,0,255) n7 x+ r- C* m" T8 _) i% I& |
cyberpi.console.println("WIFI已連接")7 \0 i& y3 A0 o1 q" b; b
cyberpi.console.println("--------------")
; T; A0 [$ E* b V% R% y9 dcyberpi.console.println("按下A鍵開始語音辨識")
8 }( n' T! X7 c Z( |5 ?+ Bwhile True:
! n2 u$ m5 X! k: X( M if cyberpi.controller.is_press('a'):( T0 w$ Z& V8 a5 d
keyboard.press(Key.space)
2 Q6 J, A' T' W2 P# s cyberpi.console.clear()6 s3 v2 r3 ]1 K! O( x$ E
cyberpi.led.on(100, 0, 0)4 t4 n R% B X
cyberpi.console.println("開始語音辨識")
+ T+ ?) p" k& u1 z cyberpi.audio.play("switch")
/ M$ h l8 r8 B/ A4 R- t9 \ M' D cyberpi.console.println("--------------")$ W7 J, }+ F- m) ]
cyberpi.cloud.listen("english", 2): @/ M: q: P4 d2 _: R
cyberpi.led.on(0, 0, 0)
+ {* @. R- R- D( ^" L' s say = cyberpi.cloud.listen_result()
7 s' O- N1 E, }, F$ D4 n+ e( ? cyberpi.console.println(say)
. t4 I% Z" {" |0 w/ v- Z& V" E cyberpi.console.println("--------------")
3 Z" t/ S! {( U. ~ for i in say:
( Q1 g+ z$ Y5 A( Y, `$ ~ if i == '':
6 d( |/ M6 |: V9 {; z, n keyboard.press(' ')
7 \* y3 C& d8 F* g# J else:: l! E6 U6 t+ k/ a5 @* ]. I
keyboard.press(str(i))0 P' e! O- K* N
time.sleep(0.03)
! t3 ?! w5 Y1 ^; W. ]9 m5 @' L+ |: O1 [: h& C4 r6 K8 R) `5 X
matplotlib音量柱狀圖 ; p- l2 _* l9 P$ `, Z Y* R
import cyberpi% Q& }4 f! s3 c9 D5 K3 @. Z
import time
5 m7 \3 i+ [0 f# o0 T! }7 Cfrom matplotlib import pyplot as plt9 p0 Z# R' t/ d1 J
4 J4 w J1 m# }, xplt.rcParams['font.sans-serif'] =['Microsoft YaHei']+ ~/ y6 l$ m4 ~# N
plt.ion()
* w, P0 D! P5 n! G7 z. h# h
( [, ?% U# x' U4 a, w2 K* U8 x9 f4 Swhile True:/ n3 z* V. r( D3 \/ v O9 O
loud = cyberpi.get_loudness()4 p1 ?, a% V+ |, y2 x; x7 p
plt.clf()
- |. I. V1 C; h plt.xlabel("數據名稱")
5 T: [$ F9 L7 B% W% @& ` plt.ylabel("音量讀值")7 H9 A5 z% p9 X
y_locator = plt.MultipleLocator(5)( G- X# U' G) q' H% C
ax=plt.gca()
/ f: K* F: G' H+ ?! j% A# R ax.yaxis.set_major_locator(y_locator)* b3 Y+ d" ]$ s
plt.ylim(0,100)6 @; |$ W- _# a1 u
plt.bar('音量', loud, align='center',label=f'溫度{loud}')
0 a0 l' W7 i; F plt.title('童芯派聲音感測器讀值')
/ a$ o$ m2 ?0 S3 V* x plt.legend(), p- g6 M; X/ n: [1 R. S C) H# _
plt.show()
7 v: B5 T; r4 C1 U( C. _+ p plt.pause(0.01)( w% D O8 L) o- L) [
* `: R3 @! C0 ?8 {- e' }& I! c, v. C. z0 o; j6 e9 v7 \
matplotlib 多圖表顯示
2 t- @6 S' S8 d6 a
/ q# g' u- f% {from matplotlib import pyplot as plt
: y# C! p: e! Q! Z* U- E. T1 m4 T' Eimport time$ q1 T9 T5 d6 f& {% U6 c4 x
import cyberpi
1 [5 a& e6 e' Z& `( c$ @ # g* u8 y4 y7 H9 @
loud_list = []' G( B- h2 v B: c$ _: x3 |# X
bri_list = []% b0 Z6 G$ X* q3 r% n4 n ?
plt.rcParams['font.sans-serif'] =['Microsoft YaHei'] # 載入中文字體,這裡載入的是電腦的自帶的微軟雅黑字體; O- j* W3 v7 q/ G. x
plt.ion()
' _. n7 y7 F$ qplt.figure(figsize=(10,10))
' ?# B5 Q4 C& pplt.figure(1)
2 v; B1 w; }5 x; t( Zx_locator = plt.MultipleLocator(5)9 ^' G2 d( [4 d) ~8 u
y_locator = plt.MultipleLocator(5)
' c$ V! g% s0 l& R' ^' }while True:
3 K* |. `" \3 H9 \7 x loud = cyberpi.get_loudness()
9 M+ z3 H) m5 c, _6 e) E bri = cyberpi.get_bri(). H( y$ Y" r- f3 O; t& ]( a7 _
loud_list.append(loud)
( X) B' M4 O' { bri_list.append(bri)8 y' i+ V- X- _9 }
battery = cyberpi.get_battery(); d/ p9 S3 X) x% w& ?: ^
size = [battery, 100-battery]1 I+ h t N- }8 D e6 y- S3 n& A( z
status = [f'剩餘電量:{battery}%', f'已用電量:{100-battery}%']
6 E# N6 D8 C$ ]0 }- p' B s ax1 = plt.subplot(221). a! }. R9 ^5 T5 o
plt.title('光線折線圖')4 d; P1 \9 d+ i; `- j, o. c
ax1.plot(bri_list)% i* d' y0 X; c. X1 f" y
ax2 = plt.subplot(222). t5 w V* I$ M8 O& j& H( P# R
plt.title('聲音柱狀圖'), C9 |6 C C' i. F% Z l1 z
ax2.xaxis.set_major_locator(x_locator)* b/ T! z! \. i( M$ p- n
ax2.yaxis.set_major_locator(y_locator)
, }7 z3 A, @* m/ R. Z8 u. ], X plt.ylim(0,100)
* h. Z/ P7 R/ v E! @: J ax2.bar('sound', loud): p, a! @9 u6 F4 W0 n. `
ax3 = plt.subplot(223)
! B- ?/ m- |) [ ax3.xaxis.set_major_locator(x_locator)3 u1 @% D! T: a- v% N0 s' g
ax3.yaxis.set_major_locator(y_locator)1 A7 v, a6 h. d
plt.xlim(0,100)0 Y. e# [" A. g$ b$ L1 M" w" P8 p i7 H
plt.ylim(0,100)$ s3 p( a; s5 N) G
plt.title('聲音、音量散點圖')$ J' A, f2 F9 U" y1 [
ax3.scatter(loud_list,bri_list)4 z0 e! n. n/ s7 u5 ^# Q V
ax4 = plt.subplot(224)
7 N! L I2 j9 W9 U; S ax4.pie(size, labels = status, radius=1,wedgeprops = {'width': 0.3,'edgecolor': 'w'})3 m, @2 j" P, b* _8 A
plt.title('童芯派電量')
% }- S* B) x" U( Q5 | plt.pause(0.2)6 n! D7 t' t' P
plt.clf()* V: p0 |- @2 T6 l4 e7 C
if cyberpi.controller.is_press('a'):
$ j7 l/ Z; p0 B8 n* s8 e break
$ ]* f& v1 T& y( E" u if len(bri_list) > 500:9 t' D" z* `; ^* O3 E/ f
bri_list = []
- o& W( R5 } Z& k0 n$ m$ } loud_list = []" M/ D8 `! a/ E& ? }
$ g' ` Q, j4 J' N3 n
|