mohan-bee/mini-mp3-player

Portable, battery-powered MP3 player hardware with USB charging, microSD storage, an MCU, VS10xx audio decoder, headphone output, status controls, and regulated power rails.

Version
1.2.0
License
unset
Stars
0

firmware/tests/test_controls.c

#include "buttons.h"
#include "board.h"
#include "decoder.h"
#include <assert.h>
#include <stdio.h>
static uint32_t clock_ms;
static bool ready=true, selected, data_select, fail_transfer;
static unsigned position, transactions, data_bytes;
static uint8_t packet[4];
uint32_t millis(void) { return clock_ms++; }
void delay_ms(uint32_t ms) { clock_ms+=ms; }
bool vs_ready(void) { return ready; }
void vs_reset(bool asserted) { (void)asserted; }
void vs_speed(bool fast) { (void)fast; }
void vs_select(bool data,bool active) {
    if (active) { assert(!selected); selected=true; data_select=data; position=0; }
    else if (selected) {
        assert(data==data_select);
        if (!data && !fail_transfer) assert(position==4);
        selected=false; transactions++;
    }
}
bool vs_byte(uint8_t tx,uint8_t *rx) {
    assert(selected);
    if (fail_transfer) return false;
    *rx=0;
    if (data_select) { data_bytes++; position++; return true; }
    assert(position<4); packet[position]=tx;
    if (position==3 && packet[0]==3 && packet[1]==1) *rx=0x30;
    position++; return true;
}
int main(void) {
    Buttons b={0};
    assert(!buttons_poll(&b,1,10));
    assert(!buttons_poll(&b,0,12));
    assert(!buttons_poll(&b,1,14));
    assert(!buttons_poll(&b,1,38));
    assert(buttons_poll(&b,1,39)==1);
    assert(!buttons_poll(&b,1,1000));
    assert(!buttons_poll(&b,0,1001));
    assert(!buttons_poll(&b,0,1026));
    assert(!buttons_poll(&b,5,UINT32_MAX-10));
    assert(buttons_poll(&b,5,14)==5);
    assert(decoder_init(64));
    assert(transactions==5);
    assert(packet[0]==2 && packet[1]==11 && packet[2]==64 && packet[3]==64);
    uint8_t bytes[33]={0};
    assert(!decoder_send(bytes,33)); assert(data_bytes==0);
    assert(decoder_send(bytes,32)); assert(data_bytes==32 && !selected);
    ready=false;
    assert(!decoder_send(bytes,1));
    uint32_t start=clock_ms;
    assert(!decoder_volume(80)); assert(clock_ms-start>500 && clock_ms-start<510);
    ready=true; fail_transfer=true;
    assert(!decoder_send(bytes,1)); assert(!selected);
    assert(!decoder_volume(80)); assert(!selected);
    puts("PASS: button bounce/hold/release/rollover; decoder startup, burst limit, timeout, CS release");
}