|
| 1 | +# SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
1 | 5 | import M5 |
2 | 6 |
|
3 | 7 |
|
4 | 8 | class Speaker2Hat: |
| 9 | + _instance = None |
| 10 | + |
5 | 11 | def __new__(cls, *args, **kwargs): |
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=True, |
13 | | - magnification=16, |
14 | | - dma_buf_len=256, |
15 | | - dma_buf_count=8, |
16 | | - task_priority=2, |
17 | | - task_pinned_core=255, |
18 | | - i2s_port=2, |
19 | | - ) |
20 | | - M5.Speaker.end() |
21 | | - M5.Mic.end() |
22 | | - spk.begin() |
23 | | - spk.setVolume(50) |
24 | | - return spk |
| 12 | + if cls._instance is not None: |
| 13 | + return cls._instance |
| 14 | + |
| 15 | + _pin_map = { |
| 16 | + # i2s id, i2s bck, i2s dataout, i2s ws |
| 17 | + M5.BOARD.M5StickC: (2, 26, 25, 0), |
| 18 | + M5.BOARD.M5StickCPlus: (2, 26, 25, 0), |
| 19 | + M5.BOARD.M5StickCPlus2: (2, 26, 25, 0), |
| 20 | + M5.BOARD.ArduinoNessoN1: (0, 7, 2, 6), |
| 21 | + } |
| 22 | + if M5.getBoard() in _pin_map: |
| 23 | + (i2s_id, i2s_bck, i2s_dataout, i2s_ws) = _pin_map.get(M5.getBoard()) |
| 24 | + spk = M5.createSpeaker() |
| 25 | + spk.config( |
| 26 | + pin_data_out=i2s_dataout, |
| 27 | + pin_bck=i2s_bck, |
| 28 | + pin_ws=i2s_ws, |
| 29 | + # sample_rate=48000, |
| 30 | + # stereo=True, |
| 31 | + magnification=16, |
| 32 | + # dma_buf_len=256, |
| 33 | + # dma_buf_count=8, |
| 34 | + # task_priority=2, |
| 35 | + # task_pinned_core=255, |
| 36 | + i2s_port=i2s_id, |
| 37 | + ) |
| 38 | + M5.Speaker.end() |
| 39 | + M5.Mic.end() |
| 40 | + spk.begin() |
| 41 | + spk.setVolume(50) |
| 42 | + cls._instance = spk |
| 43 | + return cls._instance |
| 44 | + else: |
| 45 | + raise NotImplementedError("Speaker2 Hat is not supported on this board") |
0 commit comments