-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPixelScanner.cpp
More file actions
138 lines (120 loc) · 4.09 KB
/
Copy pathPixelScanner.cpp
File metadata and controls
138 lines (120 loc) · 4.09 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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "pch.h"
#include "Systemic/Pixels/PixelScanner.h"
#include <cassert>
#include "Systemic/BluetoothLE/Scanner.h"
#include "Systemic/BluetoothLE/ScannedPeripheral.h"
#include "Systemic/Pixels/ScannedPixel.h"
#include "Systemic/Pixels/PixelBleUuids.h"
#include "Systemic/Pixels/Helpers.h"
namespace
{
using namespace Systemic::Pixels;
ScannedPixelData readScannedPixelData(std::shared_ptr<const Systemic::BluetoothLE::ScannedPeripheral> p)
{
ScannedPixelData data{};
if (p)
{
auto& manufacturersData = p->manufacturersData();
auto& servicesData = p->servicesData();
if (!manufacturersData.empty() &&
manufacturersData[0].data().size() >= 5 &&
!servicesData.empty() &&
servicesData[0].data().size() >= 8)
{
auto& manufData = manufacturersData[0].data();
auto& servData = servicesData[0].data();
struct
{
uint32_t pixelId{};
uint32_t buildTimestamp{};
} info1;
memcpy(&info1, servData.data(), servData.size());
struct
{
uint8_t ledCount{};
PixelDesignAndColor designAndColor{};
PixelRollState rollState{};
uint8_t currentFaceIndex{};
uint8_t battery{};
} info2;
memcpy(&info2, manufData.data(), manufData.size());
data.name = p->name();
data.address = p->address();
data.rssi = p->rssi();
data.pixelId = info1.pixelId;
data.firmwareDate = Helpers::getFirmwareDate(info1.buildTimestamp);
data.ledCount = info2.ledCount;
data.designAndColor = info2.designAndColor;
data.rollState = info2.rollState;
data.currentFace = info2.currentFaceIndex + 1;
// MSB is battery charging
data.batteryLevel = info2.battery & 0x7f;
data.isCharging = (info2.battery & 0x80) > 0;
}
}
return data;
}
}
namespace Systemic::Pixels
{
PixelScanner::PixelScanner(const ScannedPixelListener& listener)
: _listener(listener)
{
}
PixelScanner::~PixelScanner()
{
stop();
}
void PixelScanner::start()
{
std::lock_guard lock{ _mutex };
_scanner.reset(new Systemic::BluetoothLE::Scanner
{
[this](auto p)
{
auto data = readScannedPixelData(p);
if (data.pixelId)
{
const auto max = _scannedPixels.size();
size_t i = 0;
for (; i < max; ++i)
{
if (_scannedPixels[i]->pixelId() == data.pixelId)
{
break;
}
}
const auto pixel = std::shared_ptr<ScannedPixel>(new ScannedPixel{ data });
{
std::lock_guard lock{ _mutex };
if (i < max)
{
_scannedPixels[i] = pixel;
}
else
{
_scannedPixels.push_back(pixel);
}
}
if (_listener)
{
_listener(pixel);
}
}
},
{
PixelBleUuids::service
}
});
}
void PixelScanner::stop()
{
std::lock_guard lock{ _mutex };
_scanner.reset();
}
void PixelScanner::clear()
{
std::lock_guard lock{ _mutex };
_scannedPixels.clear();
}
}