diff --git a/.cache/clangd/index/duckdb.h.63D53BDB55D39D0A.idx b/.cache/clangd/index/duckdb.h.63D53BDB55D39D0A.idx new file mode 100644 index 0000000..41cb69d Binary files /dev/null and b/.cache/clangd/index/duckdb.h.63D53BDB55D39D0A.idx differ diff --git a/.cache/clangd/index/lapinfo.c.F9698B9BD3295BA4.idx b/.cache/clangd/index/lapinfo.c.F9698B9BD3295BA4.idx new file mode 100644 index 0000000..93cb040 Binary files /dev/null and b/.cache/clangd/index/lapinfo.c.F9698B9BD3295BA4.idx differ diff --git a/.cache/clangd/index/lapinfo.h.702DAF4EDA4E0E47.idx b/.cache/clangd/index/lapinfo.h.702DAF4EDA4E0E47.idx new file mode 100644 index 0000000..87ab067 Binary files /dev/null and b/.cache/clangd/index/lapinfo.h.702DAF4EDA4E0E47.idx differ diff --git a/.cache/clangd/index/main.c.8770424B51F7A3DB.idx b/.cache/clangd/index/main.c.8770424B51F7A3DB.idx new file mode 100644 index 0000000..8f071c7 Binary files /dev/null and b/.cache/clangd/index/main.c.8770424B51F7A3DB.idx differ diff --git a/compile_commands.json b/compile_commands.json new file mode 100644 index 0000000..a8f4cf3 --- /dev/null +++ b/compile_commands.json @@ -0,0 +1,44 @@ +[ + { + "arguments": [ + "/usr/bin/gcc", + "-Wall", + "-O0", + "-g", + "-DDEBUG", + "-Wall", + "-Wextra", + "-Isrc", + "-I/usr/include/mysql/", + "-Ivendor/duckdb/linux/include", + "-c", + "-o", + "build/lapinfo.o", + "src/lapinfo.c" + ], + "directory": "/home/tom/Dev/HardCompound", + "file": "/home/tom/Dev/HardCompound/src/lapinfo.c", + "output": "/home/tom/Dev/HardCompound/build/lapinfo.o" + }, + { + "arguments": [ + "/usr/bin/gcc", + "-Wall", + "-O0", + "-g", + "-DDEBUG", + "-Wall", + "-Wextra", + "-Isrc", + "-I/usr/include/mysql/", + "-Ivendor/duckdb/linux/include", + "-c", + "-o", + "build/main.o", + "src/main.c" + ], + "directory": "/home/tom/Dev/HardCompound", + "file": "/home/tom/Dev/HardCompound/src/main.c", + "output": "/home/tom/Dev/HardCompound/build/main.o" + } +] diff --git a/src/lapinfo.c b/src/lapinfo.c index e1dc27e..0dd1fc4 100644 --- a/src/lapinfo.c +++ b/src/lapinfo.c @@ -1,38 +1,17 @@ #include #include -typedef enum { - BOOL, - INT, - FLOAT, - DOUBLE, -} TelemetryDataType; - -typedef struct { - TelemetryDataType type; - int8_t frequency; - void *data; -} TelemetryInfo; - -typedef struct { - int lap_number; - double start_time; - TelemetryInfo throttle_pos; - TelemetryInfo brake_pos; - TelemetryInfo steering_pos; - TelemetryInfo speed; -} LapInfo; +#include "lapinfo.h" void free_telemetry_info(TelemetryInfo *t) { free(t->data); // free(t); } -// Dont use for now void destroyLapinfo(LapInfo *info) { free_telemetry_info(&info->throttle_pos); free_telemetry_info(&info->brake_pos); free_telemetry_info(&info->steering_pos); free_telemetry_info(&info->speed); - free(info); + // free(info); } diff --git a/src/lapinfo.h b/src/lapinfo.h new file mode 100644 index 0000000..e1e7d30 --- /dev/null +++ b/src/lapinfo.h @@ -0,0 +1,31 @@ +#pragma once + +#include + +typedef enum { + BOOL, + INT, + FLOAT, + DOUBLE, +} TelemetryDataType; + +typedef struct { + TelemetryDataType type; + int8_t frequency; + void *data; + uint64_t *validity; + uint32_t data_size; + uint32_t data_last_index; +} TelemetryInfo; + +typedef struct { + int lap_number; + double start_time; + TelemetryInfo throttle_pos; // freq: 50 + TelemetryInfo brake_pos; // freq: 50 + TelemetryInfo steering_pos; // freq: 100 + TelemetryInfo speed; +} LapInfo; + +void free_telemetry_info(TelemetryInfo *t); +void destroyLapinfo(LapInfo *info); diff --git a/src/main.c b/src/main.c index 95f0198..fc56aa0 100644 --- a/src/main.c +++ b/src/main.c @@ -1,9 +1,51 @@ #include -#include #include #include #include +#include "lapinfo.h" + +void get_data_from_db(const char *query, duckdb_connection *conn, TelemetryInfo *info) { + duckdb_result result; + int32_t iterations = 0; + int32_t number_of_data_points = 0; + + duckdb_query(*conn, query, &result); + + while (true) { + iterations++; + + duckdb_data_chunk data_chunk = duckdb_fetch_chunk(result); + if (!data_chunk) { + break; + } + + idx_t row_count = duckdb_data_chunk_get_size(data_chunk); // number of rows from the data chunk + number_of_data_points += row_count; + + // set column + duckdb_vector col1 = duckdb_data_chunk_get_vector(data_chunk, 0); + float *col1_data = (float *)duckdb_vector_get_data(col1); + uint64_t *col1_validity = duckdb_vector_get_validity(col1); + + for (idx_t row = 0; row < row_count; row++) { + if (duckdb_validity_row_is_valid(col1_validity, row)) { + // put a switch here based on the info from the struct + ((float *)info->data)[info->data_last_index] = col1_data[row]; + info->data_last_index++; + // TODO: set info->data_size as capacity at struct creation + printf("%f, ", col1_data[row]); + } else { + printf("NULL"); + } + } + + duckdb_destroy_data_chunk(&data_chunk); + } + duckdb_destroy_result(&result); + printf("\nNumber of iterations: %d, number of data points: %d\n", iterations, number_of_data_points); +} + int main(int argc, char **argv) { printf("Hello from HardCompound!\n"); @@ -137,55 +179,38 @@ int main(int argc, char **argv) { const int event_table_count = sizeof(event_tables) / sizeof(event_tables[0]); - // Test query - duckdb_result result; - int32_t iterations = 0; - int32_t number_of_data_points = 0; + LapInfo info = {.lap_number = 0, + .start_time = 0, + .throttle_pos = {.data_last_index = 0, + .data_size = 75000, + .frequency = 50, + .type = FLOAT, + .validity = NULL, + .data = malloc(sizeof(float) * 75000)}, + .brake_pos = {.data_last_index = 0, + .data_size = 75000, + .frequency = 50, + .type = FLOAT, + .validity = NULL, + .data = malloc(sizeof(float) * 75000)}, + .steering_pos = {.data_last_index = 0, + .data_size = 75000, + .frequency = 50, + .type = FLOAT, + .validity = NULL, + .data = malloc(sizeof(float) * 75000)}, + .speed = {.data_last_index = 0, + .data_size = 75000, + .frequency = 50, + .type = FLOAT, + .validity = NULL, + .data = malloc(sizeof(float) * 75000)}}; - for (int i = 0; i < 1 /*channel_table_count*/; i++) { - char queryBuffer[256]; // buffer to hold the formatted query - - // snprintf(queryBuffer, sizeof(queryBuffer), "SELECT * FROM \"%s\"\n", channel_tables[i]); - - // duckdb_query(conn, queryBuffer, &result); - duckdb_query(conn, "SELECT CAST(value AS FLOAT) from \"Lap Time\"\n", &result); - - while (true) { - iterations++; - - duckdb_data_chunk data_chunk = duckdb_fetch_chunk(result); - - if (!data_chunk) { - break; - } - idx_t row_count = duckdb_data_chunk_get_size(data_chunk); // number of rows from the data chunk - number_of_data_points += row_count; - - // set column - duckdb_vector col1 = duckdb_data_chunk_get_vector(data_chunk, 0); - float_t *col1_data = (float_t *)duckdb_vector_get_data(col1); - uint64_t *col1_validity = duckdb_vector_get_validity(col1); - - for (idx_t row = 0; row < row_count; row++) { - if (duckdb_validity_row_is_valid(col1_validity, row)) { - printf("%f, ", col1_data[row]); - } else { - printf("NULL"); - } - } - - duckdb_destroy_data_chunk(&data_chunk); - } - duckdb_destroy_result(&result); - } - - printf("\nNumber of iterations: %d, number of data points: %d\n", iterations, number_of_data_points); + get_data_from_db("Select * from \"Throttle Pos\"\n", &conn, &info.throttle_pos); + destroyLapinfo(&info); printf("Shutting down HardCompound!\n"); - printf("Destroying result!\n"); - duckdb_destroy_result(&result); - printf("Closing database and any connection.\n"); duckdb_disconnect(&conn); duckdb_close(&db);