AIY Voice Kit
スピーカー
設定
- /boot/firmware/config.txt
dtoverlay=googlevoicehat-soundcard
確認
aplay -l
wpctl status -n
pactl list short sinks
pactl list cards
再生
speaker-test -D plughw:2,0 -c 1 -t wav
wpctl set-volume 67 20% # wpctl status -n
paplay --device=alsa_output.platform-soc_sound.stereo-fallback /usr/share/sounds/alsa/Front_Center.wav
aplay -D plughw:2,0 /usr/share/sounds/alsa/Front_Center.wav # 音量が調節できず、非常に大きい
録音
arecord -D hw:2,0 --dump-hw-params
arecord -D plughw:2,0 -f S32_LE -r 48000 -c 2 rec.wav
parecord --device=alsa_input.platform-soc_sound.stereo-fallback --file-format=wav --rate=16000 --channels=1 rec.wav
sox rec.wav -n stat
sox rec.wav rec-normalized.wav gain -n
paplay --device=alsa_output.platform-soc_sound.stereo-fallback rec-normalized.wav
LED付きボタン
LED
python
#!/usr/bin/python3
from gpiozero import LED
from time import sleep
led = LED(25)
try:
for i in range(3):
led.on()
print("LED on")
sleep(1)
led.off()
print("LED off")
sleep(1)
except KeyboardInterrupt:
pass
finally:
led.off()
print("LED off")
led.close()
C
#include <lgpio.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#define LED_PIN 25
static volatile sig_atomic_t running = 1;
static void handle_sigint(int sig)
{
(void)sig;
running = 0;
}
int main(void)
{
int h;
int result = EXIT_FAILURE;
signal(SIGINT, handle_sigint);
/* GPIOチップを開く */
h = lgGpiochipOpen(0);
if (h < 0) {
fprintf(stderr, "GPIO chip open error: %d\n", h);
return EXIT_FAILURE;
}
/* GPIO 25を出力に設定し、初期状態をLOWにする */
if (lgGpioClaimOutput(h, 0, LED_PIN, 0) < 0) {
fprintf(stderr, "GPIO output setup error\n");
lgGpiochipClose(h);
return EXIT_FAILURE;
}
result = EXIT_SUCCESS;
for (int i = 0; i < 3 && running; i++) {
if (lgGpioWrite(h, LED_PIN, 1) < 0) {
result = EXIT_FAILURE;
break;
}
printf("LED on\n");
fflush(stdout);
lguSleep(1);
if (lgGpioWrite(h, LED_PIN, 0) < 0) {
result = EXIT_FAILURE;
break;
}
printf("LED off\n");
fflush(stdout);
lguSleep(1);
}
/* 終了時に消灯 */
if (lgGpioWrite(h, LED_PIN, 0) < 0)
result = EXIT_FAILURE;
printf("LED off\n");
lgGpioFree(h, LED_PIN);
lgGpiochipClose(h);
return result;
}
ボタン
python
#!/usr/bin/python3
from gpiozero import Button
from signal import pause
try:
button = Button(23)
button.when_pressed = lambda: print("button pressed")
button.when_released = lambda: print("button released")
pause()
except KeyboardInterrupt:
pass
C