Skip to content

Commit cc0932c

Browse files
committed
Create color wave effect in controller test
1 parent 80410d9 commit cc0932c

File tree

2 files changed

+101
-8
lines changed

2 files changed

+101
-8
lines changed

libmk/libmkc.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,7 @@ LibMK_Instruction* libmk_create_instruction_all(
218218
LibMK_Instruction* i = libmk_create_instruction();
219219
i->colors = (unsigned char*) malloc(
220220
sizeof(unsigned char) * LIBMK_MAX_ROWS * LIBMK_MAX_COLS * 3);
221-
for (unsigned char row=0; row<LIBMK_MAX_ROWS; row++)
222-
for (unsigned char col=0; col<LIBMK_MAX_COLS; col++)
223-
for (unsigned char j=0; j<3; j++)
224-
i->colors[row * LIBMK_MAX_COLS + j] = c[row][col][j];
221+
memcpy(i->colors, c, LIBMK_MAX_ROWS * LIBMK_MAX_COLS * 3);
225222
i->type = LIBMK_INSTR_ALL;
226223
return i;
227224
}
@@ -264,7 +261,7 @@ LibMK_Result libmk_sched_instruction(
264261
c->instr->id = 1;
265262
} else {
266263
LibMK_Instruction* t = c->instr;
267-
while (true) {
264+
while (t != NULL) {
268265
if (t->next == NULL) {
269266
t->next = i;
270267
t->next->id = t->id + 1;

utils/ctrl.c

Lines changed: 99 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
* Author: RedFantom
33
* License: GNU GPLv3
44
* Copyright (c) 2018 RedFantom
5+
*
6+
* The HSV to RGB conversion algorithm was copied from:
7+
* https://stackoverflow.com/questions/3018313
8+
* The answer by Leszek Szary
59
*/
610
#include "../libmk/libmkc.h"
711
#include <stdio.h>
@@ -10,6 +14,62 @@
1014
#include "libusb.h"
1115

1216

17+
typedef struct RgbColor {
18+
unsigned char r;
19+
unsigned char g;
20+
unsigned char b;
21+
} RgbColor;
22+
23+
typedef struct HsvColor {
24+
unsigned char h;
25+
unsigned char s;
26+
unsigned char v;
27+
} HsvColor;
28+
29+
30+
RgbColor* HsvToRgb(HsvColor* hsv) {
31+
RgbColor* rgb = malloc(sizeof(RgbColor));
32+
unsigned char region, remainder, p, q, t;
33+
34+
if (hsv->s == 0) {
35+
rgb->r = hsv->v;
36+
rgb->g = hsv->v;
37+
rgb->b = hsv->v;
38+
return rgb;
39+
}
40+
41+
region = hsv->h / 43;
42+
remainder = (hsv->h - (region * 43)) * 6;
43+
44+
p = (hsv->v * (255 - hsv->s)) >> 8;
45+
q = (hsv->v * (255 - ((hsv->s * remainder) >> 8))) >> 8;
46+
t = (hsv->v * (255 - ((hsv->s * (255 - remainder)) >> 8))) >> 8;
47+
48+
switch (region) {
49+
case 0:
50+
rgb->r = hsv->v; rgb->g = t; rgb->b = p;
51+
break;
52+
case 1:
53+
rgb->r = q; rgb->g = hsv->v; rgb->b = p;
54+
break;
55+
case 2:
56+
rgb->r = p; rgb->g = hsv->v; rgb->b = t;
57+
break;
58+
case 3:
59+
rgb->r = p; rgb->g = q; rgb->b = hsv->v;
60+
break;
61+
case 4:
62+
rgb->r = t; rgb->g = p; rgb->b = hsv->v;
63+
break;
64+
default:
65+
rgb->r = hsv->v; rgb->g = p; rgb->b = q;
66+
break;
67+
}
68+
return rgb;
69+
}
70+
71+
72+
1373
int main(void) {
1474
libmk_init();
1575
printf("Detecting devices...\n");
@@ -35,18 +95,54 @@ int main(void) {
3595
libmk_start_controller(ctrl);
3696
printf("Done.\n");
3797

38-
fprintf(stdout, " Scheduling instruction... ");
98+
fprintf(stdout, " Scheduling instructions... ");
3999

40-
unsigned char color[3] = {255, 0, 0};
41-
LibMK_Instruction* full = libmk_create_instruction_flash(color, 10000, 255);
100+
unsigned char red[3] = {255, 0, 0};
101+
LibMK_Instruction* full = libmk_create_instruction_flash(red, 10000, 255);
42102
unsigned int n = libmk_sched_instruction(ctrl, full);
43103

104+
unsigned char yellow[3] = {255, 255, 0};
105+
full = libmk_create_instruction_full(yellow);
106+
full->duration = 1000000;
107+
libmk_sched_instruction(ctrl, full);
108+
109+
unsigned char blank[3] = {0};
110+
LibMK_Instruction* wave = libmk_create_instruction_full(blank);
111+
LibMK_Instruction* a, * b;
112+
a = wave;
113+
HsvColor color;
114+
color.s = 255;
115+
color.v = 255;
116+
117+
unsigned char map[LIBMK_MAX_ROWS][LIBMK_MAX_COLS][3];
118+
for (int i=0; i<1000; i++) {
119+
for (int r=0; r<LIBMK_MAX_ROWS; r++) {
120+
for (int c=0; c<LIBMK_MAX_COLS; c++) {
121+
color.h = (i+c) * (255 / (LIBMK_MAX_COLS+40));
122+
RgbColor* rgb = HsvToRgb(&color);
123+
map[r][c][0] = rgb->r;
124+
map[r][c][1] = rgb->g;
125+
map[r][c][2] = rgb->b;
126+
free(rgb);
127+
}
128+
}
129+
b = libmk_create_instruction_all(map);
130+
b->duration = 10000;
131+
a->next = b;
132+
a = b;
133+
}
134+
libmk_sched_instruction(ctrl, wave);
135+
136+
44137
printf("Done: %d.\n", full->id);
45138

46139
fprintf(stdout, " Awaiting controller... ");
47140
libmk_wait_controller(ctrl);
48141
printf("Done.\n");
49142

143+
printf("\n Now the controller will automatically exit after all instructions are done.\n");
144+
printf(" You can let your program do other stuff while the instructions execute.\n\n");
145+
50146
LibMK_Controller_State s = libmk_join_controller(ctrl, 40);
51147
if (s != LIBMK_STATE_STOPPED)
52148
printf(" Could not stop the Controller: %d\n", s);

0 commit comments

Comments
 (0)