Repository navigation
Expand file tree
/
Copy pathOpcLayoutClientApp.cpp
More file actions
90 lines (72 loc) · 2.52 KB
/
Copy pathOpcLayoutClientApp.cpp
File metadata and controls
90 lines (72 loc) · 2.52 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
#include "cinder/Json.h"
#include "cinder/Log.h"
#include "cinder/Rand.h"
#include "cinder/app/App.h"
#include "cinder/app/RendererGl.h"
#include "cinder/gl/gl.h"
#include "OpcClient.h"
using namespace ci;
using namespace ci::app;
using namespace std;
class OpcLayoutClientApp : public App {
public:
static void prepareSettings(Settings *settings);
void setup() override;
void update() override;
void draw() override;
private:
kp::opc::ClientRef mOpcClient;
vector<vec2> mLayoutPositions;
};
void OpcLayoutClientApp::prepareSettings(Settings *settings) {
settings->setWindowSize(800, 800);
settings->setPowerManagementEnabled(true); // Prevent App Nap
settings->setTitle("OPC Layout Client Sample App");
}
void OpcLayoutClientApp::setup() {
// Load layout JSON and store positions by LED index. Ignore the Z axis.
JsonTree layoutData = JsonTree(loadAsset("layout.json"));
for (auto &led : layoutData) {
const vec2 position = vec2(led["point"].getValueAtIndex<float>(0), led["point"].getValueAtIndex<float>(1));
mLayoutPositions.push_back(position);
}
mOpcClient = kp::opc::Client::create("127.0.0.1", 7890);
}
void OpcLayoutClientApp::update() {
mOpcClient->update();
}
void OpcLayoutClientApp::draw() {
gl::clear(Color(0, 0, 0));
// Draw some content to be sampled and sent out via OPC
// Using an FBO would be more hygienic.
float scaleFactor = sin(getElapsedFrames() / 30.0) * 7;
gl::pushMatrices();
gl::translate(400, 400);
gl::rotate(getElapsedFrames() / 30.0);
gl::scale(scaleFactor, scaleFactor);
gl::color(Color("red"));
gl::drawSolidRect(Rectf(-50, -50, 50, 50));
gl::popMatrices();
// Copy what we've drawn so far into surface for pixel inspection...
Surface windowImage = copyWindowSurface(getWindowBounds());
// Send drawing out as sampled points
int i = 0;
for (vec2 &position : mLayoutPositions) {
// Sample color under each point in the layout
Color8u sampledColor = Color8u(windowImage.getPixel(position));
// Set the corresponding LED
mOpcClient->setLED(i, sampledColor);
// Draw the sampled point for reference
gl::color(Color("white"));
gl::drawSolidRect(Rectf(position - vec2(0.5, 0.5), position + vec2(0.5, 0.5)));
i++;
}
// Draw stats
TextBox textBox = TextBox().size(ivec2(200, 15)).text("Messages sent: " + toString(mOpcClient->getNumMessagesSent()));
gl::pushMatrices();
gl::translate(10.0, 10.0);
gl::color(Color("white"));
gl::draw(gl::Texture2d::create(textBox.render()));
gl::popMatrices();
}
CINDER_APP(OpcLayoutClientApp, RendererGl, OpcLayoutClientApp::prepareSettings)