I have a decent know how about C and microcontrollers but have only read a book on C++. That book was not microcontroller oriented and does not speak about integrating Interrupt Service Routines (ISR’s) in a C++ Class definition.
I have written a program for a high end RC transmitter specialized in surface racing (cars, boats and robots). I have written that code originally in C (Atmel Studio so GCC) and recently also in BASIC (BASCOM). Now I want to port my code to the Arduino.
The core of an RC transmitter is a Pulse Place Modulation (PPM) generator. I want to implement that PPM-generator as a library that defines a C++ class PPM_generator. I want to offer that Library to the Arduino community so that other people can also easily develop their own transmitter code but also to make my code easy to maintain.
Ultimately the PPM_gererator would be used from my main program using something like this
PPMG PPM_Generator; //Create an object from the Class
PPMG.init(4); //Initialise the PPM generator for 4 channels (8 would be the maximum)
PPMG.pulse (Channel, Length); //Set the pulse for channel “Channel” to Length “Length”
My PPM generator uses Timer1 that is configured for timer reset at compare match with OCR1A. It uses two ISR’s: One on OCR1A (to get a rock solid pulse train starting every 20 ms) and one on OCR1B that actually times the pulse length for each channel. Almost all the intelligence is inside these ISR’s so that is where all the variables are manipulated. (I have the working in an Arduino sketch)
In C it is common (since you cannot pass variables on to an ISR) to define the variables that are manipulated inside an ISR as global variables at the start of the program. I could do that here too and that works. However… I want to write clean code that is easy to maintain so I want to make the ISR’s use PRIVATE variables that are encapsulated inside the Class in the library. How do I do that?