98 lines
1.7 KiB
C
98 lines
1.7 KiB
C
#include <wiringPi.h>
|
|
#include <wiringPiSPI.h>
|
|
#include <stdint.h>
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
#include <libconfig.h>
|
|
#include <math.h>
|
|
|
|
#include "LS7366R.h"
|
|
#include "ringbuffer.h"
|
|
#include "led.h"
|
|
#include "logging.h"
|
|
|
|
|
|
extern char VERSION[];
|
|
extern uint32_t REFCNT;
|
|
|
|
const int CTRL_OUT = 16;
|
|
const int INTR_IN = 19;
|
|
const int SPI_CHAN = 0;
|
|
const int SPI_SPEED = 1000000;
|
|
|
|
config_t cfg;
|
|
|
|
const char EPSILON_KEY[] = "epsilon";
|
|
const double DEFAULT_EPSILON = 0.01;
|
|
|
|
uint32_t skipped = 0;
|
|
|
|
|
|
void isr() {
|
|
static uint32_t lastCounter = 0;
|
|
|
|
uint32_t currentCounter = ls7366rReadOTR();
|
|
|
|
uint32_t diff = currentCounter - lastCounter;
|
|
lastCounter = currentCounter;
|
|
|
|
ringbufferPut(diff);
|
|
led(E_GREEN, true);
|
|
}
|
|
|
|
void init() {
|
|
wiringPiSetupGpio();
|
|
|
|
wiringPiSPISetup(SPI_CHAN, SPI_SPEED);
|
|
|
|
pinMode(CTRL_OUT, OUTPUT);
|
|
digitalWrite(CTRL_OUT, 0);
|
|
|
|
pinMode(INTR_IN, INPUT);
|
|
}
|
|
|
|
void readConfig() {
|
|
config_init(&cfg);
|
|
if (! config_read_file(&cfg, "/opt/etc/counter.cfg")) {
|
|
logmsg(LOG_ERR, "failed to read config file: %s:%d - %s\n",
|
|
config_error_file(&cfg), config_error_line(&cfg),
|
|
config_error_text(&cfg));
|
|
config_destroy(&cfg);
|
|
exit(-1);
|
|
}
|
|
}
|
|
|
|
void start() {
|
|
wiringPiISR(INTR_IN, INT_EDGE_RISING, isr);
|
|
}
|
|
|
|
int main (void) {
|
|
fprintf(stderr, "VERSION: %s, REFCNT: %u\n", VERSION, REFCNT);
|
|
|
|
readConfig();
|
|
init();
|
|
ledInit();
|
|
ls7366rInit(SPI_CHAN);
|
|
start();
|
|
|
|
uint8_t ledTick = 0;
|
|
|
|
while (1) {
|
|
uint32_t period = ringbufferGet();
|
|
|
|
// add averaging and forming the struct here
|
|
logmsg(LOG_DEBUG, "Period: %lu", period);
|
|
|
|
ledTick++;
|
|
if (ledTick == 50) {
|
|
ledTick = 0;
|
|
led(E_GREEN, false);
|
|
}
|
|
|
|
}
|
|
|
|
// will never be reached
|
|
config_destroy(&cfg);
|
|
}
|