1- #include " Libdevlpr.h"
2-
3- #define PACK (pin, data ) ((((pin) & 0x000F ) << 12 ) | ((data) & 0x0FFF ))
4-
51/* Set these parameters based on your hardware setup */
6- int NUM_DEVLPRS = 1 ;
2+ #define BAUD 2000000
3+ #define NUM_DEVLPRS 1
74int DEVLPR_PINS[] = {A0};
5+ int EMG_VALS[NUM_DEVLPRS];
86/* ****************************************************/
97
10- const int MAX_RESULTS = 2 ;
11- volatile int results [MAX_RESULTS];
12- volatile int resultNumber;
8+ byte bufOut[4 ];
9+ volatile bool dataReady = false ;
1310
14- unsigned long lastTickMicros = 0L ;
15- unsigned long microsSinceEMG = 0L ;
16- Devlpr dev (A0, FILTER_60HZ);
17- void printEMG (Devlpr *d);
11+ ISR (TIMER0_COMPA_vect) { // Timer0 interrupt, 1kHz
12+ if (!dataReady) {
13+ for (int i = 0 ; i < NUM_DEVLPRS; i++) {
14+ EMG_VALS[i] = analogRead (DEVLPR_PINS[i]);
15+ }
16+ dataReady = true ;
17+ }
18+ }
1819
1920void setup () {
20- Serial.begin (2000000 );
21- Serial.println ();
22- dev.scheduleFunction (printEMG, 1 );
21+ cli (); // Stop interrupts
22+ // set timer0 interrupt at 1kHz
23+ TCCR0A = 0 ;// set entire TCCR0A register to 0
24+ TCCR0B = 0 ;// same for TCCR0B
25+ TCNT0 = 0 ;// initialize counter value to 0
26+ // set compare match register for 1khz increments
27+ OCR0A = 249 ;// = (16*10^6) / (1000*64) - 1 (must be <256)
28+ // turn on CTC mode
29+ TCCR0A |= (1 << WGM01);
30+ // Set CS01 and CS00 bits for 64 prescaler
31+ TCCR0B |= (1 << CS01) | (1 << CS00);
32+ // enable timer compare interrupt
33+ TIMSK0 |= (1 << OCIE0A);
34+ sei (); // Start interrupts
35+
36+ Serial.begin (BAUD);
2337}
2438
2539/* Safety check in case the alias of A0-A5 isn't directly mapped to the number
2640 we expect */
27- uint8_t normalize_pin (int analogPin) {
41+ byte normalizePin (int analogPin) {
2842 switch (analogPin)
2943 {
3044 case A0:
@@ -45,21 +59,26 @@ uint8_t normalize_pin(int analogPin) {
4559}
4660
4761/* Helper function for formatting data in the way that the daemon expects */
48- void fill_packet (uint8_t *buffOut, uint8_t pin, uint16_t data) {
49- uint16_t payload = PACK (pin, data);
50- buffOut[0 ] = ((payload & 0xFF00 ) >> 8 ) & 0x00FF ;
51- buffOut[1 ] = payload & 0xFF ;
52- buffOut[2 ] = ' \r ' ;
53- buffOut[3 ] = ' \n ' ;
54- }
55-
56- void printEMG (Devlpr *d) {
57- int filtered = d->windowPeakToPeakAmplitude (true );
58- uint8_t buffOut[4 ];
59- fill_packet (buffOut, normalize_pin (A0), (uint16_t ) filtered);
60- Serial.write (buffOut, 4 );
62+ void fillPacket (byte *buffOut, byte pin, int value) {
63+ // pack 16-bits for the emg value and 4 bits for the pin into
64+ // three bytes in the following fashion (e=emg, p=pin)
65+ // eeee eee0
66+ // eeee eee0
67+ // eepp pp00
68+ // and terminate with a 1
69+ // 0000 0001
70+ buffOut[0 ] = (value >> 8 ) & 0xFE ;
71+ buffOut[1 ] = (value >> 1 ) & 0xFE ;
72+ buffOut[2 ] = ((value << 6 ) & 0xC0 ) | ((pin << 2 ) & 0x3C );
73+ buffOut[3 ] = 0x01 ;
6174}
6275
6376void loop () {
64- dev.tick ();
77+ if (dataReady) {
78+ for (int i = 0 ; i < NUM_DEVLPRS; i++) {
79+ fillPacket (bufOut, normalizePin (DEVLPR_PINS[i]), EMG_VALS[i]);
80+ Serial.write (bufOut, 4 );
81+ }
82+ dataReady = false ;
83+ }
6584}
0 commit comments