1313
1414
1515LibMK_Controller * libmk_create_controller (LibMK_Handle * handle ) {
16- /** Build a new LibMK Controller for a Device */
1716 LibMK_Controller * controller = (LibMK_Controller * ) malloc (
1817 sizeof (LibMK_Controller ));
1918 if (controller == NULL )
@@ -32,7 +31,6 @@ LibMK_Controller* libmk_create_controller(LibMK_Handle* handle) {
3231
3332
3433LibMK_Controller_State libmk_get_controller_state (LibMK_Controller * c ) {
35- /** Return whether the given controller is running (active) */
3634 pthread_mutex_lock (& (c -> state_lock ));
3735 LibMK_Controller_State b = c -> state ;
3836 pthread_mutex_unlock (& c -> state_lock );
@@ -41,7 +39,6 @@ LibMK_Controller_State libmk_get_controller_state(LibMK_Controller* c) {
4139
4240
4341LibMK_Result libmk_get_controller_error (LibMK_Controller * c ) {
44- /** Return an error if the controller has one set */
4542 pthread_mutex_lock (& (c -> error_lock ));
4643 LibMK_Result r = c -> error ;
4744 pthread_mutex_unlock (& (c -> error_lock ));
@@ -50,7 +47,6 @@ LibMK_Result libmk_get_controller_error(LibMK_Controller* c) {
5047
5148
5249LibMK_Result libmk_free_controller (LibMK_Controller * c ) {
53- /** Free the memory allocated for a LibMK_Controller struct */
5450 if (libmk_get_controller_state (c ) == LIBMK_STATE_ACTIVE )
5551 return LIBMK_ERR_STILL_ACTIVE ;
5652 pthread_mutex_destroy (& c -> state_lock );
@@ -67,7 +63,6 @@ LibMK_Result libmk_free_controller(LibMK_Controller* c) {
6763
6864
6965LibMK_Result libmk_start_controller (LibMK_Controller * controller ) {
70- /** Start a LibMK_Controller in a new thread */
7166 LibMK_Result r = (LibMK_Result ) libmk_enable_control (controller -> handle );
7267 if (r != LIBMK_SUCCESS )
7368 return r ;
@@ -79,7 +74,6 @@ LibMK_Result libmk_start_controller(LibMK_Controller* controller) {
7974
8075
8176void libmk_run_controller (LibMK_Controller * controller ) {
82- /** Execute instructions on a LibMK_Controller in a separate thread */
8377 pthread_mutex_lock (& (controller -> state_lock ));
8478 controller -> state = LIBMK_STATE_ACTIVE ;
8579 pthread_mutex_unlock (& (controller -> state_lock ));
@@ -124,7 +118,6 @@ void libmk_run_controller(LibMK_Controller* controller) {
124118
125119
126120void libmk_set_controller_error (LibMK_Controller * c , LibMK_Result e ) {
127- /** Set an error in the controller struct if one has not been set */
128121 pthread_mutex_lock (& (c -> error_lock ));
129122 if (c -> error == LIBMK_SUCCESS )
130123 c -> error = e ;
@@ -133,27 +126,27 @@ void libmk_set_controller_error(LibMK_Controller* c, LibMK_Result e) {
133126
134127
135128LibMK_Result libmk_exec_instruction (LibMK_Handle * h , LibMK_Instruction * i ) {
136- /** Execute a single instruction on the keyboard controlled */
137129 if (i == NULL )
138130 return libmk_send_control_packet (h );
139131 else if (i -> type == LIBMK_INSTR_ALL ) {
140132 return libmk_set_all_led_color (h , i -> colors );
141133 } else if (i -> type == LIBMK_INSTR_FULL ) {
142134 return libmk_set_full_color (h , i -> color [0 ], i -> color [1 ], i -> color [2 ]);
135+ } else if (i -> type == LIBMK_INSTR_SINGLE ) {
136+ return libmk_set_single_led (
137+ h , i -> r , i -> c , i -> color [0 ], i -> color [1 ], i -> color [2 ]);
143138 }
144139}
145140
146141
147142void libmk_stop_controller (LibMK_Controller * controller ) {
148- /** Stop a LibMK_Controller running in a separate thread */
149143 pthread_mutex_lock (& (controller -> exit_flag_lock ));
150144 controller -> exit_flag = true;
151145 pthread_mutex_unlock (& (controller -> exit_flag_lock ));
152146}
153147
154148
155149void libmk_wait_controller (LibMK_Controller * controller ) {
156- /** Set the flag to wait for the controller to finish scheduled instr */
157150 pthread_mutex_lock (& (controller -> exit_flag_lock ));
158151 controller -> wait_flag = true;
159152 pthread_mutex_unlock (& (controller -> exit_flag_lock ));
@@ -162,7 +155,6 @@ void libmk_wait_controller(LibMK_Controller* controller) {
162155
163156LibMK_Controller_State libmk_join_controller (
164157 LibMK_Controller * controller , double timeout ) {
165- /** Join a LibMK_Controller into the current thread (return state) */
166158 LibMK_Controller_State s ;
167159 clock_t t = clock ();
168160 double elapsed ;
@@ -181,7 +173,6 @@ LibMK_Controller_State libmk_join_controller(
181173
182174
183175void libmk_free_instruction (LibMK_Instruction * i ) {
184- /** Free the memory allocated for an executed instruction */
185176 fflush (stdout );
186177 if (i -> colors != NULL )
187178 free (i -> colors );
@@ -190,20 +181,30 @@ void libmk_free_instruction(LibMK_Instruction* i) {
190181
191182
192183LibMK_Instruction * libmk_create_instruction () {
193- /** Allocate a new LibMK_Instruction struct */
194184 LibMK_Instruction * i =
195185 (LibMK_Instruction * ) malloc (sizeof (LibMK_Instruction ));
196186 i -> duration = 0 ;
197- i -> id = 0 ;
187+ i -> id = -1 ;
198188 i -> type = -1 ;
199189 i -> next = NULL ;
200190 i -> colors = NULL ;
201191 return i ;
202192}
203193
204194
195+ LibMK_Instruction * libmk_create_instruction_single (
196+ unsigned char row , unsigned char column , unsigned char color [3 ]) {
197+ LibMK_Instruction * i = libmk_create_instruction ();
198+ i -> type = LIBMK_INSTR_SINGLE ;
199+ for (unsigned char j = 0 ; j < 3 ; j ++ )
200+ i -> color [j ] = color [j ];
201+ i -> r = row ;
202+ i -> c = column ;
203+ return i ;
204+ }
205+
206+
205207LibMK_Instruction * libmk_create_instruction_full (unsigned char c [3 ]) {
206- /** Create a new instruction that sets a full LED color */
207208 LibMK_Instruction * i = libmk_create_instruction ();
208209 for (unsigned char j = 0 ; j < 3 ; j ++ )
209210 i -> color [j ] = c [j ];
@@ -214,7 +215,6 @@ LibMK_Instruction* libmk_create_instruction_full(unsigned char c[3]) {
214215
215216LibMK_Instruction * libmk_create_instruction_all (
216217 unsigned char c [LIBMK_MAX_ROWS ][LIBMK_MAX_COLS ][3 ]) {
217- /** Create a new instruction that sets the color of all individual LEDs */
218218 LibMK_Instruction * i = libmk_create_instruction ();
219219 i -> colors = (unsigned char * ) malloc (
220220 sizeof (unsigned char ) * LIBMK_MAX_ROWS * LIBMK_MAX_COLS * 3 );
@@ -252,10 +252,11 @@ LibMK_Instruction* libmk_create_instruction_flash(
252252}
253253
254254
255- LibMK_Result libmk_sched_instruction (
256- LibMK_Controller * c , LibMK_Instruction * i ) {
257- /** Schedule a single instruction for execution on the controller */
255+ int libmk_sched_instruction (
256+ LibMK_Controller * c , LibMK_Instruction * i ) {
258257 pthread_mutex_lock (& (c -> instr_lock ));
258+ if (i -> id != -1 )
259+ return LIBMK_ERR_INVALID_ARG ; // Instruction already scheduled!
259260 if (c -> instr == NULL ) {
260261 c -> instr = i ;
261262 c -> instr -> id = 1 ;
@@ -279,13 +280,13 @@ LibMK_Result libmk_sched_instruction(
279280 k = k -> next ;
280281 }
281282 }
283+ int first_id = i -> id ;
282284 pthread_mutex_unlock (& (c -> instr_lock ));
283- return LIBMK_SUCCESS ;
285+ return first_id ;
284286}
285287
286288
287289LibMK_Result libmk_cancel_instruction (LibMK_Controller * c , unsigned int id ) {
288- /** Cancel a scheduled instruction from executing */
289290 pthread_mutex_lock (& (c -> instr_lock ));
290291 LibMK_Instruction * prev = NULL ;
291292 LibMK_Instruction * curr = c -> instr ;
0 commit comments