add ringbuffer
This commit is contained in:
parent
c90ffe6509
commit
33939d3b59
@ -3,7 +3,7 @@ CC=gcc
|
||||
CFLAGS=-Wall
|
||||
LDFLAGS=-lwiringPi -lcurl
|
||||
|
||||
counter: counter.o LS7366R.o influx.o
|
||||
counter: counter.o LS7366R.o influx.o ringbuffer.o
|
||||
$(CC) -o $@ $(LDFLAGS) $^
|
||||
|
||||
.c.o:
|
||||
|
@ -2,11 +2,10 @@
|
||||
#include <wiringPiSPI.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "LS7366R.h"
|
||||
#include "influx.h"
|
||||
|
||||
#include "ringbuffer.h"
|
||||
|
||||
const int CTRL_OUT = 16;
|
||||
const int INTR_IN = 19;
|
||||
@ -14,28 +13,15 @@ const int SPI_CHAN = 0;
|
||||
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() {
|
||||
static uint32_t lastCounter = 0;
|
||||
|
||||
uint32_t currentCounter = ls7366rReadOTR();
|
||||
|
||||
pthread_mutex_lock(&counterMutex);
|
||||
diff = currentCounter - lastCounter;
|
||||
pthread_mutex_unlock(&counterMutex);
|
||||
|
||||
|
||||
uint32_t diff = currentCounter - lastCounter;
|
||||
lastCounter = currentCounter;
|
||||
|
||||
pthread_mutex_lock(&eventMutex);
|
||||
pthread_cond_signal(&eventSignal);
|
||||
pthread_mutex_unlock(&eventMutex);
|
||||
|
||||
ringbufferPut(diff);
|
||||
}
|
||||
|
||||
void init() {
|
||||
@ -51,21 +37,13 @@ void init() {
|
||||
}
|
||||
|
||||
int main (void) {
|
||||
uint32_t my_diff = 0;
|
||||
|
||||
init();
|
||||
ls7366rInit(SPI_CHAN);
|
||||
|
||||
while (1) {
|
||||
pthread_mutex_lock(&eventMutex);
|
||||
pthread_cond_wait(&eventSignal, &eventMutex);
|
||||
pthread_mutex_unlock(&eventMutex);
|
||||
|
||||
pthread_mutex_lock(&counterMutex);
|
||||
my_diff = diff;
|
||||
pthread_mutex_unlock(&counterMutex);
|
||||
|
||||
double f = 1.0 / (((double) my_diff) / 1000000.0);
|
||||
uint32_t diff = ringbufferGet();
|
||||
|
||||
double f = 1.0 / (((double) diff) / 1000000.0);
|
||||
printf("%f\n", 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