-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathethercatdata.cpp
More file actions
43 lines (35 loc) · 1.14 KB
/
ethercatdata.cpp
File metadata and controls
43 lines (35 loc) · 1.14 KB
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
#include "ethercatdata.h"
EthercatData::EthercatData(QObject* parent)
: QObject { parent }
{
}
// slot: output changed
void EthercatData::onOutputChanged(int slave, int idx, bool checked)
{
auto& io = m_mapData[slave];
// calculate byte and bit offset
const int byteBits = 8;
const int byteOffset = idx / byteBits;
const int bitOffset = idx % byteBits;
const uint8_t mask = static_cast<uint8_t>(1u << bitOffset);
uint8_t& byteRef = io.output[byteOffset];
// set or clear bit
const uint8_t newByte = checked
? static_cast<uint8_t>(byteRef | mask)
: static_cast<uint8_t>(byteRef & ~mask);
// emit signal if changed
if (byteRef != newByte) {
byteRef = newByte;
emit outputChanged(slave, byteOffset, byteRef);
}
}
// slot: input changed
void EthercatData::onInputChanged(int slave, int byteOffset, uint8_t value)
{
auto& io = m_mapData[slave];
// emit signal if changed
if (io.input[byteOffset] != value) {
io.input[byteOffset] = value;
emit inputChanged(slave, byteOffset, value);
}
}