Skip to content

Commit eddcf6d

Browse files
committed
Adding rudimentary flex callback.
1 parent 388c6e9 commit eddcf6d

3 files changed

Lines changed: 58 additions & 1 deletion

File tree

Devlpr.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ void Devlpr::tick()
2626
microsSinceEMG = 0L;
2727
// NOTE just a best effort to run on time
2828
}
29+
////////////////
30+
// Flex Check //
31+
////////////////
32+
// accrue micros on the micros since last check
33+
microsSinceFlexCheck += microsDelta;
34+
// check if enough time has passed to check for a flex
35+
if (microsSinceFlexCheck >= MICROS_SCHED_FLEX) {
36+
flexCheck(currMicros);
37+
// and update micros since
38+
microsSinceFlexCheck = 0L;
39+
}
2940
////////////////////////////
3041
// User Function Schedule //
3142
////////////////////////////
@@ -113,6 +124,16 @@ int Devlpr::scheduleFunction(void (*f)(Devlpr *d), unsigned int millisPer)
113124
return (numFuncs - 1);
114125
}
115126

127+
void Devlpr::setFlexCallback(void (*f)(Devlpr *d), float threshMult,
128+
unsigned int millisCooldown)
129+
{
130+
// set the callback
131+
onFlexFunc = f;
132+
// and the parameters
133+
flexThreshMultiple = threshMult;
134+
flexCooldownMicros = millisCooldown * 1000L;
135+
}
136+
116137
void Devlpr::readEMG()
117138
{
118139
// want bufInd to sit on the most recent value until tick
@@ -129,3 +150,22 @@ void Devlpr::readEMG()
129150
// now replace
130151
buf[bufInd] = emgVal;
131152
}
153+
154+
void Devlpr::flexCheck(unsigned long currMicros)
155+
{
156+
// to check cooldown
157+
unsigned long microsDelta = currMicros - prevFlexMicros;
158+
// the actual flex check
159+
unsigned int peakToPeakThresh = prevPeakToPeak * flexThreshMultiple;
160+
unsigned int currPeakToPeak = windowPeakToPeakAmplitude();
161+
// need an attached function, cooldown passed, and a peak change
162+
if (onFlexFunc && microsDelta >= flexCooldownMicros &&
163+
currPeakToPeak >= peakToPeakThresh) {
164+
// hit the callback
165+
onFlexFunc(this);
166+
// and update the previous confirmed flex
167+
prevFlexMicros = currMicros;
168+
}
169+
// need to update the current peak-to-peak for next time
170+
prevPeakToPeak = currPeakToPeak;
171+
}

Devlpr.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class Devlpr
1414
unsigned int windowPeakAmplitude();
1515
unsigned int windowPeakToPeakAmplitude();
1616
int scheduleFunction(void (*f)(Devlpr *d), unsigned int millisPer);
17+
void setFlexCallback(void (*f)(Devlpr *d), float threshMult=1.5,
18+
unsigned int millisCooldown=400);
1719
private:
1820
// emg buffer bookkeeping
1921
static const byte BUFSIZE = 32; // power of 2 for fast integer avg calc
@@ -29,6 +31,16 @@ class Devlpr
2931
// emg scheduling
3032
static const unsigned long MICROS_SCHED_EMG = 1000L;
3133
unsigned long microsSinceEMG = 0L;
34+
// onFlex scheduling
35+
static const unsigned long MICROS_SCHED_FLEX = 1000L * BUFSIZE; // once per buffer fill
36+
unsigned long microsSinceFlexCheck = 0L;
37+
// flex tracking
38+
void (*onFlexFunc)(Devlpr *d) = NULL;
39+
void flexCheck(unsigned long currMicros);
40+
unsigned long prevFlexMicros = 0L;
41+
unsigned int prevPeakToPeak = 0L;
42+
unsigned long flexCooldownMicros = 400000L; // 400ms
43+
float flexThreshMultiple = 1.5;
3244
// user function scheduling
3345
static const byte FUNCMAX = 8;
3446
void (*funcs[FUNCMAX])(Devlpr *d);

libdevlpr.ino

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@ void printEMG(Devlpr *d) {
77
Serial.println(result);
88
}
99

10+
void writeFlex(Devlpr *d) {
11+
Serial.println("FLEX");
12+
}
13+
1014
void setup() {
1115
Serial.begin(2000000);
1216
// add our print function to our DEVLPR schedule
1317
// try to run once every 1ms
14-
devlpr.scheduleFunction(printEMG, 1);
18+
//devlpr.scheduleFunction(printEMG, 1);
19+
devlpr.setFlexCallback(writeFlex);
1520
}
1621

1722
void loop() {

0 commit comments

Comments
 (0)