-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysgw.cpp
110 lines (100 loc) · 2.59 KB
/
mysgw.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <MySensors.h>
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <iostream>
#include <map>
#include <memory>
#include <unistd.h>
#include <vector>
#include "Config.hpp"
#include "ILoopable.hpp"
#include "IMySensorsPresentable.hpp"
#include "SPIArduino.hpp"
#define MY_GATEWAY_MAX_CLIENTS 10
#define MY_DEBUG
vector<shared_ptr<pinCfg::ILoopable>> loopables;
vector<shared_ptr<pinCfg::IMySensorsPresentable>> presentables;
bool remoteConfigEnbled = true;
pinCfg::SPIArduino spiArduino;
void setup()
{
// Setup locally attached sensors
pinCfg::Config::readConfigFromFile(remoteConfigEnbled, loopables, presentables);
loopables.shrink_to_fit();
presentables.shrink_to_fit();
}
void presentation()
{
// Present locally attached sensors here
sendSketchInfo("RPI configurable IOs.", "1.0");
for (auto &presentable : presentables)
{
presentable->Present();
}
}
bool initialValueSent = false;
#ifdef MY_DEBUG
uint64_t lastLoopMs = (uint64_t)millis();
uint64_t msPerPeriod = 0;
uint32_t loopCount = 0;
#define LOOP_AVG_COUNT 1000
#endif
void loop()
{
if (!initialValueSent)
{
// Send locally attached sensors data here
for (auto &presentable : presentables)
{
presentable->PresentState();
}
wait(2000, C_SET, V_STATUS);
}
else
{
uint64_t miliseconds = (uint64_t)millis();
for (shared_ptr<pinCfg::ILoopable> &loopable : loopables)
{
loopable->loop(miliseconds);
}
#ifdef MY_DEBUG
if (loopCount == LOOP_AVG_COUNT)
{
uint32_t avg = msPerPeriod / LOOP_AVG_COUNT;
cout << "Avg loop time per " << LOOP_AVG_COUNT << " loops is: " << avg << "ms" << endl;
loopCount = 0;
msPerPeriod = 0;
}
msPerPeriod += miliseconds - lastLoopMs;
lastLoopMs = miliseconds;
loopCount++;
#endif
}
}
void receive(const MyMessage &message)
{
#ifdef MY_DEBUG
if (message.isAck())
cout << "ACK from gateway.";
#endif
if (message.type == V_STATUS || message.type == V_TEXT)
{
if (!initialValueSent)
initialValueSent = true;
if (message.getDestination() != 0)
return;
uint8_t id = message.getSensor();
shared_ptr<pinCfg::IMySensorsPresentable> fsw;
try
{
fsw = presentables.at(id);
}
catch (const out_of_range &oor)
{
cerr << "Presentable with id:" << id << " is not present." << endl;
return;
}
fsw->rcvMessage(message);
}
}