source: trunk/core/luascript.c @ 957

Revision 957, 33.2 KB checked in by reyalp, 3 years ago (diff)

ptp support based on mweerden's proposal in http://chdk.setepontos.com/index.php/topic,4338.msg52984.html#msg52984
this is currently disabled by default. See http://chdk.setepontos.com/index.php/topic,4338.msg55942.html#msg55942 for additional information

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