(摘) ReSpeaker Pi的扩展主板

声明:内容源自网络,版权归原作者所有。若有侵权请在网页聊天中联系我

ReSpeaker 2-Mics Pi HAT是专为AI和语音应用设计的Raspberry Pi双麦克风扩展板。
这是购买了比较久的主板,把它安装到了PiZeroW上。我在等着Pi0 2W,毕竟第一代的性能还是太差,做移动应用还是差了些。不过2代贵得不值,等着估计明年中旬就有供货正常,价格正常了。
这里作一个资料备份而已。官方资料地址

该板是基于WM8960开发的低功耗立体声编解码器。 电路板两侧有两个麦克风采集声音,还提供3个APA102 RGB LED,1个用户按钮和2个板载Grove接口,用于扩展应用程序。 此外,3.5mm音频插孔或JST 2.0扬声器输出均可用于音频输出。

  1. 确保您正在您的Pi上运行最新的Raspbian操作系统(debian 9),您可以用etcher进行系统烧录
  2. 驱动下载并安装
sudo apt-get update
sudo apt-get upgrade
git clone https://github.com/respeaker/seeed-voicecard.git
cd seeed-voicecard #下载声卡驱动
sudo ./install.sh  --compat-kernel #安装声卡驱动
reboot  #重启
  1. 检查声卡名称是否与源代码seeed-voicecard相匹配.
aplay -l
arecord -l
  1. 录音播放测试 arecord -f cd -Dhw:1 | aplay -Dhw:1

  2. 调节音量(可选) alsamixer

  3. 控制APA102 LED的示例

- 输入: `sudo raspi-config`;
- 选择 "Interfacing Options";
- 选择 "SPI";
- 选择 “Yes”  
- 选择 “OK”
- 选择 “Finish”
  1. 运行LED示例
cd ~/
git clone https://github.com/respeaker/mic_hat.git
sudo pip install spidev #安装spi的驱动
cd mic_hat
python pixels.py
import apa102
import time
import threading
try:
    import queue as Queue
except ImportError:
    import Queue as Queue


class Pixels:
    PIXELS_N = 3

    def __init__(self):
        self.basis = [0] * 3 * self.PIXELS_N
        self.basis[0] = 2
        self.basis[3] = 1
        self.basis[4] = 1
        self.basis[7] = 2

        self.colors = [0] * 3 * self.PIXELS_N
        self.dev = apa102.APA102(num_led=self.PIXELS_N)

        self.next = threading.Event()
        self.queue = Queue.Queue()
        self.thread = threading.Thread(target=self._run)
        self.thread.daemon = True
        self.thread.start()

    def wakeup(self, direction=0):
        def f():
            self._wakeup(direction)

        self.next.set()
        self.queue.put(f)

    def listen(self):
        self.next.set()
        self.queue.put(self._listen)

    def think(self):
        self.next.set()
        self.queue.put(self._think)

    def speak(self):
        self.next.set()
        self.queue.put(self._speak)

    def off(self):
        self.next.set()
        self.queue.put(self._off)

    def _run(self):
        while True:
            func = self.queue.get()
            func()

    def _wakeup(self, direction=0):
        for i in range(1, 25):
            colors = [i * v for v in self.basis]
            self.write(colors)
            time.sleep(0.01)

        self.colors = colors

    def _listen(self):
        for i in range(1, 25):
            colors = [i * v for v in self.basis]
            self.write(colors)
            time.sleep(0.01)

        self.colors = colors

    def _think(self):
        colors = self.colors

        self.next.clear()
        while not self.next.is_set():
            colors = colors[3:] + colors[:3]
            self.write(colors)
            time.sleep(0.2)

        t = 0.1
        for i in range(0, 5):
            colors = colors[3:] + colors[:3]
            self.write([(v * (4 - i) / 4) for v in colors])
            time.sleep(t)
            t /= 2

        # time.sleep(0.5)

        self.colors = colors

    def _speak(self):
        colors = self.colors
        gradient = -1
        position = 24

        self.next.clear()
        while not self.next.is_set():
            position += gradient
            self.write([(v * position / 24) for v in colors])

            if position == 24 or position == 4:
                gradient = -gradient
                time.sleep(0.2)
            else:
                time.sleep(0.01)

        while position > 0:
            position -= 1
            self.write([(v * position / 24) for v in colors])
            time.sleep(0.01)

        # self._off()

    def _off(self):
        self.write([0] * 3 * self.PIXELS_N)

    def write(self, colors):
        for i in range(self.PIXELS_N):
            self.dev.set_pixel(i, int(colors[3*i]), int(colors[3*i + 1]), int(colors[3*i + 2]))

        self.dev.show()


pixels = Pixels()


if __name__ == '__main__':
    while True:

        try:
            pixels.wakeup()
            time.sleep(3)
            pixels.think()
            time.sleep(3)
            pixels.speak()
            time.sleep(3)
            pixels.off()
            time.sleep(3)
        except KeyboardInterrupt:
            break


    pixels.off()
    time.sleep(1)
  1. 使用用户自定义按钮
sudo pip install rpi.gpio    // install RPi.GPIO library
nano button.py               // copy the following code in button.py
import RPi.GPIO as GPIO
import time

BUTTON = 17

GPIO.setmode(GPIO.BCM)
GPIO.setup(BUTTON, GPIO.IN)

while True:
    state = GPIO.input(BUTTON)
    if state:
        print("off")
    else:
        print("on")
    time.sleep(1)