diff --git a/MultiNode/MNLib.cpp b/MultiNode/MNLib.cpp index 263ea27..7daf215 100644 --- a/MultiNode/MNLib.cpp +++ b/MultiNode/MNLib.cpp @@ -339,6 +339,11 @@ DeviceBase* getDevice(uint8_t pointer) return new Device; } +void storeDevice(uint8_t pointer, DeviceBase* device) +{ + memcpy(&configuration_memory[pointer], reinterpret_cast(device) + 4, device->size()); +} + void loopMN() { receive(); diff --git a/MultiNode/MNLib.h b/MultiNode/MNLib.h index d7782b3..0d34745 100644 --- a/MultiNode/MNLib.h +++ b/MultiNode/MNLib.h @@ -81,6 +81,7 @@ struct DeviceBase virtual bool doSend() = 0; virtual void initialize() = 0; virtual void printStatus() = 0; + virtual size_t size() = 0; // Returns sizeof(T) - 4 (4 for the pointer) } __attribute__ ((packed)); static_assert(sizeof(DeviceBase) == 7, "Device size wrong"); @@ -102,6 +103,10 @@ struct Device : DeviceBase { SerialUSB.println("This is a dummy device"); } + size_t size() // Maybe this can be solved with templates too + { + return sizeof(Device) - 4; + } } __attribute__ ((packed)); 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.println(pin - 14); } + size_t size() // Maybe this can be solved with templates too + { + return sizeof(AnalogInput) - 4; + } } __attribute__ ((packed)); static_assert(sizeof(AnalogInput) == 24, "AnalogInput size wrong"); @@ -166,15 +175,16 @@ void initMN(); void test(); void printStatusReport(); void setLoopPower(bool state); -void saveMemory(); -void loadMemory(); +void saveMemory(); // Save everything to flash +void loadMemory(); // Load everything (configuration, cfgmem) from flash void refreshConfig(); float batteryVoltage(); void errorBlink(int n); // Quickly blink n times bool send(uint8_t data[], uint8_t len); bool receive(); 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 sendSensorData(); void sendDeviceData(); diff --git a/MultiNode/MultiNode.ino b/MultiNode/MultiNode.ino index be32348..3e866da 100644 --- a/MultiNode/MultiNode.ino +++ b/MultiNode/MultiNode.ino @@ -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 char command = SerialUSB.read(); + switch (command) { case 'C': // Print configuration for in the field debugging