Obsługa modułu
Uczenie dodatkowej frazy wybudzającej
Aby nauczyć moduł nowej frazy wybudzającej (zawsze aktywne pozostaje “Hello Robot! “) należy po wywołaniu standardowym wygłosić frazę: Learning wake word “. Następnie podać nowe sformułowanie wybudzające. Ja podałem Hello Jane. Trzeba to zrobić trzykrotnie słuchając poleceń.
Biblioteka MicroPython
Biblioteka lekko zmodyfikowana. W oryginalnej był błąd w metodzie get_cmdid() . Poprawiony przez Chat GPT . Uwaga! Kody nie działają dla ESP8266. Prawdopodobnie problem tkwi w bibliotece I2C. Może trzeba użyć softI2C? Sprawdzę. Wszystko dobrze działa na ESP32.
001
from
micropython
import
const
002
from
machine
import
I2C, Pin
003
from
utime
import
sleep
006
DF2301Q_I2C_ADDR
=
const(
0x64
)
009
class
DFRobot_DF2301Q_I2C:
014
DF2301Q_I2C_REG_CMDID
=
const(
0x02
)
015
DF2301Q_I2C_REG_PLAY_CMDID
=
const(
0x03
)
016
DF2301Q_I2C_REG_SET_MUTE
=
const(
0x04
)
017
DF2301Q_I2C_REG_SET_VOLUME
=
const(
0x05
)
018
DF2301Q_I2C_REG_WAKE_TIME
=
const(
0x06
)
019
DF2301Q_I2C_8BIT_RANGE
=
const(
0xFF
)
020
DF2301Q_I2C_PLAY_CMDID_DURATION
=
const(
1
)
022
def
__init__(
self
, sda, scl, i2c_addr
=
DF2301Q_I2C_ADDR, i2c_bus
=
0
):
030
self
._addr
=
i2c_addr
033
self
._i2c
=
I2C(i2c_bus, sda
=
Pin(sda), scl
=
Pin(scl))
034
except
Exception as err:
035
print
(f
'Could not initialize i2c! bus: {i2c_bus}, sda: {sda}, scl: {scl}, error: {err}'
)
037
def
_write_reg(
self
, reg, data)
-
>
None
:
044
if
isinstance
(data,
int
):
048
self
._i2c.writeto_mem(
self
._addr, reg, bytearray(data))
049
except
Exception as err:
050
print
(f
'Write issue: {err}'
)
052
def
_read_reg(
self
, reg, length)
-
> bytes:
060
result
=
self
._i2c.readfrom_mem(
self
._addr, reg, length)
061
except
Exception as err:
062
print
(f
'Read issue: {err}'
)
063
result
=
bytearray(length)
067
def
get_cmdid(
self
)
-
>
int
:
072
data
=
self
._read_reg(
self
.DF2301Q_I2C_REG_CMDID,
1
)
075
def
get_wake_time(
self
)
-
>
int
:
080
data
=
self
._read_reg(
self
.DF2301Q_I2C_REG_WAKE_TIME,
1
)
083
def
play_by_cmdid(
self
, cmdid:
int
)
-
>
None
:
089
self
._write_reg(
self
.DF2301Q_I2C_REG_PLAY_CMDID,
int
(cmdid))
090
sleep(
self
.DF2301Q_I2C_PLAY_CMDID_DURATION)
092
def
set_wake_time(
self
, wake_time:
int
)
-
>
None
:
098
wake_up_time
=
int
(wake_time) &
self
.DF2301Q_I2C_8BIT_RANGE
099
self
._write_reg(
self
.DF2301Q_I2C_REG_WAKE_TIME, wake_up_time)
101
def
set_volume(
self
, vol:
int
)
-
>
None
:
107
self
._write_reg(
self
.DF2301Q_I2C_REG_SET_VOLUME,
int
(vol))
109
def
set_mute_mode(
self
, mode)
-
>
None
:
115
self
._write_reg(
self
.DF2301Q_I2C_REG_SET_MUTE,
int
(
bool
(mode)))
Kod programu wyświetlającego na konsoli numery komend głosowych
01
from DFRobot_DF2301Q_I2C import DFRobot_DF2301Q_I2C
02
from micropython import const
03
from utime import sleep
08
SLEEP_SECONDS = const(3)
11
def setup(sensor) -> None:
13
Set up the DFRobot DF2301Q sensor
14
:param sensor: instance of DFRobot_DF2301Q_I2C
18
sensor.set_mute_mode(0)
19
sensor.set_wake_time(20)
22
def get_cmd_id(sensor) -> int:
24
Get the command id from the DF2301Q sensor
25
:param sensor: instance of DFRobot_DF2301Q_I2C
28
command_id = sensor.get_cmdid()
29
return command_id if command_id != 0 else None
32
if __name__ == "__main__":
34
voice_sensor = DFRobot_DF2301Q_I2C(sda=SDA_PIN, scl=SCL_PIN)
35
setup(sensor=voice_sensor)
37
print('Speak your commands:')
40
cmd_id = get_cmd_id(sensor=voice_sensor)
42
if cmd_id is not None:
43
print(f'COMMAND ID: {cmd_id}')
46
except Exception as e:
47
print(f'Initialization error: {e}')
LINKI
Oficjalna biblioteka Arduino:
Dokumentacja:
Biblioteka MicroPython (uwaga, błąd! ):
NerdCave, MicroPython
Źródła:
Konfiguracja i działanie
VIDEO
Trening
VIDEO
Inne
VIDEO
VIDEO