Add method to store a device in cfgmem

This commit is contained in:
Terra 2022-04-24 15:15:09 +02:00
parent c63bd9d2d7
commit 914dfdb1f3
3 changed files with 19 additions and 3 deletions

View File

@ -339,6 +339,11 @@ DeviceBase* getDevice(uint8_t pointer)
return new Device; return new Device;
} }
void storeDevice(uint8_t pointer, DeviceBase* device)
{
memcpy(&configuration_memory[pointer], reinterpret_cast<uint8_t*>(device) + 4, device->size());
}
void loopMN() void loopMN()
{ {
receive(); receive();

View File

@ -81,6 +81,7 @@ struct DeviceBase
virtual bool doSend() = 0; virtual bool doSend() = 0;
virtual void initialize() = 0; virtual void initialize() = 0;
virtual void printStatus() = 0; virtual void printStatus() = 0;
virtual size_t size() = 0; // Returns sizeof(T) - 4 (4 for the pointer)
} __attribute__ ((packed)); } __attribute__ ((packed));
static_assert(sizeof(DeviceBase) == 7, "Device size wrong"); static_assert(sizeof(DeviceBase) == 7, "Device size wrong");
@ -102,6 +103,10 @@ struct Device : DeviceBase
{ {
SerialUSB.println("This is a dummy device"); SerialUSB.println("This is a dummy device");
} }
size_t size() // Maybe this can be solved with templates too
{
return sizeof(Device) - 4;
}
} __attribute__ ((packed)); } __attribute__ ((packed));
static_assert(sizeof(Device) == 7, "Device size wrong"); static_assert(sizeof(Device) == 7, "Device size wrong");
@ -141,6 +146,10 @@ struct AnalogInput : Device // 20 Bytes, each device gets about 17.06 bytes on a
SerialUSB.print("AnalogInput on pin A"); SerialUSB.print("AnalogInput on pin A");
SerialUSB.println(pin - 14); SerialUSB.println(pin - 14);
} }
size_t size() // Maybe this can be solved with templates too
{
return sizeof(AnalogInput) - 4;
}
} __attribute__ ((packed)); } __attribute__ ((packed));
static_assert(sizeof(AnalogInput) == 24, "AnalogInput size wrong"); static_assert(sizeof(AnalogInput) == 24, "AnalogInput size wrong");
@ -166,15 +175,16 @@ void initMN();
void test(); void test();
void printStatusReport(); void printStatusReport();
void setLoopPower(bool state); void setLoopPower(bool state);
void saveMemory(); void saveMemory(); // Save everything to flash
void loadMemory(); void loadMemory(); // Load everything (configuration, cfgmem) from flash
void refreshConfig(); void refreshConfig();
float batteryVoltage(); float batteryVoltage();
void errorBlink(int n); // Quickly blink n times void errorBlink(int n); // Quickly blink n times
bool send(uint8_t data[], uint8_t len); bool send(uint8_t data[], uint8_t len);
bool receive(); bool receive();
void initializeDevices(); void initializeDevices();
DeviceBase* getDevice(uint8_t pointer); DeviceBase* getDevice(uint8_t pointer); // Gets the device at the specified location from cfgmem
void storeDevice(uint8_t pointer, DeviceBase* device); // Stores a device at the specified location in cfgmem (not in flash)
void loopMN(); void loopMN();
void sendSensorData(); void sendSensorData();
void sendDeviceData(); void sendDeviceData();

View File

@ -46,6 +46,7 @@ void loop()
{ {
size_t config_size = sizeof(MNConfiguration) - sizeof(configuration.client_secret_key) - sizeof(configuration.server_secret_key); // Don't leak the secret key size_t config_size = sizeof(MNConfiguration) - sizeof(configuration.client_secret_key) - sizeof(configuration.server_secret_key); // Don't leak the secret key
char command = SerialUSB.read(); char command = SerialUSB.read();
switch (command) switch (command)
{ {
case 'C': // Print configuration for in the field debugging case 'C': // Print configuration for in the field debugging