Creating a library issue

Hi all,
I am building a robotic project that requires I2C data transfer from a master which is called MINX and lots of slave boards which have sensors and actuators on them.

The MINX part is working well in the main as are the slaves if I have everything in an .ino file.

Breakdown of what I am trying to do.

For ease and consistency I want to move most of the coms side of the slaves to a library. This is my first attempt at a library file construction so I am struggling with it.

Basically what I want to be able to do is provide the I2C address, the board name, the number of sensors and the number of actuators along with the data to and from these and some control data.

I am sending and receiving data from the MINX via a 10 byte packet. This packet is created and collected into arrays TXArray and RXArray.

The arrays are looked at within the library calls and the response is actioned in the .ino if required.
As I have said this is my first attempt so I am building function by function and testing as I go. You will see that I have not got far, the sendEvent and receiveEvent code I used in the original code will not work here and as such I have remarked it out. I just need to see what I’m doing wrong so I can continue with the library. The TXArray is filled in another location in the library and the RXArray fis filled from the data from the bus.

henriComs.cpp

# include "henriComs.h"
# include "HardwareSerial.h"
# include "Wire.h"
henriComs::henriComs( HardwareSerial &print ) {
 printer = &print; //operate on the address of print
}
void henriComs::begin() {
 printer->begin(115200);
} // end of begin

void henriComs::test(String (Test1), int (address)) 
{ // this is the place to convert from public to private variable names
 String (_test) = String (Test1);  // the test references need to be changed to boardName here and in the .h
 int (_address) = int (address);
 
 printer->print("This is the power up notification for the "); printer->print(_test);  Serial.print(" board.  The I2C address is: ");  Serial.println(_address);
 printer->println(" ");
 printer->print("There are "); printer->print("x"); printer->print(" sensors configured on this board and "); printer->print("y"); printer->println(" actuators configured on this board");

 Wire.onRequest(sendEvent);
 Wire.onReceive(receiveEvent);
 Wire.begin(_address); // this is here to pick up the DeviceId address


 // printer->println(String (_test));
} //end of :: test

/*
void sendEvent(int howMany) { // This function sends data from the .ino to the I2C Bus
 int  _sendInt = 1; // this lets me know this is running in a later part of the code

 for (int _i = 0; _i < (sizeof(_TXArray)) / 2; _i ++ )  // I GET AN ERROR MESSAGE HERE, do I need to reference back to henriComs where it was initialised on the .h file
 {
   Wire.write(highByte(_TXArray[_i])); //I2C is byte-oriened Bus
   Wire.write(lowByte(_TXArray[_i]));
 }
 _sendInt = 0;
} // End of  sendEvent
*/
/*
void receiveEvent(int howMany) //howMany is always equal to bytes received   // This function receives data from the I2C Bus and sends to the application
{
 for (int i = 0; i < howMany / 2; i++)
 {
   int _x = Wire.read();
   _j++;
   if (_j == 1)
   {
     int _y = (_x << 8) | Wire.read();
     _j = 0;
     _RXArray[i] = _y;
   }
 }
} // End of receiveEvent
*/

henriComs.h

/*(C) CHANNE 2020
* henriComs.h Library for the arduino slave device to communicate to the MINX using the MINX packet transfer protocol
*/

#ifndef _HENRICOMS_H
#define _HENRICOMS_H
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "Wire.h"

class henriComs {
 public:
   //pass a reference to a Print object............ the test references need to be changed here and in the CPP
   henriComs(HardwareSerial &print);
   void begin( );
   void test(String (Test1), int (address));
   
 private:
   HardwareSerial* printer;
   String (_test);
   int (_address);
   int (sendEvent);
   int (receiveEvent);
   int (_TXArray[5]);
   int (_RXArray[5]);
   
   
};
#endif

Test.ino

/*(C) CHANNE 2020
* This is the example code for using the henriComs library
* The henriComs library does all the background work for the slave Arduino boards that connect to the MINX via the I2C bus.  This uses the MINX token protocol for data transmission. 
* the variables and the function connections in the setup are absolutely required for correct operation.
*/

#include "henriComs.h"
#include <Wire.h>
henriComs myLibrary(Serial);        // this coms test function is called myLibrary

String boardName = ("Test Board");  // for monitoring
int boardAddress = 2;               // this is the I2C address
int noOfSensors = 2;                // this is the number of sensors installed to be used in the set up via a for loop for  storing and sending data
int sensorNumber = 0;               // this is the actual sensor being read used in the for loop
int sensorValue = 0;                // this is the value of the actual sensor being read used in the for loop
int sensorSensitivity = 4;          // this is the actual sensor sensitivity setting also used in the for loop
int noOfActuators = 2;              // this is the number of actuators
int actuatorNumber = 0;             // this is the actual actuator number
int actuatorValue = 0;              // this is the actual actuator value
int boardDefaultUrgency = 4;        // this is the board default urgency level on startup
bool boardFault = 0;                // this is to advise the MINX there is a fault in the board

void setup() {
 myLibrary.begin();
 myLibrary.test(String (boardName), int (boardAddress));
}
void loop() {
}

It's the smileys and italics :wink:

Please edit your post and add
** **[code]** **
before your code and
** **[/code]** **
after your code.

Hi,

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :slight_smile: