-
Notifications
You must be signed in to change notification settings - Fork 3
/
AtmoDuinoV2Connection.cpp
312 lines (256 loc) · 8.03 KB
/
AtmoDuinoV2Connection.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
* AtmoSerialConnection.cpp: Class for communication with the serial hardware of
* Atmo Light, opens and configures the serial port
*
* See the README.txt file for copyright information and how to reach the author(s).
*
* $Id: edd37fb84029decae0236b3f1d787d16c779591e $
*/
#include "stdafx.h"
#include "AtmoDefs.h"
#include "AtmoDuinoV2Connection.h"
#if !defined(_ATMO_VLC_PLUGIN_)
# include "AtmoDuinoV2ConfigDialog.h"
#endif
#include <stdio.h>
#include <fcntl.h>
#if !defined(WIN32)
#include <termios.h>
#include <unistd.h>
#endif
CAtmoDuinoV2Connection::CAtmoDuinoV2Connection(CAtmoConfig *cfg) : CAtmoConnection(cfg) {
m_hComport = INVALID_HANDLE_VALUE;
memset(m_frame_buffer, 0, sizeof(m_frame_buffer));
// Sync Header
m_frame_buffer[0] = 0xD7;
m_frame_buffer[1] = 0xEE;
m_frame_buffer[2] = 0x23;
// Number of LEDS - 1
m_frame_buffer[3] = getNumChannels() - 1;
}
CAtmoDuinoV2Connection::~CAtmoDuinoV2Connection() {
}
ATMO_BOOL CAtmoDuinoV2Connection::OpenConnection() {
#if defined(_ATMO_VLC_PLUGIN_)
char *serdevice = m_pAtmoConfig->getSerialDevice();
if(!serdevice)
return ATMO_FALSE;
#else
int portNummer = m_pAtmoConfig->getComport();
m_dwLastWin32Error = 0;
if (portNummer < 1) return ATMO_FALSE; // make no real sense;-)
#endif
CloseConnection();
#if !defined(_ATMO_VLC_PLUGIN_)
char serdevice[16]; // com4294967295
sprintf(serdevice, "com%d", portNummer);
#endif
#if defined(WIN32)
m_hComport = CreateFile(serdevice, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (m_hComport == INVALID_HANDLE_VALUE) {
// we have a problem here can't open com port... somebody else may use it?
m_dwLastWin32Error = GetLastError();
return ATMO_FALSE;
}
/* change serial settings (Speed, stopbits etc.) */
DCB dcb; // fuer comport-parameter
dcb.DCBlength = sizeof(DCB);
GetCommState(m_hComport, &dcb); // ger current serialport settings
switch (m_pAtmoConfig->getArduV2_BaudrateIndex())
{
// set baud speed
case 1: dcb.BaudRate = 115200;
break;
case 2: dcb.BaudRate = 230400;
break;
case 3: dcb.BaudRate = 250000;
break;
case 4: dcb.BaudRate = 500000;
break;
case 5: dcb.BaudRate = 1000000;
break;
case 6: dcb.BaudRate = 2000000;
break;
case 7: dcb.BaudRate = 4000000;
break;
case 8: dcb.BaudRate = 6000000;
break;
case 9: dcb.BaudRate = 8000000;
break;
case 10: dcb.BaudRate = 10000000;
break;
default:
dcb.BaudRate = CBR_115200; // set default speed
}
dcb.ByteSize = 8; // set databits
dcb.Parity = NOPARITY; // set parity
dcb.StopBits = ONESTOPBIT; // set one stop bit
SetCommState(m_hComport, &dcb); // apply settings
#else
int bconst = B115200;
m_hComport = open(serdevice,O_RDWR |O_NOCTTY);
if(m_hComport < 0) {
return ATMO_FALSE;
}
struct termios tio;
memset(&tio,0,sizeof(tio));
tio.c_cflag = (CS8 | CREAD | HUPCL | CLOCAL);
tio.c_iflag = (INPCK | BRKINT);
cfsetispeed(&tio, bconst);
cfsetospeed(&tio, bconst);
if(!tcsetattr(m_hComport, TCSANOW, &tio)) {
tcflush(m_hComport, TCIOFLUSH);
} else {
// can't change parms
close(m_hComport);
m_hComport = -1;
return false;
}
#endif
return true;
}
void CAtmoDuinoV2Connection::CloseConnection() {
if (m_hComport != INVALID_HANDLE_VALUE) {
#if defined(WIN32)
CloseHandle(m_hComport);
#else
close(m_hComport);
#endif
m_hComport = INVALID_HANDLE_VALUE;
}
}
ATMO_BOOL CAtmoDuinoV2Connection::isOpen(void) {
return (m_hComport != INVALID_HANDLE_VALUE);
}
ATMO_BOOL CAtmoDuinoV2Connection::HardwareWhiteAdjust(int global_gamma,
int global_contrast,
int contrast_red,
int contrast_green,
int contrast_blue,
int gamma_red,
int gamma_green,
int gamma_blue,
ATMO_BOOL storeToEeprom) {
return ATMO_FALSE;
}
ATMO_BOOL CAtmoDuinoV2Connection::SendData(pColorPacket data) {
if (m_hComport == INVALID_HANDLE_VALUE)
return ATMO_FALSE;
DWORD iBytesWritten;
int iBuffer = 4;
int zoneIdx;
Lock();
for (int channel = 0; channel < getNumChannels() ; channel++) {
if (m_ChannelAssignment && (channel < m_NumAssignedChannels))
zoneIdx = m_ChannelAssignment[channel];
else
zoneIdx = -1;
if ((zoneIdx >= 0) && (zoneIdx < data->numColors))
{
if (m_pAtmoConfig->isWhiteAdjPerChannel())
{
int wr;
int wg;
int wb;
m_pAtmoConfig->getChannelWhiteAdj(channel, wr, wg, wb);
m_frame_buffer[iBuffer] = (data->zone[zoneIdx].r * wr) / 256;
m_frame_buffer[iBuffer + 1] = (data->zone[zoneIdx].g * wg) / 256;
m_frame_buffer[iBuffer + 2] = (data->zone[zoneIdx].b * wb) / 256;
} else {
m_frame_buffer[iBuffer] = data->zone[zoneIdx].r;
m_frame_buffer[iBuffer + 1] = data->zone[zoneIdx].g;
m_frame_buffer[iBuffer + 2] = data->zone[zoneIdx].b;
}
} else {
m_frame_buffer[iBuffer] = 0;
m_frame_buffer[iBuffer + 1] = 0;
m_frame_buffer[iBuffer + 2] = 0;
}
iBuffer += 3;
}
#if defined(WIN32)
WriteFile(m_hComport, m_frame_buffer, getBufferSize(), &iBytesWritten, NULL); // send to COM-Port
#else
iBytesWritten = write(m_hComport, m_frame_buffer, getBufferSize());
tcdrain(m_hComport);
#endif
Unlock();
return (iBytesWritten == getBufferSize()) ? ATMO_TRUE : ATMO_FALSE;
}
ATMO_BOOL CAtmoDuinoV2Connection::CreateDefaultMapping(CAtmoChannelAssignment *ca)
{
if (!ca) return ATMO_FALSE;
ca->setSize(getNumChannels());
for (int i = 0; i < getNumChannels(); i++)
ca->setZoneIndex(i, i);
return ATMO_TRUE;
}
#if !defined(_ATMO_VLC_PLUGIN_)
ATMO_BOOL CAtmoDuinoV2Connection::setChannelColor(int channel, tRGBColor color)
{
DWORD iBytesWritten = 0;
if ((channel >= 0) && (channel < getNumChannels()))
{
Lock();
m_frame_buffer[channel + 0 + 4] = color.r;
m_frame_buffer[channel + 1 + 4] = color.g;
m_frame_buffer[channel + 2 + 4] = color.b;
#if defined(WIN32)
WriteFile(m_hComport, m_frame_buffer, getBufferSize(), &iBytesWritten, NULL); // send to COM-Port
#else
iBytesWritten = write(m_hComport, m_frame_buffer, getBufferSize());
tcdrain(m_hComport);
#endif
Unlock();
}
return (iBytesWritten == getBufferSize()) ? ATMO_TRUE : ATMO_FALSE;
}
ATMO_BOOL CAtmoDuinoV2Connection::setChannelValues(int numValues, unsigned char *channel_values)
{
DWORD iBytesWritten;
if ((numValues & 1) || !channel_values)
return ATMO_FALSE; // numValues must be even!
/*
the array shall contain
*/
Lock();
int Index;
for (int i = 0; i < numValues; i += 2) {
Index = ((int)channel_values[i]);
if ((Index >= 0) && (Index < getNumChannels() * 3))
m_frame_buffer[Index + 4] = channel_values[i + 1];
}
#if defined(WIN32)
WriteFile(m_hComport, m_frame_buffer, getBufferSize(), &iBytesWritten, NULL); // send to COM-Port
#else
iBytesWritten = write(m_hComport, m_frame_buffer, getBufferSize());
tcdrain(m_hComport);
#endif
Unlock();
return (iBytesWritten == getBufferSize()) ? ATMO_TRUE : ATMO_FALSE;
}
int CAtmoDuinoV2Connection::getNumChannels()
{
return m_pAtmoConfig->getAtmoV2ClLeds();
}
int CAtmoDuinoV2Connection::getBufferSize(){
return getNumChannels() * 3 + 4;
}
char *CAtmoDuinoV2Connection::getChannelName(int ch)
{
if (ch < 0) return NULL;
char buf[30];
sprintf(buf, "LED [%d]", ch + 1);
return strdup(buf);
}
ATMO_BOOL CAtmoDuinoV2Connection::ShowConfigDialog(HINSTANCE hInst, HWND parent, CAtmoConfig *cfg)
{
CAtmoDuinoV2ConfigDialog *dlg = new CAtmoDuinoV2ConfigDialog(hInst, parent, cfg);
INT_PTR result = dlg->ShowModal();
delete dlg;
if (result == IDOK)
return ATMO_TRUE;
else
return ATMO_FALSE;
}
#endif