Skip to content

Commit 04228ba

Browse files
committed
Reduce USB packages when color stays the same
1 parent a41b190 commit 04228ba

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

examples/ambilight/ambilight.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ void* calculate_keyboard_color(void *void_ptr) {
166166

167167

168168
void* update_keyboard_color(void* ptr) {
169-
unsigned char current_color[3] = {0};
169+
unsigned char color[3] = {0}, prev[3] = {0};
170170
while (true) {
171171
pthread_mutex_lock(&exit_req_lock);
172172
if (exit_requested) {
@@ -176,14 +176,19 @@ void* update_keyboard_color(void* ptr) {
176176
pthread_mutex_unlock(&exit_req_lock);
177177

178178
int diff;
179+
bool equal = true;
179180
pthread_mutex_lock(&exit_req_lock);
180181
for (int i=0; i < 3; i++) {
181-
diff = (int) target_color[i] - current_color[i];
182-
current_color[i] += (unsigned char) (diff / 20.0);
182+
diff = (int) target_color[i] - color[i];
183+
prev[i] = color[i];
184+
color[i] += (unsigned char) (diff / 20.0);
185+
equal = (prev[i] == target_color[i]) && equal;
183186
}
184187
pthread_mutex_unlock(&exit_req_lock);
185188

186-
int r = libmk_set_full_color(NULL, current_color[0], current_color[1], current_color[2]);
189+
if (equal)
190+
continue;
191+
int r = libmk_set_full_color(NULL, color[0], color[1], color[2]);
187192
if (r != LIBMK_SUCCESS)
188193
printf("LibMK Error: %d\n", r);
189194
struct timespec time;

0 commit comments

Comments
 (0)