Skip to content

Commit cb6906b

Browse files
committed
libs/hat: Add Speaker/Speaker2 Hat support.
Signed-off-by: lbuque <[email protected]>
1 parent ef4dc17 commit cb6906b

File tree

9 files changed

+176
-0
lines changed

9 files changed

+176
-0
lines changed

docs/en/hats/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ HAT
2121
pir.rst
2222
servo.rst
2323
servo8.rst
24+
speaker.rst
2425
thermal.rst
2526
tof.rst
2627
vibrator.rst

docs/en/hats/speaker.rst

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
Speaker Hat
2+
===========
3+
4+
.. sku: U055
5+
6+
.. include:: ../refs/hat.speaker.ref
7+
8+
The following products are supported:
9+
10+
|Speaker Hat|
11+
12+
Below is the detailed support for Speaker on the host:
13+
14+
.. table::
15+
:widths: auto
16+
:align: center
17+
18+
+-----------------+-------------------+
19+
|Controller | Atomic Echo Base |
20+
+=================+===================+
21+
| CoreInk | |S| |
22+
+-----------------+-------------------+
23+
| StickC | |S| |
24+
+-----------------+-------------------+
25+
| StickC PLUS | |S| |
26+
+-----------------+-------------------+
27+
| StickC PLUS2 | |S| |
28+
+-----------------+-------------------+
29+
30+
.. |S| unicode:: U+2705
31+
.. |N| unicode:: U+274C
32+
33+
Micropython Example:
34+
35+
.. literalinclude:: ../../../examples/hat/speaker/stickc_plus2_speaker_example.py
36+
:language: python
37+
:linenos:
38+
39+
40+
UIFLOW2 Example:
41+
42+
|example.png|
43+
44+
45+
.. only:: builder_html
46+
47+
|stickc_plus2_speaker_example.m5f2|
48+
49+
:download:`poweron_2_5s.wav <../../../examples/hardware/speaker/poweron_2_5s.wav>`
50+
51+
class SpeakerHat
52+
----------------
53+
54+
Constructors
55+
------------
56+
57+
.. class:: SpeakerHat(*args, **kwargs)
58+
59+
Create an SpeakerHat object.
60+
61+
UIFLOW2:
62+
63+
|init.png|
64+
65+
SpeakerHat class inherits M5.Speaker class, See :ref:`hardware.Speaker.Methods <hardware.Speaker.Methods>` for more details.

docs/en/refs/hat.speaker.ref

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
.. |Speaker Hat| image:: https://static-cdn.m5stack.com/resource/docs/products/hat/hat-spk/hat-spk_01.webp
3+
:target: https://docs.m5stack.com/en/hat/hat-spk
4+
:height: 200px
5+
:width: 200px
6+
7+
.. |init.png| image:: https://static-cdn.m5stack.com/mpy_docs/hat/speaker/init.png
8+
9+
.. |example.png| image:: https://static-cdn.m5stack.com/mpy_docs/hat/speaker/example.png
10+
11+
.. |stickc_plus2_speaker_example.m5f2| raw:: html
12+
13+
<a
14+
href="https://uiflow2.m5stack.com/?example=https://raw.githubusercontent.com/m5stack/uiflow-micropython/examples/base/speaker/atoms3r_speaker_example.m5f2"
15+
target="_blank"
16+
>
17+
stickc_plus2_speaker_example.m5f2
18+
</a>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"version":"V2.0","versionNumber":"V2.2.0","type":"stickc-plus2","components":[{"name":"screen","type":"screen","layer":0,"screenId":"builtin","screenName":"","id":"__stickc-plus2_screen","createTime":1736390749863,"x":0,"y":0,"width":135,"height":240,"backgroundColor":"#000000","size":0,"isSelected":true}],"resources":[{"hardware":["hardware_button","hardware_pin_button","imu","speaker","ir"]},{"hat":["hat_spk"]}],"units":[],"hats":[{"type":"hat_spk","name":"hat_spk_0","id":"j7DlK=`NEI45Rmmr","createTime":1736390757035,"portList":["HAT"],"portType":"HAT","initBlockType":"hat_spk_init","initBlockId":"`7.vspFa/g)ZQAI1EVxz"}],"bases":[],"i2cs":[],"blockly":"<block type=\"basic_on_setup\" id=\"setup_block\" deletable=\"false\" x=\"-170\" y=\"-90\"><mutation isBegin=\"true\"></mutation><field name=\"UPDATEOP\">true</field><statement name=\"FUNC\"><block type=\"system_m5_begin\" id=\"system_m5_begin\"><next><block type=\"hat_spk_init\" id=\"`7.vspFa/g)ZQAI1EVxz\"><field name=\"NAME\">hat_spk_0</field><next><block type=\"hat_spk_set_volume_percentage\" id=\"5Fp+Plvq#9V:lPCVqz@j\"><field name=\"NAME\">hat_spk_0</field><value name=\"VALUE\"><shadow type=\"math_slider\" id=\"jSeye/=aa0`R9YU!jD=3\"><mutation max=\"100\" min=\"0\" step=\"1\" precision=\"1\"></mutation><field name=\"NUM\">100</field></shadow></value><next><block type=\"hat_spk_tone\" id=\"5S+U4sp_=83Hhsx65Tzx\"><field name=\"NAME\">hat_spk_0</field><value name=\"FREQ\"><shadow type=\"math_number\" id=\"F1B36q49G:Vfq+9E@Eut\"><mutation max=\"Infinity\" min=\"0\" precision=\"0\"></mutation><field name=\"NUM\">2000</field></shadow></value><value name=\"MS\"><shadow type=\"math_number\" id=\"^1UC-%)e.dyj[jk(Qm8_\"><mutation max=\"Infinity\" min=\"0\" precision=\"0\"></mutation><field name=\"NUM\">100</field></shadow></value><next><block type=\"hat_spk_playwav_file\" id=\"bCS.|h*EqIHg).AuYyVU\"><field name=\"NAME\">hat_spk_0</field><field name=\"TYPE\">flash</field><value name=\"FILE\"><shadow type=\"text\" id=\"irIM-MGAPBA$zaWhkM;k\"><field name=\"TEXT\">poweron_2_5s.wav</field></shadow></value></block></next></block></next></block></next></block></next></block></statement></block><block type=\"basic_on_loop\" id=\"loop_block\" deletable=\"false\" x=\"-170\" y=\"190\"><mutation isUpdate=\"true\"></mutation><field name=\"UPDATEOP\">true</field><statement name=\"FUNC\"><block type=\"system_m5_update\" id=\"system_m5_update\"></block></statement></block>","screen":[{"simulationName":"Built-in","type":"builtin","width":135,"height":240,"scale":0.92,"screenName":"","blockId":"","screenColorType":0,"id":"builtin","createTime":1736390749863}],"logicWhenNum":0,"customList":[]}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import os, sys, io
6+
import M5
7+
from M5 import *
8+
from hat import SpeakerHat
9+
10+
11+
hat_spk_0 = None
12+
13+
14+
def setup():
15+
global hat_spk_0
16+
17+
M5.begin()
18+
hat_spk_0 = SpeakerHat((26, 0))
19+
hat_spk_0.setVolumePercentage(1)
20+
hat_spk_0.tone(2000, 100)
21+
hat_spk_0.playWavFile("/flash/res/audio/poweron_2_5s.wav")
22+
23+
24+
def loop():
25+
global hat_spk_0
26+
M5.update()
27+
28+
29+
if __name__ == "__main__":
30+
try:
31+
setup()
32+
while True:
33+
loop()
34+
except (Exception, KeyboardInterrupt) as e:
35+
try:
36+
from utility import print_error_msg
37+
38+
print_error_msg(e)
39+
except ImportError:
40+
print("please update to latest firmware")

m5stack/libs/hat/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
"RS485Hat": "rs485",
2222
"ServoHat": "servo",
2323
"Servos8Hat": "servo8",
24+
"SpeakerHat": "speaker",
25+
"Speaker2Hat": "speaker2",
2426
"ThermalHat": "thermal",
2527
"ToFHat": "tof",
2628
"VibratorHat": "vibrator",

m5stack/libs/hat/manifest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
"rs485.py",
2525
"servo.py",
2626
"servo8.py",
27+
"speaker.py",
28+
"speaker2.py",
2729
"thermal.py",
2830
"tof.py",
2931
"vibrator.py",

m5stack/libs/hat/speaker.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import M5
2+
import machine
3+
4+
5+
class SpeakerHat:
6+
_en_pin = {
7+
M5.BOARD.M5StickC: 0,
8+
M5.BOARD.M5StickCPlus: 0,
9+
M5.BOARD.M5StickCPlus2: 0,
10+
M5.BOARD.M5StackCoreInk: 25,
11+
}
12+
13+
def __new__(cls, *args, **kwargs):
14+
spk = M5.createSpeaker()
15+
pin_en = machine.Pin(cls._en_pin.get(M5.getBoard()), machine.Pin.OUT)
16+
pin_en(1)
17+
spk.config(pin_data_out=26, use_dac=True, buzzer=False, magnification=32)
18+
spk.begin()
19+
spk.setVolume(80)
20+
return spk

m5stack/libs/hat/speaker2.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import M5
2+
3+
4+
class Speaker2Hat:
5+
def __new__(cls):
6+
spk = M5.createSpeaker()
7+
spk.config(
8+
pin_data_out=25,
9+
pin_bck=26,
10+
pin_ws=0,
11+
sample_rate=48000,
12+
stereo=False,
13+
buzzer=False,
14+
use_dac=False,
15+
dac_zero_level=0,
16+
magnification=16,
17+
dma_buf_len=256,
18+
dma_buf_count=8,
19+
task_priority=2,
20+
task_pinned_core=255,
21+
i2s_port=1,
22+
)
23+
M5.Speaker.end()
24+
M5.Mic.end()
25+
spk.begin()
26+
spk.setVolume(50)
27+
return spk

0 commit comments

Comments
 (0)