9 years, 4 months ago.  This question has been closed. Reason: Duplicate question

Help with classes

I'm trying to create a class that contains a private CAN bus. I've going through the cook book "Writing a library" and I figured out how to cover most of my class but I'm stumped on getting the CAN correctly.

Car.h

#ifndef MBED_CAR_H
#define MBED_CAR_H

#include "mbed.h"

class Car
{

public:
    
    Car();
    double _test;
    
private:
    CAN _canBus;

};

#endif

and then

Car.cpp

#include "Car.h"
#include "mbed.h"

Car::Car()
{
    _test = 10;
    
    CAN _canBus(p30, p29);
    _canBus.frequency(250000);
}

void Car::CanRcv()
{
    DigitalOut led1(LED1);
    CANMessage msg;

    if(_canBus.read(msg)) {
        led1 = !led1;
    }

}

I'm certain that isn't the correct way to go about it, and I've tried to do it in a similar fashion to the tutorial before, but I can't quite get it.

The code above isn't really something I expected to work, more just a representation of what I want to happen. Where _canBus is a private member, and is then initialized in the constructor. It does not have to occur exactly like this, my intention is only to have the CAN bus initialized as a part of the class.

1 Answer

9 years, 4 months ago.

Random example on how to do it (but then DigitalOuts): http://developer.mbed.org/users/simon/code/TextLCD/file/308d188a2d3a/TextLCD.cpp

In other words, in your .cpp file you need to add at line 4 :_canBus(p30, p29), then remove line 8.

Accepted Answer