BLE Application to open a Garage door

Dependencies:   BLE_API Crypto RNG mbed nRF51822

Fork of BLE_LED by Bluetooth Low Energy

Committer:
dgomes
Date:
Tue Aug 25 22:18:21 2015 +0000
Revision:
9:329af8cdc923
Child:
10:80850cd6c29e
Mostly Works :)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dgomes 9:329af8cdc923 1 #ifndef HISTORY_H
dgomes 9:329af8cdc923 2 #define HISTORY_H
dgomes 9:329af8cdc923 3
dgomes 9:329af8cdc923 4 template<uint32_t BufferSize>
dgomes 9:329af8cdc923 5 class History {
dgomes 9:329af8cdc923 6 public:
dgomes 9:329af8cdc923 7 History() : _head(0) {
dgomes 9:329af8cdc923 8 }
dgomes 9:329af8cdc923 9
dgomes 9:329af8cdc923 10 ~History() {
dgomes 9:329af8cdc923 11 }
dgomes 9:329af8cdc923 12
dgomes 9:329af8cdc923 13 void save(const uint64_t& data) {
dgomes 9:329af8cdc923 14 _pool[_head++] = data;
dgomes 9:329af8cdc923 15 _head %= BufferSize;
dgomes 9:329af8cdc923 16 }
dgomes 9:329af8cdc923 17
dgomes 9:329af8cdc923 18 bool exist(const uint64_t& data) {
dgomes 9:329af8cdc923 19 for(uint32_t i=0; i<BufferSize; i++) {
dgomes 9:329af8cdc923 20 DBG("%llu == %llu\r\n", _pool[i], data);
dgomes 9:329af8cdc923 21 if(_pool[i]==data) {
dgomes 9:329af8cdc923 22 return true;
dgomes 9:329af8cdc923 23 }
dgomes 9:329af8cdc923 24 }
dgomes 9:329af8cdc923 25 return false;
dgomes 9:329af8cdc923 26 }
dgomes 9:329af8cdc923 27
dgomes 9:329af8cdc923 28 uint64_t last() {
dgomes 9:329af8cdc923 29 uint32_t prev = (_head-1+BufferSize)%BufferSize;
dgomes 9:329af8cdc923 30 return _pool[prev];
dgomes 9:329af8cdc923 31 }
dgomes 9:329af8cdc923 32
dgomes 9:329af8cdc923 33 uint32_t last_ts() {
dgomes 9:329af8cdc923 34 return (uint32_t) last();
dgomes 9:329af8cdc923 35 }
dgomes 9:329af8cdc923 36
dgomes 9:329af8cdc923 37 /** Reset the buffer
dgomes 9:329af8cdc923 38 *
dgomes 9:329af8cdc923 39 */
dgomes 9:329af8cdc923 40 void reset() {
dgomes 9:329af8cdc923 41 _head = 0;
dgomes 9:329af8cdc923 42 }
dgomes 9:329af8cdc923 43
dgomes 9:329af8cdc923 44 private:
dgomes 9:329af8cdc923 45 uint64_t _pool[BufferSize];
dgomes 9:329af8cdc923 46 volatile uint32_t _head;
dgomes 9:329af8cdc923 47 };
dgomes 9:329af8cdc923 48
dgomes 9:329af8cdc923 49
dgomes 9:329af8cdc923 50 #endif