-
Notifications
You must be signed in to change notification settings - Fork 0
/
I2CDevice.cpp
115 lines (96 loc) · 2.65 KB
/
I2CDevice.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
111
112
113
114
115
/**
* (c)2017 CDP Technologies AS
*/
#include "I2CDevice.h"
#include "I2CAdapter.h"
#include "I2CDeviceChannelGroup.h"
#include "I2CException.h"
#include "I2CHelpers.h"
#include <CDPSystem/Base/CDPComponent.h>
#include <CDPSystem/Base/CDPProperty.h>
#include <StudioAPI/NodeStream.h>
using namespace CDP::StudioAPI;
using namespace ServerIO;
using namespace std;
class I2CDevice::Private
{
public:
void ConfigureChannelGroups(XMLElementEx* element, CDPComponent* owner, ChannelManager* channelManager)
{
for (auto rg = element->GetChild("ChannelGroup"); rg != nullptr; rg = rg->GetNext("ChannelGroup"))
ConfigureChannelGroup(rg, owner, channelManager);
}
void ConfigureChannelGroup(XMLElementEx* element, CDPComponent* owner, ChannelManager* channelManager)
{
auto cg = new I2CDeviceChannelGroup(name);
cg->Configure(element, owner, channelManager);
channelGroups.push_back(cg);
}
bool TrySetAdapterAddress(I2CAdapter& adapter) const
{
try
{
adapter.SetAddress(address);
return true;
}
catch (const I2CException& e)
{
MessageLine("I2CDevice: Cannot set address to %s: %s", c_str(address), e.what());
return false;
}
}
bool TryReadAdapter(I2CAdapter& adapter, uint8_t subaddress, std::vector<uint8_t>& buffer) const
{
try
{
buffer = adapter.Read(subaddress, buffer.size());
return true;
}
catch (const I2CException& e)
{
MessageLine("I2CDevice: Cannot read from address %s: %s", subaddress, e.what());
return false;
}
}
vector<I2CDeviceChannelGroup*> channelGroups;
CDPProperty<string> name;
CDPProperty<string> model;
CDPProperty<unsigned char> address;
};
I2CDevice::I2CDevice()
: d(new Private)
{
}
void I2CDevice::Configure(XMLElementEx* element, CDPComponent* owner, ChannelManager* channelManager)
{
ConfigureProperty(d->name, "Name", owner, element);
ConfigureProperty(d->model, "Model", owner, element);
ConfigureProperty(d->address, "Address", owner, element);
d->ConfigureChannelGroups(element, owner, channelManager);
}
I2CDevice::~I2CDevice()
{
}
void I2CDevice::Process(I2CAdapter& adapter)
{
if (!d->TrySetAdapterAddress(adapter))
return;
for (auto& cg : d->channelGroups)
cg->Process(adapter);
}
const string I2CDevice::GetNodeName() const
{
return d->name;
}
string I2CDevice::GetNodeTypeName() const
{
return d->model;
}
void I2CDevice::FillNodeChildren(NodeStream &serializer) const
{
CDPNode::FillNodeChildren(serializer);
serializer.StdContainerStreamer(d->channelGroups);
serializer << AdoptedChild(d->name)
<< AdoptedChild(d->model)
<< AdoptedChild(d->address);
}