Initial commit
This commit is contained in:
48
lib/rtlf.cpp
Normal file
48
lib/rtlf.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
#include "rtlf.h"
|
||||
|
||||
#include "cassert"
|
||||
|
||||
static int write_header_block(FILE* file, __uint32_t car_id, const char* track_name) {
|
||||
RTLF_Header_Fixed header;
|
||||
const size_t track_len = strlen(track_name);
|
||||
|
||||
memcpy(header.magic_number, RTLF_MAGIC_NUMBER, 4);
|
||||
header.version = RTLF_VERSION;
|
||||
header.car_id = car_id;
|
||||
header.track_name_len = static_cast<__uint32_t>(track_len);
|
||||
|
||||
assert(fwrite(&header, sizeof(RTLF_Header_Fixed), 1, file) == 1);
|
||||
assert(fwrite(track_name, 1, track_len, file) == track_len);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
RTLF_Context* rtlf_open_write(const char* filename, __uint32_t car_id, const char* track_name) {
|
||||
FILE* file = fopen(filename, "wb");
|
||||
|
||||
assert(file != nullptr);
|
||||
|
||||
write_header_block(file, car_id, track_name);
|
||||
auto* ctx = static_cast<RTLF_Context *>(malloc(sizeof(RTLF_Context)));
|
||||
if (!ctx) {
|
||||
fclose(file);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
ctx->file = file;
|
||||
ctx->track_name = strdup(track_name);
|
||||
return ctx;
|
||||
}
|
||||
|
||||
int rtlf_write_record(RTLF_Context* ctx, const RTLF_Data_Record* record) {
|
||||
if (!ctx || !ctx->file) return -1;
|
||||
if (fwrite(record, RTLF_RECORD_SIZE, 1, ctx->file) != 1) return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void rtlf_close(RTLF_Context* ctx) {
|
||||
if (!ctx) return;
|
||||
if (ctx->file) fclose(ctx->file);
|
||||
if (ctx->track_name) free(ctx->track_name);
|
||||
free(ctx);
|
||||
}
|
||||
53
lib/rtlf.h
Normal file
53
lib/rtlf.h
Normal file
@@ -0,0 +1,53 @@
|
||||
#ifndef MCRLCONTROLLER_RTLF_H
|
||||
#define MCRLCONTROLLER_RTLF_H
|
||||
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
|
||||
#define RTLF_MAGIC_NUMBER "RTLF"
|
||||
#define RTLF_VERSION 1
|
||||
#define RTLF_RECORD_SIZE 80 // 10 x float64 = 80 bytes
|
||||
|
||||
#pragma pack(push, 1)
|
||||
|
||||
// Fixed part of the Header Block (16 bytes)
|
||||
typedef struct {
|
||||
char magic_number[4]; // 0-4: 'RTLF'
|
||||
__uint32_t version; // 4-8: File format version (Must be 1)
|
||||
__uint32_t car_id; // 8-12: Unique numerical ID
|
||||
__uint32_t track_name_len;// 12-16: Length of the Track Name string that follows
|
||||
} RTLF_Header_Fixed;
|
||||
|
||||
// Single Data Record (80 bytes)
|
||||
typedef struct {
|
||||
double timestamp; // 0-8: Time (seconds)
|
||||
double gps_long; // 8-16: Position X (meters)
|
||||
double gps_lat; // 16-24: Position Y (meters)
|
||||
double facing_x; // 24-32: X-component of the car's orientation
|
||||
double facing_y; // 32-40: Y-component of the car's orientation
|
||||
double facing_z; // 40-48: Z-component of the car's orientation
|
||||
double acceleration_x; // 48-56: Acceleration X (m/s^2)
|
||||
double acceleration_y; // 56-64: Acceleration Y (m/s^2)
|
||||
double acceleration_z; // 64-72: Acceleration Z (m/s^2)
|
||||
double speed; // 72-80: Speed (m/s)
|
||||
} RTLF_Data_Record;
|
||||
|
||||
#pragma pack(pop)
|
||||
|
||||
typedef struct {
|
||||
FILE* file;
|
||||
RTLF_Header_Fixed header;
|
||||
char* track_name;
|
||||
long data_start_offset;
|
||||
} RTLF_Context;
|
||||
|
||||
RTLF_Context* rtlf_open_write(const char* filename, __uint32_t car_id, const char* track_name);
|
||||
|
||||
int rtlf_write_record(RTLF_Context* ctx, const RTLF_Data_Record* record);
|
||||
|
||||
void rtlf_close(RTLF_Context* ctx);
|
||||
|
||||
|
||||
#endif //MCRLCONTROLLER_RTLF_H
|
||||
33
lib/sd_reader.h
Normal file
33
lib/sd_reader.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#ifndef MCRLCONTROLLER_SD_READER_H
|
||||
#define MCRLCONTROLLER_SD_READER_H
|
||||
|
||||
#include "ff.h"
|
||||
#include "gpio_rp2350.h"
|
||||
#include "posix_io.h"
|
||||
#include "sd_spi_drv.h"
|
||||
#include "spi_rp2350.h"
|
||||
#include "cassert"
|
||||
|
||||
#define MISO_PIN 4
|
||||
#define MOSI_PIN 3
|
||||
#define SCLK_PIN 6
|
||||
#define CS_PIN 5
|
||||
|
||||
class SD_Reader {
|
||||
gpio_rp2350 cs;
|
||||
spi_rp2350 spi;
|
||||
sd_spi_drv sd;
|
||||
FatFs fs;
|
||||
public:
|
||||
SD_Reader() : cs(CS_PIN), spi(MISO_PIN, MOSI_PIN, SCLK_PIN, cs), sd(spi), fs(sd) {
|
||||
posix_io::inst.register_fileio(fs);
|
||||
assert(fs.mount() == FatFs::FR_OK);
|
||||
}
|
||||
~SD_Reader() {
|
||||
const FatFs::FRESULT erg = fs.umount();
|
||||
assert(erg == FatFs::FR_OK);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif //MCRLCONTROLLER_SD_READER_H
|
||||
Reference in New Issue
Block a user