Speaker2 Hat
This is the driver library of Speaker2 Hat, which is provides a set of methods to control the speaker.
Support the following products:
UiFlow2 Example
play audio
Open the speaker2_stickcplus2_example.m5f2 project in UiFlow2.
This example demonstrates how to play audio.
UiFlow2 Code Block:
Example output:
None
MicroPython Example
play audio
This example demonstrates how to play audio.
MicroPython Code Block:
1# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD 2# 3# SPDX-License-Identifier: MIT 4 5import os, sys, io 6import M5 7from M5 import * 8from hat import Speaker2Hat 9 10 11title1 = None 12label0 = None 13label1 = None 14hat_spk2_0 = None 15 16 17def setup(): 18 global title1, label0, label1, hat_spk2_0 19 20 Widgets.setRotation(3) 21 M5.begin() 22 title1 = Widgets.Title("SPK2 StickcPlus2 e.g.", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu18) 23 label0 = Widgets.Label( 24 "Press BtnA to Beep", 1, 39, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18 25 ) 26 label1 = Widgets.Label( 27 "Press BtnB to play wav", 1, 74, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18 28 ) 29 30 hat_spk2_0 = Speaker2Hat((26, 0)) 31 hat_spk2_0.setVolumePercentage(1) 32 33 34def loop(): 35 global title1, label0, label1, hat_spk2_0 36 M5.update() 37 if BtnA.wasPressed(): 38 hat_spk2_0.tone(2000, 100) 39 if BtnB.wasPressed(): 40 hat_spk2_0.playWavFile("/flash/res/audio/poweron_2_5s.wav") 41 42 43if __name__ == "__main__": 44 try: 45 setup() 46 while True: 47 loop() 48 except (Exception, KeyboardInterrupt) as e: 49 try: 50 from utility import print_error_msg 51 52 print_error_msg(e) 53 except ImportError: 54 print("please update to latest firmware")
Example output:
None
API
Speaker2
- class hat.speaker2.Speaker2Hat(*args, **kwargs)
Bases:
object
Speaker2 class inherits Speaker class, See hardware.Speaker.Methods for more details.