BLE Application to open a Garage door

Dependencies:   BLE_API Crypto RNG mbed nRF51822

Fork of BLE_LED by Bluetooth Low Energy

History.h

Committer:
dgomes
Date:
2015-08-25
Revision:
9:329af8cdc923
Child:
10:80850cd6c29e

File content as of revision 9:329af8cdc923:

#ifndef HISTORY_H
#define HISTORY_H

template<uint32_t BufferSize>
class History {
public:
    History() : _head(0) {
    }

    ~History() {
    }

    void save(const uint64_t& data) {
        _pool[_head++] = data;
        _head %= BufferSize;
    }

    bool exist(const uint64_t& data) {
        for(uint32_t i=0; i<BufferSize; i++) {
            DBG("%llu == %llu\r\n", _pool[i], data);
            if(_pool[i]==data) {
                return true;
            }
        }
        return false;
    }
    
    uint64_t last() {
        uint32_t prev = (_head-1+BufferSize)%BufferSize;
        return _pool[prev];
    }
    
    uint32_t last_ts() {
        return (uint32_t) last();
    }

    /** Reset the buffer
     *
     */
    void reset() {
        _head = 0;
    }

private:
    uint64_t _pool[BufferSize];
    volatile uint32_t _head;
};


#endif