main
John Lauer Software 1.3.1: test-stage firmware (J4 TMC2209, HOME, limits, self-test), Stage tab and Calibrate in smi-dash (simulat… c0c5c9b
// SPDX-License-Identifier: MIT
#include "smi_text.h"
#include <stdio.h>
#include <string.h>


static uint8_t text_ctl(smi_text_t *x, const uint8_t *p, size_t n) {
    uint8_t r[160];
    smi_ctl_command(x->ctl, p, n, 0, x->now_us(x->ctx), r);
    return r[3];
}

static uint8_t text_set(smi_text_t *x, int idx, uint8_t mode, float v) {
    uint8_t p[7] = { SMI_OP_SET, (uint8_t)idx, mode };
    memcpy(p + 3, &v, 4);
    return text_ctl(x, p, 7);
}

static uint8_t text_laser_off(smi_text_t *x) {
    uint8_t p[6] = { SMI_OP_LASER_ENABLE, 0, 0, 0, 0, 0 };
    x->laser_cmd = 0;
    return text_ctl(x, p, 6);
}

static void text_reply(smi_text_t *x, const char *what, uint8_t st) {
    if (st == SMI_OK) return;
    char t[64];
    snprintf(t, sizeof t, "error: %s: status %u", what, st);
    x->reply(x->ctx, t);
}

static long text_number(const char *s, long max) {
    if (!*s || strlen(s) > 5) return -1;
    long v = 0;
    for (; *s; s++) {
        if (*s < '0' || *s > '9') return -1;
        v = v * 10 + (*s - '0');
    }
    return v <= max ? v : -1;
}

// Set every switch whose lines are in [base, base + 3) from a bit mask.
static uint8_t text_mask(smi_text_t *x, long mask, int base) {
    uint8_t st = SMI_OK;
    for (int i = 0; i < x->ctl->n; i++) {
        const smi_control_def_t *d = &x->ctl->defs[i];
        if (d->kind != SMI_KIND_SWITCH || d->gpio[0] < base || d->gpio[0] >= base + 3) continue;
        unsigned code = 0;
        for (int b = 0; b < d->ngpio; b++) code |= ((unsigned)(mask >> (d->gpio[b] - base)) & 1u) << b;
        uint8_t s1 = text_set(x, i, 1, (float)code);
        if (s1 != SMI_OK) st = s1;
    }
    return st;
}

void smi_text_command(smi_text_t *x, const char *c) {
    long v;
    int li = 0;
    smi_ctl_t *ctl = x->ctl;
    const smi_control_def_t *defs = ctl->defs;
    for (int i = 0; i < ctl->n; i++)
        if (defs[i].is_laser) li = i;
    if (!strncmp(c, "stage ", 6)) { if (x->stage) x->stage(x->ctx, c); return; }
    if (!strcmp(c, "start")) { if (x->start) x->start(x->ctx); }
    else if (!strcmp(c, "stop")) { text_laser_off(x); if (x->stop) x->stop(x->ctx); }
    else if (!strcmp(c, "info")) { if (x->info) x->info(x->ctx); }
    else if (!strcmp(c, "laser off")) text_reply(x, c, text_laser_off(x));
    else if (!strcmp(c, "laser current")) {
        char t[96];
        snprintf(t, sizeof t, "laser_current_code=%d; command_code=%u; dac_code=%u",
                 (int)ctl->isense_raw, x->laser_cmd, ctl->laser_on ? ctl->code[li] : 0u);
        x->reply(x->ctx, t);
    } else if (!strncmp(c, "laser ", 6)) {
        if ((v = text_number(c + 6, 4095)) < 0) { text_laser_off(x); return; }
        if (v <= defs[li].guard) { text_reply(x, c, text_laser_off(x)); return; }   // disarmed command codes
        uint8_t st = SMI_OK;
        if (!ctl->laser_on) {
            uint8_t p[6] = { SMI_OP_LASER_ENABLE, 1, (uint8_t)SMI_LASER_KEY, (uint8_t)(SMI_LASER_KEY >> 8), (uint8_t)(SMI_LASER_KEY >> 16), (uint8_t)(SMI_LASER_KEY >> 24) };
            st = text_ctl(x, p, 6);
        }
        if (st == SMI_OK) st = text_set(x, li, 1, (float)v);
        if (st == SMI_OK) x->laser_cmd = (uint16_t)v;
        else text_laser_off(x);
        text_reply(x, c, st);
    } else if (!strncmp(c, "head ", 5) || !strncmp(c, "gain ", 5)) {
        if ((v = text_number(c + 5, 7)) < 0) return;
        text_laser_off(x);
        text_reply(x, c, text_mask(x, v, c[0] == 'h' ? x->exp_base : x->gain_base));
    } else if (!strncmp(c, "offset B ", 9) || !strncmp(c, "offset C ", 9)) {
        if ((v = text_number(c + 9, 4095)) < 0) return;
        text_laser_off(x);
        int ch = c[7] - 'A';
        for (int i = 0; i < ctl->n; i++)
            if (!defs[i].is_laser && defs[i].kind == SMI_KIND_DAC && defs[i].ch == ch)
                text_reply(x, c, text_set(x, i, 1, (float)v));
    } else if (!strncmp(c, "hb ", 3)) {
        // Heartbeat timeout for text-only hosts: hb MS (500..30000).
        if ((v = text_number(c + 3, 30000)) < 0) return;
        uint8_t p[4] = { SMI_OP_CONFIG, ctl->isense_hz, (uint8_t)v, (uint8_t)(v >> 8) };
        text_reply(x, c, text_ctl(x, p, 4));
    }
}


void smi_text_feed(smi_text_t *x, char c) {
    if (c == '\n' || c == '\r') {
        x->line[x->len] = 0;
        if (x->overflow) text_laser_off(x);
        else if (x->len) smi_text_command(x, x->line);
        x->len = 0;
        x->overflow = false;
    } else if (x->len < (int)sizeof x->line - 1) {
        x->line[x->len++] = c;
    } else {
        x->overflow = true;
    }
}