本教學譯自Makeblock官方教學文件,原文點[ 我] 本教學文件以windows作業系統環境為範例,Mac使用者可以參考結合實際情況進行操作。 快速教程:如果您已安裝了Python環境和第三方編輯器,請跳至第三步。或在您的編輯器配置的Python環境當中安裝pyserial和cyberpi程式庫即可實現童芯派第三方編輯器的程式設計。 pip install pyserial pip install cyberpi
; {, A9 J/ g6 T0 R( f1.安裝Python環境- 進入Python官方網站下載Python環境包,請根據自己的作業系統進行選擇。建議下載3.6-3.8的版本進行使用。如果下載最新的Python版本,可能帶來無法正常使用的問題。比如Python3.9以上的版本目前是不支援Windows 7作業系統的。' |+ q; u% \; j: }
Python Windows環境不同版本下載頁面:Windows Python下載 Python Mac OS 環境不同版本下載頁面: Mac OS Python下載
4 Y/ S/ s& Q! K) a/ b: r3 f% {- 按兩下安裝包進行安裝,注意勾選下方的Add Python 3.6 to PATH,點擊Install Now進行安裝,也可以點擊Customize installation將Python環境安裝至指定目錄下。
9 R, d+ i9 g6 f* B) ]: r/ s: e 2.下載安裝並配置協力廠商Python編輯器(以Visual Studio Code為例)提示:不同的編輯器在操作步驟上有所差異。同時建議在編輯器當中配置安裝在系統中的Python環境。 - 下載 VSCODE,請根據自己的作業系統進行選擇。 VSCODE下載地址
- 點擊下載下來的安裝包進行安裝。(根據提示進行安裝即可)
* ^; H/ u" @/ f2 K. ^2 ~
- 點擊桌面上Visual Studio Code的圖示運行程式。
- 在軟體介面左側找到下圖紅色方框當中的圖示並點擊。
. C- g# ~1 R/ I I- S: T2 y : z# o$ _! n% i0 ^- ~5 l
- 在彈出的介面的搜索框當中,搜索Chinese (Traditional) Language Pack for Visual Studio Code,並點擊進行安裝。重啟後可以實現Visual Studio Code介面切換為中文。注意該擴展外掛程式發佈者為Microsoft。(如您習慣在英文介面進行程式設計,可以跳過此步驟)! m' `1 N5 v& j; O
) k; s: s$ w4 |; v- 繼續搜索Python,安裝Visual Studio Code中的Python編輯環境。注意發佈者為Microsoft。
8 E8 X+ d2 g+ D/ t; C / X. F1 q/ O; K. `4 s6 \
- 在Visual Studio Code編輯器當中配置前面安裝的Python編輯環境。
- 新建資料夾並新建py尾碼的檔就可以對基礎的Python程式編寫。
& o% P) a; F# [: W7 S. G1 \ # `0 N% n* b9 X4 o/ ]- E
3.童芯派第三方編輯器程式設計準備4 V: k/ U' `4 Y. n
- windows環境下,按下鍵盤上的win+r鍵,輸入cmd並按下Enter,然後會彈出cmd視窗。) U! K% A- s2 H6 p; P
+ O8 g6 H) @+ ?3 M4 h- 安裝pyserial程式庫,pyserial程式庫可以確保童芯派通過USB線或藍牙Dongle與第三方編輯器建立連接。 I4 ]- k3 ~1 N' L% F
在cmd視窗中輸入pip install pyserial 並按下Enter,等待安裝完畢。 5 p% I/ f0 d; \8 Z/ A
- 安裝cyberpi程式庫,cyberpi程式庫是用於對童芯派進行線上程式設計的Python程式庫。( K/ z/ X. W* \2 n* M' j- S) O7 k
在cmd視窗中輸入pip install cyberpi 並按下Enter,等待程式庫安裝完畢即可。
% Z) g, b! M& q
/ K& K8 T3 W- y3 P+ V9 o) L- 完成安裝後,您就可以在配置了Python環境的編輯器當中進行程式編寫了。4 O! `1 z5 z5 _9 U" I/ D
. g$ H0 u2 V' f) X+ j
4.注意事項 在第三方編輯器當中目前僅支持對童芯派進行線上模式程式設計。 6 T( B& |# b: K9 G, C
5.一些程式設計案例% O! c- ~$ b6 [/ q# |: g# q" A" @
童芯派滑鼠 需要安裝pynput模組,參照上面的步驟pip install pynput
, M d2 u( A- {: u6 }- j功能介紹:
3 m# y# I. a; p4 T8 R利用童芯派的陀螺儀模組和按鍵結合pynput模組的滑鼠控制功能,實現滑鼠的功能。
/ z) H1 @% h# E# D& Z通過陀螺儀控制滑鼠移動。通過按鍵實現滑鼠按鍵功能。
. {% D J& r# o結合藍牙模組可以實現無線滑鼠的功能。/ f" W/ g4 U! h3 Y" a
( L5 A( [% f0 O) s5 A2 u( y
from pynput.mouse import Button,Controller
5 X2 I }3 v: }& X7 G: j Simport cyberpi: l9 Y8 D& L4 g! z/ u: F l
import time
! c2 M4 J, C& E6 q% q! ^( R" K
$ l- U1 [+ W, ?2 A" qmouse = Controller() r2 g ?5 ?5 k, E
7 X& |5 {6 K7 n% qwhile True:
$ m$ u! d3 b: {& m if cyberpi.is_tiltback():5 @" k3 G$ V" \6 Y
mouse.move(-3, 0)9 R$ Y$ d5 b* {" f- v1 r! y; |8 {' e
print(mouse.position)6 W1 z! b, C) I+ t Z$ f
if cyberpi.is_tiltforward():3 t% Z+ K, A. I
mouse.move(3, 0)
/ Z0 r7 K& S3 x! ?7 t5 [8 p( c if cyberpi.is_tiltleft():
6 ^7 p$ w O, t5 A0 y mouse.move(0, -3)
4 n* \' N. [8 Y if cyberpi.is_tiltright():
' B. ]$ T2 W& u/ t" O6 u! B/ T: T: { mouse.move(0, 3). Y3 J% v0 D1 @0 z9 A3 `- r
if cyberpi.controller.is_press("b"):
7 S9 o( j6 e. r$ t3 z- ^! K0 m mouse.press(Button.left)& s- S% D6 _ c/ {' U5 ^
mouse.release(Button.left)
" Q! [& W5 J- X/ o S mouse.press(Button.left)
! O, B- h9 b W2 D9 ~- W. a$ { mouse.release(Button.left), ^ v" P2 o2 b
if cyberpi.controller.is_press("a"):
& l" t) O6 J! U" C0 E mouse.press(Button.right)
4 }3 V5 @ v h, a- [5 U mouse.release(Button.right)
3 f8 H# F' k( F9 C time.sleep(0.01)
7 k/ T/ w, X3 t% B7 T& u. B# M2 X
5 B) z% Z/ X% |+ B8 R8 c
語音打字器 5 L( @2 _" k0 v5 U+ l; i
功能介紹:
$ s, i1 n# P& L7 B利用童芯派的語音辨識功能結合pynput模組的鍵盤控制功能。
# X( R/ i+ G7 {! w- n將語音辨識的結果通過pynput在文檔中打出來。
" G9 M3 M3 V- R$ n, u該程式目前只能支援英文。8 u1 [6 e* s% s( F' N: Q% v8 M j
, k4 x/ G) B" ]' {. Fimport cyberpi3 _$ i8 \2 i, g; F, v+ P
from pynput.keyboard import Key,Controller
) `- k9 X3 ?, H2 \import time
4 ` V/ O; _) \8 @7 F7 G) [- z
1 ?* A+ k( O+ {. U9 H9 gkeyboard = Controller()5 d: A. f/ v# S1 B. I
cyberpi.console.clear(). `- q0 h# \& @# S1 w" \& O
cyberpi.led.on(0, 0, 0)2 M9 d) G2 B( ^/ i) |
cyberpi.set_recognition_url()
: g. Z. \$ q% I2 @6 b gcyberpi.cloud.setkey("請輸入雲服務授權碼") # 通過慧程式設計帳號可以獲得
+ d' t# B1 ~! Jcyberpi.wifi.connect("WIFI名稱", "WIFI密碼")1 L8 i+ ?, \6 T% j5 E
while not cyberpi.wifi.is_connect():" @# L O- o4 N
pass4 i8 X7 d3 \) G' t
cyberpi.led.on(0,0,255)% g( V& K) c& A: T3 g
cyberpi.console.println("WIFI已連接")
5 } Z1 j2 w1 T3 w2 M: ccyberpi.console.println("--------------")
5 J a# r( @5 X. }; j' W0 jcyberpi.console.println("按下A鍵開始語音辨識")* K: d" t0 r. K, Y
while True:6 B: b) s' g) }8 W! i3 `$ f' z3 u
if cyberpi.controller.is_press('a'):9 D# L0 ~' r, O0 U$ a5 b& c
keyboard.press(Key.space)
: m7 X* X+ u" O* F cyberpi.console.clear()
- ]0 `* x( a! R# Q S+ D, ^ cyberpi.led.on(100, 0, 0)* g- O5 ?, H( d" a: y0 V% I0 y
cyberpi.console.println("開始語音辨識")
6 `1 J( _9 I6 N1 y5 V/ G cyberpi.audio.play("switch")
1 P3 }' k0 _9 ]1 C. p2 m+ r cyberpi.console.println("--------------")
' z2 J, P1 }* l! C+ N6 E' V cyberpi.cloud.listen("english", 2)0 E" t0 ]1 @/ m. `3 [
cyberpi.led.on(0, 0, 0)# {$ \) d& [& R6 U& n, B
say = cyberpi.cloud.listen_result()' D9 ~* {2 ~( e K Y0 _
cyberpi.console.println(say)
. F9 Q! m! r8 O' v+ z9 @% O cyberpi.console.println("--------------")
1 L# t# J2 l/ s: m for i in say:
9 e! Q& d; f2 S) B e! @4 R& K if i == '':7 C) B- i/ n9 H, T
keyboard.press(' ')8 W) d0 G4 H: k" k3 k/ f
else:
- Y; v& C$ I9 f+ k4 E keyboard.press(str(i))* M9 h' y* H/ \* [2 b' I8 E
time.sleep(0.03)8 a- `) F) i: L" G9 t" M
" v' G+ Q' I1 E5 h# f. A x
matplotlib音量柱狀圖
2 W+ X3 z# o( }2 S- P% `# Limport cyberpi4 ~1 v$ ]0 C( Q( U
import time2 W2 y0 t% l* L. B* l
from matplotlib import pyplot as plt
! H5 P. q) u3 j* R
) c$ O7 V! s. |/ F" y: bplt.rcParams['font.sans-serif'] =['Microsoft YaHei']
. k i z5 S- g( zplt.ion()$ x' H+ `9 u3 {
, J0 b6 J' |# Z+ q, Z
while True:
: g o1 E) b$ ` loud = cyberpi.get_loudness()
" V8 t/ ?! S, n6 ~ plt.clf()
& ^ z. R5 _( v9 G1 g9 o/ t plt.xlabel("數據名稱")
. p5 E9 y) Y/ | plt.ylabel("音量讀值"), P% o7 c3 [# x+ P: j1 d7 C
y_locator = plt.MultipleLocator(5)) i$ E# r; ~/ |" Q0 R. l
ax=plt.gca()8 o# _! }. }, A" Q' l3 Q7 C7 r4 ^
ax.yaxis.set_major_locator(y_locator)
1 a7 z5 c1 e4 ` plt.ylim(0,100)
6 Z# ?# C8 O; Z2 y0 H/ T plt.bar('音量', loud, align='center',label=f'溫度{loud}')# H3 |6 p& o/ n6 l
plt.title('童芯派聲音感測器讀值')
2 a x6 z5 ]* G3 j M' J plt.legend()
/ f$ t7 O6 }: U8 K plt.show()) Z k" n: f/ U) r* _- o& p$ b/ ~
plt.pause(0.01) U$ M1 C2 l0 |7 ?
+ }) f$ g& `- P7 q. `2 ?
% a3 x& N; m( n
matplotlib 多圖表顯示5 S7 q6 e; U+ Y L Z3 h
. G: q& Z2 k6 e# v& yfrom matplotlib import pyplot as plt
1 n" Q6 D0 i$ L+ \+ Y$ Y z7 kimport time' ]: g. I% w3 c$ M
import cyberpi
( b! U k3 w2 j" @, @! m& Y ) A6 I9 u( b0 S3 R4 r, f1 B
loud_list = []
) ^5 R' d% g/ Z/ t5 n" abri_list = []
7 z8 ]9 D) ?# _& w/ @ L0 Rplt.rcParams['font.sans-serif'] =['Microsoft YaHei'] # 載入中文字體,這裡載入的是電腦的自帶的微軟雅黑字體6 i9 D4 z' s7 Y" } B
plt.ion()$ t+ u/ m' U' F6 G" f( \; {* a0 ?
plt.figure(figsize=(10,10))
' ]1 U! @7 H4 s9 p/ R7 n. w( n4 Xplt.figure(1)
7 r: | L: k# N4 X9 lx_locator = plt.MultipleLocator(5)8 x; n8 T5 }0 y5 h% x" f2 e
y_locator = plt.MultipleLocator(5)- d$ j7 O7 i5 L" J7 A5 ^
while True:2 ?& |* g7 N6 O( Q7 ?7 n" U
loud = cyberpi.get_loudness()) s- Q% r; V. v6 {( a
bri = cyberpi.get_bri()" n. X6 i: y# Z* Q) o$ M
loud_list.append(loud)+ E- c6 w% a: L+ p& @! z2 s/ t
bri_list.append(bri)1 J8 L7 d8 S- B9 S7 d) S9 M0 g
battery = cyberpi.get_battery()
; T# g9 |0 z% K r size = [battery, 100-battery]8 d4 O6 \6 c( Q/ @- ~) O
status = [f'剩餘電量:{battery}%', f'已用電量:{100-battery}%']
3 U( c7 ?% g' a8 \) Z ax1 = plt.subplot(221)
: `# x3 j' y4 ^ u& C6 H6 { plt.title('光線折線圖')9 R3 a5 p/ N! B" `, L! g% f. `
ax1.plot(bri_list)
, b4 ~. S6 _9 n$ H ax2 = plt.subplot(222)) R* h, z. M7 Q/ v
plt.title('聲音柱狀圖')2 q Z8 J- n0 \* i% _# O! z5 X8 h# ~
ax2.xaxis.set_major_locator(x_locator)
. G9 V4 Z; j+ T ax2.yaxis.set_major_locator(y_locator)+ Y' y' q3 S6 l" Q8 v1 H7 Y
plt.ylim(0,100)
3 u5 i- _7 H* H0 F ax2.bar('sound', loud)8 e, }1 i& G$ Z+ B. o7 g
ax3 = plt.subplot(223)
" j. o" q, z9 X! j; n ax3.xaxis.set_major_locator(x_locator)3 |) [. b6 ], m3 E
ax3.yaxis.set_major_locator(y_locator)& l5 Z# G" G, y3 O) m" ~* l
plt.xlim(0,100)
1 ~9 l4 }# n! n: c! y# @ w! x- ]1 v plt.ylim(0,100)
& Q/ w* a" A- Y j, H6 l4 ]4 C% { plt.title('聲音、音量散點圖')# y" f. M% m' {1 w3 I( `
ax3.scatter(loud_list,bri_list)
+ {- h0 r. d3 O& m2 v ax4 = plt.subplot(224)9 t& K. q% D x0 V$ g& _
ax4.pie(size, labels = status, radius=1,wedgeprops = {'width': 0.3,'edgecolor': 'w'})
- |$ S6 e3 t6 q6 x- L% e plt.title('童芯派電量')4 S5 n6 `2 w! A
plt.pause(0.2)
4 J, c, O- w- u- T; X plt.clf()
! W8 L5 G0 H c* T% W if cyberpi.controller.is_press('a'): B9 N; W$ O* C U8 c1 N
break
# R6 G" ^2 F3 f% h' u2 R) K6 i if len(bri_list) > 500:
' j: s6 G/ J8 B8 i, N7 ^: ` bri_list = []
4 { C+ m( {+ [8 r loud_list = []
) u2 W: q7 r" u
* E {9 F# k) s- H% C# J8 J7 q |