Improved Readability, more comments.
This commit is contained in:
@@ -40,7 +40,7 @@
|
|||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||||
<ConfigurationType>Application</ConfigurationType>
|
<ConfigurationType>Application</ConfigurationType>
|
||||||
<UseDebugLibraries>true</UseDebugLibraries>
|
<UseDebugLibraries>true</UseDebugLibraries>
|
||||||
<PlatformToolset>v140</PlatformToolset>
|
<PlatformToolset>v120</PlatformToolset>
|
||||||
<CharacterSet>MultiByte</CharacterSet>
|
<CharacterSet>MultiByte</CharacterSet>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||||
@@ -78,7 +78,7 @@
|
|||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||||
<ClCompile>
|
<ClCompile>
|
||||||
<WarningLevel>Level3</WarningLevel>
|
<WarningLevel>Level4</WarningLevel>
|
||||||
<Optimization>Disabled</Optimization>
|
<Optimization>Disabled</Optimization>
|
||||||
<SDLCheck>true</SDLCheck>
|
<SDLCheck>true</SDLCheck>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
|||||||
64
Pipe/main.c
64
Pipe/main.c
@@ -3,6 +3,7 @@
|
|||||||
#include "ringbuffer.h"
|
#include "ringbuffer.h"
|
||||||
#include "pipe.h"
|
#include "pipe.h"
|
||||||
|
|
||||||
|
/* Integrate every element of the signal. */
|
||||||
void increment(pipe_t * const p)
|
void increment(pipe_t * const p)
|
||||||
{
|
{
|
||||||
while (Pipe_isFilled(p))
|
while (Pipe_isFilled(p))
|
||||||
@@ -13,6 +14,7 @@ void increment(pipe_t * const p)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Square every element of the signal. */
|
||||||
void square(pipe_t * const p)
|
void square(pipe_t * const p)
|
||||||
{
|
{
|
||||||
while (Pipe_isFilled(p))
|
while (Pipe_isFilled(p))
|
||||||
@@ -23,59 +25,69 @@ void square(pipe_t * const p)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void integrate(pipe_t * const p)
|
/* Integrate over every element of the signal. */
|
||||||
|
void integrate(pipe_t * const pipe)
|
||||||
{
|
{
|
||||||
uint32_t * state = p->state;
|
uint32_t state = *((uint32_t*)pipe->state);
|
||||||
|
|
||||||
while (Pipe_isFilled(p))
|
while (Pipe_isFilled(pipe))
|
||||||
{
|
{
|
||||||
uint32_t item = Pipe_Read(p);
|
uint32_t item = Pipe_Read(pipe);
|
||||||
*state = *state + item;
|
state = state + item;
|
||||||
Pipe_Write(p, *state);
|
Pipe_Write(pipe, state);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
*((uint32_t*)pipe->state) = state;
|
||||||
}
|
}
|
||||||
|
|
||||||
void sum(pipe_t * const p)
|
/* Build the sum of all elements of the signal. */
|
||||||
|
void sum(pipe_t * const pipe)
|
||||||
{
|
{
|
||||||
uint32_t sum = 0;
|
uint32_t sum = 0;
|
||||||
|
|
||||||
while (Pipe_isFilled(p))
|
while (Pipe_isFilled(pipe))
|
||||||
sum += Pipe_Read(p);
|
sum += Pipe_Read(pipe);
|
||||||
Pipe_Write(p, sum);
|
Pipe_Write(pipe, sum);
|
||||||
}
|
}
|
||||||
|
|
||||||
void average(pipe_t * const p)
|
/* Build the average of all elements of the signal. */
|
||||||
|
void average(pipe_t * const pipe)
|
||||||
{
|
{
|
||||||
uint32_t sum = 0;
|
uint32_t sum = 0;
|
||||||
uint32_t element_counter = 0;
|
uint32_t element_counter = 0;
|
||||||
uint32_t average = 0;
|
uint32_t average = 0;
|
||||||
|
|
||||||
while (Pipe_isFilled(p))
|
while (Pipe_isFilled(pipe))
|
||||||
{
|
{
|
||||||
sum += Pipe_Read(p);
|
sum += Pipe_Read(pipe);
|
||||||
element_counter++;
|
element_counter++;
|
||||||
}
|
}
|
||||||
average = sum / element_counter;
|
average = sum / element_counter;
|
||||||
Pipe_Write(p, average);
|
Pipe_Write(pipe, average);
|
||||||
}
|
}
|
||||||
|
|
||||||
void print(pipe_t * const p)
|
/* Print the signal. */
|
||||||
|
void print(pipe_t * const pipe)
|
||||||
{
|
{
|
||||||
printf("\nOutput:\n");
|
printf("\nOutput:\n");
|
||||||
while (Pipe_isFilled(p))
|
while (Pipe_isFilled(pipe))
|
||||||
printf("%d\n", Pipe_Read(p));
|
printf("%d\n", Pipe_Read(pipe));
|
||||||
}
|
}
|
||||||
|
|
||||||
void log(pipe_t * const from, pipe_t * const to, uint32_t elem)
|
/* Logging function. Set by user. */
|
||||||
|
void log(pipe_t * const source, pipe_t * const target, uint32_t element)
|
||||||
{
|
{
|
||||||
if (from->state == NULL && to->state == NULL)
|
if (Pipe_isFull(target))
|
||||||
printf("%s -> %d -> %s\n", from->name, elem, to->name);
|
printf("Error: Pipe %s is full!\n", target->name);
|
||||||
else if (from->state != NULL && to->state != NULL)
|
|
||||||
printf("%s(%d) -> %d -> %s(%d)\n", from->name, *((uint32_t*)from->state), elem, to->name, *((uint32_t*)to->state));
|
if (source->state == NULL && target->state == NULL)
|
||||||
else if (from->state != NULL)
|
printf("%s -> %d -> %s\n", source->name, element, target->name);
|
||||||
printf("%s(%d) -> %d -> %s\n", from->name, *((uint32_t*)from->state), elem, to->name);
|
else if (source->state != NULL && target->state != NULL)
|
||||||
|
printf("%s(%d) -> %d -> %s(%d)\n", source->name, *((uint32_t*)source->state), element, target->name, *((uint32_t*)target->state));
|
||||||
|
else if (source->state != NULL)
|
||||||
|
printf("%s(%d) -> %d -> %s\n", source->name, *((uint32_t*)source->state), element, target->name);
|
||||||
else
|
else
|
||||||
printf("%s -> %d -> %s(%d)\n", from->name, elem, to->name, *((uint32_t*)to->state));
|
printf("%s -> %d -> %s(%d)\n", source->name, element, target->name, *((uint32_t*)target->state));
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
@@ -85,7 +97,7 @@ int main(void)
|
|||||||
/* Create pipes and connect them */
|
/* Create pipes and connect them */
|
||||||
Pipe_Create(increment_pipe, NULL, 4, log);
|
Pipe_Create(increment_pipe, NULL, 4, log);
|
||||||
Pipe_Create(square_pipe, NULL, 4, log);
|
Pipe_Create(square_pipe, NULL, 4, log);
|
||||||
Pipe_Create(integrate_pipe, &counter, 8, log);
|
Pipe_Create(integrate_pipe, &counter, 4, log);
|
||||||
Pipe_Create(sum_pipe, NULL, 8, log);
|
Pipe_Create(sum_pipe, NULL, 8, log);
|
||||||
Pipe_Create(average_pipe, NULL, 8, log);
|
Pipe_Create(average_pipe, NULL, 8, log);
|
||||||
Pipe_Create(print_pipe, NULL, 4, log);
|
Pipe_Create(print_pipe, NULL, 4, log);
|
||||||
|
|||||||
129
Pipe/pipe.h
129
Pipe/pipe.h
@@ -7,87 +7,132 @@
|
|||||||
/* microsoft specific */
|
/* microsoft specific */
|
||||||
#define inline __inline
|
#define inline __inline
|
||||||
|
|
||||||
/* number of counts */
|
/* number of connections */
|
||||||
#define PIPE_OUTPUT_COUNT 4
|
#define PIPE_NUMBER_OF_CONNECTIONS 4
|
||||||
|
|
||||||
typedef struct pipe_tt
|
typedef struct pipe_tt
|
||||||
{
|
{
|
||||||
ringbuffer_t * input;
|
ringbuffer_t * input;
|
||||||
void * state;
|
void * state;
|
||||||
struct pipe_tt *output[PIPE_OUTPUT_COUNT];
|
struct pipe_tt *connection[PIPE_NUMBER_OF_CONNECTIONS];
|
||||||
uint8_t output_count;
|
uint32_t connection_count;
|
||||||
|
uint32_t connection_max;
|
||||||
char * name;
|
char * name;
|
||||||
void(*log)(struct pipe_tt * from, struct pipe_tt * to, uint32_t elem);
|
void(*log_function)(struct pipe_tt * from, struct pipe_tt * to, uint32_t elem);
|
||||||
} pipe_t;
|
} pipe_t;
|
||||||
|
|
||||||
|
|
||||||
/***********************************/
|
/***************************************/
|
||||||
/* Functions to contruct pipe mesh */
|
/* Functions to construct pipe system. */
|
||||||
/***********************************/
|
/***************************************/
|
||||||
|
|
||||||
#define Concat2(a, b) a ## b
|
#define Concat2(a, b) a ## b
|
||||||
#define Concat(a, b) Concat2(a, b)
|
#define Concat(a, b) Concat2(a, b)
|
||||||
#define SizeOfArray(arg) ( sizeof(arg) / sizeof(arg[0]) )
|
#define SizeOfArray(arg) ( sizeof(arg) / sizeof(arg[0]) )
|
||||||
|
|
||||||
#define Pipe_Create(arg_name, arg_state, arg_size, arg_log) \
|
/*
|
||||||
static uint32_t Concat(buffer, __LINE__)[arg_size]; \
|
Macro for the creation of a pipe.
|
||||||
ringbuffer_t arg_name ## _rb; \
|
Automates the creation of a ring buffer and the pipe.
|
||||||
RingBuffer_InitFromArray(&arg_name ## _rb, Concat(buffer, __LINE__), SizeOfArray(Concat(buffer, __LINE__))); \
|
arg_name is the variable name and string name of the pipe.
|
||||||
pipe_t arg_name; \
|
arg_state is the given state, which can be used in the function.
|
||||||
Pipe_Init(&arg_name, &arg_name ## _rb, arg_state, #arg_name, arg_log)
|
arg_size is the ring buffer size in bytes.
|
||||||
|
arg_log is the log function, called when an element is sent.
|
||||||
|
*/
|
||||||
|
#define Pipe_Create(arg_name, arg_state, arg_size, arg_log) \
|
||||||
|
static uint32_t Concat(buffer, __LINE__)[arg_size]; \
|
||||||
|
ringbuffer_t arg_name ## _rb; \
|
||||||
|
RingBuffer_InitFromArray(&arg_name ## _rb, Concat(buffer, __LINE__), SizeOfArray(Concat(buffer, __LINE__))); \
|
||||||
|
pipe_t arg_name; \
|
||||||
|
Pipe_Init(&arg_name, &arg_name ## _rb, arg_state, #arg_name, arg_log)
|
||||||
|
|
||||||
|
/*
|
||||||
|
Initializes a pipe.
|
||||||
|
A Ringbuffer is needed to store elements from other pipes.
|
||||||
|
A State (NULL if function has no state) for the function using the pipe.
|
||||||
|
A Name and a logging function are usefull to track the dataflow.
|
||||||
|
*/
|
||||||
static inline void Pipe_Init(
|
static inline void Pipe_Init(
|
||||||
pipe_t * const p,
|
pipe_t * const pipe,
|
||||||
ringbuffer_t * const arg_input,
|
ringbuffer_t * const input,
|
||||||
void * arg_state,
|
void * state,
|
||||||
char * const arg_name,
|
char * const name,
|
||||||
void(*arg_log)(struct pipe_tt * from, struct pipe_tt * to, uint32_t elem)
|
void(*log_function)(struct pipe_tt * source, struct pipe_tt * target, uint32_t element)
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
p->input = arg_input;
|
pipe->input = input;
|
||||||
p->state = arg_state;
|
pipe->state = state;
|
||||||
|
|
||||||
for (uint8_t i = 0; i < PIPE_OUTPUT_COUNT; i++)
|
for (uint8_t i = 0; i < PIPE_NUMBER_OF_CONNECTIONS; i++)
|
||||||
p->output[i] = NULL;
|
pipe->connection[i] = NULL;
|
||||||
|
|
||||||
p->output_count = 0;
|
pipe->connection_count = 0;
|
||||||
p->name = arg_name;
|
pipe->connection_max = PIPE_NUMBER_OF_CONNECTIONS;
|
||||||
p->log = arg_log;
|
pipe->name = name;
|
||||||
|
pipe->log_function = log_function;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void Pipe_Connect(pipe_t * const a, pipe_t * const b)
|
/* Connect two pipes. Pipe a sends elements to pipe b. */
|
||||||
|
static inline void Pipe_Connect(pipe_t * const source, pipe_t * const target)
|
||||||
{
|
{
|
||||||
a->output[a->output_count] = b;
|
if (source->connection_count < source->connection_max)
|
||||||
a->output_count++;
|
{
|
||||||
|
source->connection[source->connection_count] = target;
|
||||||
|
source->connection_count++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/************************************/
|
/*******************************************/
|
||||||
/* Functions to work with pipe mesh */
|
/* Functions to work with the pipe system. */
|
||||||
/************************************/
|
/*******************************************/
|
||||||
|
|
||||||
static inline void Pipe_Insert(pipe_t * const p, uint32_t elem)
|
/*
|
||||||
|
Inserts a element into a pipe.
|
||||||
|
So the signal can inserted to the pipe system.
|
||||||
|
*/
|
||||||
|
static inline void Pipe_Insert(pipe_t * const pipe, uint32_t element)
|
||||||
{
|
{
|
||||||
RingBuffer_Write(p->input, elem);
|
RingBuffer_Write(pipe->input, element);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline uint32_t Pipe_Read(pipe_t * p)
|
/*
|
||||||
|
Read an element from the pipe.
|
||||||
|
Used inside functions of the pipe system.
|
||||||
|
*/
|
||||||
|
static inline uint32_t Pipe_Read(pipe_t * pipe)
|
||||||
{
|
{
|
||||||
return RingBuffer_Read(p->input);
|
return RingBuffer_Read(pipe->input);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void Pipe_Write(pipe_t * p, uint32_t elem)
|
/*
|
||||||
|
Write an element to connected pipes.
|
||||||
|
Used inside functions of the pipe system.
|
||||||
|
*/
|
||||||
|
static inline void Pipe_Write(pipe_t * pipe, uint32_t element)
|
||||||
{
|
{
|
||||||
for (uint8_t i = 0; i < p->output_count; i++)
|
for (uint8_t i = 0; i < pipe->connection_count; i++)
|
||||||
{
|
{
|
||||||
p->log(p, p->output[i], elem);
|
pipe->log_function(pipe, pipe->connection[i], element);
|
||||||
RingBuffer_Write(p->output[i]->input, elem);
|
RingBuffer_Write(pipe->connection[i]->input, element);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline uint8_t Pipe_isFilled(pipe_t * p)
|
/*
|
||||||
|
Check if the pipe contents elements.
|
||||||
|
Used inside functions of the pipe system.
|
||||||
|
*/
|
||||||
|
static inline uint8_t Pipe_isFilled(pipe_t * pipe)
|
||||||
{
|
{
|
||||||
return RingBuffer_IsFilled(p->input);
|
return RingBuffer_IsFilled(pipe->input);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Check if the pipe has no place left for new elements.
|
||||||
|
Usefull for the logging.
|
||||||
|
*/
|
||||||
|
static inline uint8_t Pipe_isFull(const pipe_t * pipe)
|
||||||
|
{
|
||||||
|
return RingBuffer_IsFull(pipe->input);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -10,87 +10,64 @@ typedef struct {
|
|||||||
uint32_t * writer;
|
uint32_t * writer;
|
||||||
uint32_t * start;
|
uint32_t * start;
|
||||||
uint32_t * end;
|
uint32_t * end;
|
||||||
uint8_t write_failed;
|
|
||||||
uint8_t read_failed;
|
|
||||||
} ringbuffer_t;
|
} ringbuffer_t;
|
||||||
|
|
||||||
/* Use Array as RingBuffer */
|
/* Use Array as RingBuffer */
|
||||||
static inline void RingBuffer_InitFromArray(ringbuffer_t * const rb, uint32_t * const array, const uint32_t size)
|
static inline void RingBuffer_InitFromArray(ringbuffer_t * const ring_buffer, uint32_t * const array, uint32_t size)
|
||||||
{
|
{
|
||||||
rb->start = rb->reader = rb->writer = array;
|
ring_buffer->start = ring_buffer->reader = ring_buffer->writer = &array[0];
|
||||||
rb->end = array + size - 1;
|
ring_buffer->end = &array[0] + size - 1;
|
||||||
rb->write_failed = rb->read_failed = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline uint32_t * RingBuffer_GetNextWriterAddress(const ringbuffer_t * const rb)
|
static inline uint32_t * RingBuffer_NextAddress(ringbuffer_t * const ring_buffer, uint32_t * const pointer)
|
||||||
{
|
{
|
||||||
uint32_t * next_address = rb->writer;
|
if (pointer == ring_buffer->end)
|
||||||
|
return ring_buffer->start;
|
||||||
if (next_address == rb->end)
|
|
||||||
next_address = rb->start;
|
|
||||||
else
|
else
|
||||||
next_address++;
|
return pointer + 1;
|
||||||
|
|
||||||
return next_address;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline uint8_t RingBuffer_IsFull(const ringbuffer_t * const rb)
|
static inline uint8_t RingBuffer_IsFull(ringbuffer_t * const ring_buffer)
|
||||||
{
|
{
|
||||||
if (RingBuffer_GetNextWriterAddress(rb) == rb->reader)
|
if (RingBuffer_NextAddress(ring_buffer, ring_buffer->writer) == ring_buffer->reader)
|
||||||
return 1;
|
return 1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline uint8_t RingBuffer_IsEmpty(const ringbuffer_t * const rb)
|
static inline uint8_t RingBuffer_IsEmpty(ringbuffer_t * const ring_buffer)
|
||||||
{
|
{
|
||||||
if (rb->writer == rb->reader)
|
if (ring_buffer->writer == ring_buffer->reader)
|
||||||
return 1;
|
return 1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline uint8_t RingBuffer_IsFilled(const ringbuffer_t * const rb)
|
static inline uint8_t RingBuffer_IsFilled(ringbuffer_t * const ring_buffer)
|
||||||
{
|
{
|
||||||
return !RingBuffer_IsEmpty(rb);
|
return !RingBuffer_IsEmpty(ring_buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Write elem into RingBuffer. */
|
/* Write element into RingBuffer. */
|
||||||
static inline void RingBuffer_Write(ringbuffer_t * const rb, const uint32_t elem)
|
static inline void RingBuffer_Write(ringbuffer_t * const ring_buffer, uint32_t element)
|
||||||
{
|
{
|
||||||
if (!RingBuffer_IsFull(rb))
|
if (!RingBuffer_IsFull(ring_buffer))
|
||||||
{
|
{
|
||||||
*(rb->writer) = elem;
|
*(ring_buffer->writer) = element;
|
||||||
|
ring_buffer->writer = RingBuffer_NextAddress(ring_buffer, ring_buffer->writer);
|
||||||
if (rb->writer == rb->end)
|
|
||||||
rb->writer = rb->start;
|
|
||||||
else
|
|
||||||
rb->writer++;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
rb->write_failed = 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Read value from RingBuffer and returns it. */
|
/* Read value from RingBuffer. */
|
||||||
static inline uint32_t RingBuffer_Read(ringbuffer_t * const rb)
|
static inline uint32_t RingBuffer_Read(ringbuffer_t * const ring_buffer)
|
||||||
{
|
{
|
||||||
uint32_t ret = 0;
|
uint32_t element = 0;
|
||||||
|
|
||||||
if (!RingBuffer_IsEmpty(rb))
|
if (!RingBuffer_IsEmpty(ring_buffer))
|
||||||
{
|
{
|
||||||
ret = *(rb->reader);
|
element = *(ring_buffer->reader);
|
||||||
|
ring_buffer->reader = RingBuffer_NextAddress(ring_buffer, ring_buffer->reader);
|
||||||
if (rb->reader == rb->end)
|
|
||||||
rb->reader = rb->start;
|
|
||||||
else
|
|
||||||
rb->reader++;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
rb->read_failed = 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return element;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user