An mbed wrapper around the helium-client to communicate with the Helium Atom

Helium for ARM mbed

This code repository exposes an mbed library for the Helium Atom module. The Helium Atom makes it easy to securely connect IoT devices and applications to back-end IoT services.

Getting Started

See a getting started guide on the Helium site.

Supported Boards

The Helium mbed client should work with any mbed board with an available serial port.

Example Setup

Example applications can be found in the mbed Helium team.

Getting Help

If you have any questions or ideas about how to use this code - or any part of Helium - head over to the Helium Community Slack. We're standing by to help.

Contributing

Want to contribute to helium-mbed? That's awesome!

Please see CONTRIBUTING.md in this repository for details.

src/RingBuffer.h

Committer:
Marc Nijdam
Date:
2017-09-05
Revision:
23:cc2c1d1ed159
Parent:
14:af7682f4e610

File content as of revision 23:cc2c1d1ed159:

/**
 * \copyright Copyright 2017, Helium Systems, Inc.
 * All Rights Reserved. See LICENSE.txt for license information
 */

#ifndef RINGBUFFER_H
#define RINGBUFFER_H

#include <stdlib.h>

/*
 * \class RingBuffer
 *
 * \brief A simple circular buffer.
 *
 * This class implements a simple templated circular buffer. The
 * template variables are the type of the data in the buffer and the
 * size of the circular buffer.
 */
template <typename BufferType, size_t BufferSize>
class RingBuffer
{
  public:
    /** Create a RingBuffer */
    RingBuffer()
    {
        clear();
    }

    /** Push a value into the circular buffer
     *
     * @param value The value to push
     * @return true if pushed, false if the buffer is full
     */
    bool push(BufferType value)
    {
        // Add char to buffer
        uint16_t newhead = head + 1;
        if (newhead >= BufferSize)
        {
            newhead = 0;
        }
        else if (newhead == tail)
        {
            // Buffer full
            return false;
        }

        buf[head] = value;
        head      = newhead;

        return true;
    }

    /** Pop a value from the buffer.
     *
     * @param[out] value A pointer to the value to populate with the popped item
     * @return true if the pop succeeded, false if the buffer is empty.
     */
    bool pop(BufferType * value)
    {
        uint16_t newtail = tail;
        if (newtail == head)
        {
            return false;
        }

        *value = buf[newtail++];
        tail   = newtail >= BufferSize ? 0 : newtail;
        return true;
    }

    /** Get the number of available items in the buffer
     *
     * @return The number of items that can be popped from the buffer
     */
    uint16_t available() const
    {
        int16_t count = head - tail;
        return count < 0 ? count + BufferSize : count;
    }

    /** Clear the buffer */
    void clear()
    {
        head = tail = 0;
    }

  private:
    volatile uint16_t head;
    volatile uint16_t tail;
    BufferType        buf[BufferSize];
};

#endif