Skip to content

Commit f894b51

Browse files
committed
Adding the base library files.
1 parent c20a472 commit f894b51

3 files changed

Lines changed: 82 additions & 0 deletions

File tree

Devlpr.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "Arduino.h"
2+
#include "Devlpr.h"
3+
4+
Devlpr::Devlpr()
5+
{
6+
bufInd = 0;
7+
emgPin = A0;
8+
}
9+
10+
void Devlpr::tick()
11+
{
12+
// check the current time
13+
unsigned long currMicros = micros();
14+
unsigned long microsDelta = currMicros - lastTickMicros;
15+
// go through each function and see if we need to run it
16+
// accrue micros on the micros since last run
17+
microsSinceEMG += microsDelta;
18+
// now see if enough time has passed to run this bad boy
19+
// keep in mind that the schedule is in millis vs micros
20+
if (microsSinceEMG > MICROS_SCHED_EMG) {
21+
readEMG();
22+
// and update micros since
23+
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+
}
28+
// just pretend no time has passed since function start
29+
lastTickMicros = currMicros;
30+
}
31+
32+
int Devlpr::lastValue()
33+
{
34+
return buf[bufInd];
35+
}
36+
37+
void Devlpr::readEMG()
38+
{
39+
buf[bufInd] = analogRead(emgPin);
40+
Serial.println(buf[bufInd]);
41+
bufInd++;
42+
// circular buffer
43+
if (bufInd > BUFSIZE) {
44+
bufInd = 0;
45+
}
46+
}

Devlpr.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef Devlpr_h
2+
#define Devlpr_h
3+
4+
#include "Arduino.h"
5+
6+
class Devlpr
7+
{
8+
public:
9+
Devlpr();
10+
void tick();
11+
int lastValue();
12+
private:
13+
// general buffer bookkeeping
14+
static const byte BUFSIZE = 129;
15+
byte bufInd;
16+
int buf[BUFSIZE];
17+
unsigned long lastTickMicros = 0;
18+
// emg tracking
19+
unsigned long MICROS_SCHED_EMG = 1000;
20+
void readEMG();
21+
int emgPin;
22+
unsigned long microsSinceEMG;
23+
};
24+
25+
#endif

libdevlpr_test.ino

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "Devlpr.h"
2+
3+
Devlpr devlpr;
4+
5+
void setup() {
6+
Serial.begin(9600);
7+
}
8+
9+
void loop() {
10+
devlpr.tick();
11+
}

0 commit comments

Comments
 (0)