You can find the final version here: WireWrapper
I am trying to refactor my code. I'm currently working on the part that uses i2c modules that should be as dynamic as possible so that I do the loose coupling principle of good software design at least a bit justice.
I'm obiviously overlooking something. I don't want to use vectors at this point (no need for a dynamic structure), seems my memory is being overwritten or something.
An other classes uses it:
Header file:
CPP file:
Unfortunately I seem to be overwriting my memory since the array becomes corrupt after that. What do I overlook? I tried using new etc. to no avail. At this point the array doesn't need to be dynamic.
I am trying to refactor my code. I'm currently working on the part that uses i2c modules that should be as dynamic as possible so that I do the loose coupling principle of good software design at least a bit justice.
I'm obiviously overlooking something. I don't want to use vectors at this point (no need for a dynamic structure), seems my memory is being overwritten or something.
Code:
#pragma once
#include "Arduino.h"
#ifndef MyModule_h
#define MyModule_h
class MyModule {
private:
public:
void print();
void printName();
MyModule();
MyModule(byte _id, char* _name);
/*
Public:
niet heel erg mooi, maar in dit geval
zijn getters / setters wat overdreven
*/
byte id;
char *name;
boolean available;
};
#endif
[B]CPP of the Object:[/B]
#pragma once
#ifndef MyModule_cpp
#define MyModule_cpp
#include "MyModule.h"
#include "Arduino.h"
MyModule::MyModule() {
// default constructor
id = 0;
name = "";
available = false;
}
MyModule::MyModule(byte _id, char *_name) {
id = _id;
name = _name;
available = false;
}
void MyModule::print() {
Serial.print("ID: ");
Serial.print(id, HEX);
Serial.print(" name: ");
Serial.print(name);
Serial.print(" available: ");
Serial.print(available);
}
void MyModule::printName() {
Serial.println(name);
}
#endif
An other classes uses it:
Header file:
Code:
const numModules = 10;
class myWire
{
private:
myWire(); // constructor
MyModule myArrayOfModules[numModules];
public:
void doSomething();
}
CPP file:
Code:
myWire::myWire()
{
MyModule mainModule1 = MyModule(0x40, "Module 1");
myArrayOfModules[0] = mainModule1;
MyModule mainModule2 = MyModule(0x41, "Module 2");
myArrayOfModules[1] = mainModule2;
MyModule mainModule3 = MyModule(0x44, "Module 3");
myArrayOfModules[2] = mainModule3;
}
void myWire::doSomething {
Serial.println(myArrayOfModules[2].name); // oops, looks corrupt now...
}
Unfortunately I seem to be overwriting my memory since the array becomes corrupt after that. What do I overlook? I tried using new etc. to no avail. At this point the array doesn't need to be dynamic.
Last edited: