diff test.c @ 5:2db42eaba3c8

WIP
author Daniel O'Connor <darius@dons.net.au>
date Sat, 15 Feb 2025 22:57:18 +1030
parents b6416c4aadc8
children
line wrap: on
line diff
--- a/test.c	Tue Feb 04 14:20:07 2025 +1030
+++ b/test.c	Sat Feb 15 22:57:18 2025 +1030
@@ -4,30 +4,28 @@
 #include <stdlib.h>
 #include <string.h>
 
+#if 1
 #include <q.h>
+#else
+// We want Q24.8 - Q16.16 is too small for sample calculations
+#define FPT_WBITS 24
+#include "fptc-lib/src/fptc.h"
+#define q_t fpt
+#define qmul fpt_mul
+#define qdiv fpt_div
+#define qtoi fpt2i
+#define qint i2fpt
+#define qsub(x, y) ((x) - (y))
+#define qsprint(n, s, len) fpt_str(n, s, 3)
+#endif
 
 #include "shaped-trap.h"
 
-int
-main(int argc, char **argv) {
-  uint8_t data[50000], shapelen, codegap, slew1, slew2, dcofs;
-  uint16_t plen, ncode;
+unsigned
+compute_pulse(uint8_t *data, unsigned datalen, uint16_t plen, char *code, uint8_t ncode, uint8_t shapelen, const uint8_t *shape, uint8_t codegap, uint8_t slew1, uint8_t slew2, uint8_t dcofs) {
   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]);
+  char tmps[20];
 
   dcscale = qdiv(qsub(qint(255), qint(dcofs)), qint(255));
   qsprint(dcscale, tmps, sizeof(tmps));
@@ -35,8 +33,8 @@
 
   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;
+    // Do division first so we don't overflow Q16.16
+    shapesamples = qtoi(qmul(qdiv(qint(plen), qint(100)), qint(shapelen)));
     // Number of samples for everything
     nsamples = shapesamples * 2 + slew1 + slew2;
   } else {
@@ -44,21 +42,23 @@
     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 (nsamples > datalen) {
+    printf("Pulse too long (%d > %u)\n", nsamples, datalen);
+    return 0;
+  }
   if (shapesamples < 2) {
     printf("Pulse too short (%u < %d)\n", shapesamples, 2);
-    exit(1);
+    return 0;
   }
-
+  if (qtoi(shapesamples) > 65535) {
+    printf("Shape too long (%u > %d)\n", qtoi(shapesamples), 65535);
+    return 0;
+  }
   idx = 0;
 
   // Up slew
@@ -68,11 +68,11 @@
   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));
+      data[idx++] = dcofs + qtoi(qmul(qint(shape[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));
+      data[idx++] = dcofs + qtoi(qmul(qint(shape[qtoi(qdiv(qint(shapesamples - i - 1), stepsize))]), dcscale));
 
     // Code gap
     if (c < ncode - 1)
@@ -84,8 +84,33 @@
   for (uint16_t i = 0; i < slew2; i++)
     data[idx++] = qtoi(qdiv(qmul(qint(dcofs), qint(slew2 - i)), qint(slew2)));
 
+  return idx - 1;
+}
+
+int
+main(int argc, char **argv) {
+  uint8_t data[65536], codegap, slew1, slew2, dcofs;
+  uint16_t plen;
+  uint32_t idx;
+  char *code;
+
+  if (argc != 7) {
+    printf("Bad usage\n");
+    exit(1);
+  }
+  plen = atoi(argv[1]);
+  code = argv[2];
+  codegap = atoi(argv[3]);
+  slew1 = atoi(argv[4]);
+  slew2 = atoi(argv[5]);
+  dcofs = atoi(argv[6]);
+
+  if ((idx = compute_pulse(data, sizeof(data), plen, code, strlen(code), sizeof(shaped_trap), shaped_trap, codegap, slew1, slew2, dcofs)) == 0) {
+    printf("Failed to build pulse\n");
+    exit(1);
+  }
+
   FILE *fh;
-
   if ((fh = fopen("/tmp/out.bin", "w")) == NULL) {
     printf("Unable to open file: %s\n", strerror(errno));
     exit(1);