Same codebase for a project on Teensy 3.2 and 3.6 that uses CAN Bus

Status
Not open for further replies.

ivartaim

Member
Hi!

Firstly I would like to say that I am constantly amazed how much is possible to do with a Teensy and how lovely community and development it has.

For a project I am currently working on I would like to ideally work with one project that can be compiled on both Teensy 3.2 and 3.6. The main project is based around using CAN0 and CAN1 on the Teensy 3.6, but I would like to run a limited version also on a Teensy 3.2 that somehow would limit the functionality to using only CAN0.

What would be the best theoretical approach to this?

Changing include files based on which platform it is compiled on? Or is there a more elegant way?

I tried googling and searching, but didn't find anything promising on this topic. It could be I missed something.

Thank you for help!
 
you could add defines where you'd specify which of the processors you'd like certain code to run as, example, looking at the octows2811 library on paul's github you have:

Code:
#if defined(__MK20DX256__)
----------^^^-------- teensy 3.2
and
Code:
#if defined(__MK66FX1M0__)
----------^^^-------- teensy 3.6

Code:
#if defined(__MK20DX256__) 
pinMode(13,OUTPUT);
digitalWrite(13,HIGH);
#elif defined(__MK66FX1M0__)
pinMode(13,OUTPUT);
digitalWrite(13,LOW);
#endif
Above will set led on 3.2 ON and led on 3.6 OFF
 
Last edited:
Thank you for your replay.

Yes, I looked into this, but I didn't figure out , how to use this in my case. Let me ellaborate:

In my main sketch in setup I have the following code on the teensy 3.6:

Code:
	Can0.begin(1000000);
	Can0.setNumTXBoxes(1);
	Can0.startStats();
	Can0.attachObj(&can0_obj);
	can0_obj.attachGeneralHandler();
	
	
	
	Can1.begin(1000000);
	Can1.setNumTXBoxes(1);
	Can1.startStats();
	Can1.attachObj(&can1_obj);
	can1_obj.attachGeneralHandler();

Can1 only exists if running on 3.6

Can1 is also called from different subclasses and files in my code.

Would it be a good idea to write two classes in 2 files and then use #if defined(__MK66FX1M0__) to bring in the version that is needed. Or maybe one file would extend an already present class with can1 functionality.

This doesn't seem like a very good idea.


I think my question is about good practices in program structure. it feels not too smart to have "#if defined(__MK66FX1M0__)" everywhere through the program. Maybe I am overthinking it.
 
you can see it used in several subfunctions of octows2811 just to get an idea of how the library does it... as it's not used just for a setup procedure, but also as a per-function basis as well. Ideally in a sketch it would look a bit hard to comprehend, but people do not look into libraries most of the time anyways making the sketches look simpler to use.
 
Last edited:
Code:
#if defined(__MK20DX256__) 
        Can0.begin(1000000);
	Can0.setNumTXBoxes(1);
	Can0.startStats();
	Can0.attachObj(&can0_obj);
	can0_obj.attachGeneralHandler();
	
#elif defined(__MK66FX1M0__)
	Can0.begin(1000000);
	Can0.setNumTXBoxes(1);
	Can0.startStats();
	Can0.attachObj(&can0_obj);
	can0_obj.attachGeneralHandler();
	Can1.begin(1000000);
	Can1.setNumTXBoxes(1);
	Can1.startStats();
	Can1.attachObj(&can1_obj);
	can1_obj.attachGeneralHandler();
#endif

As you see in the 2nd define, you'll need to enable both whereas the first define only enables the first. You could drop this kind of configuration in an H/CPP file setup and use a Class in the sketch for the "user setings" like the baud rate to be used, and have the library do the background work for you.
 
Last edited:
Thank you.

Looked at Pauls library and filled my program with ifdefs and it works well. :)

Now have to figure out a logic to make it easier to read and limit number of uses.

Cheers!
 
Status
Not open for further replies.
Back
Top