source: trunk/core/luascript.c @ 1020

Revision 1020, 34.0 KB checked in by reyalP, 2 years ago (diff)

more script code refactoring from ultima in http://chdk.setepontos.com/index.php?topic=5793.msg59228#msg59228
minor modifications from the patch - didn't include ubasic error change, include lstate.h only in luascript.c

  • Property svn:eol-style set to native
Line 
1#include "luascript.h"
2#include "../lib/ubasic/camera_functions.h"
3#include "kbd.h"
4#include "platform.h"
5#include "script.h"
6#include "lua.h"
7#include "lualib.h"
8#include "lauxlib.h"
9#include "conf.h"
10#include "shot_histogram.h"
11#include "ubasic.h"
12#include "stdlib.h"
13#include "raw.h"
14#include "raw_merge.h"
15#include "levent.h"
16#include "console.h"
17#include "action_stack.h"
18
19#include "../lib/lua/lstate.h"  // for L->nCcalls, baseCcalls
20
21lua_State* L;
22lua_State* Lt;
23int lua_keep_result;
24
25void *lua_consume_result()
26{
27  lua_State* r = L;
28  L = 0;
29  return r;
30}
31
32void lua_script_reset()
33{
34  if ( !lua_keep_result )
35  {
36    lua_close( L );
37    L = 0;
38  }
39  Lt = 0;
40}
41
42static void lua_count_hook(lua_State *L, lua_Debug *ar)
43{
44  if( L->nCcalls <= L->baseCcalls )
45    lua_yield( L, 0 );
46}
47
48int lua_script_start( char const* script )
49{
50  lua_keep_result = 0;
51  L = lua_open();
52  luaL_openlibs( L );
53  register_lua_funcs( L );
54
55  Lt = lua_newthread( L );
56  lua_setfield( L, LUA_REGISTRYINDEX, "Lt" );
57  if( luaL_loadstring( Lt, script ) != 0 ) {
58    script_console_add_line( lua_tostring( Lt, -1 ) );
59    lua_script_reset();
60    return 0;
61  }
62  lua_sethook(Lt, lua_count_hook, LUA_MASKCOUNT, 1000 );
63  return 1;
64}
65
66#ifdef OPT_CURVES
67#include "curves.h"
68
69static int luaCB_set_curve_state( lua_State* L )
70{
71  int value;
72  value=luaL_checknumber( L, 1 );
73  curve_set_mode(value);
74  return 0;
75}
76#endif
77
78static int luaCB_set_aflock(lua_State* L)
79{
80  int val = luaL_checknumber(L, 1);
81  if (val>0) DoAFLock();  // 1: enable AFLock
82  else UnlockAF();       // 0: disable unlock AF
83  return 0;
84}
85
86
87static int luaCB_shoot( lua_State* L )
88{
89  action_push(AS_SHOOT);
90  return lua_yield( L, 0 );
91}
92
93static int luaCB_sleep( lua_State* L )
94{
95  action_push_delay( luaL_checknumber( L, 1 ) );
96  return lua_yield( L, 0 );
97}
98
99// for press,release and click
100static int luaCB_keyfunc( lua_State* L )
101{
102  long k = keyid_by_name( luaL_checkstring( L, 1 ) );
103  if (k > 0 ) {
104    void* func = lua_touserdata( L, lua_upvalueindex(1) );
105    ((void(*)(long))func)( k );
106  }
107  else
108    luaL_error( L, "unknown key" );
109  return lua_yield( L, 0 );
110}
111
112static int luaCB_cls( lua_State* L )
113{
114  console_clear();
115  return 0;
116}
117
118static int luaCB_set_console_layout( lua_State* L )
119{
120  console_set_layout(luaL_checknumber( L, 1 ),luaL_checknumber( L, 2 ),luaL_checknumber( L, 3 ),luaL_checknumber( L, 4 ));
121  return 0;
122}
123
124static int luaCB_set_console_autoredraw( lua_State* L )
125{
126  console_set_autoredraw(luaL_checknumber(L,1));
127  return 0;
128}
129
130static int luaCB_console_redraw( lua_State* L )
131{
132  console_redraw();
133  return 0;
134}
135static int luaCB_get_av96( lua_State* L )
136{
137  lua_pushnumber( L, shooting_get_av96() );
138  return 1;
139}
140
141static int luaCB_get_bv96( lua_State* L )
142{
143  lua_pushnumber( L, shooting_get_bv96() );
144  return 1;
145}
146
147static int luaCB_get_day_seconds( lua_State* L )
148{
149  lua_pushnumber( L, shooting_get_day_seconds() );
150  return 1;
151}
152
153static int luaCB_get_disk_size( lua_State* L )
154{
155  lua_pushnumber( L, GetTotalCardSpaceKb() );
156  return 1;
157}
158
159static int luaCB_get_dof( lua_State* L )
160{
161  lua_pushnumber( L, shooting_get_depth_of_field() );
162  return 1;
163}
164
165static int luaCB_get_far_limit( lua_State* L )
166{
167  lua_pushnumber( L, shooting_get_far_limit_of_acceptable_sharpness() );
168  return 1;
169}
170
171static int luaCB_get_free_disk_space( lua_State* L )
172{
173  lua_pushnumber( L, GetFreeCardSpaceKb() );
174  return 1;
175}
176
177static int luaCB_get_focus( lua_State* L )
178{
179  lua_pushnumber( L, shooting_get_subject_distance() );
180  return 1;
181}
182
183static int luaCB_get_hyp_dist( lua_State* L )
184{
185  lua_pushnumber( L, shooting_get_hyperfocal_distance() );
186  return 1;
187}
188
189static int luaCB_get_iso_market( lua_State* L )
190{
191  lua_pushnumber( L, shooting_get_iso_market() );
192  return 1;
193}
194
195static int luaCB_get_iso_mode( lua_State* L )
196{
197  lua_pushnumber( L, shooting_get_iso_mode() );
198  return 1;
199}
200
201static int luaCB_get_iso_real( lua_State* L )
202{
203  lua_pushnumber( L, shooting_get_iso_real() );
204  return 1;
205}
206
207static int luaCB_get_jpg_count( lua_State* L )
208{
209  lua_pushnumber( L, GetJpgCount() );
210  return 1;
211}
212
213static int luaCB_get_near_limit( lua_State* L )
214{
215  lua_pushnumber( L, shooting_get_near_limit_of_acceptable_sharpness() );
216  return 1;
217}
218
219static int luaCB_get_prop( lua_State* L )
220{
221  lua_pushnumber( L, shooting_get_prop( luaL_checknumber( L, 1 ) ) );
222  return 1;
223}
224
225static int luaCB_get_raw_count( lua_State* L )
226{
227  lua_pushnumber( L, GetRawCount() );
228  return 1;
229}
230
231static int luaCB_get_sv96( lua_State* L )
232{
233  lua_pushnumber( L, shooting_get_sv96() );
234  return 1;
235}
236
237static int luaCB_get_tick_count( lua_State* L )
238{
239  lua_pushnumber( L, shooting_get_tick_count() );
240  return 1;
241}
242
243static int luaCB_get_exp_count( lua_State* L )
244{
245  lua_pushnumber( L, get_exposure_counter() );
246  return 1;
247}
248
249static int luaCB_get_tv96( lua_State* L )
250{
251  lua_pushnumber( L, shooting_get_tv96() );
252  return 1;
253}
254
255static int luaCB_get_user_av_id( lua_State* L )
256{
257  lua_pushnumber( L, shooting_get_user_av_id() );
258  return 1;
259}
260
261static int luaCB_get_user_av96( lua_State* L )
262{
263  lua_pushnumber( L, shooting_get_user_av96() );
264  return 1;
265}
266
267static int luaCB_get_user_tv_id( lua_State* L )
268{
269  lua_pushnumber( L, shooting_get_user_tv_id() );
270  return 1;
271}
272
273static int luaCB_get_user_tv96( lua_State* L )
274{
275  lua_pushnumber( L, shooting_get_user_tv96() );
276  return 1;
277}
278
279static int luaCB_get_vbatt( lua_State* L )
280{
281  lua_pushnumber( L, stat_get_vbatt() );
282  return 1;
283}
284
285static int luaCB_get_zoom( lua_State* L )
286{
287  lua_pushnumber( L, shooting_get_zoom() );
288  return 1;
289}
290
291static int luaCB_get_parameter_data( lua_State* L )
292{
293  extern long* FlashParamsTable[];
294
295  unsigned size;
296  unsigned id = luaL_checknumber( L, 1 );
297  unsigned val;
298
299  if (id >= get_flash_params_count()) {
300    // return nil
301    return 0;
302  }
303
304  size = FlashParamsTable[id][1]>>16;
305  if (size == 0) {
306    // return nil
307    return 0;
308  }
309  if (size >= 1 && size <= 4) {
310    val = 0;
311    get_parameter_data( id, &val, size );
312    lua_pushlstring( L, (char *)&val, size );
313    // for convenience, params that fit in a number are returned in one as a second result
314    lua_pushnumber( L, val );
315    return 2;
316  }
317  else {
318    char *buf = malloc(size);
319    if(!buf) {
320      luaL_error( L, "malloc failed in luaCB_get_parameter_data" );
321    }
322    get_parameter_data( id, buf, size );
323    lua_pushlstring( L, buf, size );
324    free(buf);
325    return 1;
326  }
327}
328
329static int luaCB_get_flash_params_count( lua_State* L )
330{
331  lua_pushnumber( L, get_flash_params_count() );
332  return 1;
333}
334
335static int luaCB_set_av96_direct( lua_State* L )
336{
337  shooting_set_av96_direct( luaL_checknumber( L, 1 ), SET_LATER );
338  return 0;
339}
340
341static int luaCB_set_av96( lua_State* L )
342{
343  shooting_set_av96( luaL_checknumber( L, 1 ), SET_LATER );
344  return 0;
345}
346
347static int luaCB_set_focus( lua_State* L )
348{
349    int to = luaL_checknumber( L, 1 );
350    int m=mode_get()&MODE_SHOOTING_MASK;
351    int mode_video=MODE_IS_VIDEO(m);
352
353#if CAM_HAS_MANUAL_FOCUS
354    if (shooting_get_focus_mode() || (mode_video)) shooting_set_focus(to, SET_NOW);
355    else shooting_set_focus(to, SET_LATER);
356#else
357    if (mode_video) shooting_set_focus(to, SET_NOW);
358    else shooting_set_focus(to, SET_LATER);   
359#endif   
360  return 0;
361}
362
363static int luaCB_set_iso_mode( lua_State* L )
364{
365  shooting_set_iso_mode( luaL_checknumber( L, 1 ) );
366  return 0;
367}
368
369static int luaCB_set_iso_real( lua_State* L )
370{
371  shooting_set_iso_real( luaL_checknumber( L, 1 ), SET_LATER);
372  return 0;
373}
374
375static int luaCB_set_led( lua_State* L )
376{
377  int to, to1, to2;
378  to = luaL_checknumber( L, 1 );
379  to1 = luaL_checknumber( L, 2 );
380  to2 = 200;
381  if( lua_isnumber( L, 3 ) )
382    to2 = lua_tonumber( L, 3 );
383  camera_set_led(to, to1, to2);
384  return 0;
385}
386
387static int luaCB_set_nd_filter( lua_State* L )
388{
389  shooting_set_nd_filter_state( luaL_checknumber( L, 1 ), SET_LATER);
390  return 0;
391}
392
393static int luaCB_set_prop( lua_State* L )
394{
395  int to, to1;
396  to = luaL_checknumber( L, 1 );
397  to1 = luaL_checknumber( L, 2 );
398  shooting_set_prop(to, to1);
399  return 0;
400}
401
402static int luaCB_set_raw_nr( lua_State* L )
403{
404  camera_set_nr(luaL_checknumber( L, 1 ));
405  return 0;
406}
407
408static int luaCB_get_raw_nr( lua_State* L )
409{
410  lua_pushnumber( L, camera_get_nr() );
411  return 1;
412}
413
414static int luaCB_set_raw( lua_State* L )
415{
416  camera_set_raw(luaL_checknumber( L, 1 ));
417  return 0;
418}
419
420static int luaCB_get_raw( lua_State* L )
421{
422  lua_pushnumber( L, conf.save_raw );
423  return 1;
424}
425
426static int luaCB_set_sv96( lua_State* L )
427{
428  shooting_set_sv96(luaL_checknumber( L, 1 ), SET_LATER);
429  return 0;
430}
431
432static int luaCB_set_tv96_direct( lua_State* L )
433{
434  shooting_set_tv96_direct(luaL_checknumber( L, 1 ), SET_LATER);
435  return 0;
436}
437
438static int luaCB_set_tv96( lua_State* L )
439{
440  shooting_set_tv96(luaL_checknumber( L, 1 ), SET_LATER);
441  return 0;
442}
443
444static int luaCB_set_user_av_by_id_rel( lua_State* L )
445{
446  shooting_set_user_av_by_id_rel(luaL_checknumber( L, 1 ));
447  return 0;
448}
449
450static int luaCB_set_user_av_by_id( lua_State* L )
451{
452  shooting_set_user_av_by_id(luaL_checknumber( L, 1 ));
453  return 0;
454}
455
456static int luaCB_set_user_av96( lua_State* L )
457{
458  shooting_set_user_av96(luaL_checknumber( L, 1 ));
459  return 0;
460}
461
462static int luaCB_set_user_tv_by_id_rel( lua_State* L )
463{
464  shooting_set_user_tv_by_id_rel(luaL_checknumber( L, 1 ));
465  return 0;
466}
467
468static int luaCB_set_user_tv_by_id( lua_State* L )
469{
470  shooting_set_user_tv_by_id(luaL_checknumber( L, 1 ));
471  return 0;
472}
473
474static int luaCB_set_user_tv96( lua_State* L )
475{
476  shooting_set_user_tv96(luaL_checknumber( L, 1 ));
477  return 0;
478}
479
480static int luaCB_set_zoom_speed( lua_State* L )
481{
482  shooting_set_zoom_speed(luaL_checknumber( L, 1 ));
483  return 0;
484}
485
486static int luaCB_set_zoom_rel( lua_State* L )
487{
488  shooting_set_zoom_rel(luaL_checknumber( L, 1 ));
489  return 0;
490}
491
492static int luaCB_set_zoom( lua_State* L )
493{
494  shooting_set_zoom(luaL_checknumber( L, 1 ));
495  return 0;
496}
497
498static int luaCB_wait_click( lua_State* L )
499{
500  int timeout = luaL_optnumber( L, 1, 0 );
501  camera_wait_click(timeout);
502  return lua_yield( L, 0 );
503}
504
505static int luaCB_is_pressed( lua_State* L )
506{
507  lua_pushboolean( L, camera_is_pressed(luaL_checkstring( L, 1 )));
508  return 1;
509}
510
511static int luaCB_is_key( lua_State* L )
512{
513  lua_pushboolean( L, camera_is_clicked(luaL_checkstring( L, 1 )));
514  return 1;
515}
516
517#if CAM_HAS_JOGDIAL
518static int luaCB_wheel_right( lua_State* L )
519{
520  JogDial_CW();
521  return 0;
522}
523
524static int luaCB_wheel_left( lua_State* L )
525{
526  JogDial_CCW();
527  return 0;
528}
529#endif
530
531static int luaCB_md_get_cell_diff( lua_State* L )
532{
533  lua_pushnumber( L, md_get_cell_diff(luaL_checknumber(L,1),
534                                      luaL_checknumber(L,2)));
535  return 1;
536}
537
538static int luaCB_md_detect_motion( lua_State* L )
539{
540  int columns = (luaL_optnumber(L,1,6));
541  int rows = (luaL_optnumber(L,2,4));
542  int pixel_measure_mode = (luaL_optnumber(L,3,1));
543  int detection_timeout = (luaL_optnumber(L,4,10000));
544  int measure_interval = (luaL_optnumber(L,5,7));
545  int threshold = (luaL_optnumber(L,6,10));
546  int draw_grid = (luaL_optnumber(L,7,1));
547   // arg 8 is the return value in ubasic. We
548   // ignore it here. - AUJ
549  int clipping_region_mode = (luaL_optnumber(L,9,0));
550  int clipping_region_column1 = (luaL_optnumber(L,10,0));
551  int clipping_region_row1 = (luaL_optnumber(L,11,0));
552  int clipping_region_column2 = (luaL_optnumber(L,12,0));
553  int clipping_region_row2 = (luaL_optnumber(L,13,0));
554  int parameters = (luaL_optnumber(L,14,1));
555  int pixels_step = (luaL_optnumber(L,15,6));
556  int msecs_before_trigger = (luaL_optnumber(L,16,0));
557  ubasic_set_variable(0, 0);
558  if(md_init_motion_detector(
559    columns, rows, pixel_measure_mode, detection_timeout,
560    measure_interval, threshold, draw_grid, 0,
561    clipping_region_mode,
562    clipping_region_column1, clipping_region_row1,
563    clipping_region_column2, clipping_region_row2,
564    parameters, pixels_step, msecs_before_trigger
565  ))
566    return lua_yield(L, 0);
567  else
568    return luaL_error( L, "md_init_motion_detector failed" );
569}
570
571static int luaCB_autostarted( lua_State* L )
572{
573  lua_pushboolean( L, camera_get_script_autostart() );
574  return 1;
575}
576
577static int luaCB_get_autostart( lua_State* L )
578{
579  lua_pushnumber( L, conf.script_startup );
580  return 1;
581}
582
583static int luaCB_set_autostart( lua_State* L )
584{
585  int to;
586  to = luaL_checknumber( L, 1 );
587  if ( to >= 0 && to <= 2 ) conf.script_startup = to;
588  conf_save();
589  return 0;
590}
591
592static int luaCB_get_usb_power( lua_State* L )
593{
594  if (luaL_optnumber( L, 1, 0 )) lua_pushnumber( L, get_usb_power(1) );
595  else lua_pushnumber( L, get_usb_power(0) );
596  return 1;
597}
598
599static int luaCB_exit_alt( lua_State* L )
600{
601  exit_alt();
602  return 0;
603}
604
605// optional parameter is 0 for soft shutdown (default) or 1 for hard/immediate
606static int luaCB_shut_down( lua_State* L )
607{
608  if ( luaL_optnumber(L,1,0) == 1 )
609  {
610    shutdown();
611  } else {
612  camera_shutdown_in_a_second();
613  }
614  return 0;
615}
616
617static int luaCB_print_screen( lua_State* L )
618{
619 
620  if (lua_isboolean( L, 1 ))
621    script_print_screen_statement( lua_toboolean( L, 1 ) );
622  else
623    script_print_screen_statement( luaL_checknumber( L, 1 )+10000 );
624  return 0;
625}
626
627static int luaCB_get_movie_status( lua_State* L )
628{
629  lua_pushnumber( L, movie_status );
630  return 1;
631}
632
633static int luaCB_set_movie_status( lua_State* L )
634{
635  int to;
636  switch(luaL_checknumber( L, 1 )) {
637    case 1:
638      if (movie_status == 4) {
639        movie_status = 1;
640      }
641    break;
642    case 2:
643      if (movie_status == 1) {
644        movie_status = 4;
645      }
646    break;
647    case 3:
648      if (movie_status == 1 || movie_status == 4) {
649        movie_status = 5;
650      }
651    break;
652  }
653  return 0;
654}
655
656static int luaCB_get_drive_mode( lua_State* L )
657{
658  lua_pushnumber( L, shooting_get_drive_mode() );
659  return 1;
660}
661
662static int luaCB_get_focus_mode( lua_State* L )
663{
664  lua_pushnumber( L, shooting_get_prop(PROPCASE_FOCUS_MODE) );
665  return 1;
666}
667
668static int luaCB_get_flash_mode( lua_State* L )
669{
670  lua_pushnumber( L, shooting_get_prop(PROPCASE_FLASH_MODE) );
671  return 1;
672}
673
674static int luaCB_get_shooting( lua_State* L )
675{
676  lua_pushboolean( L, shooting_get_prop(PROPCASE_SHOOTING) );
677  return 1;
678}
679
680static int luaCB_get_flash_ready( lua_State* L )
681{
682  lua_pushboolean( L, shooting_get_prop(PROPCASE_IS_FLASH_READY) );
683  return 1;
684}
685
686static int luaCB_get_IS_mode( lua_State* L )
687{
688  lua_pushnumber( L, shooting_get_prop(PROPCASE_IS_MODE) );
689  return 1;
690}
691
692static int luaCB_get_orientation_sensor( lua_State* L )
693{
694  lua_pushnumber( L, shooting_get_prop(PROPCASE_ORIENTATION_SENSOR) );
695  return 1;
696}
697
698static int luaCB_get_zoom_steps( lua_State* L )
699{
700  lua_pushnumber( L, zoom_points );
701  return 1;
702}
703
704static int luaCB_get_nd_present( lua_State* L )
705{
706  int to;
707  #if !CAM_HAS_ND_FILTER
708  to = 0;
709  #endif
710  #if CAM_HAS_ND_FILTER && !CAM_HAS_IRIS_DIAPHRAGM
711  to = 1;
712  #endif
713  #if CAM_HAS_ND_FILTER && CAM_HAS_IRIS_DIAPHRAGM
714  to = 2;
715  #endif
716  lua_pushnumber( L, to );
717  return 1;
718}
719
720static int luaCB_get_propset( lua_State* L )
721{
722  lua_pushnumber( L, CAM_PROPSET );
723  return 1;
724}
725
726static int luaCB_get_ev( lua_State* L )
727{
728  lua_pushnumber( L, shooting_get_prop(PROPCASE_EV_CORRECTION_1) );
729  return 1;
730}
731
732static int luaCB_set_ev( lua_State* L )
733{
734  int to;
735  to = luaL_checknumber( L, 1 );
736  shooting_set_prop(PROPCASE_EV_CORRECTION_1, to);
737  shooting_set_prop(PROPCASE_EV_CORRECTION_2, to);
738  return 0;
739}
740
741static int luaCB_get_histo_range( lua_State* L )
742{
743  int from = (luaL_checknumber(L,1));
744  int to = (luaL_checknumber(L,2));
745  if (shot_histogram_isenabled()) lua_pushnumber( L, shot_histogram_get_range(from, to) );
746  else lua_pushnumber( L, -1 ); // TODO should probably return nil
747  return 1;
748}
749
750static int luaCB_shot_histo_enable( lua_State* L )
751{
752  shot_histogram_set(luaL_checknumber( L, 1 ));
753  return 0;
754}
755
756static int luaCB_play_sound( lua_State* L )
757{
758  play_sound(luaL_checknumber( L, 1 ));
759  return 0;
760}
761
762static int luaCB_get_temperature( lua_State* L )
763{
764  int which = (luaL_checknumber( L, 1 ));
765  int temp = -100; // do something insane if users passes bad value
766  switch (which)
767  {
768    case 0:
769      temp = get_optical_temp();
770      break;
771    case 1:
772      temp = get_ccd_temp();
773      break;
774    case 2:
775      temp = get_battery_temp();
776      break;
777  }
778  lua_pushnumber( L, temp );
779  return 1;
780}
781
782static int luaCB_get_time( lua_State* L )
783{
784  int r = -1;
785  unsigned long t2 = time(NULL);
786  static struct tm *ttm;
787  ttm = localtime(&t2);
788  const char *t = luaL_checkstring( L, 1 );
789  if (strncmp("s", t, 1)==0) r = ( L, ttm->tm_sec );
790  else if (strncmp("m", t, 1)==0) r = ( L, ttm->tm_min );
791  else if (strncmp("h", t, 1)==0) r = ( L, ttm->tm_hour );
792  else if (strncmp("D", t, 1)==0) r = ( L, ttm->tm_mday );
793  else if (strncmp("M", t, 1)==0) r = ( L, ttm->tm_mon+1 );
794  else if (strncmp("Y", t, 1)==0) r = ( L, 1900+ttm->tm_year );
795  lua_pushnumber( L, r );
796  return 1;
797}
798
799/*
800  val=peek(address[,size])
801  return the value found at address in memory, or nil if address or size is invalid
802  size is optional 1=byte 2=halfword 4=word. defaults is 4
803*/
804static int luaCB_peek( lua_State* L )
805{
806  unsigned addr = luaL_checknumber(L,1);
807  unsigned size = luaL_optnumber(L, 2, 4);
808  switch(size) {
809    case 1:
810      lua_pushnumber( L, *(unsigned char *)(addr) );
811    break;
812    case 2:
813      if (addr & 0x1) {
814        lua_pushnil(L);
815      }
816      else {
817        lua_pushnumber( L, *(unsigned short *)(addr) );
818      }
819    break;
820    case 4:
821      if (addr & 0x3) {
822        lua_pushnil(L);
823      }
824      else {
825        lua_pushnumber( L, *(unsigned *)(addr) );
826      }
827    break;
828    default:
829      lua_pushnil(L);
830
831  }
832  return 1;
833}
834
835/*
836  status=poke(address,value[,size])
837  writes value to address in memory
838  size is optional 1=byte 2=halfword 4=word. defaults is 4
839  returns true, or nil if address or size is invalid
840*/
841static int luaCB_poke( lua_State* L )
842{
843  unsigned addr = luaL_checknumber(L,1);
844  unsigned val = luaL_checknumber(L,2);
845  unsigned size = luaL_optnumber(L, 3, 4);
846  int status = 0;
847  switch(size) {
848    case 1:
849        *(unsigned char *)(addr) = (unsigned char)val;
850        status=1;
851    break;
852    case 2:
853      if (!(addr & 0x1)) {
854        *(unsigned short *)(addr) = (unsigned short)val;
855        status=1;
856      }
857    break;
858    case 4:
859      if (!(addr & 0x3)) {
860        *(unsigned *)(addr) = val;
861        status=1;
862      }
863    break;
864  }
865  if(status) {
866    lua_pushboolean(L,1);
867  }
868  else {
869    lua_pushnil(L);
870  }
871  return 1;
872}
873
874static int luaCB_bitand( lua_State* L )
875{
876  int v1 = (luaL_checknumber(L,1));
877  int v2 = (luaL_checknumber(L,2));
878  lua_pushnumber( L, v1 & v2 );
879  return 1;
880}
881
882static int luaCB_bitor( lua_State* L )
883{
884  int v1 = (luaL_checknumber(L,1));
885  int v2 = (luaL_checknumber(L,2));
886  lua_pushnumber( L, v1 | v2 );
887  return 1;
888}
889
890static int luaCB_bitxor( lua_State* L )
891{
892  int v1 = (luaL_checknumber(L,1));
893  int v2 = (luaL_checknumber(L,2));
894  lua_pushnumber( L, v1 ^ v2 );
895  return 1;
896}
897
898static int luaCB_bitshl( lua_State* L )
899{
900  int val = (luaL_checknumber(L,1));
901  unsigned shift = (luaL_checknumber(L,2));
902  lua_pushnumber( L, val << shift );
903  return 1;
904}
905
906static int luaCB_bitshri( lua_State* L )
907{
908  int val = (luaL_checknumber(L,1));
909  unsigned shift = (luaL_checknumber(L,2));
910  lua_pushnumber( L, val >> shift );
911  return 1;
912}
913
914static int luaCB_bitshru( lua_State* L )
915{
916  unsigned val = (luaL_checknumber(L,1));
917  unsigned shift = (luaL_checknumber(L,2));
918  lua_pushnumber( L, val >> shift );
919  return 1;
920}
921
922static int luaCB_bitnot( lua_State* L )
923{
924  unsigned val = (luaL_checknumber(L,1));
925  lua_pushnumber( L, ~val );
926  return 1;
927}
928
929static void set_string_field(lua_State* L, const char *key, const char *val)
930{
931  lua_pushstring(L, val);
932  lua_setfield(L, -2, key);
933}
934
935static int luaCB_get_buildinfo( lua_State* L )
936{
937  lua_createtable(L, 0, 8);
938  set_string_field( L,"platform", PLATFORM );
939  set_string_field( L,"platsub", PLATFORMSUB );
940  set_string_field( L,"version", HDK_VERSION );
941  set_string_field( L,"build_number", BUILD_NUMBER );
942  set_string_field( L,"build_date", __DATE__ );
943  set_string_field( L,"build_time", __TIME__ );
944#ifndef CAM_DRYOS
945  set_string_field( L,"os", "vxworks" );
946#else
947  set_string_field( L,"os", "dryos" );
948#endif
949  lua_pushnumber( L, PLATFORMID );
950  lua_setfield(L, -2, "platformid");
951  return 1;
952}
953
954static int luaCB_get_mode( lua_State* L )
955{
956  int m = mode_get();
957  lua_pushboolean( L, (m&MODE_MASK) != MODE_PLAY );
958  lua_pushboolean( L, MODE_IS_VIDEO(m) );
959  lua_pushnumber( L, m );
960  return 3;
961}
962
963// TODO sanity check file ?
964static int luaCB_set_raw_develop( lua_State* L )
965{
966  raw_prepare_develop(luaL_optstring( L, 1, NULL ));
967  return 0;
968}
969
970static int luaCB_raw_merge_start( lua_State* L )
971{
972  int op = luaL_checknumber(L,1);
973  if (op == RAW_OPERATION_SUM || op == RAW_OPERATION_AVERAGE) {
974    raw_merge_start(op);
975  }
976  else {
977    return luaL_argerror(L,1,"invalid raw merge op");
978  }
979  return 0;
980}
981
982// TODO sanity check file ?
983static int luaCB_raw_merge_add_file( lua_State* L )
984{
985  raw_merge_add_file(luaL_checkstring( L, 1 ));
986  return 0;
987}
988
989static int luaCB_raw_merge_end( lua_State* L )
990{
991  raw_merge_end();
992  return 0;
993}
994
995// Enable/disable LCD back light (input argument 1/0)
996static int luaCB_set_backlight( lua_State* L )
997{
998  int val = (luaL_checknumber(L,1));
999
1000  if (val > 0) TurnOnBackLight();
1001  else TurnOffBackLight();
1002  return 0;
1003}
1004
1005// get the string or number passed in index and return it as an event id
1006static unsigned levent_id_from_lua_arg( lua_State* L, int index)
1007{
1008  unsigned event_id;
1009  if (lua_type(L, index) == LUA_TSTRING) {
1010    const char *ev_name = lua_tostring(L, index);
1011        event_id = levent_id_for_name(ev_name);
1012    if (event_id == 0) {
1013        return luaL_error( L, "bad event name '%s'", ev_name );
1014    }
1015  }
1016  // could check here if it is in the table, but even valid ones can crash
1017  // so we avoid searching the table if given a number
1018  else if (lua_type(L,index) == LUA_TNUMBER){
1019        event_id = lua_tonumber(L,index);
1020  }
1021  else {
1022    return luaL_error( L, "expected event name or id" );
1023  }
1024  return event_id;
1025}
1026
1027/*
1028  get a value where boolean or 0/!0 are accepted for on/off.
1029  normal lua toboolean will convert 0 to true, but ubasic and c users
1030  will expect 0 to be off
1031  intentional HACK: numbers greater than 1 are returned as is
1032*/
1033static unsigned on_off_value_from_lua_arg( lua_State* L, int index)
1034{
1035  if( lua_isboolean(L,index) ) {
1036        return lua_toboolean(L,index);
1037  }
1038  else {
1039        return luaL_checknumber(L,index);
1040  }
1041}
1042
1043/*
1044  return the index of an event, given it's name or event id
1045*/
1046static unsigned levent_index_from_id_lua_arg( lua_State* L, int index )
1047{
1048  if (lua_type(L, index) == LUA_TSTRING) {
1049        return levent_index_for_name(lua_tostring(L, index));
1050  }
1051  else if (lua_type(L,index) == LUA_TNUMBER){
1052        return levent_index_for_id(lua_tonumber(L,index));
1053  }
1054  else {
1055    return luaL_error( L, "expected string or number" );
1056  }
1057}
1058
1059/*
1060  name,id,param = get_levent_def(event)
1061  event is an event id (number) or name (string)
1062  returns nil if event is not found
1063*/
1064static int luaCB_get_levent_def( lua_State* L )
1065{
1066  unsigned event_index = levent_index_from_id_lua_arg(L,1);
1067  if (event_index == LEVENT_INVALID_INDEX) {
1068    lua_pushnil(L);
1069    return 1;
1070  }
1071  lua_pushstring(L, levent_table[event_index].name);
1072  lua_pushnumber(L, levent_table[event_index].id);
1073  lua_pushnumber(L, levent_table[event_index].param);
1074  return 3;
1075}
1076
1077/*
1078  index=get_levent_index(event)
1079  event is an event id (number) or name (string)
1080  returns index or nil if not found
1081*/
1082static int luaCB_get_levent_index( lua_State* L )
1083{
1084  unsigned event_index = levent_index_from_id_lua_arg(L,1);
1085  if (event_index == LEVENT_INVALID_INDEX) {
1086    lua_pushnil(L);
1087  }
1088  else {
1089    lua_pushnumber(L, event_index);
1090  }
1091  return 1;
1092}
1093
1094/*
1095  name,id,param = get_levent_def_by_index(event_index)
1096  event_index is number index into the event table
1097  returns nil if event is not found
1098*/
1099static int luaCB_get_levent_def_by_index( lua_State* L )
1100{
1101  unsigned i = luaL_checknumber(L,1);
1102  if(i >= levent_count()) {
1103        lua_pushnil(L);
1104    return 1;
1105  }
1106  lua_pushstring(L, levent_table[i].name);
1107  lua_pushnumber(L, levent_table[i].id);
1108  lua_pushnumber(L, levent_table[i].param);
1109  return 3;
1110}
1111
1112/*
1113  post_levent_*(event[,unk])
1114  post the event with PostLogicalEventToUI or PostLogicaEventForNotPowerType
1115  This sends the event. The difference between functions isn't clear.
1116  event is an event id (number) or name (string).
1117  unk is an optional number whose meaning is unknown, defaults to zero.
1118    Based on code, other values would probably be a pointer.
1119        This is NOT the 3rd item in the event table.
1120*/
1121static int luaCB_post_levent_to_ui( lua_State* L )
1122{
1123  unsigned event_id,arg;
1124
1125  event_id = levent_id_from_lua_arg(L,1);
1126  arg = luaL_optnumber(L, 2, 0);
1127  PostLogicalEventToUI(event_id,arg);
1128  return 0;
1129}
1130
1131static int luaCB_post_levent_for_npt( lua_State* L )
1132{
1133  unsigned event_id,arg;
1134
1135  event_id = levent_id_from_lua_arg(L,1);
1136  arg = luaL_optnumber(L, 2, 0);
1137  PostLogicalEventForNotPowerType(event_id,arg);
1138  return 0;
1139}
1140
1141/*
1142  set_levent_active(event,state)
1143  event is an event id (number) or name (string)
1144  state is a numeric or boolean state. true or non zero numbers turn on zero, false or nil turn off
1145  exact meaning is unknown, but it has something to do with the delivery of the specified event.
1146*/
1147static int luaCB_set_levent_active( lua_State* L )
1148{
1149  unsigned event_id;
1150  unsigned state;
1151
1152  event_id = levent_id_from_lua_arg(L,1);
1153  state = on_off_value_from_lua_arg(L,2);
1154  SetLogicalEventActive(event_id,state);
1155  return 0;
1156}
1157
1158/*
1159  set_levent_script_mode(state)
1160  state is numeric or boolean state. true or non zero numbers turn on zero, false or nil turn off
1161  exact meaning is unknown, but it has something to do with the behavior of events and/or SetLogicalEventActive.
1162*/
1163static int luaCB_set_levent_script_mode( lua_State* L )
1164{
1165  SetScriptMode(on_off_value_from_lua_arg(L,1));
1166  return 0;
1167}
1168
1169/*
1170  result=set_capture_mode_canon(value)
1171  where value is a valid PROPCASE_SHOOTING_MODE value for the current camera
1172  result is true if the camera is in rec mode
1173*/
1174static int luaCB_set_capture_mode_canon( lua_State* L )
1175{
1176  int modenum = luaL_checknumber(L,1);
1177  // if the value as negative, assume it is a mistakenly sign extended PROPCASE_SHOOTING_MODE value
1178  if(modenum < 0)
1179    modenum &= 0xFFFF;
1180  lua_pushboolean( L, shooting_set_mode_canon(modenum) );
1181  return 1;
1182}
1183
1184/*
1185 result=set_capture_mode(modenum)
1186 where modenum is a valid CHDK modemap value
1187 result is true if modenum is a valid modemap value, otherwise false
1188*/
1189static int luaCB_set_capture_mode( lua_State* L )
1190{
1191  int modenum = luaL_checknumber(L,1);
1192  lua_pushboolean( L, shooting_set_mode_chdk(modenum) );
1193  return 1;
1194}
1195
1196/*
1197 result=is_capture_mode_valid(modenum)
1198 where modenum is a valid CHDK modemap value
1199 result is true if modenum is a valid modemap value, otherwise false
1200*/
1201static int luaCB_is_capture_mode_valid( lua_State* L )
1202{
1203  int modenum = luaL_checknumber(L,1);
1204  lua_pushboolean( L, shooting_mode_chdk2canon(modenum) != -1 );
1205  return 1;
1206}
1207
1208/*
1209  set_record(state)
1210  if state is 0 (or false) the camera is set to play mode. If 1 or true, the camera is set to record mode.
1211  NOTE: this only begins the mode change. Script should wait until get_mode() reflects the change,
1212  before doing anything that requires the new mode. e.g.
1213  set_record(true)
1214  while not get_mode() do
1215        sleep(10)
1216  end
1217*/
1218static int luaCB_set_record( lua_State* L )
1219{
1220  if(on_off_value_from_lua_arg(L,1)) {
1221    levent_set_record();
1222  }
1223  else {
1224    levent_set_play();
1225  }
1226  return 0;
1227}
1228
1229// switch mode (0 = playback, 1 = record)
1230// only for when USB is connected
1231static int luaCB_switch_mode_usb( lua_State* L )
1232{
1233  int mode = luaL_checknumber(L,1);
1234
1235  if ( mode != 0 && mode != 1 )
1236  {
1237    return 0;
1238  }
1239
1240  return switch_mode_usb(mode);
1241}
1242
1243/*
1244pack the lua args into a buffer to pass to the native code calling functions
1245currently only handles strings/numbers
1246start is the stack index of the first arg
1247*/
1248#ifdef OPT_LUA_CALL_NATIVE
1249static int pack_native_args( lua_State* L, unsigned start, unsigned *argbuf)
1250{
1251  unsigned i;
1252  unsigned end = lua_gettop(L);
1253
1254  for(i = start; i <= end; i++,argbuf++) {
1255    if (lua_type(L, i) == LUA_TSTRING) {
1256        *argbuf=(unsigned)lua_tostring( L, i);
1257    }
1258    else if (lua_type(L, i) == LUA_TNUMBER) {
1259        *argbuf=lua_tonumber( L, i);
1260    }
1261    else {
1262      return 0;
1263    }
1264  }
1265  return 1;
1266}
1267
1268/*
1269Native function call interface. Can be used to call canon eventprocs or arbitrary
1270pointers.
1271
1272NOTE: this is preliminary, interface may change in later versions!
1273All arguments must be strings or numbers.
1274If the function expects to modify it's arguments via a pointer,
1275then you must provide a number that is a valid pointer.
1276
1277You can use the "AllocateMemory" eventproc to obtain buffers.
1278
1279If the function tries to write to a string passed from lua, Bad Things may happen.
1280
1281This is potentially dangerous, functions exist which can destroy the onboard firmware.
1282*/
1283
1284/*
1285result=call_func_ptr(ptr,...)
1286ptr: address of a valid ARM or Thumb function, which uses the normal C calling convention.
1287result: R0 value after the call returns
1288*/
1289static int luaCB_call_func_ptr( lua_State* L)
1290{
1291  unsigned *argbuf=NULL;
1292  unsigned i;
1293  unsigned n_args = lua_gettop(L)-1;
1294  void *fptr;
1295
1296  fptr=(void *)luaL_checknumber( L, 1 );
1297
1298  if (n_args) {
1299    argbuf=malloc(n_args * 4);
1300    if(!argbuf) {
1301      return luaL_error( L, "malloc fail" );
1302    }
1303    if(!pack_native_args(L, 2, argbuf)) {
1304      free(argbuf);
1305      return luaL_error( L, "expected string or number" );
1306    }
1307  }
1308 
1309  lua_pushnumber( L, call_func_ptr(fptr, argbuf, n_args) );
1310  free(argbuf);
1311  return 1;
1312}
1313
1314/*
1315Call an event procedure
1316
1317result=call_event_proc("EventprocName",...)
1318result is the value returned by ExecuteEventProcedure, which is -1 if the eventproc is not found,
1319or the eventproc return value (which could also be -1)
1320NOTE:
1321Many eventprocs are not registered by default, but can be loaded by calling another event proc
1322Some useful ones are
1323SystemEventInit
1324        includes AllocateMemory, FreeMemory, sprintf, memcpy, Fut functions, log ...
1325UI_RegistDebugEventProc
1326        includes capture mode functions, PTM_ functions and much more
1327RegisterProductTestEvent
1328        includes PT_ functions
1329
1330Others:
1331RegisterShootSeqEvent
1332RegisterNRTableEvent
1333*/
1334
1335// grab from lowlevel
1336extern unsigned _ExecuteEventProcedure(const char *name,...);
1337static int luaCB_call_event_proc( lua_State* L )
1338{
1339  const char *evpname;
1340  unsigned *argbuf;
1341  unsigned i;
1342  unsigned n_args = lua_gettop(L);
1343
1344  evpname=luaL_checkstring( L, 1 );
1345
1346  argbuf=malloc(n_args * 4);
1347  if (!argbuf) {
1348    return luaL_error( L, "malloc fail" );
1349  }
1350
1351  // event proc name is first arg
1352  *argbuf = (unsigned)evpname;
1353 
1354  if(!pack_native_args(L,2,argbuf+1)) {
1355    free(argbuf);
1356    return luaL_error( L, "expected string or number" );
1357  }
1358 
1359  lua_pushnumber( L, call_func_ptr(_ExecuteEventProcedure,argbuf,n_args) );
1360  free(argbuf);
1361  return 1;
1362}
1363
1364#endif // OPT_LUA_CALL_NATIVE
1365
1366/*
1367result = reboot(["filename"])
1368returns false on failure, does not return on success
1369see lib/armutil/reboot.c for details
1370*/
1371static int luaCB_reboot( lua_State* L )
1372{
1373        lua_pushboolean(L, reboot(luaL_optstring( L, 1, NULL )));
1374        return 1;
1375}
1376
1377void register_lua_funcs( lua_State* L )
1378{
1379#define FUNC( X )                       \
1380  lua_pushcfunction( L, luaCB_##X );    \
1381  lua_setglobal( L, #X )
1382
1383  FUNC(shoot);
1384  FUNC(sleep);
1385  FUNC(cls);
1386  FUNC(set_console_layout);
1387  FUNC(set_console_autoredraw);
1388  FUNC(console_redraw);
1389
1390  lua_pushlightuserdata( L, action_push_click );
1391  lua_pushcclosure( L, luaCB_keyfunc, 1 );
1392  lua_setglobal( L, "click" );
1393
1394  lua_pushlightuserdata( L, action_push_press );
1395  lua_pushcclosure( L, luaCB_keyfunc, 1 );
1396  lua_setglobal( L, "press" );
1397
1398  lua_pushlightuserdata( L, action_push_release );
1399  lua_pushcclosure( L, luaCB_keyfunc, 1 );
1400  lua_setglobal( L, "release" );
1401
1402  FUNC(get_av96);
1403  FUNC(get_av96);
1404  FUNC(get_bv96);
1405  FUNC(get_day_seconds);
1406  FUNC(get_disk_size);
1407  FUNC(get_dof);
1408  FUNC(get_far_limit);
1409  FUNC(get_free_disk_space);
1410  FUNC(get_focus);
1411  FUNC(get_hyp_dist);
1412  FUNC(get_iso_market);
1413  FUNC(get_iso_mode);
1414  FUNC(get_iso_real);
1415  FUNC(get_jpg_count);
1416  FUNC(get_near_limit);
1417  FUNC(get_prop);
1418  FUNC(get_raw_count);
1419  FUNC(get_raw_nr);
1420  FUNC(get_raw);
1421  FUNC(get_sv96);
1422  FUNC(get_tick_count);
1423  FUNC(get_tv96);
1424  FUNC(get_user_av_id);
1425  FUNC(get_user_av96);
1426  FUNC(get_user_tv_id);
1427  FUNC(get_user_tv96);
1428  FUNC(get_vbatt);
1429  FUNC(get_zoom);
1430  FUNC(get_exp_count);
1431  FUNC(get_flash_params_count);
1432  FUNC(get_parameter_data);
1433
1434  FUNC(set_av96_direct);
1435  FUNC(set_av96);
1436  FUNC(set_focus);
1437  FUNC(set_iso_mode);
1438  FUNC(set_iso_real);
1439  FUNC(set_led);
1440  FUNC(set_nd_filter);
1441  FUNC(set_prop);
1442  FUNC(set_raw_nr);
1443  FUNC(set_raw);
1444  FUNC(set_sv96);
1445  FUNC(set_tv96_direct);
1446  FUNC(set_tv96);
1447  FUNC(set_user_av_by_id_rel);
1448  FUNC(set_user_av_by_id);
1449  FUNC(set_user_av96);
1450  FUNC(set_user_tv_by_id_rel);
1451  FUNC(set_user_tv_by_id);
1452  FUNC(set_user_tv96);
1453  FUNC(set_zoom_speed);
1454  FUNC(set_zoom_rel);
1455  FUNC(set_zoom);
1456
1457  FUNC(wait_click);
1458  FUNC(is_pressed);
1459  FUNC(is_key);
1460#ifdef CAM_HAS_JOGDIAL
1461  FUNC(wheel_right);
1462  FUNC(wheel_left);
1463#endif
1464  FUNC(md_get_cell_diff);
1465  FUNC(md_detect_motion);
1466  FUNC(autostarted);
1467  FUNC(get_autostart);
1468  FUNC(set_autostart);
1469  FUNC(get_usb_power);
1470  FUNC(exit_alt);
1471  FUNC(shut_down);
1472  FUNC(print_screen);
1473
1474  FUNC(get_focus_mode);
1475  FUNC(get_propset);
1476  FUNC(get_zoom_steps);
1477  FUNC(get_drive_mode);
1478  FUNC(get_flash_mode);
1479  FUNC(get_shooting);
1480  FUNC(get_flash_ready);
1481  FUNC(get_IS_mode);
1482  FUNC(set_ev);
1483  FUNC(get_ev);
1484  FUNC(get_orientation_sensor);
1485  FUNC(get_nd_present);
1486  FUNC(get_movie_status);
1487  FUNC(set_movie_status);
1488 
1489  FUNC(get_histo_range);
1490  FUNC(shot_histo_enable);
1491  FUNC(play_sound);
1492  FUNC(get_temperature);
1493  FUNC(peek);
1494  FUNC(poke);
1495  FUNC(bitand);
1496  FUNC(bitor);
1497  FUNC(bitxor);
1498  FUNC(bitshl);
1499  FUNC(bitshri);
1500  FUNC(bitshru);
1501  FUNC(bitnot);
1502
1503  FUNC(get_time);
1504
1505  FUNC(get_buildinfo);
1506  FUNC(get_mode);
1507 
1508  FUNC(set_raw_develop);
1509  // NOTE these functions normally run in the spytask.
1510  // called from lua they will run from kbd task instead
1511  FUNC(raw_merge_start);
1512  FUNC(raw_merge_add_file);
1513  FUNC(raw_merge_end);
1514  FUNC(set_backlight);
1515   FUNC(set_aflock);
1516#ifdef OPT_CURVES
1517   FUNC(set_curve_state);
1518#endif
1519// get levent definition by name or id, nil if not found
1520   FUNC(get_levent_def);
1521// get levent definition by index, nil if out of range
1522   FUNC(get_levent_def_by_index);
1523// get levent index from name or ID
1524   FUNC(get_levent_index);
1525   FUNC(post_levent_to_ui);
1526   FUNC(post_levent_for_npt);
1527   FUNC(set_levent_active);
1528   FUNC(set_levent_script_mode);
1529
1530   FUNC(set_capture_mode);
1531   FUNC(set_capture_mode_canon);
1532   FUNC(is_capture_mode_valid);
1533
1534   FUNC(set_record);
1535
1536   FUNC(switch_mode_usb);
1537
1538#ifdef OPT_LUA_CALL_NATIVE
1539   FUNC(call_event_proc);
1540   FUNC(call_func_ptr);
1541#endif
1542   FUNC(reboot);
1543}
Note: See TracBrowser for help on using the repository browser.