Moonlight

Source: src/moonlight.c · 88 lines · 5 functions.

Module entry points

These functions are exposed to the rest of the project. Open one to follow its decisions and the helpers it calls.

How to read these diagrams

Each function has its own expandable diagram using the actual expressions and statements from this file. Decisions split into True and False; loop bodies are indented. Returns end the function immediately; break and continue follow their C meanings. Arrows between statements show their order when execution reaches them.

These are structural source diagrams before preprocessing, not runtime traces. Conditional compilation and macros remain source expressions. Ternary choices remain inside their statement. Calls are linked separately; callback execution and task scheduling are not inferred.

Functions in this file

midiFrequency() · source line 50
static uint16_t midiFrequency(uint8_t note)
  1. Executestatic const uint16_t chromatic_c4[] = {262U, 277U, 294U, 311U, 330U, 349U, 370U, 392U, 415U, 440U, 466U, 494U};
  2. Executeuint16_t frequency = chromatic_c4[note % 12U];
  3. Executeint octave = (int)(note / 12U) - 5;
  4. WHILE loopoctave > 0

    Repeat body while the loop condition permits; then continue below.

      1. Executefrequency = (uint16_t)(frequency * 2U);
      2. Execute--octave;
  5. WHILE loopoctave < 0

    Repeat body while the loop condition permits; then continue below.

      1. Executefrequency = (uint16_t)(frequency / 2U);
      2. Execute++octave;
  6. Exit this functionreturn frequency;
Read original function source
static uint16_t midiFrequency(uint8_t note)
{
    static const uint16_t chromatic_c4[] = {262U, 277U, 294U, 311U, 330U, 349U,
                                             370U, 392U, 415U, 440U, 466U, 494U};
    uint16_t frequency = chromatic_c4[note % 12U];
    int octave = (int)(note / 12U) - 5;
    while (octave > 0) { frequency = (uint16_t)(frequency * 2U); --octave; }
    while (octave < 0) { frequency = (uint16_t)(frequency / 2U); ++octave; }
    return frequency;
}
playCurrentNote() · source line 61
static void playCurrentNote(void)

Calls into this documentation:

  1. ExecuteaudioStartTestTone(midiFrequency(score[note_index]));
Read original function source
static void playCurrentNote(void)
{
    audioStartTestTone(midiFrequency(score[note_index]));
}
startMoonlight() · source line 66
void startMoonlight(uint32_t now_ms)

Calls into this documentation:

  1. Executenote_index = 0U;
  2. Executenext_note_at = now_ms + MOONLIGHT_STEP_MS;
  3. Executerunning = true;
  4. ExecuteplayCurrentNote();
Read original function source
void startMoonlight(uint32_t now_ms)
{
    note_index = 0U;
    next_note_at = now_ms + MOONLIGHT_STEP_MS;
    running = true;
    playCurrentNote();
}
stopMoonlight() · source line 74
void stopMoonlight(void)

Calls into this documentation:

  1. ExecuteaudioStopTestTone();
  2. Executerunning = false;
Read original function source
void stopMoonlight(void)
{
    audioStopTestTone();
    running = false;
}
updateMoonlight() · source line 80
void updateMoonlight(uint32_t now_ms)

Calls into this documentation:

  1. Decision!running || now_ms - next_note_at >= 0x80000000U
    True
    1. Exit this functionreturn;
    False

    Continue after this decision.

  2. Execute++note_index;
  3. Decisionnote_index >= MOONLIGHT_NOTE_COUNT
    True
    1. Executenote_index = 0U;
    False

    Continue after this decision.

  4. Executenext_note_at += MOONLIGHT_STEP_MS;
  5. ExecuteplayCurrentNote();
Read original function source
void updateMoonlight(uint32_t now_ms)
{
    if (!running || now_ms - next_note_at >= 0x80000000U) return;
    ++note_index;
    if (note_index >= MOONLIGHT_NOTE_COUNT) note_index = 0U;
    next_note_at += MOONLIGHT_STEP_MS;
    playCurrentNote();
}
Complete original file, including constants and data
/* Coded By JD(2026) */
#error "Reference listing only — not provided as build-ready source."
#include "moonlight.h"

#include "audio.h"

#include <stdbool.h>

#define MOONLIGHT_STEP_MS 400U

// Complete first movement, reduced to the highest note at each onset for the
// single direct-tone output.  The static score remains in flash, not working RAM.
static const uint8_t score[] = {
    56U, 61U, 64U, 56U, 61U, 64U, 56U, 61U, 64U, 56U, 61U, 64U, 56U, 61U, 64U, 56U, 61U, 64U, 56U, 61U, 64U, 56U, 61U, 64U,
    57U, 61U, 64U, 57U, 61U, 64U, 57U, 62U, 66U, 57U, 62U, 66U, 56U, 60U, 66U, 56U, 61U, 64U, 56U, 61U, 63U, 54U, 60U, 63U,
    56U, 61U, 64U, 56U, 61U, 64U, 68U, 61U, 64U, 68U, 68U, 63U, 66U, 56U, 63U, 66U, 56U, 63U, 66U, 68U, 63U, 66U, 68U, 68U,
    61U, 64U, 56U, 61U, 64U, 69U, 61U, 66U, 57U, 61U, 66U, 68U, 59U, 64U, 56U, 59U, 64U, 66U, 59U, 63U, 71U, 59U, 63U, 64U,
    59U, 56U, 59U, 64U, 56U, 59U, 64U, 56U, 59U, 64U, 55U, 59U, 64U, 55U, 59U, 64U, 55U, 59U, 64U, 67U, 59U, 64U, 67U, 67U,
    59U, 65U, 55U, 59U, 65U, 55U, 59U, 65U, 67U, 59U, 65U, 67U, 67U, 60U, 64U, 55U, 59U, 64U, 55U, 61U, 64U, 66U, 66U, 67U,
    64U, 66U, 66U, 59U, 62U, 66U, 59U, 62U, 66U, 59U, 63U, 66U, 71U, 63U, 66U, 72U, 64U, 67U, 59U, 64U, 67U, 59U, 64U, 67U,
    70U, 64U, 67U, 71U, 63U, 66U, 59U, 63U, 66U, 59U, 63U, 66U, 71U, 63U, 66U, 72U, 64U, 67U, 59U, 64U, 67U, 59U, 64U, 67U,
    70U, 64U, 67U, 71U, 63U, 66U, 59U, 63U, 66U, 71U, 62U, 65U, 59U, 62U, 65U, 71U, 61U, 68U, 59U, 61U, 68U, 69U, 61U, 66U,
    57U, 61U, 66U, 67U, 66U, 61U, 61U, 61U, 66U, 61U, 66U, 69U, 73U, 66U, 69U, 73U, 73U, 68U, 71U, 61U, 68U, 71U, 61U, 68U,
    71U, 73U, 68U, 71U, 73U, 73U, 66U, 69U, 61U, 66U, 69U, 72U, 66U, 69U, 73U, 66U, 69U, 75U, 66U, 68U, 63U, 66U, 68U, 63U,
    66U, 68U, 75U, 66U, 68U, 76U, 68U, 73U, 64U, 68U, 73U, 75U, 66U, 69U, 73U, 64U, 70U, 72U, 60U, 63U, 68U, 60U, 63U, 69U,
    60U, 63U, 66U, 60U, 63U, 60U, 63U, 56U, 60U, 63U, 57U, 60U, 63U, 54U, 60U, 63U, 64U, 68U, 73U, 64U, 68U, 76U, 64U, 68U,
    73U, 64U, 68U, 60U, 57U, 63U, 60U, 66U, 63U, 69U, 66U, 72U, 64U, 61U, 68U, 64U, 73U, 68U, 76U, 73U, 68U, 61U, 67U, 64U,
    70U, 67U, 73U, 70U, 76U, 73U, 79U, 76U, 82U, 66U, 72U, 69U, 75U, 72U, 78U, 75U, 81U, 78U, 84U, 81U, 87U, 84U, 78U, 81U,
    75U, 78U, 72U, 75U, 69U, 72U, 66U, 69U, 63U, 66U, 60U, 63U, 57U, 60U, 54U, 57U, 51U, 54U, 49U, 54U, 57U, 48U, 54U, 56U,
    57U, 56U, 54U, 51U, 54U, 57U, 49U, 54U, 57U, 48U, 54U, 56U, 57U, 56U, 54U, 50U, 54U, 57U, 49U, 54U, 57U, 48U, 54U, 56U,
    57U, 56U, 54U, 49U, 52U, 61U, 49U, 52U, 61U, 51U, 57U, 61U, 51U, 57U, 61U, 51U, 56U, 60U, 51U, 54U, 60U, 52U, 56U, 61U,
    56U, 61U, 64U, 56U, 61U, 64U, 68U, 61U, 64U, 68U, 68U, 63U, 66U, 56U, 63U, 66U, 56U, 63U, 66U, 68U, 63U, 66U, 68U, 68U,
    61U, 64U, 56U, 61U, 64U, 69U, 61U, 66U, 57U, 61U, 66U, 68U, 59U, 64U, 56U, 59U, 64U, 66U, 59U, 63U, 71U, 59U, 63U, 64U,
    59U, 59U, 64U, 68U, 59U, 64U, 68U, 71U, 64U, 68U, 71U, 71U, 66U, 69U, 59U, 66U, 69U, 59U, 66U, 69U, 71U, 66U, 69U, 71U,
    71U, 64U, 68U, 59U, 64U, 68U, 72U, 66U, 68U, 73U, 64U, 68U, 75U, 66U, 68U, 63U, 66U, 68U, 76U, 68U, 73U, 64U, 68U, 73U,
    74U, 66U, 69U, 62U, 66U, 69U, 72U, 66U, 68U, 60U, 66U, 68U, 73U, 64U, 68U, 61U, 64U, 68U, 61U, 65U, 68U, 73U, 65U, 68U,
    74U, 66U, 69U, 61U, 66U, 69U, 61U, 66U, 69U, 72U, 66U, 69U, 73U, 65U, 68U, 61U, 65U, 68U, 61U, 65U, 68U, 73U, 65U, 68U,
    74U, 66U, 69U, 61U, 66U, 69U, 61U, 66U, 69U, 72U, 66U, 69U, 73U, 65U, 68U, 61U, 65U, 68U, 73U, 66U, 69U, 61U, 66U, 69U,
    71U, 66U, 69U, 59U, 66U, 69U, 59U, 66U, 69U, 71U, 64U, 68U, 69U, 64U, 68U, 69U, 63U, 66U, 68U, 63U, 66U, 68U, 61U, 64U,
    66U, 68U, 69U, 68U, 68U, 61U, 56U, 61U, 64U, 56U, 61U, 64U, 56U, 61U, 64U, 56U, 63U, 66U, 56U, 63U, 66U, 56U, 63U, 66U,
    56U, 63U, 66U, 56U, 64U, 61U, 68U, 64U, 73U, 68U, 76U, 73U, 80U, 76U, 73U, 72U, 75U, 69U, 72U, 66U, 69U, 63U, 66U, 57U,
    60U, 56U, 54U, 61U, 64U, 61U, 68U, 64U, 73U, 68U, 76U, 73U, 80U, 76U, 73U, 72U, 75U, 69U, 72U, 66U, 69U, 63U, 66U, 57U,
    60U, 56U, 54U, 61U, 56U, 61U, 64U, 61U, 56U, 52U, 56U, 61U, 56U, 52U, 49U, 52U, 56U, 52U, 49U, 61U, 61U,
};

#define MOONLIGHT_NOTE_COUNT (sizeof(score) / sizeof(score[0]))

static uint16_t note_index;
static uint32_t next_note_at;
static bool running;

static uint16_t midiFrequency(uint8_t note)
{
    static const uint16_t chromatic_c4[] = {262U, 277U, 294U, 311U, 330U, 349U,
                                             370U, 392U, 415U, 440U, 466U, 494U};
    uint16_t frequency = chromatic_c4[note % 12U];
    int octave = (int)(note / 12U) - 5;
    while (octave > 0) { frequency = (uint16_t)(frequency * 2U); --octave; }
    while (octave < 0) { frequency = (uint16_t)(frequency / 2U); ++octave; }
    return frequency;
}

static void playCurrentNote(void)
{
    audioStartTestTone(midiFrequency(score[note_index]));
}

void startMoonlight(uint32_t now_ms)
{
    note_index = 0U;
    next_note_at = now_ms + MOONLIGHT_STEP_MS;
    running = true;
    playCurrentNote();
}

void stopMoonlight(void)
{
    audioStopTestTone();
    running = false;
}

void updateMoonlight(uint32_t now_ms)
{
    if (!running || now_ms - next_note_at >= 0x80000000U) return;
    ++note_index;
    if (note_index >= MOONLIGHT_NOTE_COUNT) note_index = 0U;
    next_note_at += MOONLIGHT_STEP_MS;
    playCurrentNote();
}