the struct creates a user defined type. His struct has no functions, just data grouped together. you can use it like this:
osc x;
x.period = 3.14;
what he did was create a variable named osc1 with values ... the {} is like saying int x =3, its an init statement. Then he calls your function with the object instead of 4 loose variables. It just groups the 4 variables into one -- the most basic use of a struct is to group some variables together.
We don't know what your triangle function does, he is just calling it with reduced parameters by grouping them into the struct. everything else is the same.
however I suspect it may look more like this:
1 2 3 4 5 6 7 8
|
for (int i = 0; i < 10; i++)
{
osc1.period = someperiodvariable;
osc1.amplitude = someotherthing;
... ///etc
TriangleWave(i, osc1);
}
|
BUT, here is the thing.
it you move the triangle function INTO the struct, you can do something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
struct osc {
Type period;
Type startTime;
Type amplitude;
double triangle() { return something;} //whatever your function returns...
//this function can now use period etc as if local variables.
};
osc1.period = whatever; // maybe you set these one by one?
//OR
osc1.update(); //maybe you have a computation or function that can set all the fields to a new value at once from something?
result = osc1.triangle(); //it just works off the updated values now.
|
maybe at this point it would be best if you show us sort of the syntax you would like to write (the useage parts). It may be slightly off from what is allowed, but we will see what you want to do... It may also be worth a quick review of what your embedded tool supports; some embedded tools do not support all of C++.
you can't really completely hide the fact that you have 4 items of data and need to set them. But you can reduce the number of times you tap all 4 over and over significantly.
depending on what you need done, you can also array it.
enum structlike {period, start, amp, max_sl};
double osc1[max_sl];
osc1[period] = 3.14; //etc
now you can pass it around as one double * and the named indices from the enum make it pretty. This is kind of C-ish but again, some embedded systems need a less-is-more coding approach.