@@ -6,26 +6,46 @@ Devlpr::Devlpr(int pin)
66 emgPin = pin;
77 emgRunningSum = 0 ;
88 bufInd = BUFSIZE - 1 ;
9+ numFuncs = 0 ;
910}
1011
1112void Devlpr::tick ()
1213{
1314 // check the current time
1415 unsigned long currMicros = micros ();
1516 unsigned long microsDelta = currMicros - lastTickMicros;
16- // go through each function and see if we need to run it
17+ // ////////////////
18+ // EMG Schedule //
19+ // ////////////////
1720 // accrue micros on the micros since last run
1821 microsSinceEMG += microsDelta;
19- // now see if enough time has passed to run this bad boy
22+ // check if enough time has passed to read EMG
2023 if (microsSinceEMG > MICROS_SCHED_EMG) {
2124 readEMG ();
2225 // and update micros since
2326 microsSinceEMG = 0 ;
24- // NOTE do we want to do some sort of remainder on
25- // NOTE the micros - ie do we want to play catch up
26- // NOTE or just make a best effort to run on sched?
27+ // NOTE just a best effort to run on time
2728 }
28- // just pretend no time has passed since function start
29+ // //////////////////////////
30+ // User Function Schedule //
31+ // //////////////////////////
32+ // go through each function and check if it needs to run
33+ for (byte i = 0 ; i < numFuncs; i++) {
34+ // accrue micros on the micros since last run
35+ microsSince[i] += microsDelta;
36+ // check if enough time has passed to run it
37+ if (microsSince[i] > schedMicros[i]) {
38+ // run it
39+ funcs[i](this );
40+ // and update micros since
41+ microsSince[i] = 0 ;
42+ // NOTE just a best effort to run on time
43+ }
44+ }
45+ // //////////////
46+ // Wrap it up //
47+ // //////////////
48+ // pretend no time has passed since function start for best effort
2949 lastTickMicros = currMicros;
3050}
3151
@@ -46,6 +66,20 @@ int Devlpr::lastValueCentered()
4666 return lastVal - wAvg;
4767}
4868
69+ int Devlpr::scheduleFunction (void (*f)(Devlpr *d), unsigned int millisPer)
70+ {
71+ // check first if we are out of space to attach more functions
72+ if (numFuncs >= FUNCMAX) {
73+ return -1 ;
74+ }
75+ // otherwise add it in
76+ funcs[numFuncs] = f;
77+ schedMicros[numFuncs] = millisPer * 1000L ;
78+ microsSince[numFuncs] = 0 ;
79+ numFuncs++;
80+ return (numFuncs - 1 );
81+ }
82+
4983void Devlpr::readEMG ()
5084{
5185 // want bufInd to sit on the most recent value until tick
0 commit comments