-
Notifications
You must be signed in to change notification settings - Fork 0
/
boblightservice.c
130 lines (105 loc) · 3.49 KB
/
boblightservice.c
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
/*
* boblightservice.c
*
* Copyright (C) 2013 - Christian Völlinger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <libboblight/boblight.h>
#include "boblightservice.h"
//***************************************************************************
// Open/Close
//***************************************************************************
int cBoblight::open()
{
//init boblight
m_boblight = boblight_init();
tell(1, "Successfully loaded and initalized libboblight");
if (!boblight_connect(m_boblight, cfg.host, cfg.port, 1000000) || !boblight_setpriority(m_boblight, cfg.priority))
{
tell(0, "Error connecting to boblight %s", boblight_geterror(m_boblight));
close();
return fail;
}
tell(0, "Connected to boblight");
sendOptions();
return success;
}
int cBoblight::close()
{
if(m_boblight != 0) {
tell(1, "Destroying boblight");
boblight_destroy(m_boblight); // calls delete *void
m_boblight = 0;
} // set pointer to 0
return success;
}
int cBoblight::ping() {
if(m_boblight == 0) {
tell(1, "boblight not initalized");
return fail;
}
if (!boblight_ping(m_boblight, NULL)) {
tell(0, "Connecting to boblight lost: %s", boblight_geterror(m_boblight));
return fail;
}
return success;
}
//***************************************************************************
// Write Pixel
//***************************************************************************
int cBoblight::writePix(int *rgb)
{
tell(2, "writePix r:%d g:%d b:%d", rgb[0], rgb[1], rgb[2]);
boblight_addpixel(m_boblight, -1, rgb);
return success;
}
int cBoblight::writeColor(int *rgb, int x, int y)
{
boblight_addpixelxy(m_boblight, x, y, rgb);
return success;
}
int cBoblight::send() {
if (!boblight_sendrgb(m_boblight, 0, NULL))
{
tell(1, "Error sendrgb boblight %s", boblight_geterror(m_boblight));
return fail;
}
return success;
}
int cBoblight::setScanRange(int width, int height) {
boblight_setscanrange(m_boblight, width, height);
return success;
}
int cBoblight::sendOptions() {
if (m_boblight == 0) return fail;
char buf[32];
sprintf(buf, "%s %1f", "value", cfg.value * 0.1f);
boblight_setoption(m_boblight, -1, buf);
sprintf(buf, "%s %d", "threshold", cfg.threshold);
boblight_setoption(m_boblight, -1, buf);
sprintf(buf, "%s %.1f", "gamma", cfg.gamma * 0.1f);
boblight_setoption(m_boblight, -1, buf);
sprintf(buf, "%s %1f", "saturation", cfg.saturation * 0.1f);
boblight_setoption(m_boblight, -1, buf);
sprintf(buf, "%s %d", "speed", cfg.speed);
boblight_setoption(m_boblight, -1, buf);
sprintf(buf, "%s %d", "autospeed", cfg.autospeed);
boblight_setoption(m_boblight, -1, buf);
sprintf(buf, "%s %s", "interpolation", cfg.interpolation ? "true" : "false");
boblight_setoption(m_boblight, -1, buf);
boblight_setpriority(m_boblight, cfg.priority);
return success;
}