/******************************************************************************
* PPO Demo
*
* Proportional Pulse Output implemented using incremental error accumulation
* based on Bresenham's Line Algorithm.
*
* This sample generates pulse output respresentations of duty cycles for
* values 0-100 in a span of 100.
*
* Copyright (c) 2024 - )|( Sanctuary Software Studio, Inc.
******************************************************************************/
#include <stdio.h>
#include <stdbool.h>
int error;
int value2;
int span2;
void PPO_Set(int value, int span)
{
value2 = value << 1;
span2 = span << 1;
error = value2 - span;
}
bool PPO_Update(void)
{
bool output = false;
if (error > 0)
{
output = true;
error -= span2;
}
error += value2;
return output;
}
void PPO_Run(int value, int span)
{
int t;
PPO_Set(value, span);
printf("%2d/%2d : ", value, span);
for ( t=0; t <= span; t++)
{
putchar( PPO_Update() ? '-' : '_');
}
printf("\n");
}
int main()
{
int i;
for (i=0; i <= 100; i++)
{
PPO_Run(i, 100);
}
return 0;
}