I have a library class which requires a struct object reference to be passed to the constructor. My struct is contained in a separate header file, because I want the struct to be able to vary (not just the value of the struct members, but the different members themselves). I have to determine the sizeof the struct inside the class, but it doesn't seem to work. It seems like I need to include the struct in my library, which I don't want to do.
Post the code you have.
It's not pretty. Here's my header file for my class:
#ifndef RS485Network_h
#define RS485Network_h
#pragma once
//#include "Arduino.h"
#include <RS485_non_blocking.h>
class RS485Network
{
private:
Stream& RS485Serial;
Stream& debugSerial;
struct& rs485packet;
//RS485 hub(int, int, int, int);
unsigned long BAUD_RATE, TIME_BETWEEN_MESSAGES;
uint8_t XMIT_ENABLE_PIN;
uint8_t myAddress;
uint8_t numberOfDevices;
float TIME_PER_BYTE, PACKET_LENGTH, PACKET_TIME;
unsigned long noMessagesTimeout;
uint8_t nextAddress;
unsigned long lastMessageTime;
unsigned long lastCommsTime;
unsigned long randomTime;
// Initial seed for JKISS32
static unsigned long x, y, z, w, c;
void RS485Setup();
unsigned long JKISS32 ();
void Seed_JKISS32(const unsigned long newseed);
enum {
STATE_NO_DEVICES,
STATE_RECENT_RESPONSE,
STATE_TIMED_OUT,
} state;
// enum {
// ROOM_VAULT,
// ROOM_SLEUTH,
// ROOM_HOSPITAL,
// ROOM_DUNGEON
// } room;
// callbacks for the non-blocking RS485 library
size_t fWrite (const uint8_t what) { RS485Serial.write (what); }
int fAvailable () { return RS485Serial.available (); }
int fRead () { lastCommsTime = micros(); return RS485Serial.read ();}
public:
RS485Network(byte room_, Stream& RS485Serial_, Stream& debugSerial_, struct& rs485packet_, uint8_t XMIT_ENABLE_PIN_, uint8_t myAddress_, uint8_t numberOfDevices_,
unsigned long BAUD_RATE_ = 9600, unsigned long TIME_BETWEEN_MESSAGES_ = 3000)
: , RS485Serial(RS485Serial_), debugSerial(debugSerial_), XMIT_ENABLE_PIN(XMIT_ENABLE_PIN_),
myAddress(myAddress_), numberOfDevices(numberOfDevices_),
BAUD_RATE(BAUD_RATE_), TIME_BETWEEN_MESSAGES(TIME_BETWEEN_MESSAGES_), rs485packet(rs485packet_)
{
// RS485 hub(fRead, fAvailable, fWrite, 20);
RS485Setup();
}
RS485Network(Stream& RS485Serial_, struct& rs485packet_, uint8_t
XMIT_ENABLE_PIN_, uint8_t myAddress_, uint8_t numberOfDevices_,
unsigned long BAUD_RATE_ = 9600, unsigned long TIME_BETWEEN_MESSAGES_ = 3000)
: RS485Serial(RS485Serial_),
XMIT_ENABLE_PIN(XMIT_ENABLE_PIN_), myAddress(myAddress_), numberOfDevices(numberOfDevices_),
BAUD_RATE(BAUD_RATE_), TIME_BETWEEN_MESSAGES(TIME_BETWEEN_MESSAGES_), rs485packet(rs485packet_)
{
// RS485 hub(fRead, fAvailable, fWrite, 20);
RS485Setup();
}
};
#endif
I have two constructors because I want them to have the option of giving the library access to the debugSerial. If there is a better way to do that with only one constructor, let me know.
The issue in question is the rs485packet as a parameter for the constructor. This info comes from a separate header file not included in the library, but included at the top of the sketch.
"vault_header.h":
struct {
byte defaultHubArray[2];
byte cashRegister;
byte tubes;
byte exitCode;
int exitCodeEntry;
byte controlPanel[3];
long controlPanelCodeEntry;
byte dotPuzzle;
byte armHole[2];
byte lightKeypad;
long blankKeypadEntry;
byte lights;
} rs485packet;
The makeup of rs485packet will change depending on which header file I include at the top of my sketch.
That's not the only thing wrong with what I have so far, but it's what I've been trying to figure out this afternoon.
You didn't provide enough info for a specific answer. An COMPLETE MCVE would help immensely. Please don't post the entire project if it's large, messy, and contains tons clutter unrelated to the problem. Again, an MCVE is best.
In general, the compiler needs to know the specifics of the struct when it compiles the class's .cpp file. So, one possibility is to put the declarations of all the possible different structs into a single header file and #include that in the .h file for your class. Of course, each different struct is a different datatype, so you'd have to accommodate that.
Again, with more specifics a better answer may be possible. Perhaps by using tricky templates, polymorphism, or putting the entire class implementation inside the .h file.