Not sure what you mean exactly by 'Embedded C', but I write C for embedded systems, and I'd use something like the following. (The GpioPrint()
function is really only for (my) debugging.)
#include <stdio.h>
#include <string.h>
// Returns the size of a[].
#define ARRAY_SZ(a) (sizeof a / sizeof a[0])
// Holds MCU-GPIO pin values.
typedef struct
{
unsigned a[10]; // replace unsigned with your 'pin value' type
unsigned n; // values are in a[0..n-1]
} Gpio_t;
// Initialise the pin value store.
static void GpioInit(Gpio_t *gpio)
{
gpio->n = 0;
}
// Add the new value to the pin store, removing the oldest if the store
// is full.
static void GpioAdd(Gpio_t *gpio, unsigned newVal)
{
if (gpio->n < ARRAY_SZ(gpio->a))
{
gpio->a[gpio->n++] = newVal;
}
else
{
memmove(&gpio->a[0], &gpio->a[1], (gpio->n - 1) * sizeof gpio->a[0]);
gpio->a[gpio->n - 1] = newVal;
}
}
// Output the pin store contents.
static void GpioPrint(const Gpio_t *gpio)
{
unsigned i;
for (i = 0; i < gpio->n; i++)
{
printf("%2u: %10u\n", i, gpio->a[i]);
}
}
// Test the functions.
int main(void)
{
Gpio_t gpio;
unsigned newVal;
GpioInit(&gpio);
// Add 20 values to the pin store, checking the contents after each
// addition.
for (newVal = 0; newVal < 20; newVal++)
{
printf("add %u:\n", newVal);
GpioAdd(&gpio, newVal);
GpioPrint(&gpio);
}
return 0;
}