I use the AIN_NotifyChange(unsigned char pin, unsigned int pin_value) function to send MIDI message and update display when a pot is moved. It works quite well.
Now, I want, at startup, to do the same thing for each AIN channel in order to send to the PC software the initial AIN channels state. How can I achieve this? Should I call in the Init(void) function (within a loop) the function above or is there another cleaner way to do that?
Should I call in the Init(void) function (within a loop) the function above
yup, you can do that
Edit:
…but of course, you have to read the AIN states, because you don’t know what to hand over to this notifier function; so it might be easier, if you read and send the states in the Init() … or even better: split your functions into useful parts and call those…
If I understand well, I should use the MIOS_AIN_Pin7bitGet function in order to get the AIN states? I don’t know in detail how works the MIOS OS. Is it allowed to run this function in the Init()? I’m not sure that the pots are already read…
No, at the time the Init() hook is invoked, the pots haven’t been scanned. I would add a request mechanism into Tick() which sends all pot values when a certain flag is set (e.g. POT_SNAPSHOT_REQ, similar handling like the DISPLAY_UPDATE_REQ you will find in many C based applications).
This flag has to be set after a certain time (which depends on the number of pots). I guess, that with 30 mS you are at the save side.
So, how to add a cheap delay mechanism without sacrificing the Timer() hook: use SR_Service_Prepare() or SR_Service_Finish(), these hooks are called each mS if number of SRIO is > 0 (I guess that you are using DIN/DOUT modules, so SRIO length is probably already initialized?)
Then define a 8bit variable as counter, set it to 30 inside Init()
Inside SR_Service_Prepare() (or _Finish() - doesn’t really matter), check if the counter is already 0
if not, decrement it. Check again if it is 0 - yes: set the POT_SNAPSHOT_REQ flag
Once the routine in Tick() is executed (this happens whenever nothing else is to do), it clears POT_SNAPSHOT_REQ (so that this mechanism won’t be triggered again), sends the pot values - voila!