Simple ring buffer

nine_ring/nine_ring.cpp

Committer:
jont
Date:
2015-07-13
Revision:
0:c050eb7b0c10

File content as of revision 0:c050eb7b0c10:

#include "mbed.h"
#include "nine_ring.h"
/*
J.J.Trinder, based on original code for midi projects 199X
updateded sometime to the MBED

Yeh its atad klunky, it works and is useful to explain things to people...
*/
#define _PC
NRing::NRing()
{
   ring_init();
}



/*======================================================================*/
/* implementation of Ringbuffer                     */
/*======================================================================*/
 /** Init the indexes
        
*/
void NRing::ring_init(void)
{
    inIndex = 0;
    inCount = 0;
}

/*======================================================================*/
/* RingWriteToBuffer                     */
/*======================================================================*/
/**
Write value (long) into the ring buffer
@param <n> A long value to store in the ring buffer
*/
//add critical section ie disable interrupts while fiddling with buffers
int NRing::RingWriteToBuffer(long n)
{
   // printf("WriteInBuffer%d",inCount);
__disable_irq();    // Disable Interrupts
 
// do something that can't be interrupted
   opBuffer[inIndex] = n;
    inCount++;
    inIndex++;
    if (inIndex >= buff_size)
        inIndex = 0;
   
 
__enable_irq();     // Enable Interrupts 
  //printf("ZWriteInBuffer%d\n",inCount);       
    return 0;
}


//todo fix this as we dont want to do this
/*======================================================================*/
/* return current count of stuff in buffer           */
/*======================================================================*/
int NRing::ring_count()
{
   // printf("InCount %d",inCount);
    return (inCount);
}


//todo fix this as we dont want to do this
/*======================================================================*/
/* Output whats remaining....            */
/*======================================================================*/
long NRing::get_next()
{
   
    long toSend;
#ifdef _PC
    printf("\nCall in %x out %x count %x",inIndex,outIndex, inCount);
#endif
    if (inCount == 0)
        return -1;     /* nowt to send */
    __disable_irq();    // Disable Interrupts    
    toSend = opBuffer[outIndex];

#ifdef _PC
    printf(" <%i> ",toSend);
#endif
 
    inCount--;
    outIndex++;
    if (outIndex >=buff_size)
        outIndex = 0;
    __enable_irq();     // Enable Interrupts       
    return toSend;
}