本教學譯自Makeblock官方教學文件,原文點[ 我] 本教學文件以windows作業系統環境為範例,Mac使用者可以參考結合實際情況進行操作。 快速教程:如果您已安裝了Python環境和第三方編輯器,請跳至第三步。或在您的編輯器配置的Python環境當中安裝pyserial和cyberpi程式庫即可實現童芯派第三方編輯器的程式設計。 pip install pyserial pip install cyberpi * h& R# {5 G6 R+ Y F& O1 r4 b5 i
1.安裝Python環境- 進入Python官方網站下載Python環境包,請根據自己的作業系統進行選擇。建議下載3.6-3.8的版本進行使用。如果下載最新的Python版本,可能帶來無法正常使用的問題。比如Python3.9以上的版本目前是不支援Windows 7作業系統的。2 y: b7 J" a b. J2 x% p. z
Python Windows環境不同版本下載頁面:Windows Python下載 Python Mac OS 環境不同版本下載頁面: Mac OS Python下載
1 x; ]3 a6 c4 B y% v& ^) ]- 按兩下安裝包進行安裝,注意勾選下方的Add Python 3.6 to PATH,點擊Install Now進行安裝,也可以點擊Customize installation將Python環境安裝至指定目錄下。
5 r( Z. j( l; B8 n" c 2.下載安裝並配置協力廠商Python編輯器(以Visual Studio Code為例)提示:不同的編輯器在操作步驟上有所差異。同時建議在編輯器當中配置安裝在系統中的Python環境。 - 下載 VSCODE,請根據自己的作業系統進行選擇。 VSCODE下載地址
- 點擊下載下來的安裝包進行安裝。(根據提示進行安裝即可)
- D9 W! i4 ]5 D
- 點擊桌面上Visual Studio Code的圖示運行程式。
- 在軟體介面左側找到下圖紅色方框當中的圖示並點擊。
, { F+ f( K/ T0 H
4 d* v8 l5 ^* T h- 在彈出的介面的搜索框當中,搜索Chinese (Traditional) Language Pack for Visual Studio Code,並點擊進行安裝。重啟後可以實現Visual Studio Code介面切換為中文。注意該擴展外掛程式發佈者為Microsoft。(如您習慣在英文介面進行程式設計,可以跳過此步驟), y$ I- ? |. S. U+ y/ Q
# b$ J1 H; ]8 s# A) m$ R- 繼續搜索Python,安裝Visual Studio Code中的Python編輯環境。注意發佈者為Microsoft。9 _8 V: O) y# [6 V/ b( S, u
0 B# ~* a) f* D! P) ?2 h# I* X
- 在Visual Studio Code編輯器當中配置前面安裝的Python編輯環境。
- 新建資料夾並新建py尾碼的檔就可以對基礎的Python程式編寫。/ q5 U2 j w( j8 D1 T+ J( n1 K
: w6 h9 q4 h3 v3 S* M! U$ K/ H
3.童芯派第三方編輯器程式設計準備
) k8 i( s* J* W- windows環境下,按下鍵盤上的win+r鍵,輸入cmd並按下Enter,然後會彈出cmd視窗。4 L5 T4 P& ~ n$ N, h, [
8 n: i& I D8 P( i- 安裝pyserial程式庫,pyserial程式庫可以確保童芯派通過USB線或藍牙Dongle與第三方編輯器建立連接。0 L+ ?9 b( D; S, n
在cmd視窗中輸入pip install pyserial 並按下Enter,等待安裝完畢。
0 Z1 I+ s' j4 Q3 |! I( V/ V( N- 安裝cyberpi程式庫,cyberpi程式庫是用於對童芯派進行線上程式設計的Python程式庫。
2 O# O. W' O% P) S+ g+ y, u
在cmd視窗中輸入pip install cyberpi 並按下Enter,等待程式庫安裝完畢即可。
, j8 P# F3 X$ r) {+ F
0 s3 X! l" [& o+ i. t' }7 M- 完成安裝後,您就可以在配置了Python環境的編輯器當中進行程式編寫了。" a7 @$ g( |: b2 G: H
" f( w4 |/ `# f; K, ~$ Q4.注意事項 在第三方編輯器當中目前僅支持對童芯派進行線上模式程式設計。 " ]$ D# G! e& j! U7 Q! f
5.一些程式設計案例
8 t1 O; p: T3 R% e0 C童芯派滑鼠 需要安裝pynput模組,參照上面的步驟pip install pynput
9 P$ s& s1 m6 L8 n( x6 L功能介紹:
( Q, X" o4 {+ D. K" x% l. }2 q' f利用童芯派的陀螺儀模組和按鍵結合pynput模組的滑鼠控制功能,實現滑鼠的功能。
5 _- d% r0 _% P5 [通過陀螺儀控制滑鼠移動。通過按鍵實現滑鼠按鍵功能。
8 Q2 h4 U. Q- S結合藍牙模組可以實現無線滑鼠的功能。! [! b [. P/ c8 s
3 ^! j: k/ u \8 d1 e8 i4 W% R0 ]
from pynput.mouse import Button,Controller0 k/ ?- f c- l# M- Y
import cyberpi
& u9 y; H M% L" R) U, Nimport time
4 O3 _7 a3 ~9 a% e. L
9 I6 }+ H* a! cmouse = Controller(). ]0 s: g4 T5 c v; n9 V2 ~5 W% }
' ~& r. A: s0 c5 A3 _( zwhile True:
9 w: U& g( A3 O! M, p$ ~# _ if cyberpi.is_tiltback():
9 g* K1 O3 K- F: J: W mouse.move(-3, 0)& n6 h }( }) g7 Q; v8 i
print(mouse.position)5 L; l9 @# Q4 O4 m6 p5 Q! M, U- |
if cyberpi.is_tiltforward():
$ j `0 ?( P6 X# |( M mouse.move(3, 0)5 }# z' z; ]8 U/ i4 ?" F/ s6 ~
if cyberpi.is_tiltleft():
3 w1 T: H0 I& p g9 {8 N4 t' N mouse.move(0, -3)
- ^# O& B* c& e6 L6 A if cyberpi.is_tiltright():
) Q* T4 Z' G+ n6 ^5 w9 A: x mouse.move(0, 3)
" {( i0 u3 A* y* W6 P# V if cyberpi.controller.is_press("b"):
3 \2 M6 U) u M3 K4 F mouse.press(Button.left)
- [" z, K! J+ h$ N0 ^0 \! f; M' D mouse.release(Button.left)
# o& M5 ]% w$ o! z mouse.press(Button.left)
5 T# _! m, H, O7 y3 Q7 \1 ?" S* z mouse.release(Button.left)1 R/ b7 u# n6 L' K6 A# k8 Y* ]
if cyberpi.controller.is_press("a"):
6 v; N6 j3 Y$ Y# G/ n% q mouse.press(Button.right)/ c% D9 p4 p7 i/ K- y* P
mouse.release(Button.right)
3 t# V$ B% q0 I) F- O) f; ~3 G5 d time.sleep(0.01)5 G* \5 i. m2 h1 f# r6 ]: i& _( t/ v
9 P1 v" {5 w3 E3 B* ?/ \ @
! [# H3 s. ^# ~語音打字器
0 |. ]# E: {( \ Y4 V {, U# i功能介紹:
; d, Q9 q; a) Z/ g! T; P( Z利用童芯派的語音辨識功能結合pynput模組的鍵盤控制功能。1 a) A4 s- _9 t& w7 l
將語音辨識的結果通過pynput在文檔中打出來。
0 \: g7 ~" P" I5 G- C& C: X' I該程式目前只能支援英文。+ h9 k) s, u$ K$ t9 L2 @9 x
J: g: `0 y; |5 Z9 \/ S' H/ G6 Vimport cyberpi% I. e2 w- |9 E; E$ q8 c3 T
from pynput.keyboard import Key,Controller
7 C* [9 j( m4 a& t# r- Vimport time
5 i/ i" L7 Q6 a) C H5 ?
$ I: J5 F9 h7 h5 r' jkeyboard = Controller()
K3 [+ Y# H. }cyberpi.console.clear()* i0 ?9 _' w% q
cyberpi.led.on(0, 0, 0) R, n: ?4 f0 H) q' g2 l
cyberpi.set_recognition_url(). @; X. g7 y; U/ k. ^! }
cyberpi.cloud.setkey("請輸入雲服務授權碼") # 通過慧程式設計帳號可以獲得& _2 G p% h! i* c. ~- x
cyberpi.wifi.connect("WIFI名稱", "WIFI密碼")
, i1 a* ~6 I0 p9 O* M, Y: n' V" vwhile not cyberpi.wifi.is_connect():
8 Q7 v, f+ k& I# W/ X m2 r pass! s3 w: r0 Y; A( }7 x
cyberpi.led.on(0,0,255)
: M% O6 w$ d: Z5 A+ [3 h7 d$ m6 P% tcyberpi.console.println("WIFI已連接")
7 @' s0 v- a |: Pcyberpi.console.println("--------------")2 U. W' B* U) _: s& S
cyberpi.console.println("按下A鍵開始語音辨識")
' ^9 }. H) F6 Q1 C; G% ?+ X* Iwhile True:0 N) l) J" T( v* {. P# }
if cyberpi.controller.is_press('a'):7 [! ~3 k; F2 A# q6 U9 g# C
keyboard.press(Key.space)! R: u# w! e7 u7 I
cyberpi.console.clear()
# w& M/ r7 v4 w# R7 E# r1 U* c* k- Z cyberpi.led.on(100, 0, 0)4 i# J0 E0 q! V0 |
cyberpi.console.println("開始語音辨識"). O: I+ D# J1 b1 C0 z" }( L% H% S
cyberpi.audio.play("switch")+ [; E Z$ e! P0 D3 m5 Q% v+ K
cyberpi.console.println("--------------")
2 m7 f2 |3 \8 y R cyberpi.cloud.listen("english", 2) `; f w- `7 ^* o
cyberpi.led.on(0, 0, 0)2 Z" {! U# s- s( l
say = cyberpi.cloud.listen_result()
5 E s* V: s" L5 u, Y cyberpi.console.println(say). Z: z |9 S: @* X# R
cyberpi.console.println("--------------"): v$ I, b/ j2 ? B
for i in say:0 s4 _5 H, m4 u" f/ M/ a
if i == '':( w @3 B; K7 u: P+ t% P1 B
keyboard.press(' ')
! V, k- v1 X9 M4 k% S else:
! D2 X* ^6 d' R- F keyboard.press(str(i))
2 F: m5 S. x- j: y time.sleep(0.03)2 ~/ j$ N, Z3 f# \( V
( m% M* | |! q7 u- F0 l
matplotlib音量柱狀圖
1 z& i& ` I0 G+ K1 }import cyberpi
. v( t7 E; N$ j9 Vimport time" e/ D% X' `' |7 I! ^
from matplotlib import pyplot as plt1 @9 Z- A( \' V1 `# X
0 @ e+ a! @% a: S1 | Y4 C
plt.rcParams['font.sans-serif'] =['Microsoft YaHei']
" \" t' C @, e) X# gplt.ion()+ Z5 A! ~1 e- B
" p5 \% U) ~, W( E1 c0 T2 @
while True:9 Q" b% \: G2 T# M, H C8 w! ?
loud = cyberpi.get_loudness()
- m( e5 ]# E: {5 j; h: s% j& J7 d0 ] plt.clf()
+ `0 W" e- p" V2 S- G J plt.xlabel("數據名稱")2 u, x3 @: D4 K/ \. u
plt.ylabel("音量讀值")7 h% v' {0 v6 x+ k
y_locator = plt.MultipleLocator(5)
. C( m1 B5 @4 E- H* l ax=plt.gca()
0 \* \3 d( S7 u" Q( d4 h) @, q+ G ax.yaxis.set_major_locator(y_locator)
9 G" F% A, ~/ ^4 p plt.ylim(0,100)
" H5 d0 y* D" O/ ]+ M plt.bar('音量', loud, align='center',label=f'溫度{loud}')! i% i) V+ Z3 V" m6 h# Y7 |
plt.title('童芯派聲音感測器讀值')
7 S, J: U: l7 e+ A) ]; R7 ]' k plt.legend()
! \. ?# f) e6 w6 i plt.show()+ d; u9 p2 `% J
plt.pause(0.01)
1 S h1 v6 L9 r! h% c9 w( u" a
( e& h3 J1 u ~9 K5 I" V/ E" O" Z4 F! \) ^3 g* k0 R2 f D2 W
matplotlib 多圖表顯示" ]0 N7 Q D3 n; x/ |- E
7 ]0 S6 v6 I, u5 S6 a, {from matplotlib import pyplot as plt% |+ V5 r/ p' Q1 o: R
import time& T" q0 F+ e) u' r; w5 P0 V2 u
import cyberpi2 O) J- I& w/ j0 _! e
" Q8 K, W, I# C: vloud_list = []
% @+ U) l+ j' S' S4 b9 h' Rbri_list = []' S( |6 {& j% y& i: t7 M$ ^
plt.rcParams['font.sans-serif'] =['Microsoft YaHei'] # 載入中文字體,這裡載入的是電腦的自帶的微軟雅黑字體& R+ P! E! S" R% ~2 L' l
plt.ion()1 v8 n' j+ L' Y
plt.figure(figsize=(10,10))$ q- @$ w% t2 M8 ~3 z; M
plt.figure(1)+ V5 Q8 F3 R, t# P7 F. @" C! a
x_locator = plt.MultipleLocator(5)" u Y4 b2 h/ m3 @
y_locator = plt.MultipleLocator(5)# i# l$ j4 h& [8 w2 s4 V4 T
while True:. Q1 p8 g* I+ O4 y
loud = cyberpi.get_loudness()
1 p) O. W1 G( h$ u- D j7 S bri = cyberpi.get_bri()
" }, q8 G6 x/ t5 L [ loud_list.append(loud)+ K" B* P; k7 v) G' ?* @
bri_list.append(bri)
) c; H, y6 ?( J9 l, v battery = cyberpi.get_battery()
* t* M. u( w9 ~ size = [battery, 100-battery]; j0 J9 G, l1 ^# a6 l6 O6 [
status = [f'剩餘電量:{battery}%', f'已用電量:{100-battery}%']
: ^ R! s) @, y9 ]- b, f ax1 = plt.subplot(221)
I' @ w4 ]# u: n: i! D plt.title('光線折線圖')
' A, K: d3 Z: M6 ~ ax1.plot(bri_list)
5 E% Q) v& n6 m" b& r2 D ax2 = plt.subplot(222)
9 y, G5 S( w5 Z" ~: l, a; b4 h plt.title('聲音柱狀圖')
* ?" o5 U. s6 b( s ax2.xaxis.set_major_locator(x_locator)
2 G& [1 q- U9 _2 R9 z$ i$ j8 K ax2.yaxis.set_major_locator(y_locator)
8 P% E! _( ^8 p1 ^4 z& g plt.ylim(0,100)' A5 R/ R( q5 j9 d% u1 u x
ax2.bar('sound', loud)2 I, i( v7 L& h4 @" P8 U
ax3 = plt.subplot(223)$ I8 N5 X6 d! J8 i) y! w U
ax3.xaxis.set_major_locator(x_locator)
* l K% e' [- e6 a1 K8 N( R f ax3.yaxis.set_major_locator(y_locator)2 Q' {. M5 M7 u! p5 Q
plt.xlim(0,100)
7 n7 m% g8 L# O4 ]0 ` plt.ylim(0,100)
5 D$ _& j' `! ` ~, M6 r plt.title('聲音、音量散點圖')# Z1 f- J) g5 v. \
ax3.scatter(loud_list,bri_list): P; y3 X1 W4 P1 [/ \6 J& ]
ax4 = plt.subplot(224)
# k7 T, e6 [1 F* O ax4.pie(size, labels = status, radius=1,wedgeprops = {'width': 0.3,'edgecolor': 'w'})
! E; Y0 H" s. ~0 |9 l1 \5 t plt.title('童芯派電量')4 k- j! c8 l. p8 Y
plt.pause(0.2)5 j4 _5 W, n% |4 w, g0 i' O
plt.clf()
P% c" d% e" n+ S* t, L. o if cyberpi.controller.is_press('a'):
- F: z% `# j* Q break! r; L5 P1 e7 y3 x
if len(bri_list) > 500:
0 ~0 y4 H3 B% P M; O& {4 V) R bri_list = []
* J5 E1 Z a7 y: P0 R loud_list = []
6 ~7 }3 y t$ [. a. d) S$ d* _+ ^# t- _0 \, z
|