main
John Lauer software/: Pico firmware (500 ksps two-channel ADC over DMA, SYNC on GPIO15, framed delta-packed USB stream), smi-dash… e09b832
// SPDX-License-Identifier: MIT
// Test helper: read raw little-endian u16 sample pairs (ch0, ch1) from stdin and
// write the firmware's framed stream to stdout, 1024 pairs per DATA frame, with a
// SYNC rising edge frame every `sync_every` pairs (argv[1], 0 = none). Uses the
// firmware's own smi_frame.c, so the dashboard is tested against the real packer.
#include <stdio.h>
#include <stdlib.h>
#include "smi_frame.h"

int main(int argc, char **argv) {
    long sync_every = argc > 1 ? atol(argv[1]) : 0;
    static uint16_t pairs[2048];
    static uint8_t frame[8 + 13 + 32 * SMI_MAX_BLOCK_BYTES + 2];
    uint64_t first = 0;
    uint16_t seq = 0;
    while (fread(pairs, 4, 1024, stdin) == 1024) {
        if (sync_every && (first / sync_every) != ((first + 1024) / sync_every)) {
            uint8_t f[8 + 13 + 2];
            uint64_t at = ((first + 1024) / sync_every) * sync_every;
            smi_put_u64(f + 8, at); smi_put_u32(f + 16, 0); f[20] = 1;
            smi_begin_frame(f, SMI_T_SYNC, 13, seq++);
            fwrite(f, 1, smi_finish_frame(f, 13), stdout);
        }
        uint8_t *pl = frame + 8;
        smi_put_u64(pl, first);
        smi_put_u32(pl + 8, (uint32_t)(first * 2));
        pl[12] = 16;
        size_t o = 13;
        for (int g = 0; g < 16; g++) {
            o += smi_pack_block(pairs + g * 128, 2, pl + o);
            o += smi_pack_block(pairs + g * 128 + 1, 2, pl + o);
        }
        smi_begin_frame(frame, SMI_T_DATA, (uint16_t)o, seq++);
        fwrite(frame, 1, smi_finish_frame(frame, (uint16_t)o), stdout);
        first += 1024;
    }
    return 0;
}