Skip to content

Instantly share code, notes, and snippets.

@0xPIT
Created January 16, 2016 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0xPIT/93a42eec15e9e93d1738 to your computer and use it in GitHub Desktop.
Save 0xPIT/93a42eec15e9e93d1738 to your computer and use it in GitHub Desktop.
typedef struct __attribute__((packed)) {
double kP;
double kI;
double kD;
uint8_t checksum;
} PID_t;
PID_t pidParam = { 16.0, 0.10, 1.0, 0 };
void setup() {
Serial.begin(115200);
while (!Serial);
if (!SPIFFS.begin()) {
Serial.println("Failed to mount file system");
return;
}
else {
Serial.println("file system mounted.");
}
}
// xt_rsil does not prevent timer0 interrupts to fire, so we remove the ISR manually.
//
class InterruptLock {
public:
InterruptLock() {
Tmr.stopInterrupt();
_state = xt_rsil(15);
}
~InterruptLock() {
xt_wsr_ps(_state);
Tmr.initInterrupt(&timerIsrNoTable);
}
uint32_t savedInterruptLevel() const {
return _state & 0x0f;
}
protected:
uint32_t _state;
};
const char* configFile = "/pid.conf";
bool loadParameters() {
InterruptLock lock;
File fd = SPIFFS.open(configFile, "r");
if (!fd) {
return false;
}
size_t size = fd.size();
if (size < 1) {
fd.close();
return false;
}
fd.read((uint8_t *)&pidParam, sizeof(PID_t));
fd.close();
return pidParam.checksum == crc8((uint8_t *)&pidParam, sizeof(PID_t) - sizeof(uint8_t));
}
bool saveParameters() {
InterruptLock lock;
File fd = SPIFFS.open(configFile, "w");
if (!fd) {
return false;
}
pidParam.checksum = crc8((uint8_t *)&pidParam, sizeof(PID_t) - sizeof(uint8_t));
size_t written = fd.write((uint8_t *)&pidParam, sizeof(PID_t));
fd.flush();
fd.close();
return written == sizeof(PID_t);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment