From cece79bffe45929927d784bf618d0e707d0f25ee Mon Sep 17 00:00:00 2001 From: Andre Heber Date: Fri, 4 Mar 2016 13:58:56 +0100 Subject: [PATCH] Improved Readability, more comments. --- Pipe/Pipe.vcxproj | 4 +- Pipe/main.c | 64 +++++++++++++---------- Pipe/pipe.h | 129 +++++++++++++++++++++++++++++++--------------- Pipe/ringbuffer.h | 73 +++++++++----------------- 4 files changed, 152 insertions(+), 118 deletions(-) diff --git a/Pipe/Pipe.vcxproj b/Pipe/Pipe.vcxproj index b19c45c..67b3c20 100644 --- a/Pipe/Pipe.vcxproj +++ b/Pipe/Pipe.vcxproj @@ -40,7 +40,7 @@ Application true - v140 + v120 MultiByte @@ -78,7 +78,7 @@ - Level3 + Level4 Disabled true diff --git a/Pipe/main.c b/Pipe/main.c index 7bcb03d..78e4f87 100644 --- a/Pipe/main.c +++ b/Pipe/main.c @@ -3,6 +3,7 @@ #include "ringbuffer.h" #include "pipe.h" +/* Integrate every element of the signal. */ void increment(pipe_t * const 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) { 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); - *state = *state + item; - Pipe_Write(p, *state); + uint32_t item = Pipe_Read(pipe); + state = state + item; + 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; - while (Pipe_isFilled(p)) - sum += Pipe_Read(p); - Pipe_Write(p, sum); + while (Pipe_isFilled(pipe)) + sum += Pipe_Read(pipe); + 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 element_counter = 0; uint32_t average = 0; - while (Pipe_isFilled(p)) + while (Pipe_isFilled(pipe)) { - sum += Pipe_Read(p); + sum += Pipe_Read(pipe); 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"); - while (Pipe_isFilled(p)) - printf("%d\n", Pipe_Read(p)); + while (Pipe_isFilled(pipe)) + 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) - printf("%s -> %d -> %s\n", from->name, elem, to->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)); - else if (from->state != NULL) - printf("%s(%d) -> %d -> %s\n", from->name, *((uint32_t*)from->state), elem, to->name); + if (Pipe_isFull(target)) + printf("Error: Pipe %s is full!\n", target->name); + + if (source->state == NULL && target->state == NULL) + printf("%s -> %d -> %s\n", source->name, element, target->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 - 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) @@ -85,7 +97,7 @@ int main(void) /* Create pipes and connect them */ Pipe_Create(increment_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(average_pipe, NULL, 8, log); Pipe_Create(print_pipe, NULL, 4, log); diff --git a/Pipe/pipe.h b/Pipe/pipe.h index dc7efe6..5de284b 100644 --- a/Pipe/pipe.h +++ b/Pipe/pipe.h @@ -7,87 +7,132 @@ /* microsoft specific */ #define inline __inline -/* number of counts */ -#define PIPE_OUTPUT_COUNT 4 +/* number of connections */ +#define PIPE_NUMBER_OF_CONNECTIONS 4 typedef struct pipe_tt { ringbuffer_t * input; void * state; - struct pipe_tt *output[PIPE_OUTPUT_COUNT]; - uint8_t output_count; + struct pipe_tt *connection[PIPE_NUMBER_OF_CONNECTIONS]; + uint32_t connection_count; + uint32_t connection_max; 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; -/***********************************/ -/* Functions to contruct pipe mesh */ -/***********************************/ +/***************************************/ +/* Functions to construct pipe system. */ +/***************************************/ #define Concat2(a, b) a ## b #define Concat(a, b) Concat2(a, b) #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]; \ - 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) +/* +Macro for the creation of a pipe. +Automates the creation of a ring buffer and the pipe. +arg_name is the variable name and string name of the pipe. +arg_state is the given state, which can be used in the function. +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( - pipe_t * const p, - ringbuffer_t * const arg_input, - void * arg_state, - char * const arg_name, - void(*arg_log)(struct pipe_tt * from, struct pipe_tt * to, uint32_t elem) + pipe_t * const pipe, + ringbuffer_t * const input, + void * state, + char * const name, + void(*log_function)(struct pipe_tt * source, struct pipe_tt * target, uint32_t element) ) { - p->input = arg_input; - p->state = arg_state; + pipe->input = input; + pipe->state = state; - for (uint8_t i = 0; i < PIPE_OUTPUT_COUNT; i++) - p->output[i] = NULL; + for (uint8_t i = 0; i < PIPE_NUMBER_OF_CONNECTIONS; i++) + pipe->connection[i] = NULL; - p->output_count = 0; - p->name = arg_name; - p->log = arg_log; + pipe->connection_count = 0; + pipe->connection_max = PIPE_NUMBER_OF_CONNECTIONS; + 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; - a->output_count++; + if (source->connection_count < source->connection_max) + { + 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); - RingBuffer_Write(p->output[i]->input, elem); + pipe->log_function(pipe, pipe->connection[i], element); + 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 diff --git a/Pipe/ringbuffer.h b/Pipe/ringbuffer.h index f7e7799..f7293aa 100644 --- a/Pipe/ringbuffer.h +++ b/Pipe/ringbuffer.h @@ -10,87 +10,64 @@ typedef struct { uint32_t * writer; uint32_t * start; uint32_t * end; - uint8_t write_failed; - uint8_t read_failed; } ringbuffer_t; /* 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; - rb->end = array + size - 1; - rb->write_failed = rb->read_failed = 0; + ring_buffer->start = ring_buffer->reader = ring_buffer->writer = &array[0]; + ring_buffer->end = &array[0] + size - 1; } -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 (next_address == rb->end) - next_address = rb->start; + if (pointer == ring_buffer->end) + return ring_buffer->start; else - next_address++; - - return next_address; + return pointer + 1; } -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 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 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. */ -static inline void RingBuffer_Write(ringbuffer_t * const rb, const uint32_t elem) +/* Write element into RingBuffer. */ +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; - - if (rb->writer == rb->end) - rb->writer = rb->start; - else - rb->writer++; - } - else - { - rb->write_failed = 1; + *(ring_buffer->writer) = element; + ring_buffer->writer = RingBuffer_NextAddress(ring_buffer, ring_buffer->writer); } } -/* Read value from RingBuffer and returns it. */ -static inline uint32_t RingBuffer_Read(ringbuffer_t * const rb) +/* Read value from RingBuffer. */ +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); - - if (rb->reader == rb->end) - rb->reader = rb->start; - else - rb->reader++; - } - else - { - rb->read_failed = 1; + element = *(ring_buffer->reader); + ring_buffer->reader = RingBuffer_NextAddress(ring_buffer, ring_buffer->reader); } - return ret; + return element; } #endif