In the past days Derek helped me a lot in understanding how middleware works and how to communicate with a car. I want to share here what I learned and hope this will be helpful for others as well.
To receive and send messages you write your own middleware. "ChannelSwap.h" and "ServiceCall.h" are good examples to have a look at in the beginning.
For your own middleware you create a new file "YourMiddleware.h". In "CANBusTriple.ino" you add the following lines:
<code>#include "YourMiddleware.h"</code>
<code>YourMiddleware *yourmiddleware = new YourMiddleware( &writeQueue );</code>
In the <code>Middleware *activeMiddleware[] = {</code> block you add:
<code>yourmiddleware,</code>
to the list.
Then move over to your "YourMiddleware.h" file. It should look like this:
<code>#include "Middleware.h"
class YourMiddleware : public Middleware
{
private:
QueueArray<Message>* mainQueue;
public:
void tick();
Message process( Message msg );
YourMiddleware( QueueArray<Message> q );
void sendMyMessage();
void commandHandler(byte bytes, int length);
unsigned long lastMsgSent;
};
YourMiddleware::YourMiddlware( QueueArray<Message> *q ) {
mainQueue = q;
lastMsgSent = millis();
}
void YourMiddleware::tick(){
if((millis() > lastMsgSent + 100) || (millis() < lastMsgSent)){ //avoid flooding the bus, only call the sendMyMessage method every 100 milliseconds
sendMyMessage();
lastMsgSent = millis();
}
}
void YourMiddleware::commandHandler(byte* bytes, int length){}
Message YourMiddleware::process( Message msg )
{
//msg.busId
//msg.frame_id
//msg.frame_data[0] to msg.frame_data[7] do what ever you want with the messages received...
return msg;
}
void YourMiddleware::sendMyMessage(){
Message msg;
//msg.busId
//msg.frame_id
//msg.frame_data[0] to msg.frame_data[7] put whatever data you want in the message to send...
msg.length = 8;
msg.dispatch = true;
mainQueue->push(msg);
}
</code>