本教學譯自Makeblock官方教學文件,原文點[ 我] 本教學文件以windows作業系統環境為範例,Mac使用者可以參考結合實際情況進行操作。 快速教程:如果您已安裝了Python環境和第三方編輯器,請跳至第三步。或在您的編輯器配置的Python環境當中安裝pyserial和cyberpi程式庫即可實現童芯派第三方編輯器的程式設計。 pip install pyserial pip install cyberpi
7 S3 j K" P- P( D3 X" h, H0 C1.安裝Python環境- 進入Python官方網站下載Python環境包,請根據自己的作業系統進行選擇。建議下載3.6-3.8的版本進行使用。如果下載最新的Python版本,可能帶來無法正常使用的問題。比如Python3.9以上的版本目前是不支援Windows 7作業系統的。
+ d7 F. P. M+ R' \) @+ j- Q# p
Python Windows環境不同版本下載頁面:Windows Python下載 Python Mac OS 環境不同版本下載頁面: Mac OS Python下載 6 l S+ q, j+ }1 U9 ^1 l" Z
- 按兩下安裝包進行安裝,注意勾選下方的Add Python 3.6 to PATH,點擊Install Now進行安裝,也可以點擊Customize installation將Python環境安裝至指定目錄下。
% t! M) j% O* j7 Y2 s$ a g& F 2.下載安裝並配置協力廠商Python編輯器(以Visual Studio Code為例)提示:不同的編輯器在操作步驟上有所差異。同時建議在編輯器當中配置安裝在系統中的Python環境。 - 下載 VSCODE,請根據自己的作業系統進行選擇。 VSCODE下載地址
- 點擊下載下來的安裝包進行安裝。(根據提示進行安裝即可)
& s0 P# e8 n, b. Q# S: I
- 點擊桌面上Visual Studio Code的圖示運行程式。
- 在軟體介面左側找到下圖紅色方框當中的圖示並點擊。
/ W& I! |6 b7 o, D C% q1 V% f. m; ?- y ' D x! }8 y! x# N3 T
- 在彈出的介面的搜索框當中,搜索Chinese (Traditional) Language Pack for Visual Studio Code,並點擊進行安裝。重啟後可以實現Visual Studio Code介面切換為中文。注意該擴展外掛程式發佈者為Microsoft。(如您習慣在英文介面進行程式設計,可以跳過此步驟)/ b. g+ T* K/ C
" O2 h: h. d6 s: W! x- 繼續搜索Python,安裝Visual Studio Code中的Python編輯環境。注意發佈者為Microsoft。
4 ?% j3 E* ]$ l/ J. Y+ w1 n: O 8 K9 H; r; V' I" l; x& V
- 在Visual Studio Code編輯器當中配置前面安裝的Python編輯環境。
- 新建資料夾並新建py尾碼的檔就可以對基礎的Python程式編寫。
Q }/ j( Q1 U' K0 |1 t 3 T8 W; n9 i6 c7 ]. f3 U
3.童芯派第三方編輯器程式設計準備2 j7 ]6 s+ }, _& B" {
- windows環境下,按下鍵盤上的win+r鍵,輸入cmd並按下Enter,然後會彈出cmd視窗。
5 r( ?% D9 J7 g' ?! o3 J
/ z9 O% s' h1 Y: W# v9 t- 安裝pyserial程式庫,pyserial程式庫可以確保童芯派通過USB線或藍牙Dongle與第三方編輯器建立連接。" v& g. A1 |% @# m; s$ V1 v* a- W
在cmd視窗中輸入pip install pyserial 並按下Enter,等待安裝完畢。 . ?9 Z: ~% S: }
- 安裝cyberpi程式庫,cyberpi程式庫是用於對童芯派進行線上程式設計的Python程式庫。
$ E0 O! T; D0 w+ x. w* P' n
在cmd視窗中輸入pip install cyberpi 並按下Enter,等待程式庫安裝完畢即可。
M/ j# p3 I8 q/ m+ e- |/ O/ F
4 }* R9 s4 b& Z0 I) ?4 x- 完成安裝後,您就可以在配置了Python環境的編輯器當中進行程式編寫了。& L+ U/ p- G/ j q
" W# f$ x* t! y4.注意事項 在第三方編輯器當中目前僅支持對童芯派進行線上模式程式設計。 |3 t* E$ l9 @: g
5.一些程式設計案例5 d9 @# X- v; n6 a: K/ G! H( Q
童芯派滑鼠 需要安裝pynput模組,參照上面的步驟pip install pynput - ^0 E5 M" y4 g9 Q
功能介紹:
) L' J, G( ~$ Y& F% s, G利用童芯派的陀螺儀模組和按鍵結合pynput模組的滑鼠控制功能,實現滑鼠的功能。, o6 \1 x9 [7 |
通過陀螺儀控制滑鼠移動。通過按鍵實現滑鼠按鍵功能。
0 L5 ~; y& a8 }/ j8 V) p% F結合藍牙模組可以實現無線滑鼠的功能。
* ?* i) [; u0 u8 ?* Q4 \0 N. [. G3 |: Q$ z4 w8 H
from pynput.mouse import Button,Controller
8 ~* v1 P' C: Qimport cyberpi
* \( t9 e" I! x- k% R0 n6 w( w5 nimport time4 [7 x8 a; O S; g6 p, {
* z! E X. K% }+ T8 Vmouse = Controller()3 N, m3 Q6 b% @7 L
! p2 f9 M% k$ i/ f/ D+ |6 E9 ? dwhile True:
: G* x0 h! k f! m5 ^2 o! b$ N) C if cyberpi.is_tiltback():3 o5 j) z' P. @7 [ B; Y
mouse.move(-3, 0)3 Z3 Z( v+ a( Q2 d M2 n
print(mouse.position)
8 }3 K; o, a9 K q: k7 ~8 ] ]- p- W if cyberpi.is_tiltforward():5 r) A5 U8 k- k+ B+ j
mouse.move(3, 0)
0 c1 f! \8 x/ L8 i8 M if cyberpi.is_tiltleft():
/ e8 X n) I2 ?2 j' e mouse.move(0, -3)
# |" ~: W8 f: u if cyberpi.is_tiltright():
, }: _+ V. p' L7 k mouse.move(0, 3)
. m9 p5 P4 ?2 e* l3 d3 y9 h3 K if cyberpi.controller.is_press("b"):( O5 D2 \; l( r& G
mouse.press(Button.left)% E+ F: S" N$ I: | i
mouse.release(Button.left)
+ c: U3 J' d4 O9 ?# X3 J. g2 K mouse.press(Button.left)7 }2 f' F8 z' x& @, M) B& d7 V4 a
mouse.release(Button.left)& y$ c9 ^ {7 _+ n4 h8 d- s6 G
if cyberpi.controller.is_press("a"):$ r1 {; R0 E' Q2 r" J
mouse.press(Button.right)
* l! ~* K0 R1 X) k+ X mouse.release(Button.right)
! P# E: e! A" Q& I1 o- K time.sleep(0.01)
9 B2 q$ e, P* G4 g; a! b _- @# d7 v3 X% F
! n7 x0 D* l* ]! m- ^. i語音打字器 1 D! {0 Q. N2 }
功能介紹:: N& [8 w! D: ~' i0 z m
利用童芯派的語音辨識功能結合pynput模組的鍵盤控制功能。/ }5 |% p# y6 C: E1 W* v" c
將語音辨識的結果通過pynput在文檔中打出來。: i, O6 A" q4 j
該程式目前只能支援英文。# g+ D0 S5 w K. D+ x6 w
, L3 h* v) a# ximport cyberpi
, u/ z% _; L0 H/ {( w7 ]" }from pynput.keyboard import Key,Controller
! Z$ T# q) n {; _, z# o2 Kimport time
5 v, e1 [3 v' f7 ^0 J # S- l, [2 P! e
keyboard = Controller()9 o+ r3 r2 D" z- O5 d
cyberpi.console.clear()
' |' Q, e% y `; f" @cyberpi.led.on(0, 0, 0)
( k2 b3 C1 s) Q5 ] ^9 \cyberpi.set_recognition_url()5 G" W( y0 I, j
cyberpi.cloud.setkey("請輸入雲服務授權碼") # 通過慧程式設計帳號可以獲得
: g7 Y# m* o3 h6 L* Tcyberpi.wifi.connect("WIFI名稱", "WIFI密碼")
8 C8 r F7 f! e4 wwhile not cyberpi.wifi.is_connect():
Y! c6 P8 B; i0 t2 w pass7 n" m( z4 ?& u
cyberpi.led.on(0,0,255)% H4 W+ \' N! f) `( {
cyberpi.console.println("WIFI已連接")
, H+ z6 \$ D; t7 p3 Icyberpi.console.println("--------------")
3 b- M" F% u6 Z! Gcyberpi.console.println("按下A鍵開始語音辨識")
3 v2 T9 q/ i1 ]" Z4 Nwhile True:7 T/ P) y. I, T2 J: i* v9 M$ o
if cyberpi.controller.is_press('a'): Y) W. V o# G1 Y. W
keyboard.press(Key.space)$ b0 }8 L7 N5 _& q* E v) }
cyberpi.console.clear()) B$ j) `3 Z7 c
cyberpi.led.on(100, 0, 0)
. f' @$ Y5 m+ p. u4 R8 m cyberpi.console.println("開始語音辨識")7 Z3 n! K9 l* u, n" R
cyberpi.audio.play("switch")3 \3 Q! t1 F; `4 o9 N* s
cyberpi.console.println("--------------")4 N0 n. @% e" y2 ?4 E) E
cyberpi.cloud.listen("english", 2)' [! {) r& L* V- u5 ?, @
cyberpi.led.on(0, 0, 0)
8 w3 q* R2 p# M9 D; I! ~ say = cyberpi.cloud.listen_result()$ W: ~% W0 L4 E( O1 @: {
cyberpi.console.println(say)2 [$ s# C( g6 n- i
cyberpi.console.println("--------------")3 q" |& Q/ m; A6 o @5 T
for i in say:
+ f; w6 |9 q" }2 y) d/ M9 H0 j if i == '':% m+ k. K2 G, W* A" d2 g5 z4 e
keyboard.press(' ')0 c: {! t; D3 S
else:
" @8 a0 u: S! C' {6 X+ Y* ]; s keyboard.press(str(i))$ z$ M* S# R$ k T6 k% X% w) ^
time.sleep(0.03)
" H# g4 V" d4 K$ |& S) U0 x3 Q N+ s% [' i* E& k
matplotlib音量柱狀圖
/ R# y- G! C" p: Z$ k! E himport cyberpi$ b: B. o% I ~2 D# \
import time/ Y+ b) U" l0 {' M
from matplotlib import pyplot as plt4 D3 ] L; D/ p0 e+ l; }6 v
) j" m8 O& r+ m9 B# i# A: D! ^
plt.rcParams['font.sans-serif'] =['Microsoft YaHei']' M* E }+ L; \4 E9 j
plt.ion()
/ T4 x% n7 r e( G, B9 Q* @ V/ M
+ w G" b+ n0 k! \3 G" awhile True:
0 o6 H: B. D6 n* e$ C+ _. c loud = cyberpi.get_loudness()
1 z5 s n! ~2 c* ~& X+ R" Q plt.clf()9 q8 w# k9 q8 y. T5 G, n+ }
plt.xlabel("數據名稱")
4 f1 t6 K" x" c) _9 u+ c plt.ylabel("音量讀值")
/ c8 |; w2 E, y! b y_locator = plt.MultipleLocator(5)
. f, X/ s0 _. v2 t0 R ax=plt.gca()
7 w h9 m$ j8 l- V7 m3 A' @ ax.yaxis.set_major_locator(y_locator); z! C3 g" I7 r/ r2 U5 r0 }6 F
plt.ylim(0,100)
4 M9 T* C0 \& h' Y+ h! x. K# i plt.bar('音量', loud, align='center',label=f'溫度{loud}')- _, Q9 k0 c* Y" s/ g2 t+ @3 C" \5 k
plt.title('童芯派聲音感測器讀值')
5 p8 ~; B; Q, J2 [ H plt.legend()! J, i4 v5 `0 a0 u3 ?4 ~3 C7 N) m
plt.show()
) B p8 j7 a4 X plt.pause(0.01)
# c# O: n$ C5 l% _
0 B' W+ t5 L7 q. m0 E1 T0 D) A- k
matplotlib 多圖表顯示
6 ?7 N) x2 P4 [. @' t
: N" f7 [* N; E+ Afrom matplotlib import pyplot as plt
, ?) _- [( x( L7 [% `) B1 i' jimport time2 y! i7 U" y; f# ~. l& ]! C
import cyberpi
* o& m8 a; x/ p, e1 H% W/ g: ~
) c$ b2 @) j$ l& R" Tloud_list = []
6 M( C) Y: o: W% W% lbri_list = []9 |& U0 }: C: Q! R
plt.rcParams['font.sans-serif'] =['Microsoft YaHei'] # 載入中文字體,這裡載入的是電腦的自帶的微軟雅黑字體" d' Q& s A0 S6 V# i# l
plt.ion()0 g8 R1 \1 g* N, n( L5 g1 }
plt.figure(figsize=(10,10))- |2 A7 C b; \& Q$ h6 i, z5 O0 y/ l
plt.figure(1)3 K+ G& F2 s7 E9 {6 g* f- T: `
x_locator = plt.MultipleLocator(5); b( x' q* e$ ^+ C0 H- O. n# p
y_locator = plt.MultipleLocator(5)6 F. G5 i0 Z5 o$ P/ w" I; g3 M f
while True:
- h7 F' z) r9 i; Y% m loud = cyberpi.get_loudness()
4 S+ u/ ^0 C0 M9 f( ^% ^ bri = cyberpi.get_bri()
" ?# ^% v* ^/ B7 f loud_list.append(loud)( n* D4 |5 @; a: w/ x1 S
bri_list.append(bri)
% K" J8 Y6 c1 `9 z2 I8 @# E. o battery = cyberpi.get_battery()7 R' \$ ^' L: m
size = [battery, 100-battery]
5 A5 f+ {6 e: D1 G4 u status = [f'剩餘電量:{battery}%', f'已用電量:{100-battery}%']
: G& ~* ]1 C) s% j4 U ax1 = plt.subplot(221)
E/ o# M, U3 q+ u7 n plt.title('光線折線圖')5 ]$ N5 E. U; d8 P* L# Q) g
ax1.plot(bri_list)7 V3 t7 p. w2 f' q" M4 M1 s
ax2 = plt.subplot(222)
9 e6 b# T$ u6 ]1 M plt.title('聲音柱狀圖')& C) W, |6 u4 y1 m$ f& A+ t; G
ax2.xaxis.set_major_locator(x_locator)
* F% |: U9 v: m" }( d ax2.yaxis.set_major_locator(y_locator)/ T7 q+ G: @, |; V. e
plt.ylim(0,100)
- I, s4 R1 @8 O/ ]% U ax2.bar('sound', loud)# N+ S( O) w3 H( e, n
ax3 = plt.subplot(223)6 v* v4 h' w; |3 M
ax3.xaxis.set_major_locator(x_locator)
. B! K1 E; D6 u6 b7 m, F( E4 I1 o ax3.yaxis.set_major_locator(y_locator)
# B- o0 @7 M2 g, H' ` g plt.xlim(0,100)
/ J9 S. \+ e" `+ w1 I plt.ylim(0,100)0 R+ w6 [! W; _
plt.title('聲音、音量散點圖')4 N- o8 p4 X' v6 M
ax3.scatter(loud_list,bri_list)1 Z Q6 M3 e/ V- F0 c9 y- @/ F
ax4 = plt.subplot(224)
1 I3 G E6 l6 D+ B ax4.pie(size, labels = status, radius=1,wedgeprops = {'width': 0.3,'edgecolor': 'w'})& C+ \) |( [) x! @+ l% G, i
plt.title('童芯派電量')1 ]/ W! C" I/ Y/ ~3 e
plt.pause(0.2)
4 P( d* D% g! J plt.clf()
; p7 s; T4 K/ H8 s3 o3 H8 ?% _! h if cyberpi.controller.is_press('a'):
# c5 c7 [" w, N9 i p i) P break
: ^- \" u' H" O* \7 L* C7 ^( a if len(bri_list) > 500:! t3 n4 j. i X. ^( C) I
bri_list = []5 B' f& x9 q1 T& s1 y) [* ?' h
loud_list = []
0 z* r, T, L1 s5 l7 K- L) w; R& W( Z V- x+ j4 g8 u) X
|