logger, implementation and test
This commit is contained in:
@ -1,6 +1,19 @@
|
||||
#ifndef _LOGGER_H_
|
||||
#define _LOGGER_H_
|
||||
|
||||
void log(char *msg);
|
||||
// initialize the logger, creates a ringbuffer
|
||||
void logInit();
|
||||
|
||||
// de-initialize the logger, free the ringbuffer
|
||||
void logFree();
|
||||
|
||||
// log a message, make sure it is a null-terminated string
|
||||
// return value can be ignored, it is only used in test
|
||||
int logMsg(char *msg);
|
||||
|
||||
// reads the ringbuffer and transfers data to output channel
|
||||
// call this from the idle-loop
|
||||
// return value can be ignored, it is only used in test
|
||||
int logExecute();
|
||||
|
||||
#endif // _LOGGER_H_
|
||||
|
@ -14,10 +14,13 @@ typedef struct {
|
||||
|
||||
void ringbufferInit(ringbuffer_t *handle, uint32_t bufferSize);
|
||||
void ringbufferFree(ringbuffer_t *handle);
|
||||
|
||||
int ringbufferPut(ringbuffer_t *handle, uint8_t *data, uint32_t dataLen);
|
||||
uint8_t *ringbufferGet(ringbuffer_t *handle, uint32_t dataLen);
|
||||
bool ringbufferEmpty(ringbuffer_t *handle);
|
||||
int ringbufferGetOne(ringbuffer_t *handle); // if positive, cast to uint8_t and be happy, if negative error
|
||||
|
||||
// not yet implemented
|
||||
uint8_t *ringbufferGet(ringbuffer_t *handle, uint32_t dataLen);
|
||||
|
||||
|
||||
#endif // _RINGBUFFER_H_
|
||||
|
@ -1,17 +1,65 @@
|
||||
#ifndef TEST
|
||||
#include <main.h>
|
||||
#include <usart.h>
|
||||
#include <logger.h>
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
void log(char *msg) {
|
||||
uint16_t len = strlen(msg);
|
||||
#ifdef TEST
|
||||
#include <stdio.h>
|
||||
#endif
|
||||
|
||||
if (HAL_UART_Transmit_IT(&debugUart, msg, len) != HAL_OK) {
|
||||
if(RingBuffer_Write(&txBuf, msg, len) != RING_BUFFER_OK)
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
|
||||
#include <logger.h>
|
||||
#include <ringbuffer.h>
|
||||
|
||||
|
||||
#ifdef TEST
|
||||
#define LOGBUFFER_SIZE 32
|
||||
#else
|
||||
#define LOGBUFFER_SIZE 1024
|
||||
#endif // TEST
|
||||
|
||||
|
||||
static ringbuffer_t logBuffer;
|
||||
|
||||
void logInit() {
|
||||
ringbufferInit(&logBuffer, LOGBUFFER_SIZE);
|
||||
}
|
||||
|
||||
void logFree() {
|
||||
ringbufferFree(&logBuffer);
|
||||
}
|
||||
|
||||
int logExecute() {
|
||||
int c = -1;
|
||||
#ifndef TEST
|
||||
if (false) { // is the TX channel free
|
||||
#endif // TEST
|
||||
c = ringbufferGetOne(&logBuffer);
|
||||
if (c > 0) {
|
||||
#ifndef TEST
|
||||
// transfer to TX channel
|
||||
#endif // TEST
|
||||
}
|
||||
#ifndef TEST
|
||||
}
|
||||
#endif // TEST
|
||||
return c;
|
||||
}
|
||||
|
||||
int logMsg(char *msg) {
|
||||
int res = -1;
|
||||
|
||||
if (-1 == (res = ringbufferPut(&logBuffer, msg, strlen(msg)))) {
|
||||
#ifndef TEST
|
||||
// blink the red light or so
|
||||
#else
|
||||
printf("\n*** red blink ***\n");
|
||||
#endif // TEST
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
@ -3,18 +3,6 @@
|
||||
#include <ringbuffer.h>
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
typedef struct {
|
||||
uint32_t bufferReadIdx;
|
||||
uint32_t bufferWriteIdx;
|
||||
uint32_t bufferSize;
|
||||
uint8_t* buffer;
|
||||
} ringbuffer_t;
|
||||
*/
|
||||
|
||||
|
||||
void ringbufferInit(ringbuffer_t *handle, uint32_t bufferSize) {
|
||||
handle->bufferSize = bufferSize;
|
||||
handle->buffer = (uint8_t*) malloc(handle->bufferSize);
|
||||
@ -31,27 +19,6 @@ void ringbufferFree(ringbuffer_t *handle) {
|
||||
handle->bufferWriteIdx = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
void ringbufferPut(ringbuffer_t *handle, void *f) {
|
||||
if (handle->bufferWriteIdx == (BUFFER_SIZE - 1)) {
|
||||
while (handle->bufferReadIdx == BUFFER_SIZE);
|
||||
} else {
|
||||
while (handle->bufferReadIdx == (handle->bufferWriteIdx + 1));
|
||||
}
|
||||
|
||||
handle->buffer[handle->bufferWriteIdx] = f;
|
||||
handle->bufferWriteIdx++;
|
||||
|
||||
if (handle->bufferWriteIdx > BUFFER_SIZE) {
|
||||
handle->bufferWriteIdx = 0;
|
||||
}
|
||||
|
||||
pthread_mutex_lock(&(handle->eventMutex));
|
||||
pthread_cond_signal(&(handle->eventSignal));
|
||||
pthread_mutex_unlock(&(handle->eventMutex));
|
||||
}
|
||||
*/
|
||||
|
||||
int ringbufferPut(ringbuffer_t *handle, uint8_t *data, uint32_t dataLen) {
|
||||
uint32_t freeSpace = 0;
|
||||
if (handle->bufferReadIdx == handle->bufferWriteIdx) {
|
||||
|
Reference in New Issue
Block a user