openVSM  1.0
 All Data Structures Files Functions Variables Pages
lua_bind.c
Go to the documentation of this file.
1 
24 #include <vsm_api.h>
25 
26 static int lua_state_to_string ( lua_State* L );
27 static int lua_set_pin_state ( lua_State* L );
28 static int lua_set_pin_bool ( lua_State* L );
29 static int lua_get_pin_bool ( lua_State* L );
30 static int lua_get_pin_state ( lua_State* L );
31 static int lua_is_pin_active ( lua_State* L );
32 static int lua_is_pin_edge ( lua_State* L );
33 static int lua_is_pin_posedge ( lua_State* L );
34 static int lua_is_pin_negedge ( lua_State* L );
35 static int lua_is_pin_low ( lua_State* L );
36 static int lua_is_pin_high ( lua_State* L );
37 static int lua_is_pin_floating ( lua_State* L );
38 static int lua_toggle_pin_state ( lua_State* L );
39 static int lua_out_log ( lua_State* L );
40 static int lua_out_message ( lua_State* L );
41 static int lua_out_warning ( lua_State* L );
42 static int lua_out_error ( lua_State* L );
43 static int lua_set_callback ( lua_State* L );
44 static int lua_create_debug_popup ( lua_State* L );
45 static int lua_print_to_debug_popup ( lua_State* L );
46 static int lua_dump_to_debug_popup ( lua_State* L );
47 static int lua_create_memory_popup ( lua_State* L );
48 static int lua_create_source_popup ( lua_State* L );
49 static int lua_create_status_popup ( lua_State* L );
50 static int lua_create_var_popup ( lua_State* L );
51 static int lua_delete_popup ( lua_State* L );
52 static int lua_set_memory_popup ( lua_State* L );
53 static int lua_repaint_memory_popup ( lua_State* L );
54 static int lua_get_string_param ( lua_State* L );
55 static int lua_get_bool_param ( lua_State* L );
56 static int lua_get_num_param ( lua_State* L );
57 static int lua_get_hex_param ( lua_State* L );
58 static int lua_get_init_param ( lua_State* L );
59 static int lua_add_source_file ( lua_State* L );
60 
61 static int lua_get_bit ( lua_State* L );
62 static int lua_set_bit ( lua_State* L );
63 static int lua_toggle_bit ( lua_State* L );
64 static int lua_clear_bit ( lua_State* L );
65 
66 static int lua_get_systime ( lua_State* L );
67 
68 static const lua_bind_var lua_var_api_list[]=
69 {
70  {.var_name="SHI", .var_value=SHI},
71  {.var_name="SLO", .var_value=SLO},
72  {.var_name="FLT", .var_value=FLT},
73  {.var_name="PLO", .var_value=PLO},
74  {.var_name="ILO", .var_value=ILO},
75  {.var_name="WLO", .var_value=WLO},
76  {.var_name="WHI", .var_value=WHI},
77  {.var_name="IHI", .var_value=IHI},
78  {.var_name="PHI", .var_value=PHI},
79  {.var_name="WUD", .var_value=WUD},
80  {.var_name="SUD", .var_value=SUD},
81  {.var_name="TSTATE", .var_value=TSTATE},
82  {.var_name="FSTATE", .var_value=FSTATE},
83  {.var_name="UNDEFINED", .var_value=UNDEFINED},
84  {.var_name="MSEC", .var_value=1000000000L},
85  {.var_name="NSEC", .var_value=100000000L},
86  {.var_name="SEC", .var_value=1000000000000L},
87  {.var_name="NOW", .var_value=0L},
88  {.var_name=0},
89 };
90 
91 static const lua_bind_func lua_c_api_list[] =
92 {
93  {.lua_func_name="state_to_string", .lua_c_api=&lua_state_to_string},
94  {.lua_func_name="is_pin_active", .lua_c_api=&lua_is_pin_active},
95  {.lua_func_name="is_pin_edge", .lua_c_api=&lua_is_pin_edge},
96  {.lua_func_name="is_pin_posedge", .lua_c_api=&lua_is_pin_posedge},
97  {.lua_func_name="is_pin_negedge", .lua_c_api=&lua_is_pin_negedge},
98  {.lua_func_name="set_pin_state", .lua_c_api=&lua_set_pin_state},
99  {.lua_func_name="set_pin_bool", .lua_c_api=&lua_set_pin_bool},
100  {.lua_func_name="get_pin_bool", .lua_c_api=&lua_get_pin_bool},
101  {.lua_func_name="get_pin_state", .lua_c_api=&lua_get_pin_state},
102  {.lua_func_name="is_pin_low", .lua_c_api=&lua_is_pin_low},
103  {.lua_func_name="is_pin_high", .lua_c_api=&lua_is_pin_high},
104  {.lua_func_name="is_pin_floating", .lua_c_api=&lua_is_pin_floating},
105  {.lua_func_name="toggle_pin_state", .lua_c_api=&lua_toggle_pin_state},
106  {.lua_func_name="out_log", .lua_c_api=&lua_out_log},
107  {.lua_func_name="out_message", .lua_c_api=&lua_out_message},
108  {.lua_func_name="out_warning", .lua_c_api=&lua_out_warning},
109  {.lua_func_name="out_error", .lua_c_api=&lua_out_error},
110  {.lua_func_name="set_callback", .lua_c_api=&lua_set_callback},
111  {.lua_func_name="create_debug_popup", .lua_c_api=&lua_create_debug_popup},
112  {.lua_func_name="create_memory_popup", .lua_c_api=&lua_create_memory_popup},
113  {.lua_func_name="create_source_popup", .lua_c_api=&lua_create_source_popup},
114  {.lua_func_name="create_status_popup", .lua_c_api=&lua_create_status_popup},
115  {.lua_func_name="create_var_popup", .lua_c_api=&lua_create_var_popup},
116  {.lua_func_name="delete_popup", .lua_c_api=&lua_delete_popup},
117  {.lua_func_name="set_memory_popup", .lua_c_api=&lua_set_memory_popup},
118  {.lua_func_name="repaint_memory_popup", .lua_c_api=&lua_repaint_memory_popup},
119  {.lua_func_name="print_to_debug_popup", .lua_c_api=&lua_print_to_debug_popup},
120  {.lua_func_name="dump_to_debug_popup", .lua_c_api=&lua_dump_to_debug_popup},
121  {.lua_func_name="get_string_param", .lua_c_api=&lua_get_string_param},
122  {.lua_func_name="get_num_param", .lua_c_api=&lua_get_num_param},
123  {.lua_func_name="get_bool_param", .lua_c_api=&lua_get_bool_param},
124  {.lua_func_name="get_init_param", .lua_c_api=&lua_get_init_param},
125  {.lua_func_name="get_hex_param", .lua_c_api=&lua_get_hex_param},
126  {.lua_func_name="add_source_file", .lua_c_api=&lua_add_source_file},
127  {.lua_func_name="get_bit", .lua_c_api=&lua_get_bit},
128  {.lua_func_name="set_bit", .lua_c_api=&lua_set_bit},
129  {.lua_func_name="clear_bit", .lua_c_api=&lua_clear_bit},
130  {.lua_func_name="toggle_bit", .lua_c_api=&lua_toggle_bit},
131  {.lua_func_name="systime", .lua_c_api=&lua_get_systime},
132  { NULL, NULL},
133 };
134 
135 void
136 register_functions ( lua_State* L )
137 {
138  /* Declare functions */
139  for ( int32_t i=0; lua_c_api_list[i].lua_func_name; i++ )
140  {
141  lua_pushcfunction ( L, lua_c_api_list[i].lua_c_api );
142  lua_setglobal ( L, lua_c_api_list[i].lua_func_name );
143  }
144  /* Declare variables */
145  for ( int32_t i=0; lua_var_api_list[i].var_name ; i++ )
146  {
147  lua_pushinteger ( L, lua_var_api_list[i].var_value );
148  lua_setglobal ( L, lua_var_api_list[i].var_name );
149  }
150  /* Declare pins */
151  for ( int i=0; device_pins[i].name; i++ )
152  {
153  lua_pushinteger ( L, i );
154  lua_setglobal ( L, device_pins[i].name );
155  }
156 }
157 
158 void
159 lua_load_script ( const char* device_name )
160 {
161  char spath[512] = {0};
162  if ( 0 == GetEnvironmentVariable ( "LUAVSM", spath, sizeof spath ) )
163  {
164  out_error ( "LUAVSM env variable was not set" );
165  }
166  char script[512]= {0};
167  sprintf ( script, "%s\\%s", spath, device_name );
168 
169  int32_t lua_err = luaL_loadfile ( luactx, script );
170  if ( 0 != lua_err )
171  {
172  const char* mess = NULL;
173  switch ( lua_err )
174  {
175  case LUA_ERRSYNTAX:
176  mess = lua_tostring(luactx, -1);
177  out_error ( "Syntax error in Lua script\n%s", mess );
178  return;
179  case LUA_ERRMEM:
180  out_error ( "Not enough memory to load script" );
181  return;
182  case LUA_ERRFILE:
183  out_error ( "Error loading script file" );
184  return;
185  default:
186  out_error ( "Unknown error, shouldn't happen" );
187  assert ( 0 );
188  }
189  }
190  /* Primer run, if not run it - nothing works, need for parse */
191  if ( 0 != lua_pcall ( luactx, 0, 0, 0 ) )
192  {
193  out_error ( "Failed to load the script" );
194  return;
195  }
196 
197  out_log ( "Successfully loaded Lua script" );
198 }
199 
200 void
201 lua_run_function ( const char* func_name )
202 {
203  /* Declare function to run */
204  lua_getglobal ( luactx, func_name );
205  /* First argument */
206  lua_pcall ( luactx, 0, 0, 0 );
207 }
208 
209 static int
210 lua_get_string_param ( lua_State* L )
211 {
212  lua_pushstring ( L, get_string_param ( ( char* ) lua_tostring ( L, -1 ) ) );
213  return 1;
214 }
215 
216 static int
217 lua_get_bool_param ( lua_State* L )
218 {
219  lua_pushboolean ( L, get_bool_param ( ( char* ) lua_tostring ( L, -1 ) ) );
220  return 1;
221 }
222 
223 static int
224 lua_get_num_param ( lua_State* L )
225 {
226  lua_pushnumber ( L, get_num_param ( ( char* ) lua_tostring ( L, -1 ) ) );
227  return 1;
228 }
229 
230 static int
231 lua_get_hex_param ( lua_State* L )
232 {
233  lua_pushinteger ( L, get_hex_param ( ( char* ) lua_tostring ( L, -1 ) ) );
234  return 1;
235 }
236 
237 static int
238 lua_get_init_param ( lua_State* L )
239 {
240  lua_pushinteger ( L, get_init_param ( ( char* ) lua_tostring ( L, -1 ) ) );
241  return 1;
242 }
243 
244 static int
245 lua_delete_popup ( lua_State* L )
246 {
247  int id = lua_tonumber ( L, -1 );
248  delete_popup ( id );
249  return 0;
250 }
251 
252 static int
253 lua_create_debug_popup ( lua_State* L )
254 {
255  const char* text = lua_tostring ( L, -1 );
256  lua_pushlightuserdata ( L, create_debug_popup ( text, ++popup_id ) );
257  lua_pushinteger ( L, popup_id );
258  return 2;
259 }
260 
266 static int
267 lua_print_to_debug_popup ( lua_State* L )
268 {
269  lua_Number argnum = lua_gettop ( L );
270  if ( 2 > argnum )
271  {
272  out_error ( "Function %s expects 2 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
273  return 0;
274  }
278  print_to_debug_popup ( lua_touserdata ( L, -2 ), lua_tostring ( L, -1 ) );
279  return 0;
280 }
281 
287 static int
288 lua_dump_to_debug_popup ( lua_State* L )
289 {
290  lua_Number argnum = lua_gettop ( L );
291  if ( 4 > argnum )
292  {
293  out_error ( "Function %s expects 4 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
294  return 0;
295  }
296 
297  lua_Number offset = luaL_checknumber ( L,-1 );
298  lua_Number size = luaL_checknumber ( L,-2 );
299  const char* buf = luaL_checkstring ( L,-3 );
300  dump_to_debug_popup ( lua_touserdata ( L, -4 ), ( BYTE* ) buf, offset, size );
301  return 0;
302 }
303 
304 static int
305 lua_create_source_popup ( lua_State* L )
306 {
307  const char* text = lua_tostring ( L, -1 );
308  lua_pushlightuserdata ( L, create_source_popup ( text, ++popup_id ) );
309  lua_pushinteger ( L, popup_id );
310  return 2;
311 }
312 static int
313 lua_create_status_popup ( lua_State* L )
314 {
315  const char* text = lua_tostring ( L, -1 );
316  lua_pushlightuserdata ( L, create_status_popup ( text, ++popup_id ) );
317  lua_pushinteger ( L, popup_id );
318  return 2;
319 }
320 static int
321 lua_create_var_popup ( lua_State* L )
322 {
323  const char* text = lua_tostring ( L, -1 );
324  lua_pushlightuserdata ( L, create_var_popup ( text, ++popup_id ) );
325  lua_pushinteger ( L, popup_id );
326  return 2;
327 }
328 
329 static int
330 lua_create_memory_popup ( lua_State* L )
331 {
332  const char* text = lua_tostring ( L, -1 );
333  lua_pushlightuserdata ( L, create_memory_popup ( text, ++popup_id ) );
334  lua_pushinteger ( L, popup_id );
335  return 2;
336 }
337 
338 static int
339 lua_set_memory_popup ( lua_State* L )
340 {
341 
342  lua_Number argnum = lua_gettop ( L );
343  if ( 3 > argnum )
344  {
345  out_error ( "Function %s expects 3 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
346  return 0;
347  }
348 
349  lua_Number size = luaL_checknumber ( L,-1 );
350  const char* buf = luaL_checkstring ( L,-2 );
351 
352  set_memory_popup ( lua_touserdata ( L, -3 ), 0, ( void* ) buf, size );
353 
354  return 0;
355 }
356 
357 static int
358 lua_add_source_file ( lua_State* L )
359 {
360 
361  lua_Number argnum = lua_gettop ( L );
362  if ( 2 > argnum )
363  {
364  out_error ( "Function %s expects 2 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
365  return 0;
366  }
367 
368  if ( false == add_source_file ( lua_touserdata ( L, -3 ), ( char* ) lua_tostring ( L, -2 ), lua_toboolean ( L, -1 ) ) )
369  {
370  out_log ( "Fail" );
371  }
372 
373  return 0;
374 }
375 
376 static int
377 lua_repaint_memory_popup ( lua_State* L )
378 {
379  lua_Number argnum = lua_gettop ( L );
380  if ( 1 > argnum )
381  {
382  out_error ( "Function %s expects 1 argument got %d\n", __PRETTY_FUNCTION__, argnum );
383  return 0;
384  }
385  if ( 0 == lua_isuserdata ( L, -1 ) )
386  {
387  out_error ( "Bad argument" );
388  return 0;
389  }
390 
391  repaint_memory_popup ( lua_touserdata ( L, -1 ) );
392  return 0;
393 }
394 
395 static int
396 lua_set_pin_state ( lua_State* L )
397 {
398  lua_Number argnum = lua_gettop ( L );
399  if ( 2 > argnum )
400  {
401  out_error ( "Function %s expects 2 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
402  return 0;
403  }
404  int32_t pin_num = lua_tonumber ( L, -2 );
405  int32_t pin_state = lua_tonumber ( L, -1 );
406  set_pin_state ( device_pins[pin_num], pin_state );
407  return 0;
408 }
409 
410 static int
411 lua_set_pin_bool ( lua_State* L )
412 {
413  lua_Number argnum = lua_gettop ( L );
414  if ( 2 > argnum )
415  {
416  out_error ( "Function %s expects 2 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
417  return 0;
418  }
419  int32_t pin_num = lua_tonumber ( L, -2 );
420  bool pin_level = lua_toboolean ( L, -1 );
421  set_pin_bool ( device_pins[pin_num], pin_level );
422  return 0;
423 }
424 
425 static int
426 lua_get_pin_bool ( lua_State* L )
427 {
428  lua_Number argnum = lua_gettop ( L );
429  if ( 1 > argnum )
430  {
431  out_error ( "Function %s expects 1 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
432  return 0;
433  }
434  int32_t pin_num = lua_tonumber ( L, -1 );
435  int32_t state = get_pin_bool ( device_pins[pin_num]);
436  if ( -1 == state )
437  {
438  lua_pushnil(L);
439  return 1;
440  }
441  lua_pushboolean ( L, state);
442  return 1;
443 }
444 
445 static int
446 lua_state_to_string ( lua_State* L )
447 {
448  lua_Number argnum = lua_gettop ( L );
449  if ( 1 > argnum )
450  {
451  out_error ( "Function %s expects 1 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
452  return 0;
453  }
454  int32_t state = lua_tonumber ( L, -1 );
455 
456  lua_pushstring ( L, state_to_string(state));
457  return 1;
458 }
459 
460 static int
461 lua_get_pin_state ( lua_State* L )
462 {
463  lua_Number argnum = lua_gettop ( L );
464  if ( 1 > argnum )
465  {
466  out_error ( "Function %s expects 1 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
467  return 0;
468  }
469  int32_t pin_num = lua_tonumber ( L, -1 );
470 
471  if ( TRUE == is_pin_high ( device_pins[pin_num].pin ) )
472  {
473  lua_pushnumber ( L, SHI );
474  return 1;
475  }
476  else if ( TRUE == is_pin_low ( device_pins[pin_num].pin ) )
477  {
478  lua_pushnumber ( L, SLO );
479  return 1;
480  }
481  else if ( TRUE == is_pin_floating ( device_pins[pin_num].pin ) )
482  {
483  lua_pushnumber ( L, FLT );
484  return 1;
485  }
486  else
487  {
488  lua_pushnumber ( L, UNDEFINED );
489  return 1;
490  }
491 }
492 
493 static int
494 lua_is_pin_low ( lua_State* L )
495 {
496  lua_Number argnum = lua_gettop ( L );
497  if ( 1 > argnum )
498  {
499  out_error ( "Function %s expects 1 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
500  return 0;
501  }
502  int32_t pin_num = lua_tonumber ( L, -1 );
503  lua_pushboolean ( L, is_pin_low ( device_pins[pin_num].pin ) );
504  return 1;
505 }
506 
507 static int
508 lua_is_pin_high ( lua_State* L )
509 {
510  lua_Number argnum = lua_gettop ( L );
511  if ( 1 > argnum )
512  {
513  out_error ( "Function %s expects 1 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
514  return 0;
515  }
516  int32_t pin_num = lua_tonumber ( L, -1 );
517  lua_pushboolean ( L, is_pin_high ( device_pins[pin_num].pin ) );
518  return 1;
519 }
520 
521 static int
522 lua_is_pin_edge ( lua_State* L )
523 {
524  lua_Number argnum = lua_gettop ( L );
525  if ( 1 > argnum )
526  {
527  out_error ( "Function %s expects 1 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
528  return 0;
529  }
530  int32_t pin_num = lua_tonumber ( L, -1 );
531  lua_pushboolean ( L, is_pin_edge ( device_pins[pin_num].pin ) );
532  return 1;
533 }
534 
535 static int
536 lua_is_pin_posedge ( lua_State* L )
537 {
538  lua_Number argnum = lua_gettop ( L );
539  if ( 1 > argnum )
540  {
541  out_error ( "Function %s expects 1 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
542  return 0;
543  }
544  int32_t pin_num = lua_tonumber ( L, -1 );
545  lua_pushboolean ( L, is_pin_posedge ( device_pins[pin_num].pin ) );
546  return 1;
547 }
548 
549 static int
550 lua_is_pin_negedge ( lua_State* L )
551 {
552  lua_Number argnum = lua_gettop ( L );
553  if ( 1 > argnum )
554  {
555  out_error ( "Function %s expects 1 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
556  return 0;
557  }
558  int32_t pin_num = lua_tonumber ( L, -1 );
559  lua_pushboolean ( L, is_pin_negedge ( device_pins[pin_num].pin ) );
560  return 1;
561 }
562 
563 static int
564 lua_is_pin_active ( lua_State* L )
565 {
566  lua_Number argnum = lua_gettop ( L );
567  if ( 1 > argnum )
568  {
569  out_error ( "Function %s expects 1 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
570  return 0;
571  }
572  int32_t pin_num = lua_tonumber ( L, -1 );
573  lua_pushboolean ( L, is_pin_active ( device_pins[pin_num].pin ) );
574  return 1;
575 }
576 
577 static int
578 lua_toggle_pin_state ( lua_State* L )
579 {
580  lua_Number argnum = lua_gettop ( L );
581  if ( 1 > argnum )
582  {
583  out_error ( "Function %s expects 1 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
584  return 0;
585  }
586  int32_t pin_num = lua_tonumber ( L, -1 );
587  toggle_pin_state ( device_pins[pin_num] );
588  return 0;
589 }
590 
591 static int
592 lua_is_pin_floating ( lua_State* L )
593 {
594  lua_Number argnum = lua_gettop ( L );
595  if ( 1 > argnum )
596  {
597  out_error ( "Function %s expects 1 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
598  return 0;
599  }
600  int32_t pin_num = lua_tonumber ( L, -1 );
601  lua_pushboolean ( L, is_pin_floating ( device_pins[pin_num].pin ) );
602  return 1;
603 }
604 
605 static int
606 lua_out_log ( lua_State* L )
607 {
608  lua_Number argnum = lua_gettop ( L );
609  if ( 1 > argnum )
610  {
611  out_error ( "Function %s expects 1 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
612  return 0;
613  }
614  const char* text = lua_tostring ( L, -1 );
615  out_log ( text );
616  return 0;
617 }
618 
619 static int
620 lua_out_message ( lua_State* L )
621 {
622  lua_Number argnum = lua_gettop ( L );
623  if ( 1 > argnum )
624  {
625  out_error ( "Function %s expects 1 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
626  return 0;
627  }
628  const char* text = lua_tostring ( L, -1 );
629  out_message ( text );
630  return 0;
631 }
632 
633 static int
634 lua_out_warning ( lua_State* L )
635 {
636  lua_Number argnum = lua_gettop ( L );
637  if ( 1 > argnum )
638  {
639  out_error ( "Function %s expects 1 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
640  return 0;
641  }
642  const char* text = lua_tostring ( L, -1 );
643  out_warning ( text );
644  return 0;
645 }
646 
647 static int
648 lua_out_error ( lua_State* L )
649 {
650  lua_Number argnum = lua_gettop ( L );
651  if ( 1 > argnum )
652  {
653  out_error ( "Function %s expects 1 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
654  return 0;
655  }
656  const char* text = lua_tostring ( L, -1 );
657  out_error ( text );
658  return 0;
659 }
660 
661 static int
662 lua_set_callback ( lua_State* L )
663 {
664  lua_Number argnum = lua_gettop ( L );
665  if ( 2 > argnum )
666  {
667  out_error ( "Function %s expects 2 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
668  return 0;
669  }
670  //TODO: Add check integer type
671  lua_Number picotime = lua_tonumber ( L, -2 );
672  lua_Number eventid = lua_tonumber ( L, -1 );
673 
674  set_callback ( picotime, eventid );
675  return 0;
676 }
677 
678 static int
679 lua_get_systime ( lua_State* L )
680 {
681  ABSTIME curtime = 0;
682  systime ( &curtime );
683  lua_pushnumber ( L, curtime );
684  return 1;
685 }
686 
687 static int
688 lua_get_bit ( lua_State* L )
689 {
690  lua_Number argnum = lua_gettop ( L );
691  if ( 2 > argnum )
692  {
693  out_error ( "Function %s expects 2 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
694  return 0;
695  }
696  //TODO: Add check integer type
697  size_t byte = lua_tointeger ( L, -2 );
698  size_t bit = lua_tointeger ( L, -1 );
699  lua_pushboolean ( L, ( byte >> bit & 0x01 ) );
700  return 1;
701 }
702 
703 static int
704 lua_clear_bit ( lua_State* L )
705 {
706  lua_Number argnum = lua_gettop ( L );
707  if ( 2 > argnum )
708  {
709  out_error ( "Function %s expects 2 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
710  return 0;
711  }
712  //TODO: Add check integer type
713  size_t byte = lua_tointeger ( L, -2 );
714  size_t bit = lua_tointeger ( L, -1 );
715  byte &= ~ ( 1 << bit );
716  lua_pushnumber ( L, byte );
717  return 1;
718 }
719 
720 static int
721 lua_set_bit ( lua_State* L )
722 {
723  lua_Number argnum = lua_gettop ( L );
724  if ( 2 > argnum )
725  {
726  out_error ( "Function %s expects 2 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
727  return 0;
728  }
729  //TODO: Add check integer type
730  size_t byte = lua_tointeger ( L, -2 );
731  size_t bit = lua_tointeger ( L, -1 );
732  byte |= ( 1 << bit );
733  lua_pushnumber ( L, byte );
734  return 1;
735 }
736 
737 static int
738 lua_toggle_bit ( lua_State* L )
739 {
740  lua_Number argnum = lua_gettop ( L );
741  if ( 2 > argnum )
742  {
743  out_error ( "Function %s expects 2 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
744  return 0;
745  }
746  //TODO: Add check integer type
747  size_t byte = lua_tointeger ( L, -2 );
748  size_t bit = lua_tointeger ( L, -1 );
749  byte ^= 1 << bit;
750  lua_pushnumber ( L, byte );
751  return 1;
752 }
bool is_pin_posedge(IDSIMPIN *pin)
Definition: c_bind.c:423
void toggle_pin_state(VSM_PIN pin)
Definition: c_bind.c:366
IDEBUGPOPUP * create_source_popup(const char *title, const int32_t id)
Definition: c_bind.c:223
int64_t get_init_param(char *field_name)
Definition: c_bind.c:151
bool is_pin_edge(IDSIMPIN *pin)
Definition: c_bind.c:443
bool is_pin_active(IDSIMPIN *pin)
Definition: c_bind.c:413
void set_memory_popup(IMEMORYPOPUP *popup, size_t offset, void *buffer, size_t size)
Definition: c_bind.c:293
bool is_pin_low(IDSIMPIN *pin)
Definition: c_bind.c:533
void set_pin_state(VSM_PIN pin, STATE state)
Definition: c_bind.c:76
void out_message(const char *format,...)
Definition: c_bind.c:477
void out_error(const char *format,...)
Definition: c_bind.c:507
void print_to_debug_popup(IDEBUGPOPUP *popup, const char *message)
Definition: c_bind.c:345
void out_warning(const char *format,...)
Definition: c_bind.c:492
int32_t get_pin_bool(VSM_PIN pin)
Definition: c_bind.c:394
int32_t get_hex_param(char *field_name)
Definition: c_bind.c:141
void delete_popup(POPUPID id)
Definition: c_bind.c:281
bool get_bool_param(char *field_name)
Definition: c_bind.c:119
IDEBUGPOPUP * create_debug_popup(const char *title, const int32_t id)
Definition: c_bind.c:203
char * get_string_param(char *field_name)
Definition: c_bind.c:109
void dump_to_debug_popup(IDEBUGPOPUP *popup, const uint8_t *buf, uint32_t offset, uint32_t size)
Definition: c_bind.c:357
bool add_source_file(ISOURCEPOPUP *popup, char *filename, bool lowlevel)
Definition: c_bind.c:305
IDEBUGPOPUP * create_var_popup(const char *title, const int32_t id)
Definition: c_bind.c:263
bool is_pin_floating(IDSIMPIN *pin)
Definition: c_bind.c:553
IDEBUGPOPUP * create_status_popup(const char *title, const int32_t id)
Definition: c_bind.c:243
void systime(ABSTIME *at)
Definition: c_bind.c:99
void set_callback(RELTIME picotime, EVENTID id)
Definition: c_bind.c:453
bool is_pin_high(IDSIMPIN *pin)
Definition: c_bind.c:543
IMEMORYPOPUP * create_memory_popup(const char *title, const int32_t id)
Definition: c_bind.c:183
void set_pin_bool(VSM_PIN pin, bool level)
Definition: c_bind.c:88
const char * state_to_string(STATE pinstate)
[Convert pin state to string]
Definition: c_bind.c:17
void repaint_memory_popup(IMEMORYPOPUP *popup)
Definition: c_bind.c:335
double get_num_param(char *field_name)
Definition: c_bind.c:129
int popup_id
Global pop identificator. Should be unique.
Definition: c_bind.c:54
void out_log(const char *format,...)
Definition: c_bind.c:462
bool is_pin_negedge(IDSIMPIN *pin)
Definition: c_bind.c:433