Attached is a preliminary sketch that provides an interface to low-power ANT
radios (used by most Garmin and Suunto sports products). ANT is a TDMA protocol in which the radio Tx/Rx are only powered during very short pre-arranged timeslots and spend the other 99.9% of their time sleeping. All the timing is handled by the radio IC - when data is transmitted or received (which occurs at a regular interval), it generates an interrupt and transfers an event message to the host (e.g. "Tx OK" event or received payload, etc.). Message rate can range from once per two seconds (minimum) to up to some low hundreds per second(?) maximum, although a dozen or less per second would be typical.
The code attached shows a very simple use case where the Arduino creates a single broadcast channel and then sends one payload (in this case, the same payload) each time it is waked by the radio interrupt (e.g. in response to the last payload being transmitted, which generates an "EVENT_TX" event). As ANT is intended for very low-power systems, the interrupt-driven "sleep until something interesting happens" use-case shown will be very common.
While the code attached "works", I'd like to make it into a full-fledged Arduino library that can ideally be called from higher-level libraries (e.g. future mesh-networking stack), and operate by having the user or higher-level library register their own event handlers (e.g. callback functions) for the different types of event messages generated by the radio.
Some specific advice I could use on the 'best' way to make this into a library are:Code efficiency
- C vs c++ libraries? (I've heard it's possible to make a straight C library for Arduino, but never actually seen one. Is it worth it?)Function calls (e.g. digitalWrite) inside an ISR
- does this generate problems, performance or otherwise? (I know that the compiler for PIC18 micros adds an absolutely dreadful amount of context save/restore the moment the ISR includes a function call, is this the case for avr-gcc as well?)
Best practices on making the library functions usable by other libraries?
Using and passing callbacks to a function inside a class? (related to previous question - a function in library B passing its address to a function in library A to register as an event handler. I seem to recall reading this is a giant nono.)