-
-
Notifications
You must be signed in to change notification settings - Fork 359
Expand file tree
/
Copy pathArcticController.cpp
More file actions
159 lines (127 loc) · 4.2 KB
/
ArcticController.cpp
File metadata and controls
159 lines (127 loc) · 4.2 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*---------------------------------------------------------*\
| ArcticController.cpp |
| |
| Driver for Arctic devices |
| |
| Armin Wolf (Wer-Wolf) 09 Jan 2023 |
| |
| This file is part of the OpenRGB project |
| SPDX-License-Identifier: GPL-2.0-or-later |
\*---------------------------------------------------------*/
#include <cstring>
#include "ArcticController.h"
using namespace std::chrono_literals;
#define ARCTIC_COMMAND_SET_RGB 0x00
#define ARCTIC_COMMAND_IDENTIFY 0x5C
#define ARCTIC_RESPONSE_BUFFER_LENGTH 15
#define ARCTIC_RESPONSE_COMMAND_OFFSET 0
#define ARCTIC_RESPONSE_DATA_OFFSET 1
#define ARCTIC_RESPONSE_DATA_LENGTH 12
#define ARCTIC_RESPONSE_XOR_CSUM_OFFSET 13
#define ARCTIC_RESPONSE_ADD_CSUM_OFFSET 14
#define ARCTIC_COMMAND_BUFFER_LENGTH(payload_size) (sizeof(header) + 1 + payload_size)
#define ARCTIC_COMMAND_COMMAND_OFFSET (sizeof(header))
#define ARCTIC_COMMAND_PAYLOAD_OFFSET (sizeof(header) + 1)
const unsigned char header[] =
{
0x01,
0x02,
0x03,
0xFF,
0x05,
0xFF,
0x02,
0x03
};
const unsigned char identify_payload[] =
{
0x01,
0xFE,
0x01,
0xFE
};
ArcticController::ArcticController(const std::string &portname)
: serialport(portname.c_str(), 250000, SERIAL_PORT_PARITY_NONE, SERIAL_PORT_SIZE_8, SERIAL_PORT_STOP_BITS_2, false)
{
port_name = portname;
serialport.serial_set_dtr(true);
}
ArcticController::~ArcticController()
{
serialport.serial_set_dtr(false);
}
static void FormatCommandBuffer(char *buffer, char command)
{
std::memcpy(buffer, header, sizeof(header));
buffer[ARCTIC_COMMAND_COMMAND_OFFSET] = command;
}
void ArcticController::SetChannels(std::vector<RGBColor> colors)
{
size_t buffer_length = ARCTIC_COMMAND_BUFFER_LENGTH(colors.size() * 3);
char* buffer = new char[buffer_length];
FormatCommandBuffer(buffer, ARCTIC_COMMAND_SET_RGB);
for(unsigned int channel = 0; channel < colors.size(); channel++)
{
const unsigned int offset = ARCTIC_COMMAND_PAYLOAD_OFFSET + channel * 3;
buffer[offset + 0x00] = (char)std::min<unsigned int>(254, RGBGetRValue(colors[channel]));
buffer[offset + 0x01] = (char)std::min<unsigned int>(254, RGBGetGValue(colors[channel]));
buffer[offset + 0x02] = (char)std::min<unsigned int>(254, RGBGetBValue(colors[channel]));
}
serialport.serial_write(buffer, buffer_length);
delete[] buffer;
}
static char XORChecksum(char *data, int length)
{
char sum = 0;
for(int i = 0; i < length; i++)
{
sum ^= data[i];
}
return sum;
}
static char AddChecksum(char *data, int length)
{
char sum = 0;
for(int i = 0; i < length; i++)
{
sum = (char)(sum + data[i]);
}
return sum;
}
bool ArcticController::IsPresent()
{
char buffer[ARCTIC_COMMAND_BUFFER_LENGTH(sizeof(identify_payload))];
char response[ARCTIC_RESPONSE_BUFFER_LENGTH];
int ret;
FormatCommandBuffer(buffer, ARCTIC_COMMAND_IDENTIFY);
std::memcpy(buffer + ARCTIC_COMMAND_PAYLOAD_OFFSET, identify_payload, sizeof(identify_payload));
serialport.serial_flush_rx();
ret = serialport.serial_write(buffer, sizeof(buffer));
if(ret != sizeof(buffer))
{
return false;
}
std::this_thread::sleep_for(100ms);
ret = serialport.serial_read(response, sizeof(response));
if(ret != sizeof(response))
{
return false;
}
if(response[ARCTIC_RESPONSE_COMMAND_OFFSET] != ARCTIC_COMMAND_IDENTIFY)
{
return false;
}
if(response[ARCTIC_RESPONSE_XOR_CSUM_OFFSET] != XORChecksum(&response[ARCTIC_RESPONSE_DATA_OFFSET], ARCTIC_RESPONSE_DATA_LENGTH))
{
return false;
}
if(response[ARCTIC_RESPONSE_ADD_CSUM_OFFSET] != AddChecksum(&response[ARCTIC_RESPONSE_DATA_OFFSET], ARCTIC_RESPONSE_DATA_LENGTH))
{
return false;
}
return true;
}
std::string ArcticController::GetLocation()
{
return port_name;
}