本教學譯自Makeblock官方教學文件,原文點[ 我] 本教學文件以windows作業系統環境為範例,Mac使用者可以參考結合實際情況進行操作。 快速教程:如果您已安裝了Python環境和第三方編輯器,請跳至第三步。或在您的編輯器配置的Python環境當中安裝pyserial和cyberpi程式庫即可實現童芯派第三方編輯器的程式設計。 pip install pyserial pip install cyberpi
1 g7 h: y7 p( V& s" C1 p1.安裝Python環境- 進入Python官方網站下載Python環境包,請根據自己的作業系統進行選擇。建議下載3.6-3.8的版本進行使用。如果下載最新的Python版本,可能帶來無法正常使用的問題。比如Python3.9以上的版本目前是不支援Windows 7作業系統的。
( G+ ~7 }0 S- @. i7 A; B3 P
Python Windows環境不同版本下載頁面:Windows Python下載 Python Mac OS 環境不同版本下載頁面: Mac OS Python下載 2 }2 S+ T) G! S! o
- 按兩下安裝包進行安裝,注意勾選下方的Add Python 3.6 to PATH,點擊Install Now進行安裝,也可以點擊Customize installation將Python環境安裝至指定目錄下。* O' I9 r* V- U
2.下載安裝並配置協力廠商Python編輯器(以Visual Studio Code為例)提示:不同的編輯器在操作步驟上有所差異。同時建議在編輯器當中配置安裝在系統中的Python環境。 - 下載 VSCODE,請根據自己的作業系統進行選擇。 VSCODE下載地址
- 點擊下載下來的安裝包進行安裝。(根據提示進行安裝即可)* z E2 ?% G. V1 Q, j0 `# P' b
- 點擊桌面上Visual Studio Code的圖示運行程式。
- 在軟體介面左側找到下圖紅色方框當中的圖示並點擊。9 N) H5 B% Z9 a' b
; J) ^# [! y" r1 C8 o2 o
- 在彈出的介面的搜索框當中,搜索Chinese (Traditional) Language Pack for Visual Studio Code,並點擊進行安裝。重啟後可以實現Visual Studio Code介面切換為中文。注意該擴展外掛程式發佈者為Microsoft。(如您習慣在英文介面進行程式設計,可以跳過此步驟)" u; ^6 O- V- B) x. L0 g# Q
. J8 s8 |9 ]0 p" ^# @
- 繼續搜索Python,安裝Visual Studio Code中的Python編輯環境。注意發佈者為Microsoft。
; f7 _8 Z& X! L2 z i' x
( M+ C3 B! M; Y! u$ @' }- z- 在Visual Studio Code編輯器當中配置前面安裝的Python編輯環境。
- 新建資料夾並新建py尾碼的檔就可以對基礎的Python程式編寫。! H5 z# h* j6 Y5 v% S$ j
% _' T6 }2 a2 z4 u/ s' f" w2 J3 g1 l
3.童芯派第三方編輯器程式設計準備
$ q# s1 r$ w( s8 m; |' \. `- windows環境下,按下鍵盤上的win+r鍵,輸入cmd並按下Enter,然後會彈出cmd視窗。
+ ~: ~2 @, }; D! U
: |7 [( `7 u4 m/ `; f- 安裝pyserial程式庫,pyserial程式庫可以確保童芯派通過USB線或藍牙Dongle與第三方編輯器建立連接。
8 |9 V) Z+ N I5 ~$ z
在cmd視窗中輸入pip install pyserial 並按下Enter,等待安裝完畢。 0 A- q/ ~: F2 h
- 安裝cyberpi程式庫,cyberpi程式庫是用於對童芯派進行線上程式設計的Python程式庫。2 C8 G0 b: n( W3 d7 l
在cmd視窗中輸入pip install cyberpi 並按下Enter,等待程式庫安裝完畢即可。 & n; ~* E9 U7 V
9 I8 m- W9 @0 U, O- 完成安裝後,您就可以在配置了Python環境的編輯器當中進行程式編寫了。9 }( _5 U8 i' r' u- Z' h
$ _ O4 Y8 J/ b+ i0 c
4.注意事項 在第三方編輯器當中目前僅支持對童芯派進行線上模式程式設計。 $ G/ F: d5 m5 l/ n" g/ U0 @
5.一些程式設計案例
1 F+ V/ W& K% Z, S# `# ~! A! \童芯派滑鼠 需要安裝pynput模組,參照上面的步驟pip install pynput 7 f, N8 ]2 h& C( P, c8 T" m1 g
功能介紹:
M3 o3 x W& N5 ]) S2 r) |! V% ?5 X利用童芯派的陀螺儀模組和按鍵結合pynput模組的滑鼠控制功能,實現滑鼠的功能。
% B0 \2 T6 l' @通過陀螺儀控制滑鼠移動。通過按鍵實現滑鼠按鍵功能。6 @8 |" \ C2 @% J8 ~1 T& z
結合藍牙模組可以實現無線滑鼠的功能。
/ d, D( h# V! J6 \5 n' p& A4 h% | [9 U
from pynput.mouse import Button,Controller
1 \3 [# J! _5 h1 ~import cyberpi# _+ N1 `. B: A, G* S- |
import time
" M' R" X& t. C$ k+ [7 ]4 p, m
' Z) N v% Q+ g* i1 b& \mouse = Controller()
6 V/ j0 X% F1 O6 m" C ( o- j) J1 G6 r5 a1 H8 |
while True:
2 v0 ~0 N, H( S! n1 {7 I9 ^ if cyberpi.is_tiltback():
! r, a: t8 q, I: q( t% d) y mouse.move(-3, 0)
- b- q! p$ \% Q/ V+ f print(mouse.position)
+ j! F# c3 `( j if cyberpi.is_tiltforward():
. [# O" g& P _( J mouse.move(3, 0)
" y2 a1 |; g* |, f8 h& T if cyberpi.is_tiltleft():0 C( H- T2 O# }' p, `% U9 D
mouse.move(0, -3)7 H" y# G1 T. s6 s/ `1 L, B
if cyberpi.is_tiltright():) `% t- q n; N W6 J
mouse.move(0, 3)2 c( d4 R" x' ~5 a3 s' C& |
if cyberpi.controller.is_press("b"):* L$ [0 P$ ?7 s- n- e3 t1 y
mouse.press(Button.left)" c- f8 e2 X5 I! p& }
mouse.release(Button.left)
& p) _! T$ m+ C8 J# o" ~' X mouse.press(Button.left)
; M2 g1 S9 C, f' u! S4 g7 V A mouse.release(Button.left)
3 [" |( C+ M0 A& } if cyberpi.controller.is_press("a"):. y3 t5 {' Y0 f1 a5 O% P
mouse.press(Button.right): v7 E% M$ E' M5 N4 q( U4 z/ P, ]
mouse.release(Button.right)/ T# e+ i4 w6 X5 B o
time.sleep(0.01)
7 D) ^# y% C5 C8 \7 m( B' S) A4 t
0 T# j6 E9 H4 M& _0 ?, {0 }. a! r6 L9 R% j7 a2 t" X
語音打字器 ! C7 X/ h4 ^5 V
功能介紹:
% p" c9 y0 i l7 z利用童芯派的語音辨識功能結合pynput模組的鍵盤控制功能。
b9 Y! S0 B0 P將語音辨識的結果通過pynput在文檔中打出來。0 @; J" ^6 r5 I d) T" N
該程式目前只能支援英文。# @0 H( n. S2 O: E3 k1 e
8 S$ p6 { v4 B$ L ?
import cyberpi
. h. n* {' J0 Z: o$ v/ hfrom pynput.keyboard import Key,Controller
$ |% C7 p( x7 q% A5 R; L" j4 ~import time$ u6 N/ f% e# C7 v% p* g
; K/ L) V9 M* h8 Bkeyboard = Controller()1 P: K7 t- t( H x& `3 \+ q8 `
cyberpi.console.clear()0 o2 k9 B& x+ s J+ l
cyberpi.led.on(0, 0, 0)
* s5 t, ]6 t$ k1 v, x F3 Pcyberpi.set_recognition_url()
" z+ T2 w9 l- Pcyberpi.cloud.setkey("請輸入雲服務授權碼") # 通過慧程式設計帳號可以獲得
3 j1 e1 N3 c$ A2 s: _cyberpi.wifi.connect("WIFI名稱", "WIFI密碼")
( y( c9 D2 i1 Awhile not cyberpi.wifi.is_connect():4 b( ] ]9 D# P% Z% a$ d# h# x
pass( z3 o# G% N. ^
cyberpi.led.on(0,0,255)- ^7 {9 |$ q# g1 e
cyberpi.console.println("WIFI已連接")
3 v: i9 }: f6 w2 lcyberpi.console.println("--------------")5 _4 Z+ {0 r" t: t" G" a: ^
cyberpi.console.println("按下A鍵開始語音辨識")
9 U6 B2 l4 a# }while True:& C7 ^2 h0 @ R1 c5 n
if cyberpi.controller.is_press('a'):& ?- k* {8 }2 n% K/ Q' ?) Z
keyboard.press(Key.space)
- F* r( n8 G5 w: E% B6 Q T cyberpi.console.clear()
2 ~' ] V# F& a; b. g6 w/ Y cyberpi.led.on(100, 0, 0)1 w; ]- d+ J. T, W2 R
cyberpi.console.println("開始語音辨識"). a& K" M [$ ~2 {+ L3 E! x
cyberpi.audio.play("switch")
" Y5 r a) k S# V( `5 f cyberpi.console.println("--------------")4 n1 Q, l, }1 U! x( H4 L: I
cyberpi.cloud.listen("english", 2)$ O% g8 V! a/ S, o1 N
cyberpi.led.on(0, 0, 0) P+ T/ c/ |: K5 ?: ?
say = cyberpi.cloud.listen_result()' \3 s" w4 V& Q$ j5 M* U
cyberpi.console.println(say)
9 }: \8 |# j, g5 U& l cyberpi.console.println("--------------")6 z3 {9 }7 T- e" F4 }
for i in say:! `" {9 }! c- Z+ a* h8 J
if i == '':9 l) W O% S/ F- i/ c
keyboard.press(' ')4 Q# m; k) b2 u3 g: Q
else:
8 E, z7 ]1 d. y$ ~9 t keyboard.press(str(i))
" ^( D8 ~0 f" e8 X3 x" P% Z time.sleep(0.03)
1 ^. n# t" @: e4 {
' f' l5 H* h! |, K0 t* Umatplotlib音量柱狀圖
, H! _0 b. [* simport cyberpi( C3 A2 e; ?% s) K6 ]8 d
import time9 R, ~4 ^% \- X+ ]- C
from matplotlib import pyplot as plt
8 D% W! E; u2 g8 E2 C; S% ` ?
7 x, D) W( {' ^) J3 ~* hplt.rcParams['font.sans-serif'] =['Microsoft YaHei']
* r0 y4 h7 T- \* J( o! g- I. lplt.ion(); J9 B4 U9 m; Y3 s
9 S7 Z0 _, b a+ T V; o* ]while True:
% n6 _7 ^* ^6 j' e loud = cyberpi.get_loudness()$ U% U/ a C, u) Y7 L
plt.clf()( I( p2 _1 n- T# K) J+ N& {1 q
plt.xlabel("數據名稱")
' Q; A _; b7 P/ m) H# y plt.ylabel("音量讀值")) i- Q4 A6 q$ a' z
y_locator = plt.MultipleLocator(5)
0 E5 j* q I/ w; _) A, U* e ax=plt.gca()
3 j# Z, P3 F- y, e, g ax.yaxis.set_major_locator(y_locator)) V, o) W3 s$ n4 C: O; _( S' k* B' A2 M
plt.ylim(0,100)
- C. X. h8 C8 _; k: ~, @; J plt.bar('音量', loud, align='center',label=f'溫度{loud}')
0 x$ w7 y- U, v" ?$ | plt.title('童芯派聲音感測器讀值')* O. l' O" [4 n$ s
plt.legend()
3 e: u1 y& E- i plt.show()( n! \* ^5 S0 L$ i
plt.pause(0.01)
* Z4 X" p) }* t0 P. s7 U$ D/ t# F: r
( o; t, n( w/ Z5 r: Fmatplotlib 多圖表顯示
) i2 Z6 L F# m2 y# i. a; U' ^: k
) C1 C7 g" U. u+ U9 B6 o3 ]+ Ofrom matplotlib import pyplot as plt; ], ~. V0 L, e
import time; d7 ]! e2 e' a
import cyberpi
0 v1 O3 y$ g% O; Q/ [6 b6 q " f1 n9 L/ d: W+ {2 \4 R5 j3 L
loud_list = []5 y) @; I1 R! x# z) Z2 g& k( j. B
bri_list = []
; B+ t. _: A$ Z9 l' V" y; Q- f/ Gplt.rcParams['font.sans-serif'] =['Microsoft YaHei'] # 載入中文字體,這裡載入的是電腦的自帶的微軟雅黑字體9 Y8 V7 j. x4 T) s/ X
plt.ion()) }5 R9 O+ I; |1 p9 m7 |0 N
plt.figure(figsize=(10,10))
# y, N( |1 \6 r% v! Aplt.figure(1)
9 c# q1 U0 u) c9 S( T: }8 jx_locator = plt.MultipleLocator(5)
9 Y7 X2 `8 e {, Hy_locator = plt.MultipleLocator(5)" r9 f$ C. K+ g9 C! b& h* v; Z
while True:; M/ z% S5 n, S) ?2 D Z4 U
loud = cyberpi.get_loudness()
2 R: i+ f1 ?5 d% z& W* O) W bri = cyberpi.get_bri()
@/ ^) v3 V! }2 I' P loud_list.append(loud)- o9 N1 o5 T) Q2 T- h# I! X( e
bri_list.append(bri)
- b. y) b% _9 Q5 D# |9 l4 L7 Y. b battery = cyberpi.get_battery()
2 J! [$ J/ }7 ^' W1 f size = [battery, 100-battery]3 T3 z. H) f5 ^# r. R* l
status = [f'剩餘電量:{battery}%', f'已用電量:{100-battery}%']
) p- C0 S" b; p2 ~$ O8 x( e ax1 = plt.subplot(221)! J6 A: W, X E
plt.title('光線折線圖')0 J: A8 u2 d* v: D3 K" p
ax1.plot(bri_list)( B9 R# d! B( H% I, _
ax2 = plt.subplot(222)
- ~2 Y F( p) }) r' i plt.title('聲音柱狀圖')
# W/ _7 v' {- N$ B ax2.xaxis.set_major_locator(x_locator)0 [2 a/ k4 G2 Y/ h9 J4 M* E$ }
ax2.yaxis.set_major_locator(y_locator)
0 }+ k( c; ^5 K" _- F4 V" X plt.ylim(0,100)
) U% I: R4 F9 s) H# m- O8 @ ax2.bar('sound', loud)
# |& n( ?, S# Z* D ax3 = plt.subplot(223)
) ` W+ R: d. s% z ax3.xaxis.set_major_locator(x_locator)
: j- I4 a/ j% O) T9 Y ax3.yaxis.set_major_locator(y_locator); G8 t- p+ {+ K
plt.xlim(0,100)- r" i! q6 f( H$ d, G6 i
plt.ylim(0,100)' \: Y) Z! w! l: \8 r( }! l
plt.title('聲音、音量散點圖')- V7 D) I8 s7 l9 g- J9 M6 \' E
ax3.scatter(loud_list,bri_list), f1 A2 J9 ^. m% e9 H) |+ s; r
ax4 = plt.subplot(224)) ^% E- {4 H2 u5 H$ z9 m( g: t
ax4.pie(size, labels = status, radius=1,wedgeprops = {'width': 0.3,'edgecolor': 'w'})1 u2 k- z: z& t B# T% l
plt.title('童芯派電量')6 M2 Q$ V) z- K1 B5 N8 S! B i# n7 ?
plt.pause(0.2)
1 I5 f5 H9 h4 q j plt.clf()" f6 |( s# v; p5 t& J4 S5 m7 I1 Y
if cyberpi.controller.is_press('a'):
" j3 a z$ x; u1 o5 i break
% v3 S+ \9 c9 X0 W. Y( n* n n if len(bri_list) > 500:& z8 j* R8 D# C/ K5 ?; f' w
bri_list = []
( Q3 K" M# X3 ]! U loud_list = []* L6 `/ q9 R& q8 ?
& e6 f( A1 E9 y# q7 Y& H1 y. r- K |