replaced preferences with littlefs

This commit is contained in:
jusax23 2023-09-21 16:04:40 +02:00
parent b5fb77fe35
commit 2f561cdcdd
Signed by: jusax23
GPG key ID: 499E2AA870C1CD41
4 changed files with 131 additions and 48 deletions

46
fs_helper.h Normal file
View file

@ -0,0 +1,46 @@
#include <LittleFS.h>
#include <jmutex.h>
#include "FS.h"
mutex_t littelfs_mut;
void init_little_fs() {
mutex_init(&littelfs_mut);
mutex_enter_blocking(&littelfs_mut);
if (!LittleFS.begin()) {
LittleFS.format();
LittleFS.begin();
}
mutex_exit(&littelfs_mut);
}
bool fs_read(String path, void *buffer, size_t len) {
char centerData[len];
mutex_enter_blocking(&littelfs_mut);
File f = LittleFS.open(path, "r");
bool succ = false;
if (f) {
if (f.readBytes((char *)centerData, len) == len) {
memmove(&buffer, &centerData, len);
succ = true;
}
f.close();
}
mutex_exit(&littelfs_mut);
return succ;
}
bool fs_write(String path, void *buffer, size_t len) {
mutex_enter_blocking(&littelfs_mut);
File f = LittleFS.open(path, "w");
bool succ = false;
if (f) {
if (f.write((const unsigned char *)buffer, len) == len) {
succ = true;
}
f.close();
}
mutex_exit(&littelfs_mut);
return succ;
}

View file

@ -16,12 +16,14 @@
#define debugReceive false #define debugReceive false
#endif #endif
#include <Preferences.h> // #include <Preferences.h>
#include <SPI.h> #include <SPI.h>
#include <Wire.h> #include <Wire.h>
#include <jmutex.h> #include <jmutex.h>
#include <jqueue.h> #include <jqueue.h>
#include "fs_helper.h"
// //
#include "structsAndDefines.h" #include "structsAndDefines.h"
@ -37,8 +39,6 @@ void jSetOnData(on_data conn) { _j_on_data = conn; }
// 3 (CE) 0 //4 (CSN) 4 //5 (SCK) 18 //6 (MOSI) 23 //7 (MISO) 19 // 3 (CE) 0 //4 (CSN) 4 //5 (SCK) 18 //6 (MOSI) 23 //7 (MISO) 19
// CE CSN // CE CSN
Preferences preferences;
unsigned long last = 0; unsigned long last = 0;
unsigned long delta = 0; unsigned long delta = 0;
float avg = 10; float avg = 10;
@ -57,13 +57,7 @@ void jSetup() {
#if debug #if debug
Serial.println("start up"); Serial.println("start up");
#endif #endif
#if debug
bool prefsucc =
#endif
preferences.begin("data", false);
#if debug
if (!prefsucc) Serial.println("prefs false");
#endif
initRC(); initRC();
#if debug #if debug
Serial.println("inited rc"); Serial.println("inited rc");
@ -89,7 +83,7 @@ void jSetup() {
last = millis(); last = millis();
} }
void jSensorLoop() { void jSensorLoop() {
unsigned long now = millis(); unsigned long now = micros();
delta = now - last; delta = now - last;
last = now; last = now;
avg = avg * 0.9 + delta * 0.1; avg = avg * 0.9 + delta * 0.1;

View file

@ -17,6 +17,12 @@ typedef struct {
#ifdef jCOMP #ifdef jCOMP
typedef struct {
float x;
float y;
float z;
} compassPrefData;
#include <QMC5883LCompass.h> #include <QMC5883LCompass.h>
QMC5883LCompass compass; QMC5883LCompass compass;
@ -24,12 +30,21 @@ bool compass_is_calibrating = false;
void initCOMP() { void initCOMP() {
compass.init(); compass.init();
compass.setCalibrationOffsets(preferences.getFloat("compOffX", 0),
preferences.getFloat("compOffY", 0), compassPrefData buffer;
preferences.getFloat("compOffZ", 0));
compass.setCalibrationScales(preferences.getFloat("compScaleX", 1), if (!fs_read("compOff", &buffer, sizeof(buffer))) {
preferences.getFloat("compScaleY", 1), buffer.x = 0;
preferences.getFloat("compScaleZ", 1)); buffer.y = 0;
buffer.z = 0;
}
compass.setCalibrationOffsets(buffer.x, buffer.y, buffer.z);
if (!fs_read("compScale", &buffer, sizeof(buffer))) {
buffer.x = 1;
buffer.y = 1;
buffer.z = 1;
}
compass.setCalibrationScales(buffer.x, buffer.y, buffer.z);
} }
void COMPLoop() { void COMPLoop() {
@ -58,13 +73,15 @@ void COMPradioTask() {
delay(10); delay(10);
compass.calibrate(); compass.calibrate();
preferences.putFloat("compOffX", compass.getCalibrationOffset(0)); compassPrefData buffer;
preferences.putFloat("compOffY", compass.getCalibrationOffset(1)); buffer.x = compass.getCalibrationOffset(0);
preferences.putFloat("compOffZ", compass.getCalibrationOffset(2)); buffer.y = compass.getCalibrationOffset(1);
buffer.z = compass.getCalibrationOffset(2);
preferences.putFloat("compScaleX", compass.getCalibrationScale(0)); fs_write("compOff", &buffer, sizeof(buffer));
preferences.putFloat("compScaleY", compass.getCalibrationScale(1)); buffer.x = compass.getCalibrationScale(0);
preferences.putFloat("compScaleZ", compass.getCalibrationScale(2)); buffer.y = compass.getCalibrationScale(1);
buffer.z = compass.getCalibrationScale(2);
fs_write("compScale", &buffer, sizeof(buffer));
compass_is_calibrating = false; compass_is_calibrating = false;
} break; } break;
} }

View file

@ -32,6 +32,12 @@ typedef struct {
#ifdef jMPU6050 #ifdef jMPU6050
typedef struct {
float x;
float y;
float z;
} mpuPrefData;
#include <MPU6050_jusax23.h> #include <MPU6050_jusax23.h>
MPU6050 mpu6050(Wire, 0.001f, 0.999f); MPU6050 mpu6050(Wire, 0.001f, 0.999f);
@ -42,12 +48,22 @@ void initMPU() {
Serial.println("began mpu6050"); Serial.println("began mpu6050");
#endif #endif
mpu6050.setGyroOffsets(preferences.getFloat("mpuOffGyrX", -7.8), mpuPrefData buffer;
preferences.getFloat("mpuOffGyrY", 2.32),
preferences.getFloat("mpuOffGyrZ", -0.22)); if (!fs_read("mpuOffGyr", &buffer, sizeof(buffer))) {
mpu6050.setAccXoffset(preferences.getFloat("mpuOffAccX", 0.03), buffer.x = -7.8;
preferences.getFloat("mpuOffAccY", 0.0), buffer.y = 2.32;
preferences.getFloat("mpuOffAccZ", 0.3)); buffer.z = -0.22;
}
mpu6050.setGyroOffsets(buffer.x, buffer.y, buffer.z);
if (!fs_read("mpuOffAcc", &buffer, sizeof(buffer))) {
buffer.x = 0.03;
buffer.y = 0.0;
buffer.z = 0.3;
}
mpu6050.setAccXoffset(buffer.x, buffer.y, buffer.z);
#if debug #if debug
Serial.println("set mpu6050 gyro"); Serial.println("set mpu6050 gyro");
#endif #endif
@ -128,18 +144,16 @@ void MPUradioTask() {
float offX = mpu6050.calcAccXoffset(); float offX = mpu6050.calcAccXoffset();
float offY = mpu6050.calcAccYoffset(); float offY = mpu6050.calcAccYoffset();
float offZ = mpu6050.calcAccZoffset(); float offZ = mpu6050.calcAccZoffset();
preferences.putFloat("mpuOffAccX", offX); mpuPrefData buffer{.x = offX, .y = offY, .z = offZ};
preferences.putFloat("mpuOffAccY", offY); fs_write("mpuOffAcc", &buffer, sizeof(buffer));
preferences.putFloat("mpuOffAccZ", offZ);
mpu6050.setAccXoffset(offX, offY, offZ); mpu6050.setAccXoffset(offX, offY, offZ);
} break; } break;
case jCommand_MPU_set_calc_offsets_gyro: { case jCommand_MPU_set_calc_offsets_gyro: {
float offX = mpu6050.getGyroXoffsetGlide(); float offX = mpu6050.getGyroXoffsetGlide();
float offY = mpu6050.getGyroYoffsetGlide(); float offY = mpu6050.getGyroYoffsetGlide();
float offZ = mpu6050.getGyroZoffsetGlide(); float offZ = mpu6050.getGyroZoffsetGlide();
preferences.putFloat("mpuOffGyrX", offX); mpuPrefData buffer{.x = offX, .y = offY, .z = offZ};
preferences.putFloat("mpuOffGyrY", offY); fs_write("mpuOffGyr", &buffer, sizeof(buffer));
preferences.putFloat("mpuOffGyrZ", offZ);
mpu6050.setGyroOffsets(offX, offY, offZ); mpu6050.setGyroOffsets(offX, offY, offZ);
} break; } break;
case jCommand_MPU_get_calc_offsets_acc: { case jCommand_MPU_get_calc_offsets_acc: {
@ -161,34 +175,46 @@ void MPUradioTask() {
case jCommand_MPU_set_offsets_acc: { case jCommand_MPU_set_offsets_acc: {
mpuSendData data; mpuSendData data;
memmove(&data, &jRCbuff, sizeof(data)); memmove(&data, &jRCbuff, sizeof(data));
preferences.putFloat("mpuOffGyrX", data.X); mpuPrefData buffer{.x = data.X, .y = data.Y, .z = data.Z};
preferences.putFloat("mpuOffGyrY", data.Y); fs_write("mpuOffAcc", &buffer, sizeof(buffer));
preferences.putFloat("mpuOffGyrZ", data.Z);
mpu6050.setGyroOffsets(data.X, data.Y, data.Z); mpu6050.setGyroOffsets(data.X, data.Y, data.Z);
} break; } break;
case jCommand_MPU_set_offsets_gyro: { case jCommand_MPU_set_offsets_gyro: {
mpuSendData data; mpuSendData data;
memmove(&data, &jRCbuff, sizeof(data)); memmove(&data, &jRCbuff, sizeof(data));
preferences.putFloat("mpuOffAccX", data.X); mpuPrefData buffer{.x = data.X, .y = data.Y, .z = data.Z};
preferences.putFloat("mpuOffAccY", data.Y); fs_write("mpuOffGyr", &buffer, sizeof(buffer));
preferences.putFloat("mpuOffAccZ", data.Z);
mpu6050.setAccXoffset(data.X, data.Y, data.Z); mpu6050.setAccXoffset(data.X, data.Y, data.Z);
} break; } break;
case jCommand_MPU_get_offsets_acc: { case jCommand_MPU_get_offsets_acc: {
mpuSendData data; mpuSendData data;
data.command = jCommand_MPU_get_offsets_acc; data.command = jCommand_MPU_get_offsets_acc;
data.X = preferences.getFloat("mpuOffGyrX", -7.8);
data.Y = preferences.getFloat("mpuOffGyrY", 2.32); mpuPrefData buffer;
data.Z = preferences.getFloat("mpuOffGyrZ", -0.22); if (!fs_read("mpuOffAcc", &buffer, sizeof(buffer))) {
buffer.x = 0.03;
buffer.y = 0.0;
buffer.z = 0.3;
}
data.X = buffer.x;
data.Y = buffer.y;
data.Z = buffer.z;
jSendNRF(&data, sizeof(data)); jSendNRF(&data, sizeof(data));
} break; } break;
case jCommand_MPU_get_offsets_gyro: { case jCommand_MPU_get_offsets_gyro: {
mpuSendData data; mpuSendData data;
data.command = jCommand_MPU_get_offsets_gyro; data.command = jCommand_MPU_get_offsets_gyro;
data.X = preferences.getFloat("mpuOffAccX", 0.03);
data.Y = preferences.getFloat("mpuOffAccY", 0.0); mpuPrefData buffer;
data.Z = preferences.getFloat("mpuOffAccZ", 0.3); if (!fs_read("mpuOffGyr", &buffer, sizeof(buffer))) {
buffer.x = -7.8;
buffer.y = 2.32;
buffer.z = -0.22;
}
data.X = buffer.x;
data.Y = buffer.y;
data.Z = buffer.z;
jSendNRF(&data, sizeof(data)); jSendNRF(&data, sizeof(data));
} }
} }