-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSensorMemory.h
61 lines (50 loc) · 1.71 KB
/
SensorMemory.h
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
#pragma once
#include "ArduinoWorkflow.h"
extern unsigned int __bss_end;
extern unsigned int __heap_start;
extern void *__brkval;
namespace AW {
class TSensorMemory : public TActor {
public:
TActor* Owner;
TTime Period = AW::TTime::MilliSeconds(3000);
bool SendValues = true;
TSensor<1> Sensor;
enum ESensor {
Free
};
TSensorMemory(TActor* owner)
: Owner(owner)
{
Sensor.Name = "memory";
Sensor.Values[ESensor::Free].Name = "free";
}
protected:
void OnEvent(TEventPtr event, const TActorContext& context) override {
switch (event->EventID) {
case TEventBootstrap::EventID:
return OnBootstrap(static_cast<TEventBootstrap*>(event.Release()), context);
case TEventReceive::EventID:
return OnReceive(static_cast<TEventReceive*>(event.Release()), context);
}
}
void OnBootstrap(TUniquePtr<TEventBootstrap>, const TActorContext& context) {
context.Send(this, this, new AW::TEventReceive(context.Now + Period));
}
static uint16_t GetFreeMemory() {
uint16_t freeMemory;
if ((int)__brkval == 0)
return ((uint16_t)&freeMemory) - ((uint16_t)&__bss_end);
else
return ((uint16_t)&freeMemory) - ((uint16_t)__brkval);
}
void OnReceive(AW::TUniquePtr<AW::TEventReceive> event, const AW::TActorContext& context) {
Sensor.Values[ESensor::Free].Value = GetFreeMemory();
Sensor.Updated = context.Now;
if (SendValues)
context.Send(this, Owner, new AW::TEventSensorData(Sensor, Sensor.Values[ESensor::Free]));
event->NotBefore = context.Now + Period;
context.Resend(this, event.Release());
}
};
}