BLE Application to open a Garage door

Dependencies:   BLE_API Crypto RNG mbed nRF51822

Fork of BLE_LED by Bluetooth Low Energy

Committer:
duststorm
Date:
Mon May 01 00:33:07 2017 +0000
Revision:
15:88ea59cb2b9e
Parent:
9:329af8cdc923
force address for Nano to F2:9B:4B:03:02:01 to match the prefix expected by the app

Who changed what in which revision?

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