Mercurial > ~darius > hgwebdir.cgi > modulator
changeset 4:b6416c4aadc8
Test implementation
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Tue, 04 Feb 2025 14:20:07 +1030 |
parents | b10097c3383d |
children | 2db42eaba3c8 |
files | test.c |
diffstat | 1 files changed, 97 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test.c Tue Feb 04 14:20:07 2025 +1030 @@ -0,0 +1,97 @@ +#include <sys/errno.h> +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include <q.h> + +#include "shaped-trap.h" + +int +main(int argc, char **argv) { + uint8_t data[50000], shapelen, codegap, slew1, slew2, dcofs; + uint16_t plen, ncode; + uint32_t shapesamples, nsamples, idx; + q_t dcscale, stepsize; + char *code, tmps[20]; + + if (argc != 7) { + printf("Bad usage\n"); + exit(1); + } + plen = atoi(argv[1]); + code = argv[2]; + ncode = strlen(code); + shapelen = sizeof(shaped_trap); + codegap = atoi(argv[3]); + slew1 = atoi(argv[4]); + slew2 = atoi(argv[5]); + dcofs = atoi(argv[6]); + + dcscale = qdiv(qsub(qint(255), qint(dcofs)), qint(255)); + qsprint(dcscale, tmps, sizeof(tmps)); + printf("dcscale = %s\n", tmps); + + if (ncode == 1) { + // Number of samples for half of the pulse + // Can't use q library here because it is Q16.16 so not large enough + shapesamples = (plen * shapelen) / 100; + // Number of samples for everything + nsamples = shapesamples * 2 + slew1 + slew2; + } else { + shapesamples = plen; + nsamples = shapesamples * 2 * ncode + codegap * (ncode - 1) + slew1 + slew2; + } + + if (nsamples > sizeof(data)) { + printf("Pulse too long (%d > %lu)\n", nsamples, sizeof(data)); + exit(1); + } + + // Number of samples per step in the pulse shape + stepsize = qdiv(qint(shapesamples), qint(shapelen)); + qsprint(stepsize, tmps, sizeof(tmps)); + printf("shapelen = %d shapesamples = %u nsamples = %u stepsize = %s\n", shapelen, shapesamples, nsamples, tmps); + + if (shapesamples < 2) { + printf("Pulse too short (%u < %d)\n", shapesamples, 2); + exit(1); + } + + idx = 0; + + // Up slew + for (uint16_t i = 0; i < slew1; i++) + data[idx++] = qtoi(qdiv(qmul(qint(dcofs), qint(i)), qint(slew1))); + + for (uint16_t c = 0; c < ncode; c++) { + // Pulse up + for (uint16_t i = 0; i < shapesamples; i++) + data[idx++] = dcofs + qtoi(qmul(qint(shaped_trap[qtoi(qdiv(qint(i), stepsize))]), dcscale)); + + // Pulse down + for (uint16_t i = 0; i < shapesamples; i++) + data[idx++] = dcofs + qtoi(qmul(qint(shaped_trap[qtoi(qdiv(qint(shapesamples - i - 1), stepsize))]), dcscale)); + + // Code gap + if (c < ncode - 1) + for (uint16_t i = 0; i < codegap; i++) + data[idx++] = dcofs; + } + + // Down slew + for (uint16_t i = 0; i < slew2; i++) + data[idx++] = qtoi(qdiv(qmul(qint(dcofs), qint(slew2 - i)), qint(slew2))); + + FILE *fh; + + if ((fh = fopen("/tmp/out.bin", "w")) == NULL) { + printf("Unable to open file: %s\n", strerror(errno)); + exit(1); + } + fwrite(data, idx, 1, fh); + fclose(fh); + + exit(0); +}