Skip to content

Instantly share code, notes, and snippets.

@dernster
Created March 16, 2016 00:37
Show Gist options
  • Save dernster/497f791ad1848bc1ffad to your computer and use it in GitHub Desktop.
Save dernster/497f791ad1848bc1ffad to your computer and use it in GitHub Desktop.
ESP Sync Meter
#include "Arduino.h"
// Include API-Headers
extern "C" {
#include "ets_sys.h"
#include "os_type.h"
#include "osapi.h"
#include "mem.h"
#include "user_interface.h"
#include "cont.h"
}
#define DEVICE_QTY (2)
#define tandaInterval (500000)
#define MAXINT (2147483647)
unsigned long interruptionMoments[DEVICE_QTY];
unsigned long toPrint[DEVICE_QTY];
bool hasToPrint = false;
int cagada = 0;
int interruptionQty = 0;
unsigned long tandaTime;
unsigned long currentInterruptTime = 0;
void interruptionCommon(int deviceId){
currentInterruptTime = micros();
if (interruptionQty == 0) {
tandaTime = currentInterruptTime + tandaInterval;
}
if (currentInterruptTime > tandaTime) {
cagada = interruptionQty;
for (int i=0; i<DEVICE_QTY; i++)
toPrint[i] = interruptionMoments[i];
hasToPrint = true;
tandaTime = currentInterruptTime + tandaInterval;
interruptionQty = 0;
}
interruptionMoments[deviceId] = currentInterruptTime;
interruptionQty++;
}
#define MAPPER_LENGTH (sizeof(mapper)/sizeof(mapper[0]))
/* D1, D2, D3, D4, D5, D6, D7, D8 */
//int mapper[] = {5, 4, 0, 2, 14, 12, 13, 15};
int mapper[] = {5, 0, 2, 14, 12, 13, 15, 4};
#define INTERRUPT_NAME(n) d ## n ## _interrupt
#define INTERRUPT_D(n)\
void d ## n ## _interrupt(){\
interruptionCommon(n-1);\
};
INTERRUPT_D(1);
INTERRUPT_D(2);
INTERRUPT_D(3);
INTERRUPT_D(4);
INTERRUPT_D(5);
INTERRUPT_D(6);
INTERRUPT_D(7);
INTERRUPT_D(8);
int getMaximumDistanceBetweenTimes(){
long max = 0;
for(int i = 0; i < DEVICE_QTY; i++)
for(int j = i+1; j < DEVICE_QTY; j++){
long distance = toPrint[i] - toPrint[j];
if (abs(distance) > abs(max))
max = distance;
}
return max;
}
#define MODE CHANGE
void setup(){
Serial.begin(9600);
while (!Serial){};
Serial.printf("Hello World! %i devices\n", DEVICE_QTY);
if (DEVICE_QTY > MAPPER_LENGTH){
Serial.printf("ERROR: update mapper!\n");
}else{
/* initialize pins */
for(int i = 0; i < DEVICE_QTY; i++) {
pinMode(mapper[i], INPUT);
}
/* create interruption handler for each device */
attachInterrupt(digitalPinToInterrupt(mapper[0]), INTERRUPT_NAME(1), MODE);
attachInterrupt(digitalPinToInterrupt(mapper[1]), INTERRUPT_NAME(2), MODE);
attachInterrupt(digitalPinToInterrupt(mapper[2]), INTERRUPT_NAME(3), MODE);
attachInterrupt(digitalPinToInterrupt(mapper[3]), INTERRUPT_NAME(4), MODE);
attachInterrupt(digitalPinToInterrupt(mapper[4]), INTERRUPT_NAME(5), MODE);
attachInterrupt(digitalPinToInterrupt(mapper[5]), INTERRUPT_NAME(6), MODE);
attachInterrupt(digitalPinToInterrupt(mapper[6]), INTERRUPT_NAME(7), MODE);
attachInterrupt(digitalPinToInterrupt(mapper[7]), INTERRUPT_NAME(8), MODE);
}
}
void loop() {
if (hasToPrint) {
// if (!cagada){
for(int i = 0; i < DEVICE_QTY; i++){
Serial.printf("%i\t", toPrint[i]);
}
Serial.printf("%i\t%i\n", getMaximumDistanceBetweenTimes(), cagada);
// }
hasToPrint = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment