The Inevitable Questions Section
- I don't understand the attach functions, could anyone shed some light on the subject and/or share some web-links?
- I had wanted to split my driver library into seeed_can, containing a SEEED_CAN class and functions and seeed_can_api, containing all of the driver functions but I can't work out how to tell seeed_can_api about the port pins and SPI interface declared in seeed_can. Can anyone share a method of passing this information from one cpp file to another?
1. Could you specify which part you don't understand? There are generally two attach functions, one for a normal function, one a bit more complicated for a function called upon an object of a class. Often it is easiest to use the FunctionPointer class (standard within mbed) to do the attaching (you just make a FunctionPointer object, and in your attach functions you call the FunctionPointer attach functions). Then when you want the function to be called, you do functionpointer.call().
2. The seeed_can_api would be pure C functions? Or also a class? If they are C functions you will need to send the interface as function argument. You can wrap the different things you need to send it in a structure, so you only need a single argument for that.
Thanks for your tips Erik
1. Makes some sense to me, at least I grasp enough to know what I'm trying to achieve and what to search for help with. I've written a little test program using Ticker's attach function to reset a Timer to help me understand what's what, but Im not sure which is the FunctionPointer object; 'minute' or 'void resetTimestamp()' ?
Serial pc(USBTX, USBRX); // USB serial port TTYACM0
Ticker minute;
Timer timestamp;
void resetTimestamp()
{
timestamp.reset();
}
int main()
{
minute.attach(&resetTimestamp, 60.0);
while (1) {
printf("time: %05d", i, timestamp.read_ms());
pc.getc();
}
}
2. Yes, seeed_can_api contains pure C functions and using a struct made perfect sense right up to the moment I tried to actually do it. At the moment my class constructor looks like this:
private:
SPI _spi;
DigitalOut _ncs;
DigitalIn _irq;
SEEED_CAN::SEEED_CAN(PinName ncs) :
_spi(SEEED_CAN_MOSI, SEEED_CAN_MISO, SEEED_CAN_CLK),
_ncs(ncs),
_irq(SEEED_CAN_IRQ),
irq(SEEED_CAN_IRQ)
{
// Make sure CS is high
_ncs = 1;
// Set up the spi interface
_spi.format(8, 3);
_spi.frequency(1000000);
mcp2515_init(100000);
}
But as soon as I replace _spi, _ncs, _irq with a struct:
struct Seeed_MCP_CAN_Shield {
SPI spi;
DigitalOut ncs;
DigitalIn irq;
};
typedef struct Seeed_MCP_CAN_Shield _can;
and change the constructor to:
SEEED_CAN::SEEED_CAN(PinName ncs) :
_can.spi(SEEED_CAN_MOSI, SEEED_CAN_MISO, SEEED_CAN_CLK),
_can.ncs(ncs),
_can.irq(SEEED_CAN_IRQ)
{
etc...
I get these errors and haven't been able to resolve them:
Error: "_can" is not a nonstatic data member or base class of class "SEEED_CAN" in "seeed_can.cpp", Line: 462, Col: 4 |
Error: Expected a "(" in "seeed_can.cpp", Line: 462, Col: 8 |
Error: Expected a ")" in "seeed_can.cpp", Line: 462, Col: 27 |
and so on for _can.ncs and _can.irq. I think the answer may lie in understanding Initailising base classes and members (C++ only) but it goes way above my head. I'd like to work out how to do this but if not I'll stick with what I have - a huge C++ file with public and private sections...
Any help much appreciated ,
Sophie x
Weekly Updates 1 & 2
Well sorry for the delay, here we go:
I am working with the freeScale platform and a relayShield, i soldered pins to the freeScale to make easier to connect to the Shield. I am making a little library based in the BusOut class for controlling the relayShield. The general idea of the project can be seen in the diagram below. I am making some test right now. Hope to upload more photos this weekend. I have the code for tcp server working in my mbed platform with SSR so i just have to make some modifications to make it work with the freedom scale
Greetings