Raspberry Pi 樹莓派 安裝 與 使用
Raspberry Pi 樹莓派,2012年12月買了 Raspberry Pi 2 ,也用了很多年了,
差不多就是個小電腦,滿好用的,適合 24小時開機,
執行相對簡單而想要一直執行的程式功能。 好處是省電,又比電腦便宜。
後來又陸續買了 Raspberry Pi 3B, Raspberry Pi 3B+, Raspberry Pi 4B,
最近新款是 Raspberry Pi 4B, 記憶體有分 2G, 4G, 與 8G 三款。
可惜 2022, 2023年以後漲價,變的很貴,
以前 Raspberry Pi 4B 大概三千多元,最近漲到四至七千元,實在買不下去,
原則上,規格需求低的,就改 Banana Pi, Orange Pi, MapleBoard,
如果規格需求較高的,就改用 小電腦,小 PC 吧。
Raspberry Pi 樹莓派 硬體 與 軟體
Raspberry Pi 樹莓派 硬體
Raspberry Pi 樹莓派 軟體 (作業系統)
Raspberry Pi 樹莓派 基金會
接腳圖
欣賞一下硬體,越做越好,越來越好用了。
最近買了 Raspberry Pi 4B, 裝壓克力盒子
原則上一定要用風扇,不然夏天容易熱當機!
到 Raspberry Pi 官網,下載 OS Imager
將作業系統寫入 SD 卡裡面,現在常用 32GB 以上的 SD 卡
作業系統寫入 SD 卡之後,插入 Raspberry Pi 就可以開機了
啟用 ssh 與 vnc,方便以後連線操作:
設好螢幕解析度
我個人常用 mode 35 1280x1024
桌面右上角,按右鍵可以設定音效
用 ALSA 指令,放音或錄音
可以內建 default audio device bcm2835 ALSA 的音效。
如果用 USB sound card 音效卡,ALSA 指令包括: aplay, arecord, amixer, alsamixer, alsaloop, alsactl, speaker-test, etc.
Test USB Audio Device
speaker-test -Dplughw:CARD=Device -c2 -twav
參數 -D option 選擇 plughw:CARD=Device,
參數 -c2 is the left and right channel, 選擇左右聲道
參數 -twav selects to play the .wav sound file. 選擇 wav 檔
指令 arecord for audio capture, 可錄音
指令 aplay to capture audio playback. 可播音
arecord -Dplughw:CARD=Device -fcd -c2 -twav test.wav
aplay -Dplughw:CARD=Device test.wav
參數 -f option sets the audio format, 是音效檔格式
** 安裝 pygame 支援 音效卡 **
有些版本的 Raspberry Pi 無內建 pygame 需要安裝
sudo apt-get update
sudo apt-get install python3-pygame
Python 程式中使用 pygame 播放音效檔
import pygame
pygame.mixer.init()
pygame.mixer.music.load("myFile.wav")
pygame.mixer.music.play()
while pygame.mixer.music.get_busy() == True:
continue
發現滿好用的!
(不用) * 安裝 PyAudio 支援 音效卡 較早的硬體如 3B,可能播放音效會斷續!
說明網頁
"""PyAudio Example: Play a wave file."""
import pyaudio
import wave
import sys
CHUNK = 1024
if len(sys.argv) < 2:
print("Plays a wave file.\n\nUsage: %s filename.wav" % sys.argv[0])
sys.exit(-1)
wf = wave.open(sys.argv[1], 'rb')
# instantiate PyAudio (1)
p = pyaudio.PyAudio()
# open stream (2)
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)
# read data
data = wf.readframes(CHUNK)
# play stream (3)
while len(data) > 0:
stream.write(data)
data = wf.readframes(CHUNK)
# stop stream (4)
stream.stop_stream()
stream.close()
# close PyAudio (5)
p.terminate()
以上為 python 範例
某些較早的硬體軟體版本,有可能播放音效會斷續,若要移除
Uninstall PyAudio
How to uninstall or remove python-pyaudio software package
** 有關 time 的用法 **
import time
time.sleep(1) # 暫停 1 秒鐘
time.strftime('%X')
time.strftime() result: 可以輸出指定的日期或時間字串:
Directive | Meaning | Notes |
|---|
%a
| Locale’s abbreviated weekday name. | |
%A
| Locale’s full weekday name. | |
%b
| Locale’s abbreviated month name. | |
%B
| Locale’s full month name. | |
%c
| Locale’s appropriate date and time representation. | |
%d
| Day of the month as a decimal number [01,31]. | |
%H
| Hour (24-hour clock) as a decimal number [00,23]. | |
%I
| Hour (12-hour clock) as a decimal number [01,12]. | |
%j
| Day of the year as a decimal number [001,366]. | |
%m
| Month as a decimal number [01,12]. | |
%M
| Minute as a decimal number [00,59]. | |
%p
| Locale’s equivalent of either AM or PM. | (1) |
%S
| Second as a decimal number [00,61]. | (2) |
%U
| Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0. | (3) |
%w
| Weekday as a decimal number [0(Sunday),6]. | |
%W
| Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0. | (3) |
%x
| Locale’s appropriate date representation. | |
%X
| Locale’s appropriate time representation. | |
%y
| Year without century as a decimal number [00,99]. | |
%Y
| Year with century as a decimal number. | |
%z
| Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59]. | |
%Z
| Time zone name (no characters if no time zone exists). | |
%%
| A literal '%' character. | |
範例:
>>> from time import gmtime, strftime
>>> strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
'Thu, 28 Jun 2001 14:17:15 +0000'
2021-07-17
2023-01-23