c,c

                Never    
C
       
#include <stdio.h>
#include <stdint.h>
//#include <avr/io.h>

#define RINGZ 16
uint8_t ring[RINGZ];
uint8_t head, tail;


uint8_t ring_push(uint8_t c)
{   if( (head==tail) || 		// empty
        ((head+1)&RINGZ)!=tail )	// chasing tail
    {	ring[head]= c;
        head++;		// advance head
        head&= RINGZ;	// wrap as required
printf("push %hhd\thead=%hhd\n", c, head );
        return c;
    }
printf("BAD push: h(%hhd) t(%hhd)\n", head, tail);
    return 0xFF-c;	// this would be "FULL"
}

uint8_t ring_pull()
{   if( tail!=head )	// !empty
    { uint8_t c;
        c= ring[tail];
        tail++;
        tail&= RINGZ;
printf("pull %hhd\thead=%hhd\n", c, tail );
        return c;
    }
printf("BAD pull: h(%hhd) t(%hhd)\n", head, tail);
    return -1;
}

#if 0
ISR(USART_RXC_vect)
{
    // Read and echo to host...
    UDR0= ring_push(UDR0);
}
#endif

int main()
{ int i, j;

    for(int i=0; i<3; i++)
    {	for(j=i*10; j<(i*10)+10; j++)
            printf("%d:\t",j), ring_push(j);
        for(j=0; j<6; j++)
            printf("%d:\t",j), ring_pull();
    }
    return 0;
}

Raw Text