@@ -41,12 +41,11 @@ typedef enum LibMK_Effect LibMK_Effect;
4141typedef struct LibMK_Device LibMK_Device ;
4242
4343/** Constants */
44- const unsigned char HEADER_DEFAULT = 0x41 ; // 0100 0001
45- const unsigned char HEADER_EFFECT = 0x50 ; // 0101 0000
44+ const unsigned char HEADER_SET = 0x51 ; // 1001 0001
45+ const unsigned char HEADER_GET = 0x52 ; // 1001 0010
46+
4647const unsigned char HEADER_FULL_COLOR = 0xc0 ; // 1100 0000
4748const unsigned char HEADER_ERROR = 0xFF ; // 1111 1111
48- const unsigned char OPCODE_ENABLE = 0x02 ; // 0000 0010
49- const unsigned char OPCODE_DISABLE = 0x00 ; // 0000 0000
5049const unsigned char OPCODE_EFFECT = 0x28 ; // 0010 1000
5150const unsigned char OPCODE_ALL_LED = 0xA8 ; // 1010 1000
5251const unsigned char OPCODE_EFFECT_ARGS = 0x2c ; // 0010 1100
@@ -451,14 +450,17 @@ int libmk_enable_control(LibMK_Handle* handle) {
451450
452451
453452int libmk_send_control_packet (LibMK_Handle * handle ) {
454- unsigned char * disable = libmk_build_packet (
455- 2 , 0x41 , 0x01 );
456- int r = libmk_send_packet (handle , disable );
453+ r = libmk_set_control_mode (handle , LIBMK_CUSTOM_CTRL );
457454 if (r != LIBMK_SUCCESS )
455+ return LIBMK_ERR_SEND ;
456+ LibMK_Firmware * fw ;
457+ r = libmk_get_firmware_version (handle , & fw );
458+ if (r != LIBMK_SUCCESS ) {
459+ libusb_close (handle -> handle );
458460 return r ;
459- unsigned char * packet = libmk_build_packet (
460- 2 , HEADER_DEFAULT , OPCODE_ENABLE ) ;
461- return libmk_send_packet ( handle , packet ) ;
461+ }
462+ handle -> layout = fw -> layout ;
463+ return LIBMK_SUCCESS ;
462464}
463465
464466
@@ -468,11 +470,7 @@ int libmk_disable_control(LibMK_Handle* handle) {
468470 if (handle == NULL )
469471 return LIBMK_ERR_DEV_NOT_SET ;
470472
471- int r ;
472-
473- unsigned char * packet = libmk_build_packet (
474- 2 , HEADER_DEFAULT , OPCODE_DISABLE );
475- r = libmk_send_packet (handle , packet );
473+ int r = libmk_set_control_mode (handle , LIBMK_FIRMWARE_CTRL );
476474 if (r != LIBMK_SUCCESS )
477475 return r ;
478476
@@ -525,8 +523,7 @@ int libmk_set_effect(LibMK_Handle* handle, LibMK_Effect effect) {
525523 if (r != LIBMK_SUCCESS )
526524 return r ;
527525 packet = libmk_build_packet (
528- 5 , HEADER_DEFAULT | HEADER_EFFECT , OPCODE_EFFECT ,
529- 0x00 , 0x00 , (unsigned char ) effect );
526+ 5 , HEADER_SET , OPCODE_EFFECT , 0x00 , 0x00 , (unsigned char ) effect );
530527 return libmk_send_packet (handle , packet );
531528}
532529
@@ -546,6 +543,40 @@ int libmk_set_full_color(LibMK_Handle* handle,
546543
547544
548545int libmk_send_packet (LibMK_Handle * handle , unsigned char * packet ) {
546+ <<<<<<< HEAD
547+ == = == ==
548+ /** Send a single packet of data to the specified device
549+ *
550+ * Calls libmk_send_recv_packet but instructs it not to care about
551+ * receiving a response.
552+ */
553+ return libmk_send_recv_packet (handle , packet , true);
554+ }
555+
556+
557+ int libmk_send_recv_packet (
558+ LibMK_Handle * handle , unsigned char * packet , bool response_required ) {
559+ /** Send a single packet of data to the specified device
560+ *
561+ * The device operates in INTERRUPT mode, expects 64 bytes of data
562+ * every time. First sends the packet to the device OUT endpoint,
563+ * then reads a packet from the device IN endpoint. If the packet
564+ * was correctly formatted, this received packet has the same header
565+ * as the packet sent. If the header is HEADER_ERROR, then a
566+ * protocol error has occurred and the packet was rejected.
567+ *
568+ * response_required: Boolean argument that determines whether an
569+ * acknowledgement packet is required from the keyboard. Nearly
570+ * all operations yield a confirmation packet, but some do not. If
571+ * it is not required, the function will still attempt to retrieve
572+ * a response, but not care if it is not available.
573+ */
574+ if (handle == NULL )
575+ handle = DeviceHandle ;
576+ if (handle == NULL )
577+ return LIBMK_ERR_DEV_NOT_SET ;
578+
579+ >>>>>>> 748 c903 ... Fix errors during profile control
549580 int t , result ;
550581 int r = libusb_interrupt_transfer (
551582 handle -> handle , LIBMK_EP_OUT | LIBUSB_ENDPOINT_OUT ,
@@ -564,9 +595,9 @@ int libmk_send_packet(LibMK_Handle* handle, unsigned char* packet) {
564595#ifdef LIBMK_DEBUG
565596 libmk_print_packet (packet , "Response" );
566597#endif // LIBMK_DEBUG
567- if (r != LIBUSB_SUCCESS ) {
598+ if (r != LIBUSB_SUCCESS && response_required ) {
568599 result = LIBMK_ERR_TRANSFER ;
569- } else if (t != LIBMK_PACKET_SIZE ) {
600+ } else if (t != LIBMK_PACKET_SIZE && response_required ) {
570601 result = LIBMK_ERR_TRANSFER ;
571602 } else if (packet [0 ] == HEADER_ERROR ) {
572603 libmk_print_packet (packet , "Error response" );
@@ -583,13 +614,19 @@ int libmk_exch_packet(LibMK_Handle* handle, unsigned char* packet) {
583614 int r = libusb_interrupt_transfer (
584615 handle -> handle , LIBMK_EP_OUT | LIBUSB_ENDPOINT_OUT ,
585616 packet , LIBMK_PACKET_SIZE , & t , LIBMK_PACKET_TIMEOUT );
617+ #ifdef LIBMK_DEBUG
618+ libmk_print_packet (packet , "Sent" );
619+ #endif // LIBMK_DEBUG
586620 if (r != LIBUSB_SUCCESS || t != LIBMK_PACKET_SIZE ) {
587621 free (packet );
588622 return LIBMK_ERR_TRANSFER ;
589623 }
590624 r = libusb_interrupt_transfer (
591625 handle -> handle , LIBMK_EP_IN | LIBUSB_ENDPOINT_IN ,
592626 packet , LIBMK_PACKET_SIZE , & t , LIBMK_PACKET_TIMEOUT );
627+ #ifdef LIBMK_DEBUG
628+ libmk_print_packet (packet , "Received" );
629+ #endif // LIBMK_DEBUG
593630 if (r != LIBUSB_SUCCESS || t != LIBMK_PACKET_SIZE )
594631 return LIBMK_ERR_TRANSFER ;
595632 return LIBMK_SUCCESS ;
@@ -636,9 +673,7 @@ int libmk_set_all_led_color(LibMK_Handle* handle, unsigned char* colors) {
636673 unsigned char * packets [LIBMK_ALL_LED_PCK_NUM ];
637674
638675 for (short i = 0 ; i < LIBMK_ALL_LED_PCK_NUM ; i ++ )
639- packets [i ] = libmk_build_packet (
640- 3 , HEADER_DEFAULT | HEADER_EFFECT ,
641- OPCODE_ALL_LED , (unsigned char ) i * 2 );
676+ packets [i ] = libmk_build_packet (3 , HEADER_SET , 0xA8 , (unsigned char ) i * 2 );
642677
643678 unsigned char offset ;
644679 int packet , index , result ;
@@ -698,7 +733,7 @@ int libmk_set_single_led(
698733 handle = DeviceHandle ;
699734 if (handle == NULL )
700735 return LIBMK_ERR_DEV_NOT_SET ;
701- int result = libmk_send_control_packet (handle );
736+ int result = libmk_set_control_mode (handle , LIBMK_CUSTOM_CTRL );
702737 if (result != LIBMK_SUCCESS )
703738 return result ;
704739 unsigned char offset ;
@@ -718,7 +753,7 @@ int libmk_set_effect_details(
718753 if (handle == NULL )
719754 return LIBMK_ERR_DEV_NOT_SET ;
720755 unsigned char * packet = libmk_build_packet (
721- 10 , HEADER_DEFAULT | HEADER_EFFECT , OPCODE_EFFECT_ARGS , 0x00 , 0x00 ,
756+ 10 , HEADER_SET , OPCODE_EFFECT_ARGS , 0x00 , 0x00 ,
722757 (unsigned char ) effect -> effect , effect -> speed , effect -> direction ,
723758 effect -> amount , 0xFF , 0xFF );
724759 unsigned char i ;
@@ -747,7 +782,7 @@ int libmk_get_firmware_version(LibMK_Handle* handle, LibMK_Firmware** fw) {
747782 (* fw ) = (LibMK_Firmware * ) malloc (sizeof (LibMK_Firmware ));
748783 for (unsigned char i = 0 ; i < 5 ; i ++ )
749784 (* fw )-> string [i ] = p [0x04 + i ];
750- (* fw )-> string [5 ] = ( char ) NULL ;
785+ (* fw )-> string [5 ] = 0x00 ;
751786 (* fw )-> major = p [0x04 ] & 0x0F ;
752787 (* fw )-> minor = p [0x06 ] & 0x0F ;
753788 (* fw )-> patch = p [0x08 ] & 0x0F ;
@@ -761,8 +796,14 @@ int libmk_save_profile(LibMK_Handle* handle) {
761796 handle = DeviceHandle ;
762797 if (handle == NULL )
763798 return LIBMK_ERR_DEV_NOT_SET ;
764- unsigned char * p = libmk_build_packet (2 , 0x55 , 0x50 );
765- return libmk_send_packet (handle , p );
799+ int r = libmk_set_control_mode (handle , LIBMK_PROFILE_CTRL );
800+ if (r != LIBMK_SUCCESS )
801+ return r ;
802+ unsigned char * p = libmk_build_packet (2 , 0x50 , 0x55 );
803+ r = libmk_send_recv_packet (handle , p , false);
804+ if (r != LIBMK_SUCCESS )
805+ return r ;
806+ return libmk_set_control_mode (handle , LIBMK_CUSTOM_CTRL );
766807}
767808
768809
@@ -771,10 +812,16 @@ int libmk_set_active_profile(LibMK_Handle* handle, char profile) {
771812 handle = DeviceHandle ;
772813 if (handle == NULL )
773814 return LIBMK_ERR_DEV_NOT_SET ;
815+ int r = libmk_set_control_mode (handle , LIBMK_PROFILE_CTRL );
816+ if (r != LIBMK_SUCCESS )
817+ return r ;
774818 if (!(1 <= profile <= 4 ))
775819 return LIBMK_ERR_INVALID_ARG ;
776- unsigned char * p = libmk_build_packet (4 , 0x51 , 0x00 , 0x00 , profile );
777- return libmk_send_packet (handle , p );
820+ unsigned char * p = libmk_build_packet (5 , HEADER_SET , 0x00 , 0x00 , 0x00 , profile );
821+ r = libmk_send_packet (handle , p );
822+ if (r != LIBMK_SUCCESS )
823+ return r ;
824+ return libmk_set_control_mode (handle , LIBMK_CUSTOM_CTRL );
778825}
779826
780827
@@ -787,7 +834,17 @@ int libmk_get_active_profile(LibMK_Handle* handle, char* profile) {
787834 int r = libmk_exch_packet (handle , p );
788835 if (r != LIBMK_SUCCESS )
789836 return r ;
790- * profile = p [3 ];
837+ * profile = p [4 ];
791838 free (p );
792839 return LIBMK_SUCCESS ;
793840}
841+
842+
843+ int libmk_set_control_mode (LibMK_Handle * handle , LibMK_ControlMode mode ) {
844+ if (handle == NULL )
845+ handle = DeviceHandle ;
846+ if (handle == NULL )
847+ return LIBMK_ERR_DEV_NOT_SET ;
848+ char * p = libmk_build_packet (2 , 0x41 , mode );
849+ return libmk_send_recv_packet (handle , p , false);
850+ }
0 commit comments