source: trunk/core/luascript.c @ 1034

Revision 1034, 34.2 KB checked in by reyalP, 2 years ago (diff)

Thanks to ultimaA, Lua and ubasic optional at compile time, from http://chdk.setepontos.com/index.php?topic=5793.msg59376#msg59376 with minor modifications: http://chdk.setepontos.com/index.php?topic=5793.msg59606#msg59606

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