Skip to content

Commit 8ce7377

Browse files
committed
Adding in user function scheduling
1 parent 4a048a4 commit 8ce7377

3 files changed

Lines changed: 62 additions & 30 deletions

File tree

Devlpr.cpp

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,46 @@ Devlpr::Devlpr(int pin)
66
emgPin = pin;
77
emgRunningSum = 0;
88
bufInd = BUFSIZE - 1;
9+
numFuncs = 0;
910
}
1011

1112
void 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+
4983
void Devlpr::readEMG()
5084
{
5185
// want bufInd to sit on the most recent value until tick

Devlpr.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,28 @@ class Devlpr
1111
unsigned int lastValue();
1212
int lastValueCentered();
1313
unsigned int windowAvg();
14+
int scheduleFunction(void (*f)(Devlpr *d), unsigned int millisPer);
1415
private:
15-
// general buffer bookkeeping
16-
static const byte BUFSIZE = 32; // power of 2 for integer avg calc
16+
// emg buffer bookkeeping
17+
static const byte BUFSIZE = 32; // power of 2 for fast integer avg calc
1718
unsigned int buf[BUFSIZE];
1819
byte bufInd;
19-
// scheduling
20-
unsigned long MICROS_SCHED_EMG = 1000;
21-
unsigned long lastTickMicros = 0;
22-
unsigned long microsSinceEMG = 0;
2320
// emg tracking
2421
int emgPin;
2522
unsigned int emgVal; // ATMEGA boards have 10-bit ADC (0-1023)
2623
unsigned int emgRunningSum; // if BUFSIZE is small, uint is fine
2724
void readEMG();
25+
// scheduling
26+
unsigned long lastTickMicros = 0;
27+
// emg scheduling
28+
static const unsigned long MICROS_SCHED_EMG = 1000;
29+
unsigned long microsSinceEMG = 0;
30+
// user function scheduling
31+
static const byte FUNCMAX = 8;
32+
void (*funcs[FUNCMAX])(Devlpr *d);
33+
unsigned long schedMicros[FUNCMAX];
34+
unsigned long microsSince[FUNCMAX];
35+
byte numFuncs;
2836
};
2937

3038
#endif

libdevlpr.ino

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,19 @@
22

33
Devlpr devlpr;
44

5+
void printEMG(Devlpr *d) {
6+
int result = d->lastValueCentered();
7+
Serial.println(result);
8+
}
9+
510
void setup() {
611
Serial.begin(2000000);
12+
// add our print function to our DEVLPR schedule
13+
// try to run once every 1ms
14+
devlpr.scheduleFunction(printEMG, 1);
715
}
816

9-
unsigned long microsSincePrint = 0;
10-
unsigned long prevLoopMicros = 0;
11-
int result = 0;
1217
void loop() {
1318
// let the DEVLPR library do its job
1419
devlpr.tick();
15-
16-
// try to sample and print data at 1000Hz
17-
unsigned long currMicros = micros();
18-
unsigned long microsDelta = currMicros - prevLoopMicros;
19-
microsSincePrint += microsDelta;
20-
// has 1ms passed
21-
if (microsSincePrint > 1000) {
22-
// 1ms has passed, get the value
23-
result = devlpr.lastValueCentered();
24-
Serial.println(result);
25-
// update our last filtered call
26-
microsSincePrint = 0;
27-
}
28-
// update our last loop time
29-
prevLoopMicros = currMicros;
3020
}

0 commit comments

Comments
 (0)