#include "device_ring_buffer.h"

void rb_initialize(struct rb* _rb)
{
    memset(_rb, 0, sizeof(struct rb));
}

int rb_put(struct rb* _rb, char element)
{
    if (_rb->count < sizeof(_rb->buf))
    {
        _rb->count++;
        if (_rb->tail != _rb->head)
        {
            _rb->buf[_rb->tail] = element;
            _rb->tail++;
            if (_rb->tail == sizeof(_rb->buf))
            {
             _rb->tail = 0;
            }
            return 1;
        }
        else
        {
            return 0;
        }
    } 
    else 
    {
        return -1;
    }
   
}

int rb_get(struct rb* _rb, char *element)
{
    if (_rb->count > 0)
    {
        _rb->count--;
        if (_rb->tail != _rb->head)
        {
            *element = _rb->buf[_rb->head];
            _rb->head++;
            if (_rb->head == sizeof(_rb->buf))
            {
                _rb->head = 0;
            }
            return 1;
        }
        else
        {
            return 0;
        }
    } 
    else
    {
        return -1;
    }

}
int count_elements(struct rb* _rb)
{
    return _rb->count;
}