Init
This commit is contained in:
28
Pipe.sln
Normal file
28
Pipe.sln
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
# Visual Studio 14
|
||||||
|
VisualStudioVersion = 14.0.24720.0
|
||||||
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
|
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Pipe", "Pipe\Pipe.vcxproj", "{FA5ADF78-687F-4538-B7DB-C62B46B697C7}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|x64 = Debug|x64
|
||||||
|
Debug|x86 = Debug|x86
|
||||||
|
Release|x64 = Release|x64
|
||||||
|
Release|x86 = Release|x86
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{FA5ADF78-687F-4538-B7DB-C62B46B697C7}.Debug|x64.ActiveCfg = Debug|x64
|
||||||
|
{FA5ADF78-687F-4538-B7DB-C62B46B697C7}.Debug|x64.Build.0 = Debug|x64
|
||||||
|
{FA5ADF78-687F-4538-B7DB-C62B46B697C7}.Debug|x86.ActiveCfg = Debug|Win32
|
||||||
|
{FA5ADF78-687F-4538-B7DB-C62B46B697C7}.Debug|x86.Build.0 = Debug|Win32
|
||||||
|
{FA5ADF78-687F-4538-B7DB-C62B46B697C7}.Release|x64.ActiveCfg = Release|x64
|
||||||
|
{FA5ADF78-687F-4538-B7DB-C62B46B697C7}.Release|x64.Build.0 = Release|x64
|
||||||
|
{FA5ADF78-687F-4538-B7DB-C62B46B697C7}.Release|x86.ActiveCfg = Release|Win32
|
||||||
|
{FA5ADF78-687F-4538-B7DB-C62B46B697C7}.Release|x86.Build.0 = Release|Win32
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
||||||
122
Pipe/Pipe.vcxproj
Normal file
122
Pipe/Pipe.vcxproj
Normal file
@ -0,0 +1,122 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<ItemGroup Label="ProjectConfigurations">
|
||||||
|
<ProjectConfiguration Include="Debug|Win32">
|
||||||
|
<Configuration>Debug</Configuration>
|
||||||
|
<Platform>Win32</Platform>
|
||||||
|
</ProjectConfiguration>
|
||||||
|
<ProjectConfiguration Include="Release|Win32">
|
||||||
|
<Configuration>Release</Configuration>
|
||||||
|
<Platform>Win32</Platform>
|
||||||
|
</ProjectConfiguration>
|
||||||
|
<ProjectConfiguration Include="Debug|x64">
|
||||||
|
<Configuration>Debug</Configuration>
|
||||||
|
<Platform>x64</Platform>
|
||||||
|
</ProjectConfiguration>
|
||||||
|
<ProjectConfiguration Include="Release|x64">
|
||||||
|
<Configuration>Release</Configuration>
|
||||||
|
<Platform>x64</Platform>
|
||||||
|
</ProjectConfiguration>
|
||||||
|
</ItemGroup>
|
||||||
|
<PropertyGroup Label="Globals">
|
||||||
|
<ProjectGuid>{FA5ADF78-687F-4538-B7DB-C62B46B697C7}</ProjectGuid>
|
||||||
|
<RootNamespace>Pipe</RootNamespace>
|
||||||
|
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
|
||||||
|
</PropertyGroup>
|
||||||
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||||
|
<ConfigurationType>Application</ConfigurationType>
|
||||||
|
<UseDebugLibraries>true</UseDebugLibraries>
|
||||||
|
<PlatformToolset>v140</PlatformToolset>
|
||||||
|
<CharacterSet>MultiByte</CharacterSet>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||||
|
<ConfigurationType>Application</ConfigurationType>
|
||||||
|
<UseDebugLibraries>false</UseDebugLibraries>
|
||||||
|
<PlatformToolset>v140</PlatformToolset>
|
||||||
|
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||||
|
<CharacterSet>MultiByte</CharacterSet>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||||
|
<ConfigurationType>Application</ConfigurationType>
|
||||||
|
<UseDebugLibraries>true</UseDebugLibraries>
|
||||||
|
<PlatformToolset>v140</PlatformToolset>
|
||||||
|
<CharacterSet>MultiByte</CharacterSet>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||||
|
<ConfigurationType>Application</ConfigurationType>
|
||||||
|
<UseDebugLibraries>false</UseDebugLibraries>
|
||||||
|
<PlatformToolset>v140</PlatformToolset>
|
||||||
|
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||||
|
<CharacterSet>MultiByte</CharacterSet>
|
||||||
|
</PropertyGroup>
|
||||||
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||||
|
<ImportGroup Label="ExtensionSettings">
|
||||||
|
</ImportGroup>
|
||||||
|
<ImportGroup Label="Shared">
|
||||||
|
</ImportGroup>
|
||||||
|
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||||
|
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||||
|
</ImportGroup>
|
||||||
|
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||||
|
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||||
|
</ImportGroup>
|
||||||
|
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||||
|
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||||
|
</ImportGroup>
|
||||||
|
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||||
|
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||||
|
</ImportGroup>
|
||||||
|
<PropertyGroup Label="UserMacros" />
|
||||||
|
<PropertyGroup />
|
||||||
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||||
|
<ClCompile>
|
||||||
|
<WarningLevel>Level3</WarningLevel>
|
||||||
|
<Optimization>Disabled</Optimization>
|
||||||
|
<SDLCheck>true</SDLCheck>
|
||||||
|
</ClCompile>
|
||||||
|
</ItemDefinitionGroup>
|
||||||
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||||
|
<ClCompile>
|
||||||
|
<WarningLevel>Level3</WarningLevel>
|
||||||
|
<Optimization>Disabled</Optimization>
|
||||||
|
<SDLCheck>true</SDLCheck>
|
||||||
|
</ClCompile>
|
||||||
|
</ItemDefinitionGroup>
|
||||||
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||||
|
<ClCompile>
|
||||||
|
<WarningLevel>Level3</WarningLevel>
|
||||||
|
<Optimization>MaxSpeed</Optimization>
|
||||||
|
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||||
|
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||||
|
<SDLCheck>true</SDLCheck>
|
||||||
|
</ClCompile>
|
||||||
|
<Link>
|
||||||
|
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||||
|
<OptimizeReferences>true</OptimizeReferences>
|
||||||
|
</Link>
|
||||||
|
</ItemDefinitionGroup>
|
||||||
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||||
|
<ClCompile>
|
||||||
|
<WarningLevel>Level3</WarningLevel>
|
||||||
|
<Optimization>MaxSpeed</Optimization>
|
||||||
|
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||||
|
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||||
|
<SDLCheck>true</SDLCheck>
|
||||||
|
</ClCompile>
|
||||||
|
<Link>
|
||||||
|
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||||
|
<OptimizeReferences>true</OptimizeReferences>
|
||||||
|
</Link>
|
||||||
|
</ItemDefinitionGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClCompile Include="main.c" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClInclude Include="pipe.h" />
|
||||||
|
<ClInclude Include="ringbuffer.h" />
|
||||||
|
</ItemGroup>
|
||||||
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
|
<ImportGroup Label="ExtensionTargets">
|
||||||
|
</ImportGroup>
|
||||||
|
</Project>
|
||||||
30
Pipe/Pipe.vcxproj.filters
Normal file
30
Pipe/Pipe.vcxproj.filters
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<ItemGroup>
|
||||||
|
<Filter Include="Source Files">
|
||||||
|
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||||
|
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||||
|
</Filter>
|
||||||
|
<Filter Include="Header Files">
|
||||||
|
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||||
|
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
|
||||||
|
</Filter>
|
||||||
|
<Filter Include="Resource Files">
|
||||||
|
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||||
|
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||||
|
</Filter>
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClCompile Include="main.c">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClInclude Include="pipe.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="ringbuffer.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
</ItemGroup>
|
||||||
|
</Project>
|
||||||
110
Pipe/main.c
Normal file
110
Pipe/main.c
Normal file
@ -0,0 +1,110 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include "ringbuffer.h"
|
||||||
|
#include "pipe.h"
|
||||||
|
|
||||||
|
void increment(pipe_t * const p)
|
||||||
|
{
|
||||||
|
while (Pipe_isFilled(p))
|
||||||
|
{
|
||||||
|
uint32_t item = Pipe_Read(p);
|
||||||
|
item++;
|
||||||
|
Pipe_Write(p, item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void square(pipe_t * const p)
|
||||||
|
{
|
||||||
|
while (Pipe_isFilled(p))
|
||||||
|
{
|
||||||
|
uint32_t item = Pipe_Read(p);
|
||||||
|
item = item * item;
|
||||||
|
Pipe_Write(p, item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void integrate(pipe_t * const p)
|
||||||
|
{
|
||||||
|
static uint32_t state = 0;
|
||||||
|
|
||||||
|
while (Pipe_isFilled(p))
|
||||||
|
{
|
||||||
|
uint32_t item = Pipe_Read(p);
|
||||||
|
state = state + item;
|
||||||
|
Pipe_Write(p, state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void sum(pipe_t * const p)
|
||||||
|
{
|
||||||
|
uint32_t sum = 0;
|
||||||
|
|
||||||
|
while (Pipe_isFilled(p))
|
||||||
|
sum += Pipe_Read(p);
|
||||||
|
Pipe_Write(p, sum);
|
||||||
|
}
|
||||||
|
|
||||||
|
void average(pipe_t * const p)
|
||||||
|
{
|
||||||
|
uint32_t sum = 0;
|
||||||
|
uint32_t element_counter = 0;
|
||||||
|
uint32_t average = 0;
|
||||||
|
|
||||||
|
while (Pipe_isFilled(p))
|
||||||
|
{
|
||||||
|
sum += Pipe_Read(p);
|
||||||
|
element_counter++;
|
||||||
|
}
|
||||||
|
average = sum / element_counter;
|
||||||
|
Pipe_Write(p, average);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char * argv[])
|
||||||
|
{
|
||||||
|
/* Create pipes and connect them */
|
||||||
|
Pipe_Create(increment_pipe, 4);
|
||||||
|
Pipe_Create(square_pipe, 4);
|
||||||
|
Pipe_Create(integrate_pipe, 8);
|
||||||
|
Pipe_Create(sum_pipe, 8);
|
||||||
|
Pipe_Create(average_pipe, 8);
|
||||||
|
|
||||||
|
Pipe_Connect(&increment_pipe, &integrate_pipe);
|
||||||
|
Pipe_Connect(&square_pipe, &integrate_pipe);
|
||||||
|
Pipe_CreateOutput(integrate_pipe, integrate_output_rb, 8);
|
||||||
|
Pipe_Connect(&integrate_pipe, &sum_pipe);
|
||||||
|
Pipe_CreateOutput(sum_pipe, sum_output_rb, 4);
|
||||||
|
Pipe_Connect(&integrate_pipe, &average_pipe);
|
||||||
|
Pipe_CreateOutput(average_pipe, average_output_rb, 4);
|
||||||
|
|
||||||
|
/* Generate input */
|
||||||
|
Pipe_Insert(&increment_pipe, 1);
|
||||||
|
Pipe_Insert(&increment_pipe, 3);
|
||||||
|
Pipe_Insert(&increment_pipe, 5);
|
||||||
|
|
||||||
|
Pipe_Insert(&square_pipe, 2);
|
||||||
|
Pipe_Insert(&square_pipe, 4);
|
||||||
|
Pipe_Insert(&square_pipe, 6);
|
||||||
|
|
||||||
|
/* run the functions */
|
||||||
|
increment(&increment_pipe);
|
||||||
|
square(&square_pipe);
|
||||||
|
integrate(&integrate_pipe);
|
||||||
|
sum(&sum_pipe);
|
||||||
|
average(&average_pipe);
|
||||||
|
|
||||||
|
/* print the output */
|
||||||
|
printf("Output:\n");
|
||||||
|
printf("%d\n", RingBuffer_Read(&integrate_output_rb));
|
||||||
|
printf("%d\n", RingBuffer_Read(&integrate_output_rb));
|
||||||
|
printf("%d\n", RingBuffer_Read(&integrate_output_rb));
|
||||||
|
printf("%d\n", RingBuffer_Read(&integrate_output_rb));
|
||||||
|
printf("%d\n", RingBuffer_Read(&integrate_output_rb));
|
||||||
|
printf("%d\n", RingBuffer_Read(&integrate_output_rb));
|
||||||
|
printf("\n");
|
||||||
|
printf("Summe: %d\n", RingBuffer_Read(&sum_output_rb));
|
||||||
|
printf("Average: %d\n", RingBuffer_Read(&average_output_rb));
|
||||||
|
|
||||||
|
getchar();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
89
Pipe/pipe.h
Normal file
89
Pipe/pipe.h
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
#ifndef PIPE_H_
|
||||||
|
#define PIPE_H_
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include "ringbuffer.h"
|
||||||
|
|
||||||
|
/* microsoft specific */
|
||||||
|
#define inline __inline
|
||||||
|
|
||||||
|
/* number of counts */
|
||||||
|
#define PIPE_OUTPUT_COUNT 4
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
ringbuffer_t * input;
|
||||||
|
ringbuffer_t *output[PIPE_OUTPUT_COUNT];
|
||||||
|
uint8_t output_count;
|
||||||
|
} pipe_t;
|
||||||
|
|
||||||
|
/***********************************/
|
||||||
|
/* Functions to contruct pipe mesh */
|
||||||
|
/***********************************/
|
||||||
|
|
||||||
|
static inline void Pipe_Init(pipe_t * const p, ringbuffer_t * const arg_input)
|
||||||
|
{
|
||||||
|
p->input = arg_input;
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < PIPE_OUTPUT_COUNT; i++)
|
||||||
|
p->output[i] = NULL;
|
||||||
|
|
||||||
|
p->output_count = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void Pipe_Connect(pipe_t * const a, pipe_t * const b)
|
||||||
|
{
|
||||||
|
a->output[a->output_count] = b->input;
|
||||||
|
a->output_count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void Pipe_AddOutput(pipe_t * const p, ringbuffer_t * const rb)
|
||||||
|
{
|
||||||
|
p->output[p->output_count] = rb;
|
||||||
|
p->output_count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
#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_size) \
|
||||||
|
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)
|
||||||
|
|
||||||
|
#define Pipe_CreateOutput(arg_pipe, arg_name, arg_size) \
|
||||||
|
static uint32_t Concat(buffer, __LINE__)[arg_size]; \
|
||||||
|
ringbuffer_t arg_name; \
|
||||||
|
RingBuffer_InitFromArray(&arg_name, Concat(buffer, __LINE__), SizeOfArray(Concat(buffer, __LINE__))); \
|
||||||
|
Pipe_AddOutput(&arg_pipe, &arg_name);
|
||||||
|
|
||||||
|
|
||||||
|
/************************************/
|
||||||
|
/* Functions to work with pipe mesh */
|
||||||
|
/************************************/
|
||||||
|
|
||||||
|
static inline void Pipe_Insert(pipe_t * const p, uint32_t elem)
|
||||||
|
{
|
||||||
|
RingBuffer_Write(p->input, elem);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline uint32_t Pipe_Read(pipe_t * p)
|
||||||
|
{
|
||||||
|
return RingBuffer_Read(p->input);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void Pipe_Write(pipe_t * p, uint32_t elem)
|
||||||
|
{
|
||||||
|
for (uint8_t i = 0; i < p->output_count; i++)
|
||||||
|
RingBuffer_Write(p->output[i], elem);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline uint8_t Pipe_isFilled(pipe_t * p)
|
||||||
|
{
|
||||||
|
return RingBuffer_IsFilled(p->input);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
96
Pipe/ringbuffer.h
Normal file
96
Pipe/ringbuffer.h
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
#ifndef RINGBUFFER_H_
|
||||||
|
#define RINGBUFFER_H_
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#define inline __inline
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint32_t * reader;
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
rb->start = rb->reader = rb->writer = array;
|
||||||
|
rb->end = array + size - 1;
|
||||||
|
rb->write_failed = rb->read_failed = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline uint32_t * RingBuffer_GetNextWriterAddress(const ringbuffer_t * const rb)
|
||||||
|
{
|
||||||
|
uint32_t * next_address = rb->writer;
|
||||||
|
|
||||||
|
if (next_address == rb->end)
|
||||||
|
next_address = rb->start;
|
||||||
|
else
|
||||||
|
next_address++;
|
||||||
|
|
||||||
|
return next_address;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline uint8_t RingBuffer_IsFull(const ringbuffer_t * const rb)
|
||||||
|
{
|
||||||
|
if (RingBuffer_GetNextWriterAddress(rb) == rb->reader)
|
||||||
|
return 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline uint8_t RingBuffer_IsEmpty(const ringbuffer_t * const rb)
|
||||||
|
{
|
||||||
|
if (rb->writer == rb->reader)
|
||||||
|
return 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline uint8_t RingBuffer_IsFilled(const ringbuffer_t * const rb)
|
||||||
|
{
|
||||||
|
return !RingBuffer_IsEmpty(rb);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Write elem into RingBuffer. */
|
||||||
|
static inline void RingBuffer_Write(ringbuffer_t * const rb, const uint32_t elem)
|
||||||
|
{
|
||||||
|
if (!RingBuffer_IsFull(rb))
|
||||||
|
{
|
||||||
|
*(rb->writer) = elem;
|
||||||
|
|
||||||
|
if (rb->writer == rb->end)
|
||||||
|
rb->writer = rb->start;
|
||||||
|
else
|
||||||
|
rb->writer++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
rb->write_failed = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Read value from RingBuffer and returns it. */
|
||||||
|
static inline uint32_t RingBuffer_Read(ringbuffer_t * const rb)
|
||||||
|
{
|
||||||
|
uint32_t ret = 0;
|
||||||
|
|
||||||
|
if (!RingBuffer_IsEmpty(rb))
|
||||||
|
{
|
||||||
|
ret = *(rb->reader);
|
||||||
|
|
||||||
|
if (rb->reader == rb->end)
|
||||||
|
rb->reader = rb->start;
|
||||||
|
else
|
||||||
|
rb->reader++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
rb->read_failed = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user