-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObservable.cpp
81 lines (64 loc) · 1.28 KB
/
Observable.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#ifdef LINUX
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <ctime>
#include <time.h>
#include <math.h>
#else
#include <Arduino.h>
#endif
#include "Observable.h"
#include "Observer.h"
#include "Data.h"
Observable::Observable()
{
_obsListIdx = 0;
_dataListIdx = 0;
}
Observable::~Observable()
{
}
void Observable::subscribe(Observer* obs)
{
_obsList[_obsListIdx++] = obs;
}
void Observable::push(Data* data)
{
_dataList[_dataListIdx++] = data;
}
#ifdef LINUX
unsigned long Observable::millis() {
unsigned long ret = 0;
struct timespec spec;
unsigned long ms;
time_t s;
clock_gettime(CLOCK_REALTIME, &spec);
s = spec.tv_sec;
ms = round(spec.tv_nsec/1.0e6);
ret = ((unsigned long)s * 1000) + ms;
return ret;
}
#endif
bool Observable::setup()
{
return true;
}
void Observable::loop()
{
Data* tmpDataList[MAX_ARR_SIZE_EVT];
uint32_t dataListLen;
this->generateData();
dataListLen = _dataListIdx;
for (int i = 0; i < dataListLen; i++) {
tmpDataList[i] = _dataList[i];
_dataList[i] = NULL;
}
_dataListIdx = 0;
for (int i = 0; i < dataListLen; i++) {
for (int j = 0; j < _obsListIdx; j++) {
_obsList[j]->notify(*(tmpDataList[i]));
}
delete tmpDataList[i]; // TODO: we need another way to release.
}
}