source: trunk/core/luascript.c @ 944

Revision 944, 32.8 KB checked in by reyalp, 3 years ago (diff)

experimental reboot support. See http://chdk.setepontos.com/index.php/topic,5648.msg55194.html#msg55194

  • 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
556static int luaCB_shut_down( lua_State* L )
557{
558  camera_shutdown_in_a_second();
559  return 0;
560}
561
562static int luaCB_print_screen( lua_State* L )
563{
564 
565  if (lua_isboolean( L, 1 ))
566    script_print_screen_statement( lua_toboolean( L, 1 ) );
567  else
568    script_print_screen_statement( luaL_checknumber( L, 1 )+10000 );
569  return 0;
570}
571
572static int luaCB_get_movie_status( lua_State* L )
573{
574  lua_pushnumber( L, movie_status );
575  return 1;
576}
577
578static int luaCB_set_movie_status( lua_State* L )
579{
580  int to;
581  switch(luaL_checknumber( L, 1 )) {
582    case 1:
583      if (movie_status == 4) {
584        movie_status = 1;
585      }
586    break;
587    case 2:
588      if (movie_status == 1) {
589        movie_status = 4;
590      }
591    break;
592    case 3:
593      if (movie_status == 1 || movie_status == 4) {
594        movie_status = 5;
595      }
596    break;
597  }
598  return 0;
599}
600
601static int luaCB_get_drive_mode( lua_State* L )
602{
603  lua_pushnumber( L, shooting_get_drive_mode() );
604  return 1;
605}
606
607static int luaCB_get_focus_mode( lua_State* L )
608{
609  lua_pushnumber( L, shooting_get_prop(PROPCASE_FOCUS_MODE) );
610  return 1;
611}
612
613static int luaCB_get_flash_mode( lua_State* L )
614{
615  lua_pushnumber( L, shooting_get_prop(PROPCASE_FLASH_MODE) );
616  return 1;
617}
618
619static int luaCB_get_shooting( lua_State* L )
620{
621  lua_pushboolean( L, shooting_get_prop(PROPCASE_SHOOTING) );
622  return 1;
623}
624
625static int luaCB_get_flash_ready( lua_State* L )
626{
627  lua_pushboolean( L, shooting_get_prop(PROPCASE_IS_FLASH_READY) );
628  return 1;
629}
630
631static int luaCB_get_IS_mode( lua_State* L )
632{
633  lua_pushnumber( L, shooting_get_prop(PROPCASE_IS_MODE) );
634  return 1;
635}
636
637static int luaCB_get_orientation_sensor( lua_State* L )
638{
639  lua_pushnumber( L, shooting_get_prop(PROPCASE_ORIENTATION_SENSOR) );
640  return 1;
641}
642
643static int luaCB_get_zoom_steps( lua_State* L )
644{
645  lua_pushnumber( L, zoom_points );
646  return 1;
647}
648
649static int luaCB_get_nd_present( lua_State* L )
650{
651  int to;
652  #if !CAM_HAS_ND_FILTER
653  to = 0;
654  #endif
655  #if CAM_HAS_ND_FILTER && !CAM_HAS_IRIS_DIAPHRAGM
656  to = 1;
657  #endif
658  #if CAM_HAS_ND_FILTER && CAM_HAS_IRIS_DIAPHRAGM
659  to = 2;
660  #endif
661  lua_pushnumber( L, to );
662  return 1;
663}
664
665static int luaCB_get_propset( lua_State* L )
666{
667  lua_pushnumber( L, CAM_PROPSET );
668  return 1;
669}
670
671static int luaCB_get_ev( lua_State* L )
672{
673  lua_pushnumber( L, shooting_get_prop(PROPCASE_EV_CORRECTION_1) );
674  return 1;
675}
676
677static int luaCB_set_ev( lua_State* L )
678{
679  int to;
680  to = luaL_checknumber( L, 1 );
681  shooting_set_prop(PROPCASE_EV_CORRECTION_1, to);
682  shooting_set_prop(PROPCASE_EV_CORRECTION_2, to);
683  return 0;
684}
685
686static int luaCB_get_histo_range( lua_State* L )
687{
688  int from = (luaL_checknumber(L,1));
689  int to = (luaL_checknumber(L,2));
690  if (shot_histogram_isenabled()) lua_pushnumber( L, shot_histogram_get_range(from, to) );
691  else lua_pushnumber( L, -1 ); // TODO should probably return nil
692  return 1;
693}
694
695static int luaCB_shot_histo_enable( lua_State* L )
696{
697  shot_histogram_set(luaL_checknumber( L, 1 ));
698  return 0;
699}
700
701static int luaCB_play_sound( lua_State* L )
702{
703  play_sound(luaL_checknumber( L, 1 ));
704  return 0;
705}
706
707static int luaCB_get_temperature( lua_State* L )
708{
709  int which = (luaL_checknumber( L, 1 ));
710  int temp = -100; // do something insane if users passes bad value
711  switch (which)
712  {
713    case 0:
714      temp = get_optical_temp();
715      break;
716    case 1:
717      temp = get_ccd_temp();
718      break;
719    case 2:
720      temp = get_battery_temp();
721      break;
722  }
723  lua_pushnumber( L, temp );
724  return 1;
725}
726
727static int luaCB_get_time( lua_State* L )
728{
729  int r = -1;
730  unsigned long t2 = time(NULL);
731  static struct tm *ttm;
732  ttm = localtime(&t2);
733  const char *t = luaL_checkstring( L, 1 );
734  if (strncmp("s", t, 1)==0) r = ( L, ttm->tm_sec );
735  else if (strncmp("m", t, 1)==0) r = ( L, ttm->tm_min );
736  else if (strncmp("h", t, 1)==0) r = ( L, ttm->tm_hour );
737  else if (strncmp("D", t, 1)==0) r = ( L, ttm->tm_mday );
738  else if (strncmp("M", t, 1)==0) r = ( L, ttm->tm_mon+1 );
739  else if (strncmp("Y", t, 1)==0) r = ( L, 1900+ttm->tm_year );
740  lua_pushnumber( L, r );
741  return 1;
742}
743
744/*
745  val=peek(address[,size])
746  return the value found at address in memory, or nil if address or size is invalid
747  size is optional 1=byte 2=halfword 4=word. defaults is 4
748*/
749static int luaCB_peek( lua_State* L )
750{
751  unsigned addr = luaL_checknumber(L,1);
752  unsigned size = luaL_optnumber(L, 2, 4);
753  switch(size) {
754    case 1:
755      lua_pushnumber( L, *(unsigned char *)(addr) );
756    break;
757    case 2:
758      if (addr & 0x1) {
759        lua_pushnil(L);
760      }
761      else {
762        lua_pushnumber( L, *(unsigned short *)(addr) );
763      }
764    break;
765    case 4:
766      if (addr & 0x3) {
767        lua_pushnil(L);
768      }
769      else {
770        lua_pushnumber( L, *(unsigned *)(addr) );
771      }
772    break;
773    default:
774      lua_pushnil(L);
775
776  }
777  return 1;
778}
779
780/*
781  status=poke(address,value[,size])
782  writes value to address in memory
783  size is optional 1=byte 2=halfword 4=word. defaults is 4
784  returns true, or nil if address or size is invalid
785*/
786static int luaCB_poke( lua_State* L )
787{
788  unsigned addr = luaL_checknumber(L,1);
789  unsigned val = luaL_checknumber(L,2);
790  unsigned size = luaL_optnumber(L, 3, 4);
791  int status = 0;
792  switch(size) {
793    case 1:
794        *(unsigned char *)(addr) = (unsigned char)val;
795        lua_pushboolean(L,1);
796        status=1;
797    break;
798    case 2:
799      if (!(addr & 0x1)) {
800        *(unsigned short *)(addr) = (unsigned short)val;
801        status=1;
802      }
803    break;
804    case 4:
805      if (!(addr & 0x3)) {
806        *(unsigned *)(addr) = val;
807        status=1;
808      }
809    break;
810  }
811  if(status) {
812    lua_pushboolean(L,1);
813  }
814  else {
815    lua_pushnil(L);
816  }
817  return 1;
818}
819
820static int luaCB_bitand( lua_State* L )
821{
822  int v1 = (luaL_checknumber(L,1));
823  int v2 = (luaL_checknumber(L,2));
824  lua_pushnumber( L, v1 & v2 );
825  return 1;
826}
827
828static int luaCB_bitor( lua_State* L )
829{
830  int v1 = (luaL_checknumber(L,1));
831  int v2 = (luaL_checknumber(L,2));
832  lua_pushnumber( L, v1 | v2 );
833  return 1;
834}
835
836static int luaCB_bitxor( lua_State* L )
837{
838  int v1 = (luaL_checknumber(L,1));
839  int v2 = (luaL_checknumber(L,2));
840  lua_pushnumber( L, v1 ^ v2 );
841  return 1;
842}
843
844static int luaCB_bitshl( lua_State* L )
845{
846  int val = (luaL_checknumber(L,1));
847  unsigned shift = (luaL_checknumber(L,2));
848  lua_pushnumber( L, val << shift );
849  return 1;
850}
851
852static int luaCB_bitshri( lua_State* L )
853{
854  int val = (luaL_checknumber(L,1));
855  unsigned shift = (luaL_checknumber(L,2));
856  lua_pushnumber( L, val >> shift );
857  return 1;
858}
859
860static int luaCB_bitshru( lua_State* L )
861{
862  unsigned val = (luaL_checknumber(L,1));
863  unsigned shift = (luaL_checknumber(L,2));
864  lua_pushnumber( L, val >> shift );
865  return 1;
866}
867
868static int luaCB_bitnot( lua_State* L )
869{
870  unsigned val = (luaL_checknumber(L,1));
871  lua_pushnumber( L, ~val );
872  return 1;
873}
874
875static void set_string_field(lua_State* L, const char *key, const char *val)
876{
877  lua_pushstring(L, val);
878  lua_setfield(L, -2, key);
879}
880
881static int luaCB_get_buildinfo( lua_State* L )
882{
883  lua_createtable(L, 0, 8);
884  set_string_field( L,"platform", PLATFORM );
885  set_string_field( L,"platsub", PLATFORMSUB );
886  set_string_field( L,"version", HDK_VERSION );
887  set_string_field( L,"build_number", BUILD_NUMBER );
888  set_string_field( L,"build_date", __DATE__ );
889  set_string_field( L,"build_time", __TIME__ );
890#ifndef CAM_DRYOS
891  set_string_field( L,"os", "vxworks" );
892#else
893  set_string_field( L,"os", "dryos" );
894#endif
895  lua_pushnumber( L, PLATFORMID );
896  lua_setfield(L, -2, "platformid");
897  return 1;
898}
899
900static int luaCB_get_mode( lua_State* L )
901{
902  int m = mode_get();
903  lua_pushboolean( L, (m&MODE_MASK) != MODE_PLAY );
904  lua_pushboolean( L, MODE_IS_VIDEO(m) );
905  lua_pushnumber( L, m );
906  return 3;
907}
908
909// TODO sanity check file ?
910static int luaCB_set_raw_develop( lua_State* L )
911{
912  raw_prepare_develop(luaL_optstring( L, 1, NULL ));
913  return 0;
914}
915
916static int luaCB_raw_merge_start( lua_State* L )
917{
918  int op = luaL_checknumber(L,1);
919  if (op == RAW_OPERATION_SUM || op == RAW_OPERATION_AVERAGE) {
920    raw_merge_start(op);
921  }
922  else {
923    return luaL_argerror(L,1,"invalid raw merge op");
924  }
925  return 0;
926}
927
928// TODO sanity check file ?
929static int luaCB_raw_merge_add_file( lua_State* L )
930{
931  raw_merge_add_file(luaL_checkstring( L, 1 ));
932  return 0;
933}
934
935static int luaCB_raw_merge_end( lua_State* L )
936{
937  raw_merge_end();
938  return 0;
939}
940
941// Enable/disable LCD back light (input argument 1/0)
942static int luaCB_set_backlight( lua_State* L )
943{
944  int val = (luaL_checknumber(L,1));
945
946  if (val > 0) TurnOnBackLight();
947  else TurnOffBackLight();
948  return 0;
949}
950
951// get the string or number passed in index and return it as an event id
952static unsigned levent_id_from_lua_arg( lua_State* L, int index)
953{
954  unsigned event_id;
955  if (lua_type(L, index) == LUA_TSTRING) {
956    const char *ev_name = lua_tostring(L, index);
957        event_id = levent_id_for_name(ev_name);
958    if (event_id == 0) {
959        return luaL_error( L, "bad event name '%s'", ev_name );
960    }
961  }
962  // could check here if it is in the table, but even valid ones can crash
963  // so we avoid searching the table if given a number
964  else if (lua_type(L,index) == LUA_TNUMBER){
965        event_id = lua_tonumber(L,index);
966  }
967  else {
968    return luaL_error( L, "expected event name or id" );
969  }
970  return event_id;
971}
972
973/*
974  get a value where boolean or 0/!0 are accepted for on/off.
975  normal lua toboolean will convert 0 to true, but ubasic and c users
976  will expect 0 to be off
977  intentional HACK: numbers greater than 1 are returned as is
978*/
979static unsigned on_off_value_from_lua_arg( lua_State* L, int index)
980{
981  if( lua_isboolean(L,index) ) {
982        return lua_toboolean(L,index);
983  }
984  else {
985        return luaL_checknumber(L,index);
986  }
987}
988
989/*
990  return the index of an event, given it's name or event id
991*/
992static unsigned levent_index_from_id_lua_arg( lua_State* L, int index )
993{
994  if (lua_type(L, index) == LUA_TSTRING) {
995        return levent_index_for_name(lua_tostring(L, index));
996  }
997  else if (lua_type(L,index) == LUA_TNUMBER){
998        return levent_index_for_id(lua_tonumber(L,index));
999  }
1000  else {
1001    return luaL_error( L, "expected string or number" );
1002  }
1003}
1004
1005/*
1006  name,id,param = get_levent_def(event)
1007  event is an event id (number) or name (string)
1008  returns nil if event is not found
1009*/
1010static int luaCB_get_levent_def( lua_State* L )
1011{
1012  unsigned event_index = levent_index_from_id_lua_arg(L,1);
1013  if (event_index == LEVENT_INVALID_INDEX) {
1014    lua_pushnil(L);
1015    return 1;
1016  }
1017  lua_pushstring(L, levent_table[event_index].name);
1018  lua_pushnumber(L, levent_table[event_index].id);
1019  lua_pushnumber(L, levent_table[event_index].param);
1020  return 3;
1021}
1022
1023/*
1024  index=get_levent_index(event)
1025  event is an event id (number) or name (string)
1026  returns index or nil if not found
1027*/
1028static int luaCB_get_levent_index( lua_State* L )
1029{
1030  unsigned event_index = levent_index_from_id_lua_arg(L,1);
1031  if (event_index == LEVENT_INVALID_INDEX) {
1032    lua_pushnil(L);
1033  }
1034  else {
1035    lua_pushnumber(L, event_index);
1036  }
1037  return 1;
1038}
1039
1040/*
1041  name,id,param = get_levent_def_by_index(event_index)
1042  event_index is number index into the event table
1043  returns nil if event is not found
1044*/
1045static int luaCB_get_levent_def_by_index( lua_State* L )
1046{
1047  unsigned i = luaL_checknumber(L,1);
1048  if(i >= levent_count()) {
1049        lua_pushnil(L);
1050    return 1;
1051  }
1052  lua_pushstring(L, levent_table[i].name);
1053  lua_pushnumber(L, levent_table[i].id);
1054  lua_pushnumber(L, levent_table[i].param);
1055  return 3;
1056}
1057
1058/*
1059  post_levent_*(event[,unk])
1060  post the event with PostLogicalEventToUI or PostLogicaEventForNotPowerType
1061  This sends the event. The difference between functions isn't clear.
1062  event is an event id (number) or name (string).
1063  unk is an optional number whose meaning is unknown, defaults to zero.
1064    Based on code, other values would probably be a pointer.
1065        This is NOT the 3rd item in the event table.
1066*/
1067static int luaCB_post_levent_to_ui( lua_State* L )
1068{
1069  unsigned event_id,arg;
1070
1071  event_id = levent_id_from_lua_arg(L,1);
1072  arg = luaL_optnumber(L, 2, 0);
1073  PostLogicalEventToUI(event_id,arg);
1074  return 0;
1075}
1076
1077static int luaCB_post_levent_for_npt( lua_State* L )
1078{
1079  unsigned event_id,arg;
1080
1081  event_id = levent_id_from_lua_arg(L,1);
1082  arg = luaL_optnumber(L, 2, 0);
1083  PostLogicalEventForNotPowerType(event_id,arg);
1084  return 0;
1085}
1086
1087/*
1088  set_levent_active(event,state)
1089  event is an event id (number) or name (string)
1090  state is a numeric or boolean state. true or non zero numbers turn on zero, false or nil turn off
1091  exact meaning is unknown, but it has something to do with the delivery of the specified event.
1092*/
1093static int luaCB_set_levent_active( lua_State* L )
1094{
1095  unsigned event_id;
1096  unsigned state;
1097
1098  event_id = levent_id_from_lua_arg(L,1);
1099  state = on_off_value_from_lua_arg(L,2);
1100  SetLogicalEventActive(event_id,state);
1101  return 0;
1102}
1103
1104/*
1105  set_levent_script_mode(state)
1106  state is numeric or boolean state. true or non zero numbers turn on zero, false or nil turn off
1107  exact meaning is unknown, but it has something to do with the behavior of events and/or SetLogicalEventActive.
1108*/
1109static int luaCB_set_levent_script_mode( lua_State* L )
1110{
1111  SetScriptMode(on_off_value_from_lua_arg(L,1));
1112  return 0;
1113}
1114
1115/*
1116  result=set_capture_mode_canon(value)
1117  where value is a valid PROPCASE_SHOOTING_MODE value for the current camera
1118  result is true if the camera is in rec mode
1119*/
1120static int luaCB_set_capture_mode_canon( lua_State* L )
1121{
1122  int modenum = luaL_checknumber(L,1);
1123  // if the value as negative, assume it is a mistakenly sign extended PROPCASE_SHOOTING_MODE value
1124  if(modenum < 0)
1125    modenum &= 0xFFFF;
1126  lua_pushboolean( L, shooting_set_mode_canon(modenum) );
1127  return 1;
1128}
1129
1130/*
1131 result=set_capture_mode(modenum)
1132 where modenum is a valid CHDK modemap value
1133 result is true if modenum is a valid modemap value, otherwise false
1134*/
1135static int luaCB_set_capture_mode( lua_State* L )
1136{
1137  int modenum = luaL_checknumber(L,1);
1138  lua_pushboolean( L, shooting_set_mode_chdk(modenum) );
1139  return 1;
1140}
1141
1142/*
1143 result=is_capture_mode_valid(modenum)
1144 where modenum is a valid CHDK modemap value
1145 result is true if modenum is a valid modemap value, otherwise false
1146*/
1147static int luaCB_is_capture_mode_valid( lua_State* L )
1148{
1149  int modenum = luaL_checknumber(L,1);
1150  lua_pushboolean( L, shooting_mode_chdk2canon(modenum) != -1 );
1151  return 1;
1152}
1153
1154/*
1155  set_record(state)
1156  if state is 0 (or false) the camera is set to play mode. If 1 or true, the camera is set to record mode.
1157  NOTE: this only begins the mode change. Script should wait until get_mode() reflects the change,
1158  before doing anything that requires the new mode. e.g.
1159  set_record(true)
1160  while not get_mode() do
1161        sleep(10)
1162  end
1163*/
1164static int luaCB_set_record( lua_State* L )
1165{
1166  if(on_off_value_from_lua_arg(L,1)) {
1167    levent_set_record();
1168  }
1169  else {
1170    levent_set_play();
1171  }
1172  return 0;
1173}
1174
1175/*
1176pack the lua args into a buffer to pass to the native code calling functions
1177currently only handles strings/numbers
1178start is the stack index of the first arg
1179*/
1180#ifdef OPT_LUA_CALL_NATIVE
1181static int pack_native_args( lua_State* L, unsigned start, unsigned *argbuf)
1182{
1183  unsigned i;
1184  unsigned end = lua_gettop(L);
1185
1186  for(i = start; i <= end; i++,argbuf++) {
1187    if (lua_type(L, i) == LUA_TSTRING) {
1188        *argbuf=(unsigned)lua_tostring( L, i);
1189    }
1190    else if (lua_type(L, i) == LUA_TNUMBER) {
1191        *argbuf=lua_tonumber( L, i);
1192    }
1193    else {
1194      return 0;
1195    }
1196  }
1197  return 1;
1198}
1199
1200/*
1201Native function call interface. Can be used to call canon eventprocs or arbitrary
1202pointers.
1203
1204NOTE: this is preliminary, interface may change in later versions!
1205All arguments must be strings or numbers.
1206If the function expects to modify it's arguments via a pointer,
1207then you must provide a number that is a valid pointer.
1208
1209You can use the "AllocateMemory" eventproc to obtain buffers.
1210
1211If the function tries to write to a string passed from lua, Bad Things may happen.
1212
1213This is potentially dangerous, functions exist which can destroy the onboard firmware.
1214*/
1215
1216/*
1217result=call_func_ptr(ptr,...)
1218ptr: address of a valid ARM or Thumb function, which uses the normal C calling convention.
1219result: R0 value after the call returns
1220*/
1221static int luaCB_call_func_ptr( lua_State* L)
1222{
1223  unsigned *argbuf=NULL;
1224  unsigned i;
1225  unsigned n_args = lua_gettop(L)-1;
1226  void *fptr;
1227
1228  fptr=(void *)luaL_checknumber( L, 1 );
1229
1230  if (n_args) {
1231    argbuf=malloc(n_args * 4);
1232    if(!argbuf) {
1233      return luaL_error( L, "malloc fail" );
1234    }
1235    if(!pack_native_args(L, 2, argbuf)) {
1236      free(argbuf);
1237      return luaL_error( L, "expected string or number" );
1238    }
1239  }
1240 
1241  lua_pushnumber( L, call_func_ptr(fptr, argbuf, n_args) );
1242  free(argbuf);
1243  return 1;
1244}
1245
1246/*
1247Call an event procedure
1248
1249result=call_event_proc("EventprocName",...)
1250result is the value returned by ExecuteEventProcedure, which is -1 if the eventproc is not found,
1251or the eventproc return value (which could also be -1)
1252NOTE:
1253Many eventprocs are not registered by default, but can be loaded by calling another event proc
1254Some useful ones are
1255SystemEventInit
1256        includes AllocateMemory, FreeMemory, sprintf, memcpy, Fut functions, log ...
1257UI_RegistDebugEventProc
1258        includes capture mode functions, PTM_ functions and much more
1259RegisterProductTestEvent
1260        includes PT_ functions
1261
1262Others:
1263RegisterShootSeqEvent
1264RegisterNRTableEvent
1265*/
1266
1267// grab from lowlevel
1268extern unsigned _ExecuteEventProcedure(const char *name,...);
1269static int luaCB_call_event_proc( lua_State* L )
1270{
1271  const char *evpname;
1272  unsigned *argbuf;
1273  unsigned i;
1274  unsigned n_args = lua_gettop(L);
1275
1276  evpname=luaL_checkstring( L, 1 );
1277
1278  argbuf=malloc(n_args * 4);
1279  if (!argbuf) {
1280    return luaL_error( L, "malloc fail" );
1281  }
1282
1283  // event proc name is first arg
1284  *argbuf = (unsigned)evpname;
1285 
1286  if(!pack_native_args(L,2,argbuf+1)) {
1287    free(argbuf);
1288    return luaL_error( L, "expected string or number" );
1289  }
1290 
1291  lua_pushnumber( L, call_func_ptr(_ExecuteEventProcedure,argbuf,n_args) );
1292  free(argbuf);
1293  return 1;
1294}
1295
1296#endif // OPT_LUA_CALL_NATIVE
1297
1298/*
1299result = reboot(["filename"])
1300returns false on failure, does not return on success
1301see lib/armutil/reboot.c for details
1302*/
1303static int luaCB_reboot( lua_State* L )
1304{
1305        lua_pushboolean(L, reboot(luaL_optstring( L, 1, NULL )));
1306        return 1;
1307}
1308
1309void register_lua_funcs( lua_State* L )
1310{
1311#define FUNC( X )                       \
1312  lua_pushcfunction( L, luaCB_##X );    \
1313  lua_setglobal( L, #X )
1314
1315  FUNC(shoot);
1316  FUNC(sleep);
1317  FUNC(cls);
1318  FUNC(set_console_layout);
1319  FUNC(set_console_autoredraw);
1320  FUNC(console_redraw);
1321
1322  lua_pushlightuserdata( L, kbd_sched_click );
1323  lua_pushcclosure( L, luaCB_keyfunc, 1 );
1324  lua_setglobal( L, "click" );
1325
1326  lua_pushlightuserdata( L, kbd_sched_press );
1327  lua_pushcclosure( L, luaCB_keyfunc, 1 );
1328  lua_setglobal( L, "press" );
1329
1330  lua_pushlightuserdata( L, kbd_sched_release );
1331  lua_pushcclosure( L, luaCB_keyfunc, 1 );
1332  lua_setglobal( L, "release" );
1333
1334  FUNC(get_av96);
1335  FUNC(get_av96);
1336  FUNC(get_bv96);
1337  FUNC(get_day_seconds);
1338  FUNC(get_disk_size);
1339  FUNC(get_dof);
1340  FUNC(get_far_limit);
1341  FUNC(get_free_disk_space);
1342  FUNC(get_focus);
1343  FUNC(get_hyp_dist);
1344  FUNC(get_iso_market);
1345  FUNC(get_iso_mode);
1346  FUNC(get_iso_real);
1347  FUNC(get_jpg_count);
1348  FUNC(get_near_limit);
1349  FUNC(get_prop);
1350  FUNC(get_raw_count);
1351  FUNC(get_raw_nr);
1352  FUNC(get_raw);
1353  FUNC(get_sv96);
1354  FUNC(get_tick_count);
1355  FUNC(get_tv96);
1356  FUNC(get_user_av_id);
1357  FUNC(get_user_av96);
1358  FUNC(get_user_tv_id);
1359  FUNC(get_user_tv96);
1360  FUNC(get_vbatt);
1361  FUNC(get_zoom);
1362  FUNC(get_exp_count);
1363  FUNC(get_flash_params_count);
1364  FUNC(get_parameter_data);
1365
1366  FUNC(set_av96_direct);
1367  FUNC(set_av96);
1368  FUNC(set_focus);
1369  FUNC(set_iso_mode);
1370  FUNC(set_iso_real);
1371  FUNC(set_led);
1372  FUNC(set_nd_filter);
1373  FUNC(set_prop);
1374  FUNC(set_raw_nr);
1375  FUNC(set_raw);
1376  FUNC(set_sv96);
1377  FUNC(set_tv96_direct);
1378  FUNC(set_tv96);
1379  FUNC(set_user_av_by_id_rel);
1380  FUNC(set_user_av_by_id);
1381  FUNC(set_user_av96);
1382  FUNC(set_user_tv_by_id_rel);
1383  FUNC(set_user_tv_by_id);
1384  FUNC(set_user_tv96);
1385  FUNC(set_zoom_speed);
1386  FUNC(set_zoom_rel);
1387  FUNC(set_zoom);
1388
1389  FUNC(wait_click);
1390  FUNC(is_pressed);
1391  FUNC(is_key);
1392#ifdef CAM_HAS_JOGDIAL
1393  FUNC(wheel_right);
1394  FUNC(wheel_left);
1395#endif
1396  FUNC(md_get_cell_diff);
1397  FUNC(md_detect_motion);
1398  FUNC(autostarted);
1399  FUNC(get_autostart);
1400  FUNC(set_autostart);
1401  FUNC(get_usb_power);
1402  FUNC(exit_alt);
1403  FUNC(shut_down);
1404  FUNC(print_screen);
1405
1406  FUNC(get_focus_mode);
1407  FUNC(get_propset);
1408  FUNC(get_zoom_steps);
1409  FUNC(get_drive_mode);
1410  FUNC(get_flash_mode);
1411  FUNC(get_shooting);
1412  FUNC(get_flash_ready);
1413  FUNC(get_IS_mode);
1414  FUNC(set_ev);
1415  FUNC(get_ev);
1416  FUNC(get_orientation_sensor);
1417  FUNC(get_nd_present);
1418  FUNC(get_movie_status);
1419  FUNC(set_movie_status);
1420 
1421  FUNC(get_histo_range);
1422  FUNC(shot_histo_enable);
1423  FUNC(play_sound);
1424  FUNC(get_temperature);
1425  FUNC(peek);
1426  FUNC(poke);
1427  FUNC(bitand);
1428  FUNC(bitor);
1429  FUNC(bitxor);
1430  FUNC(bitshl);
1431  FUNC(bitshri);
1432  FUNC(bitshru);
1433  FUNC(bitnot);
1434
1435  FUNC(get_time);
1436
1437  FUNC(get_buildinfo);
1438  FUNC(get_mode);
1439 
1440  FUNC(set_raw_develop);
1441  // NOTE these functions normally run in the spytask.
1442  // called from lua they will run from kbd task instead
1443  FUNC(raw_merge_start);
1444  FUNC(raw_merge_add_file);
1445  FUNC(raw_merge_end);
1446  FUNC(set_backlight);
1447   FUNC(set_aflock);
1448#ifdef OPT_CURVES
1449   FUNC(set_curve_state);
1450#endif
1451// get levent definition by name or id, nil if not found
1452   FUNC(get_levent_def);
1453// get levent definition by index, nil if out of range
1454   FUNC(get_levent_def_by_index);
1455// get levent index from name or ID
1456   FUNC(get_levent_index);
1457   FUNC(post_levent_to_ui);
1458   FUNC(post_levent_for_npt);
1459   FUNC(set_levent_active);
1460   FUNC(set_levent_script_mode);
1461
1462   FUNC(set_capture_mode);
1463   FUNC(set_capture_mode_canon);
1464   FUNC(is_capture_mode_valid);
1465
1466   FUNC(set_record);
1467
1468#ifdef OPT_LUA_CALL_NATIVE
1469   FUNC(call_event_proc);
1470   FUNC(call_func_ptr);
1471#endif
1472   FUNC(reboot);
1473}
Note: See TracBrowser for help on using the repository browser.