Added the fluxdump command, for exporting a fluxmap into something like

Audacity where it can be visualised.
This commit is contained in:
David Given
2018-10-10 23:02:45 +02:00
parent 1c0cc1f831
commit 5d4e929302
5 changed files with 117 additions and 0 deletions

View File

@@ -13,6 +13,7 @@ SRCS = \
cmd_write.c \
cmd_decode.c \
cmd_testpattern.c \
cmd_fluxdump.c \
OBJS = $(patsubst %.c, .objs/%.o, $(SRCS))

112
cmd_fluxdump.c Normal file
View File

@@ -0,0 +1,112 @@
#include "globals.h"
#include "sql.h"
#include <unistd.h>
static const char* input_filename = NULL;
static const char* output_filename = NULL;
static int track = 0;
static int side = 0;
static int resolution_ns = NS_PER_TICK;
static sqlite3* db;
static void syntax_error(void)
{
fprintf(stderr,
"syntax: fluxclient fluxdump <options>:\n"
" -i <filename> input filename (.flux)\n"
" -o <filename> output filename (.raw)\n"
" -t <track> track to dump\n"
" -0 dump side 0\n"
" -1 dump side 1\n"
" -r <resolution> time per sample in ns (default: one tick)\n"
);
exit(1);
}
static char* const* parse_options(char* const* argv)
{
for (;;)
{
switch (getopt(countargs(argv), argv, "+i:o:t:01r:L"))
{
case -1:
return argv + optind - 1;
case 'i':
input_filename = optarg;
break;
case 'o':
output_filename = optarg;
break;
case 't':
track = atoi(optarg);
break;
case '0':
side = 0;
break;
case '1':
side = 1;
break;
case 'r':
resolution_ns = atoi(optarg);
break;
default:
syntax_error();
}
}
}
static void close_file(void)
{
sqlite3_close(db);
}
static void open_file(void)
{
db = sql_open(input_filename, SQLITE_OPEN_READONLY);
atexit(close_file);
}
void cmd_fluxdump(char* const* argv)
{
argv = parse_options(argv);
if (countargs(argv) != 1)
syntax_error();
if (!input_filename)
error("you must supply a filename to read from");
if (!output_filename)
error("you must supply a filename to write to");
if (resolution_ns < NS_PER_TICK)
error("resolution too fine (minimum: %dns)", NS_PER_TICK);
open_file();
struct fluxmap* fluxmap = sql_read_flux(db, track, side);
if (!fluxmap)
error("no data for that track in the file");
printf("Length: %d ms\n", fluxmap->length_us / 1000);
int length_r = fluxmap->length_us * 1000 / resolution_ns;
printf("Output size: %d bytes\n", length_r);
uint8_t* buffer = calloc(1, length_r);
int cursor_ns = 0;
for (int i=0; i<fluxmap->bytes; i++)
{
uint8_t t = fluxmap->intervals[i];
cursor_ns += t * NS_PER_TICK / resolution_ns;
if (cursor_ns < length_r)
buffer[cursor_ns] = 0x7f;
}
FILE* fp = fopen(output_filename, "wb");
fwrite(buffer, length_r, 1, fp);
fclose(fp);
free(buffer);
}

View File

@@ -63,5 +63,6 @@ extern void cmd_read(char* const* argv);
extern void cmd_write(char* const* argv);
extern void cmd_decode(char* const* argv);
extern void cmd_testpattern(char* const* argv);
extern void cmd_fluxdump(char* const* argv);
#endif

2
main.c
View File

@@ -78,6 +78,8 @@ int main(int argc, char* const* argv)
cmd_decode(argv);
else if (strcmp(argv[0], "testpattern") == 0)
cmd_testpattern(argv);
else if (strcmp(argv[0], "fluxdump") == 0)
cmd_fluxdump(argv);
else
syntax_error();

View File

@@ -28,6 +28,7 @@ enum
FRAME_SIZE = 64,
TICK_FREQUENCY = 12000000,
TICKS_PER_US = TICK_FREQUENCY / 1000000,
NS_PER_TICK = 1000000000 / TICK_FREQUENCY,
};
enum