Hi Jose,
The pin is assigned in the constructor, and constant for the lifetime of the object. One solution is therefore to dynamically create the DigitalOut objects:
#include "mbed.h"
int main() {
DigitalOut *d;
d = new DigitalOut(LED1);
delete d;
d = new DigitalOut(LED2);
}
However, before you rush out and start doing that, I think there much be a much more elegant approach to what you are trying to do. If I understand correctly, you are basically using a SPI bus with multiple devices connected. This is a pretty normal setup, and can be handled nicely.
The SPI interface allows you to have multiple virtual instances using the same physical interface. This means you can have a SPI interface associated with each object, and a different DigitalOut controlling the CS. So, for example:
#include "mbed.h"
SPI spi1(p5,p6,p7);
DigitalOut cs1(p8);
SPI spi2(p5,p6,p7);
DigitalOut cs2(p9);
int main() {
cs1 = 1;
cs2 = 1;
spi1.format(8,0);
spi1.frequency(1000000);
spi2.format(8,3);
spi2.frequency(3500000);
// write to device 1
cs1 = 0;
spi1.write(0xAA);
cs1 = 1;
// write to device 2
cs2 = 0;
spi2.write(0xA5);
cs2 = 1;
}
So in the case of your accelerometer, lets say you had two connected to the same bus, each with a different DigitalOut cs. You could (without modification to the AXDL345 code) do the following:
#include "mbed.h"
#include "AXDL345.h"
AXDL345 a1(p5, p6, p7, p8); // cs on p8
AXDL345 a2(p5, p6, p7, p9); // cs on p9
int main() {
// ...
}
Importantly, each library component doesn't need to know about the other library components sharing the same bus! This is really useful, and means you don't need to modify the library to start connecting up multiple things. I think this may actually give you what you are after for free!
Hope this helps,
Simon
I would like to know if there is a way to a change the PinName after you created a DigitalOut object.
Example:
DigitalOut Led(p21);
Led.NewPin(p22);