add ringbuffer
This commit is contained in:
parent
c90ffe6509
commit
33939d3b59
@ -3,7 +3,7 @@ CC=gcc
|
|||||||
CFLAGS=-Wall
|
CFLAGS=-Wall
|
||||||
LDFLAGS=-lwiringPi -lcurl
|
LDFLAGS=-lwiringPi -lcurl
|
||||||
|
|
||||||
counter: counter.o LS7366R.o influx.o
|
counter: counter.o LS7366R.o influx.o ringbuffer.o
|
||||||
$(CC) -o $@ $(LDFLAGS) $^
|
$(CC) -o $@ $(LDFLAGS) $^
|
||||||
|
|
||||||
.c.o:
|
.c.o:
|
||||||
|
@ -2,11 +2,10 @@
|
|||||||
#include <wiringPiSPI.h>
|
#include <wiringPiSPI.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <pthread.h>
|
|
||||||
|
|
||||||
#include "LS7366R.h"
|
#include "LS7366R.h"
|
||||||
#include "influx.h"
|
#include "influx.h"
|
||||||
|
#include "ringbuffer.h"
|
||||||
|
|
||||||
const int CTRL_OUT = 16;
|
const int CTRL_OUT = 16;
|
||||||
const int INTR_IN = 19;
|
const int INTR_IN = 19;
|
||||||
@ -14,28 +13,15 @@ const int SPI_CHAN = 0;
|
|||||||
const int SPI_SPEED = 1000000;
|
const int SPI_SPEED = 1000000;
|
||||||
|
|
||||||
|
|
||||||
uint32_t diff = 0;
|
|
||||||
|
|
||||||
pthread_mutex_t counterMutex = PTHREAD_MUTEX_INITIALIZER;
|
|
||||||
pthread_mutex_t eventMutex = PTHREAD_MUTEX_INITIALIZER;
|
|
||||||
pthread_cond_t eventSignal = PTHREAD_COND_INITIALIZER;
|
|
||||||
|
|
||||||
|
|
||||||
void isr() {
|
void isr() {
|
||||||
static uint32_t lastCounter = 0;
|
static uint32_t lastCounter = 0;
|
||||||
|
|
||||||
uint32_t currentCounter = ls7366rReadOTR();
|
uint32_t currentCounter = ls7366rReadOTR();
|
||||||
|
|
||||||
pthread_mutex_lock(&counterMutex);
|
uint32_t diff = currentCounter - lastCounter;
|
||||||
diff = currentCounter - lastCounter;
|
|
||||||
pthread_mutex_unlock(&counterMutex);
|
|
||||||
|
|
||||||
|
|
||||||
lastCounter = currentCounter;
|
lastCounter = currentCounter;
|
||||||
|
|
||||||
pthread_mutex_lock(&eventMutex);
|
ringbufferPut(diff);
|
||||||
pthread_cond_signal(&eventSignal);
|
|
||||||
pthread_mutex_unlock(&eventMutex);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void init() {
|
void init() {
|
||||||
@ -51,21 +37,13 @@ void init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main (void) {
|
int main (void) {
|
||||||
uint32_t my_diff = 0;
|
|
||||||
|
|
||||||
init();
|
init();
|
||||||
ls7366rInit(SPI_CHAN);
|
ls7366rInit(SPI_CHAN);
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
pthread_mutex_lock(&eventMutex);
|
uint32_t diff = ringbufferGet();
|
||||||
pthread_cond_wait(&eventSignal, &eventMutex);
|
|
||||||
pthread_mutex_unlock(&eventMutex);
|
double f = 1.0 / (((double) diff) / 1000000.0);
|
||||||
|
|
||||||
pthread_mutex_lock(&counterMutex);
|
|
||||||
my_diff = diff;
|
|
||||||
pthread_mutex_unlock(&counterMutex);
|
|
||||||
|
|
||||||
double f = 1.0 / (((double) my_diff) / 1000000.0);
|
|
||||||
printf("%f\n", f);
|
printf("%f\n", f);
|
||||||
influxSendFrequency(f);
|
influxSendFrequency(f);
|
||||||
}
|
}
|
||||||
|
53
src/ringbuffer.c
Normal file
53
src/ringbuffer.c
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
#include <stdint.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
|
|
||||||
|
#define BUFFER_SIZE 256
|
||||||
|
|
||||||
|
uint32_t buffer[BUFFER_SIZE+5];
|
||||||
|
|
||||||
|
uint16_t bufferReadIdx = 0;
|
||||||
|
uint16_t bufferWriteIdx = 0;
|
||||||
|
|
||||||
|
pthread_mutex_t eventMutex = PTHREAD_MUTEX_INITIALIZER;
|
||||||
|
pthread_cond_t eventSignal = PTHREAD_COND_INITIALIZER;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void ringbufferPut(uint32_t f) {
|
||||||
|
if (bufferWriteIdx == (BUFFER_SIZE - 1)) {
|
||||||
|
while (bufferReadIdx == BUFFER_SIZE);
|
||||||
|
} else {
|
||||||
|
while (bufferReadIdx == (bufferWriteIdx + 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer[bufferWriteIdx] = f;
|
||||||
|
bufferWriteIdx++;
|
||||||
|
|
||||||
|
if (bufferWriteIdx > BUFFER_SIZE) {
|
||||||
|
bufferWriteIdx = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
pthread_mutex_lock(&eventMutex);
|
||||||
|
pthread_cond_signal(&eventSignal);
|
||||||
|
pthread_mutex_unlock(&eventMutex);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
uint32_t ringbufferGet() {
|
||||||
|
if (bufferReadIdx == bufferWriteIdx) {
|
||||||
|
pthread_mutex_lock(&eventMutex);
|
||||||
|
pthread_cond_wait(&eventSignal, &eventMutex);
|
||||||
|
pthread_mutex_unlock(&eventMutex);
|
||||||
|
}
|
||||||
|
|
||||||
|
double res = buffer[bufferReadIdx];
|
||||||
|
bufferReadIdx++;
|
||||||
|
if (bufferReadIdx > BUFFER_SIZE) {
|
||||||
|
bufferReadIdx = 0;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
9
src/ringbuffer.h
Normal file
9
src/ringbuffer.h
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#ifndef _RINGBUFFER_H_
|
||||||
|
#define _RINGBUFFER_H_
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
void ringbufferPut(uint32_t f);
|
||||||
|
uint32_t ringbufferGet();
|
||||||
|
|
||||||
|
#endif // _RINGBUFFER_H_
|
Loading…
x
Reference in New Issue
Block a user