Skip to content

Commit a41b190

Browse files
committed
Fix set_all_led_colors and set_single_led_color
These functions only worked under certain conditions.
1 parent 78c2aa4 commit a41b190

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

libmk/libmk.c

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,11 @@ int libmk_enable_control(LibMK_Handle* handle) {
448448

449449
int libmk_send_control_packet(LibMK_Handle* handle) {
450450
/** Build and send a packet to enable per-led control on a device */
451+
unsigned char* disable = libmk_build_packet(
452+
2, 0x41, 0x01);
453+
int r = libmk_send_packet(handle, disable);
454+
if (r != LIBMK_SUCCESS)
455+
return r;
451456
unsigned char* packet = libmk_build_packet(
452457
2, HEADER_DEFAULT, OPCODE_ENABLE);
453458
return libmk_send_packet(handle, packet);
@@ -535,15 +540,15 @@ int libmk_set_effect(LibMK_Handle* handle, LibMK_Effect effect) {
535540
unsigned char* packet = libmk_build_packet(
536541
2, 0x41, 0x01);
537542
int r = libmk_send_packet(handle, packet);
538-
if (r != LIBMK_SUCCESS)
539-
return r;
540-
r = libmk_send_flush_packet(handle);
541543
if (r != LIBMK_SUCCESS)
542544
return r;
543545
packet = libmk_build_packet(
544546
5, HEADER_DEFAULT | HEADER_EFFECT, OPCODE_EFFECT,
545547
0x00, 0x00, (unsigned char) effect);
546-
return libmk_send_packet(handle, packet);
548+
r = libmk_send_packet(handle, packet);
549+
if (r != LIBMK_SUCCESS)
550+
return r;
551+
return libmk_send_flush_packet(handle);
547552
}
548553

549554

@@ -582,11 +587,13 @@ int libmk_send_packet(LibMK_Handle* handle, unsigned char* packet) {
582587
return LIBMK_ERR_TRANSFER;
583588
packet = libmk_build_packet(0);
584589
r = libusb_interrupt_transfer(
585-
handle->handle, LIBMK_EP_IN | LIBUSB_ENDPOINT_IN,
586-
packet, LIBMK_PACKET_SIZE, &t, LIBMK_PACKET_TIMEOUT);
587-
if (r != 0 || t != LIBMK_PACKET_SIZE)
590+
handle->handle, LIBMK_EP_IN | LIBUSB_ENDPOINT_IN,
591+
packet, LIBMK_PACKET_SIZE, &t, LIBMK_PACKET_TIMEOUT);
592+
if (r != LIBUSB_SUCCESS) {
593+
result = LIBMK_SUCCESS;
594+
} else if (t != LIBMK_PACKET_SIZE) {
588595
result = LIBMK_ERR_TRANSFER;
589-
else if (packet[0] == HEADER_ERROR) {
596+
} else if (packet[0] == HEADER_ERROR) {
590597
libmk_print_packet(packet);
591598
result = LIBMK_ERR_PROTOCOL;
592599
} else
@@ -652,6 +659,10 @@ int libmk_set_all_led_color(
652659

653660
unsigned char offset;
654661
int packet, index, result;
662+
663+
664+
unsigned char packet_colors[3] = {255, 0, 0};
665+
655666

656667
for (unsigned char r = 0; r < LIBMK_MAX_ROWS; r++)
657668
for (unsigned char c = 0; c < LIBMK_MAX_COLS; c++) {
@@ -668,8 +679,11 @@ int libmk_set_all_led_color(
668679
}
669680
}
670681

682+
int r = libmk_send_flush_packet(handle);
683+
if (r != LIBMK_SUCCESS)
684+
return r;
671685
for (short k = 0; k < LIBMK_ALL_LED_PCK_NUM; k++) {
672-
int r = libmk_send_packet(handle, packets[k]);
686+
r = libmk_send_packet(handle, packets[k]);
673687
if (r != LIBMK_SUCCESS)
674688
return r;
675689
}
@@ -744,9 +758,11 @@ int libmk_set_single_led(
744758
handle = DeviceHandle;
745759
if (handle == NULL)
746760
return LIBMK_ERR_DEV_NOT_SET;
747-
libmk_send_control_packet(handle);
761+
int result = libmk_send_control_packet(handle);
762+
if (result != LIBMK_SUCCESS)
763+
return result;
748764
unsigned char offset;
749-
int result = libmk_get_offset(&offset, handle, row, col);
765+
result = libmk_get_offset(&offset, handle, row, col);
750766
if (result != LIBMK_SUCCESS)
751767
return result;
752768
unsigned char* packet = libmk_build_packet(

utils/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ int main(void) {
4747

4848
int color = 200;
4949
unsigned char colors[LIBMK_MAX_ROWS][LIBMK_MAX_COLS][3] = {0};
50-
for (short row=0; row < 1; row++)
50+
for (short row=0; row < LIBMK_MAX_ROWS; row++)
5151
for (short col=0; col < LIBMK_MAX_COLS; col++) {
5252
color += 8;
5353
colors[row][col][(color / 256 + 1) % 3] = 0xFF -(color % 256);

0 commit comments

Comments
 (0)