Skip to content

WIP: Working version of MIC#121

Open
mhummels wants to merge 8 commits intofossasia:masterfrom
mhummels:mic
Open

WIP: Working version of MIC#121
mhummels wants to merge 8 commits intofossasia:masterfrom
mhummels:mic

Conversation

@mhummels
Copy link

@mhummels mhummels commented Dec 30, 2025

I used the branch in PR #104 and got it running now on the latest prototypes. Max audio level is now calculated dynamically to make sure it always shows correct levels. Additionally the audio mode uses the same way for updating as the other modes.

Summary by Sourcery

Add a microphone-based audio visualization mode that dynamically maps input levels to LED patterns and integrates it into the existing mode/task framework.

New Features:

  • Introduce an AUDIO operating mode that visualizes microphone input on the LED matrix.
  • Add microphone initialization and ADC sampling helpers for reading and normalizing audio levels.

Enhancements:

  • Integrate audio visualization updates into the common task loop using a periodic AUDIO_STEP event.
  • Disable the charging display on startup to go directly into the configured splash and normal modes.

Build:

  • Include the new audio.c compilation unit in the firmware build.

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Dec 30, 2025

Reviewer's Guide

Adds a microphone-based audio visualization mode that samples the ADC, computes a dynamic audio level, and updates the LED framebuffer on a periodic task, along with basic audio driver initialization and build wiring.

Sequence diagram for periodic audio visualization update

sequenceDiagram
    participant TMOS_Scheduler
    participant CommonTask
    participant AudioModule
    participant ADC_Hardware
    participant LEDDriver

    TMOS_Scheduler->>CommonTask: AUDIO_STEP event
    CommonTask->>AudioModule: audio_visualize_poll()

    activate AudioModule
    AudioModule->>AudioModule: mic_adc()

    activate AudioModule
    AudioModule->>ADC_Hardware: ADC_ChannelCfg(11)
    loop 64 samples
        AudioModule->>ADC_Hardware: ADC_ExcutSingleConver()
        ADC_Hardware-->>AudioModule: raw_sample
    end
    AudioModule->>AudioModule: compute average and subtract 2048
    deactivate AudioModule

    AudioModule->>AudioModule: store mic value in values buffer
    AudioModule->>AudioModule: scan values to compute max
    AudioModule->>AudioModule: scale mic = mic * 7 / max
    AudioModule->>AudioModule: clamp mic to 0..7

    loop for each LED column
        AudioModule->>LEDDriver: fb[i] = amp_wav_lut[mic]
    end
    deactivate AudioModule

    CommonTask-->>TMOS_Scheduler: clear AUDIO_STEP event
Loading

Updated class diagram for audio module and common task integration

classDiagram
    class AudioModule {
        +uint16_t amp_wav_lut[8]
        +uint16_t amp_wav_lut_w1[8]
        +int16_t mic_adc()
        +void mic_init()
    }

    class MainTasks {
        +uint16_t AUDIO_STEP
        +uint16_t fb[LED_COLS]
        +void audio_visualize_poll()
        +void mode_setup_audio_visualize()
        +uint16_t common_tasks(tmosTaskID task_id, uint16_t events)
    }

    class SystemInit {
        +int main()
    }

    SystemInit --> AudioModule : calls mic_init
    SystemInit --> MainTasks : initializes tasks
    MainTasks --> AudioModule : calls mic_adc
    MainTasks --> AudioModule : uses amp_wav_lut
Loading

File-Level Changes

Change Details Files
Introduce microphone-based audio visualization mode driven by the common task loop
  • Add new AUDIO mode enum and corresponding AUDIO_STEP event flag
  • Register mode_setup_audio_visualize in the modes dispatch table
  • Add audio_visualize_poll function that samples mic_adc, maintains a rolling window to compute dynamic max amplitude, scales to 0–7, and writes LED patterns using amp_wav_lut
  • Hook AUDIO_STEP handling into common_tasks to periodically update visualization
  • Define mode_setup_audio_visualize to stop other periodic tasks, clear the framebuffer, and start the AUDIO_STEP reload task at a fixed rate
src/main.c
Wire up microphone hardware access and amplitude-to-LED lookup tables
  • Add audio.h with amplitude-to-waveform lookup tables and mic_adc/mic_init declarations
  • Implement mic_adc in audio.c using ADC channel 11 oversampling and DC offset removal
  • Implement mic_init to configure GPIO and ADC sampling for the microphone channel
  • Include audio.c in the build via the Makefile and initialize the microphone in main()
src/audio.h
src/audio.c
src/Makefile
src/main.c
Adjust startup behavior around charging display
  • Comment out disp_charging() call in main() so the charging screen is no longer shown at boot
src/main.c

Possibly linked issues

  • #Audio visualization: The PR adds mic init and an audio visualization mode, providing the firmware support requested in the issue.
  • #(not specified): PR adds mic ADC, initialization, and an audio visualization mode, fulfilling the mic and visualization feature request.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 3 issues, and left some high level feedback:

  • The amp_wav_lut and amp_wav_lut_w1 arrays are defined (not just declared) in audio.h, which will cause multiple definition linker errors; move the definitions into audio.c and keep only extern declarations in the header.
  • In audio_visualize_poll, max can be zero before the buffer is meaningfully populated or under very low/no input, which will lead to a divide-by-zero when computing mic * 7 / max; add a guard (e.g., skip scaling or set a minimum max) to avoid this.
  • The ADC sampling loop in mic_adc is synchronous and relatively heavy (64 samples per call) and is now polled periodically; consider reducing the sample count or moving to an interrupt/continuous mode earlier to avoid potential timing/latency issues in the common task loop.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `amp_wav_lut` and `amp_wav_lut_w1` arrays are defined (not just declared) in `audio.h`, which will cause multiple definition linker errors; move the definitions into `audio.c` and keep only `extern` declarations in the header.
- In `audio_visualize_poll`, `max` can be zero before the buffer is meaningfully populated or under very low/no input, which will lead to a divide-by-zero when computing `mic * 7 / max`; add a guard (e.g., skip scaling or set a minimum `max`) to avoid this.
- The ADC sampling loop in `mic_adc` is synchronous and relatively heavy (64 samples per call) and is now polled periodically; consider reducing the sample count or moving to an interrupt/continuous mode earlier to avoid potential timing/latency issues in the common task loop.

## Individual Comments

### Comment 1
<location> `src/main.c:126-135` </location>
<code_context>
 	bmlist_drop(curr_bm);
 }

+static void audio_visualize_poll()
+{
+	static uint8_t current = 0;
+	static int16_t values[64];
+	int16_t mic = abs(mic_adc());
+	int16_t max = 0;	
+	values[current++] = mic;
+	if (current >= sizeof(values)/sizeof(values[0])) current=0;
+	for (int i=0; i<sizeof(values)/sizeof(values[0]); i++) {
+		if (values[i] > max) max = values[i];
+	}
+
+	mic = mic * 7 / max;
+	if (mic > 7) mic = 7;
+
</code_context>

<issue_to_address>
**issue (bug_risk):** Guard against division by zero when `max` stays at 0.

If this is called before `values` has any non-zero samples, `max` remains 0 and `mic * 7 / max` will divide by zero. Add a guard (e.g., early-return when `max == 0` or clamp `max` to at least 1) before performing the division.
</issue_to_address>

### Comment 2
<location> `src/audio.h:6-15` </location>
<code_context>
+
+#include <stdint.h>
+
+const uint16_t amp_wav_lut[8] = {
+	0b00000000000,
+	0b00000100000,
+	0b00001110000,
+	0b00011111000,
+	0b00111111100,
+	0b01111111110,
+	0b11111111111,
+	0b11111111111,
+};
+
+const uint16_t amp_wav_lut_w1[8] = {
</code_context>

<issue_to_address>
**issue (bug_risk):** Move the LUT definition to a single `.c` file and declare it `extern` in the header.

Defining `amp_wav_lut` in the header creates a separate definition in every including translation unit, which can lead to multiple-definition link errors. Keep the definition in `audio.c` and only declare it in `audio.h` as `extern const uint16_t amp_wav_lut[8];`.
</issue_to_address>

### Comment 3
<location> `src/audio.h:17-15` </location>
<code_context>
+	0b11111111111,
+};
+
+const uint16_t amp_wav_lut_w1[8] = {
+	0b00000100000,
+	0b00001110000,
+	0b00011111000,
+	0b00111111100,
+	0b01111111110,
+	0b11111111111,
+	0b11111111111,
+	0b11111111111,
+};
+
+int16_t mic_adc();
</code_context>

<issue_to_address>
**suggestion (bug_risk):** Apply the same `extern` pattern to `amp_wav_lut_w1` as for `amp_wav_lut`.

Define `amp_wav_lut_w1` in `audio.c` and declare it as `extern` in `audio.h`, matching the pattern used for `amp_wav_lut` to prevent multiple definitions.

Suggested implementation:

```c
extern const uint16_t amp_wav_lut_w1[8];

```

1. In `src/audio.c`, add the actual definition of `amp_wav_lut_w1` (matching `amp_wav_lut`'s pattern), for example:
   ```c
   const uint16_t amp_wav_lut_w1[8] = {
       0b00000100000,
       0b00001110000,
       0b00011111000,
       0b00111111100,
       0b01111111110,
       0b11111111111,
       0b11111111111,
       0b11111111111,
   };
   ```
2. Ensure `audio.c` already includes `"audio.h"` so the declaration and definition stay in sync.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants