#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>

// ! ========================= 变 量 声 明 ========================= ! //

#define COMMANDS(X) X(POSITION, "position") X(READBACK, "readback")
#define AS_ID(id, text) id,
typedef enum { COMMANDS(AS_ID) COMMAND_COUNT } Command;
#undef AS_ID
#define AS_NAME(id, text) [id] = text,
static const char *const command_names[COMMAND_COUNT] = {COMMANDS(AS_NAME)};
#undef AS_NAME

typedef enum { LEFT, RIGHT, MOTOR_COUNT } MotorId;
typedef struct {
    /** 桌面端口同步复制设备编号和整数设定值，返回本次入队是否成功 */
    bool (*write)(void *bus, unsigned address, int value);
    void *bus;
} PortOps;
typedef struct Motor Motor;
typedef bool (*PositionFn)(const Motor *motor, float radians);
struct Motor { PortOps port; unsigned address; PositionFn submit; };
typedef struct { unsigned address; int value; } Capture;
typedef struct { float position_rad; unsigned batch; } Observation;
typedef struct { bool reached; } Arrival;
typedef struct { float rad; } FilterState;
static Motor motors[MOTOR_COUNT];

// ! ========================= 私 有 函 数 声 明 ========================= ! //

static bool capture_write(void *bus, unsigned address, int value);
static bool send_mrad(const Motor *motor, float radians);
static bool send_centideg(const Motor *motor, float radians);
static bool motor_position(MotorId id, float radians);
static FilterState lowpass(FilterState old, float sample_rad, float tau_s, float dt_s);
static void update_arrival(Arrival *arrival, Observation obs, float target_rad);
static void assemble(Capture *bus);

// ! ========================= 接 口 函 数 实 现 ========================= ! //

int main(void)
{
    Capture wire = {0};
    assemble(&wire);
    static const struct { bool (*pos)(MotorId, float); } motor = {motor_position};
    bool accepted = motor.pos(LEFT, 1.0f);
    assert(accepted && wire.address == 3 && wire.value == 1000);
    printf("%s left: addr=%u raw=%d\n", command_names[POSITION], wire.address, wire.value);
    accepted = motor.pos(RIGHT, 0.5235988f);
    assert(accepted && wire.address == 9 && wire.value == 3000);
    printf("%s right: addr=%u raw=%d\n", command_names[POSITION], wire.address, wire.value);

    FilterState left = {0.0f}, right = {1.0f};
    FilterState next_left = lowpass(left, 1.0f, 0.06f, 0.02f);
    FilterState next_right = lowpass(right, 0.0f, 0.06f, 0.02f);
    assert(left.rad == 0 && right.rad == 1);
    left = next_left;
    right = next_right;
    assert(fabsf(left.rad - 0.25f) < 0.00001f && fabsf(right.rad - 0.75f) < 0.00001f);
    printf("filtered left=%.2f right=%.2f rad\n", left.rad, right.rad);

    const Observation published[] = {{0.86f, 7}, {0.99f, 8}};
    Arrival arrival = {false};
    for (unsigned i = 0; i < sizeof published / sizeof published[0]; ++i) {
        update_arrival(&arrival, published[i], 1.0f);
        assert(arrival.reached == (i == 1));
        printf("%s batch=%u reached=%d\n", command_names[READBACK],
               published[i].batch, arrival.reached);
    }
    return 0;
}

// ! ========================= 私 有 函 数 实 现 ========================= ! //

static bool capture_write(void *bus, unsigned address, int value)
{
    Capture *wire = bus;
    *wire = (Capture){address, value};
    return true;
}

static bool send_mrad(const Motor *motor, float radians)
{
    return motor->port.write(motor->port.bus, motor->address, (int)lroundf(radians * 1000.0f));
}

static bool send_centideg(const Motor *motor, float radians)
{
    const float centidegrees_per_rad = 18000.0f / 3.14159265f;
    return motor->port.write(motor->port.bus, motor->address,
                             (int)lroundf(radians * centidegrees_per_rad));
}

static bool motor_position(MotorId id, float radians)
{
    const Motor *motor = &motors[id];
    return motor->submit(motor, radians);
}

static FilterState lowpass(FilterState old, float sample_rad, float tau_s, float dt_s)
{
    float alpha = dt_s / (tau_s + dt_s);
    return (FilterState){old.rad + alpha * (sample_rad - old.rad)};
}

static void update_arrival(Arrival *arrival, Observation obs, float target_rad)
{
    arrival->reached = fabsf(obs.position_rad - target_rad) <= 0.02f;
}

static void assemble(Capture *bus)
{
    PortOps port = {.write = capture_write, .bus = bus};
    motors[LEFT] = (Motor){port, 3, send_mrad};
    motors[RIGHT] = (Motor){port, 9, send_centideg};
}
