I have 2 microchips that I want to control using a Raspberry Pico. As I'm familiar with the Arduino environment, I'm using the Arduino Pico core. This library offers the SerialPIO library used to transmit serial data on the pins rather than the USB port.
Both of my classes need use this library. So I want to create the SerialPIO object in my main .ino file, and then pass it to both classes and store it in the devices classes for later use. I know that passing as a reference is the way to go, but I'm having problems. When I try to store the reference to the SerialPIO object in the device class I get the error:
error: cannot convert 'SerialPIO' to 'SerialPIO*' in assignment
Here is my minimal example showing where I get stuck: Main .ino file
Thank you all for the quick replies. I've changes SerialPIO *_spio in MyClass.h to SerialPIO &_spio and _spio->print(data) to _spio.print(data), but now I'm getting different errors:
error: uninitialized reference member in 'class SerialPIO&' [-fpermissive] 4 | MyClass::MyClass(SerialPIO &spio) | ^~~~~~~ In file included from C:\Users\...\Documents\Arduino\Class_as_parameter_Test\src\MyClass\MyClass.cpp:2: C:\Users\...\Documents\Arduino\Class_as_parameter_Test\src\MyClass\MyClass.h:12:14: note: 'SerialPIO& MyClass::_spio' should be initialized 12 | SerialPIO &_spio; | ^~~~~
I've then added Juraj suggestion, but this only gave more errors. What exactly should this part do? I've never seen that syntex before.
error: class 'MyClass' does not have any field named 'spio' 4 | MyClass::MyClass(SerialPIO &_spio) : spio(_spio) {} | ^~~~ C:\Users\Bas\Documents\Arduino\Class_as_parameter_Test\src\MyClass\MyClass.cpp:4:1: error: uninitialized reference member in 'class SerialPIO&' [-fpermissive] 4 | MyClass::MyClass(SerialPIO &_spio) : spio(_spio) {} | ^~~~~~~
Btw, just for fun, I pasted my orgininal question 1:1 in ChatGPT. It gave the same solution as you. I was genuinely shocked it was able to do that! Even the formatting is correct for this forum
ChatGPT response
It seems like you're trying to pass a reference to the SerialPIO object to your MyClass constructor and store it as a pointer. The issue you're encountering is due to a mismatch between the variable types. The SerialPIO object is not a pointer, but you're trying to assign it to a pointer variable.
To resolve this, you need to modify your code as follows:
Main .ino file:
#include "src/MyClass/MyClass.h"
SerialPIO spio(0, 1);
MyClass test(&spio); // Pass the address of the SerialPIO object
void setup() {
spio.begin(9600);
}
void loop() {
char string[] = "hello";
test.printstring(string);
}
cpp file:
#include "Arduino.h"
#include "MyClass.h"
MyClass::MyClass(SerialPIO* spio) { // Change the parameter type to pointer
_spio = spio;
}
void MyClass::printstring(char data[]) {
_spio->print(data);
}
h file:
#ifndef MyClass_h
#define MyClass_h
#include "Arduino.h"
class MyClass {
public:
MyClass(SerialPIO* spio); // Change the parameter type to pointer
void printstring(char data[]);
private:
SerialPIO* _spio; // Change the variable type to pointer
};
#endif
By using &spio in the main .ino file, you are passing the address of the SerialPIO object. In the MyClass constructor, you should change the parameter type to SerialPIO* to indicate that it's a pointer. Similarly, in the _spio member variable declaration, you should use SerialPIO* to declare it as a pointer.
With these changes, you should be able to store the reference to the SerialPIO object in your MyClass for later use.