source: trunk/core/luascript.c @ 826

Revision 826, 26.6 KB checked in by reyalp, 4 years ago (diff)

First part of mode override support. See http://chdk.kernreaktor.org/mantis/view.php?id=64
+ support for posting logical events, and other event fuctions in lua
+ script support for setting mode to play or rec. Note, get_mode will currently return incorrect values on many cameras if the mode is set this way, which will affect both script and some CHDK functions. This will be addressed in a future version.
+ logical event related entry point SetLogicalEventActive? for all cameras except ixus40_sd300
+ logical event related entry points SetScriptMode?, PostLogicalEventForNotPowerType?, PostLogicalEventToUI for all cameras
+ entry SetCurrentCaptureModeType? for all cameras except ixus40_sd300 ixus50_sd400 (might be right, unsure). This will be used to support capture mode overrides in a future version, not currently exposed.
+ playrec_mode variable for all cameras except ixus40_sd300 ixus50_sd400. This will allow mode_get in future version to return the right result on cameras that depended on the physw_status bits of the mod/rec hardware switch
+ to tools:

  • generic dump access code dumputil.c, dumputil.h
  • find_levent.c finds and optionally dumps levent table
  • find_eventproc.c very dumb/simple search for name/pointer pairs, useful for eventprocs that are too small for finsig These are not added to "make all" in tools, because they are not needed in the normal build process. Use the executable name or "make extras" in tools.

Except as noted above, all the entry points and addresses have been verified with at least a superficial check

New firmware function descriptions:
See comments in lowlevel.h
See: http://chdk.setepontos.com/index.php/topic,3228.msg42218.html#msg42218 for more detail on SetLogicalEventActive? and SetScriptMode?

New firmware variable descriptions:
levent_table: see description in levent.h
playrec_mode: see description in lowlevel.h

New script functions: see comments in core/luascript.c or http://chdk.setepontos.com/index.php/topic,3228.msg42842.html#msg42842

  • 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_get_av96( lua_State* L )
70{
71  lua_pushnumber( L, shooting_get_av96() );
72  return 1;
73}
74
75static int luaCB_get_bv96( lua_State* L )
76{
77  lua_pushnumber( L, shooting_get_bv96() );
78  return 1;
79}
80
81static int luaCB_get_day_seconds( lua_State* L )
82{
83  lua_pushnumber( L, shooting_get_day_seconds() );
84  return 1;
85}
86
87static int luaCB_get_disk_size( lua_State* L )
88{
89  lua_pushnumber( L, GetTotalCardSpaceKb() );
90  return 1;
91}
92
93static int luaCB_get_dof( lua_State* L )
94{
95  lua_pushnumber( L, shooting_get_depth_of_field() );
96  return 1;
97}
98
99static int luaCB_get_far_limit( lua_State* L )
100{
101  lua_pushnumber( L, shooting_get_far_limit_of_acceptable_sharpness() );
102  return 1;
103}
104
105static int luaCB_get_free_disk_space( lua_State* L )
106{
107  lua_pushnumber( L, GetFreeCardSpaceKb() );
108  return 1;
109}
110
111static int luaCB_get_focus( lua_State* L )
112{
113  lua_pushnumber( L, shooting_get_subject_distance() );
114  return 1;
115}
116
117static int luaCB_get_hyp_dist( lua_State* L )
118{
119  lua_pushnumber( L, shooting_get_hyperfocal_distance() );
120  return 1;
121}
122
123static int luaCB_get_iso_market( lua_State* L )
124{
125  lua_pushnumber( L, shooting_get_iso_market() );
126  return 1;
127}
128
129static int luaCB_get_iso_mode( lua_State* L )
130{
131  lua_pushnumber( L, shooting_get_iso_mode() );
132  return 1;
133}
134
135static int luaCB_get_iso_real( lua_State* L )
136{
137  lua_pushnumber( L, shooting_get_iso_real() );
138  return 1;
139}
140
141static int luaCB_get_jpg_count( lua_State* L )
142{
143  lua_pushnumber( L, GetJpgCount() );
144  return 1;
145}
146
147static int luaCB_get_near_limit( lua_State* L )
148{
149  lua_pushnumber( L, shooting_get_near_limit_of_acceptable_sharpness() );
150  return 1;
151}
152
153static int luaCB_get_prop( lua_State* L )
154{
155  lua_pushnumber( L, shooting_get_prop( luaL_checknumber( L, 1 ) ) );
156  return 1;
157}
158
159static int luaCB_get_raw_count( lua_State* L )
160{
161  lua_pushnumber( L, GetRawCount() );
162  return 1;
163}
164
165static int luaCB_get_sv96( lua_State* L )
166{
167  lua_pushnumber( L, shooting_get_sv96() );
168  return 1;
169}
170
171static int luaCB_get_tick_count( lua_State* L )
172{
173  lua_pushnumber( L, shooting_get_tick_count() );
174  return 1;
175}
176
177static int luaCB_get_exp_count( lua_State* L )
178{
179  lua_pushnumber( L, get_exposure_counter() );
180  return 1;
181}
182
183static int luaCB_get_tv96( lua_State* L )
184{
185  lua_pushnumber( L, shooting_get_tv96() );
186  return 1;
187}
188
189static int luaCB_get_user_av_id( lua_State* L )
190{
191  lua_pushnumber( L, shooting_get_user_av_id() );
192  return 1;
193}
194
195static int luaCB_get_user_av96( lua_State* L )
196{
197  lua_pushnumber( L, shooting_get_user_av96() );
198  return 1;
199}
200
201static int luaCB_get_user_tv_id( lua_State* L )
202{
203  lua_pushnumber( L, shooting_get_user_tv_id() );
204  return 1;
205}
206
207static int luaCB_get_user_tv96( lua_State* L )
208{
209  lua_pushnumber( L, shooting_get_user_tv_id() );
210  return 1;
211}
212
213static int luaCB_get_vbatt( lua_State* L )
214{
215  lua_pushnumber( L, stat_get_vbatt() );
216  return 1;
217}
218
219static int luaCB_get_zoom( lua_State* L )
220{
221  lua_pushnumber( L, shooting_get_zoom() );
222  return 1;
223}
224
225static int luaCB_get_parameter_data( lua_State* L )
226{
227  extern long* FlashParamsTable[];
228
229  unsigned size;
230  unsigned id = luaL_checknumber( L, 1 );
231  unsigned val;
232
233  if (id >= get_flash_params_count()) {
234    // return nil
235    return 0;
236  }
237
238  size = FlashParamsTable[id][1]>>16;
239  if (size == 0) {
240    // return nil
241    return 0;
242  }
243  if (size >= 1 && size <= 4) {
244    val = 0;
245    get_parameter_data( id, &val, size );
246    lua_pushlstring( L, (char *)&val, size );
247    // for convenience, params that fit in a number are returned in one as a second result
248    lua_pushnumber( L, val );
249    return 2;
250  }
251  else {
252    char *buf = malloc(size);
253    if(!buf) {
254      luaL_error( L, "malloc failed in luaCB_get_parameter_data" );
255    }
256    get_parameter_data( id, buf, size );
257    lua_pushlstring( L, buf, size );
258    free(buf);
259    return 1;
260  }
261}
262
263static int luaCB_get_flash_params_count( lua_State* L )
264{
265  lua_pushnumber( L, get_flash_params_count() );
266  return 1;
267}
268
269static int luaCB_set_av96_direct( lua_State* L )
270{
271  shooting_set_av96_direct( luaL_checknumber( L, 1 ), SET_LATER );
272  return 0;
273}
274
275static int luaCB_set_av96( lua_State* L )
276{
277  shooting_set_av96( luaL_checknumber( L, 1 ), SET_LATER );
278  return 0;
279}
280
281static int luaCB_set_focus( lua_State* L )
282{
283    int to = luaL_checknumber( L, 1 );
284    int m=mode_get()&MODE_SHOOTING_MASK;
285    int mode_video=MODE_IS_VIDEO(m);
286
287#if CAM_HAS_MANUAL_FOCUS
288    if (shooting_get_focus_mode() || (mode_video)) shooting_set_focus(to, SET_NOW);
289    else shooting_set_focus(to, SET_LATER);
290#else
291    if (mode_video) shooting_set_focus(to, SET_NOW);
292    else shooting_set_focus(to, SET_LATER);   
293#endif   
294  return 0;
295}
296
297static int luaCB_set_iso_mode( lua_State* L )
298{
299  shooting_set_iso_mode( luaL_checknumber( L, 1 ) );
300  return 0;
301}
302
303static int luaCB_set_iso_real( lua_State* L )
304{
305  shooting_set_iso_real( luaL_checknumber( L, 1 ), SET_LATER);
306  return 0;
307}
308
309static int luaCB_set_led( lua_State* L )
310{
311  int to, to1, to2;
312  to = luaL_checknumber( L, 1 );
313  to1 = luaL_checknumber( L, 2 );
314  to2 = 200;
315  if( lua_isnumber( L, 3 ) )
316    to2 = lua_tonumber( L, 3 );
317  ubasic_set_led(to, to1, to2);
318  return 0;
319}
320
321static int luaCB_set_nd_filter( lua_State* L )
322{
323  shooting_set_nd_filter_state( luaL_checknumber( L, 1 ), SET_LATER);
324  return 0;
325}
326
327static int luaCB_set_prop( lua_State* L )
328{
329  int to, to1;
330  to = luaL_checknumber( L, 1 );
331  to1 = luaL_checknumber( L, 2 );
332  shooting_set_prop(to, to1);
333  return 0;
334}
335
336static int luaCB_set_raw_nr( lua_State* L )
337{
338  ubasic_camera_set_nr(luaL_checknumber( L, 1 ));
339  return 0;
340}
341
342static int luaCB_get_raw_nr( lua_State* L )
343{
344  lua_pushnumber( L, ubasic_camera_get_nr() );
345  return 1;
346}
347
348static int luaCB_set_raw( lua_State* L )
349{
350  ubasic_camera_set_raw(luaL_checknumber( L, 1 ));
351  return 0;
352}
353
354static int luaCB_get_raw( lua_State* L )
355{
356  lua_pushnumber( L, conf.save_raw );
357  return 1;
358}
359
360static int luaCB_set_sv96( lua_State* L )
361{
362  shooting_set_sv96(luaL_checknumber( L, 1 ), SET_LATER);
363  return 0;
364}
365
366static int luaCB_set_tv96_direct( lua_State* L )
367{
368  shooting_set_tv96_direct(luaL_checknumber( L, 1 ), SET_LATER);
369  return 0;
370}
371
372static int luaCB_set_tv96( lua_State* L )
373{
374  shooting_set_tv96(luaL_checknumber( L, 1 ), SET_LATER);
375  return 0;
376}
377
378static int luaCB_set_user_av_by_id_rel( lua_State* L )
379{
380  shooting_set_user_av_by_id_rel(luaL_checknumber( L, 1 ));
381  return 0;
382}
383
384static int luaCB_set_user_av_by_id( lua_State* L )
385{
386  shooting_set_user_av_by_id(luaL_checknumber( L, 1 ));
387  return 0;
388}
389
390static int luaCB_set_user_av96( lua_State* L )
391{
392  shooting_set_user_av96(luaL_checknumber( L, 1 ));
393  return 0;
394}
395
396static int luaCB_set_user_tv_by_id_rel( lua_State* L )
397{
398  shooting_set_user_tv_by_id_rel(luaL_checknumber( L, 1 ));
399  return 0;
400}
401
402static int luaCB_set_user_tv_by_id( lua_State* L )
403{
404  shooting_set_user_tv_by_id(luaL_checknumber( L, 1 ));
405  return 0;
406}
407
408static int luaCB_set_user_tv96( lua_State* L )
409{
410  shooting_set_user_tv96(luaL_checknumber( L, 1 ));
411  return 0;
412}
413
414static int luaCB_set_zoom_speed( lua_State* L )
415{
416  shooting_set_zoom_speed(luaL_checknumber( L, 1 ));
417  return 0;
418}
419
420static int luaCB_set_zoom_rel( lua_State* L )
421{
422  shooting_set_zoom_rel(luaL_checknumber( L, 1 ));
423  return 0;
424}
425
426static int luaCB_set_zoom( lua_State* L )
427{
428  shooting_set_zoom(luaL_checknumber( L, 1 ));
429  return 0;
430}
431
432static int luaCB_wait_click( lua_State* L )
433{
434  int timeout = luaL_optnumber( L, 1, 0 );
435  ubasic_camera_wait_click(timeout);
436  return lua_yield( L, 0 );
437}
438
439static int luaCB_is_pressed( lua_State* L )
440{
441  lua_pushboolean( L, ubasic_camera_is_pressed(luaL_checkstring( L, 1 )));
442  return 1;
443}
444
445static int luaCB_is_key( lua_State* L )
446{
447  lua_pushboolean( L, ubasic_camera_is_clicked(luaL_checkstring( L, 1 )));
448  return 1;
449}
450
451#if CAM_HAS_JOGDIAL
452static int luaCB_wheel_right( lua_State* L )
453{
454  JogDial_CW();
455  return 0;
456}
457
458static int luaCB_wheel_left( lua_State* L )
459{
460  JogDial_CCW();
461  return 0;
462}
463#endif
464
465static int luaCB_md_get_cell_diff( lua_State* L )
466{
467  lua_pushnumber( L, md_get_cell_diff(luaL_checknumber(L,1),
468                                      luaL_checknumber(L,2)));
469  return 1;
470}
471
472static int luaCB_md_detect_motion( lua_State* L )
473{
474  int columns = (luaL_optnumber(L,1,6));
475  int rows = (luaL_optnumber(L,2,4));
476  int pixel_measure_mode = (luaL_optnumber(L,3,1));
477  int detection_timeout = (luaL_optnumber(L,4,10000));
478  int measure_interval = (luaL_optnumber(L,5,7));
479  int threshold = (luaL_optnumber(L,6,10));
480  int draw_grid = (luaL_optnumber(L,7,1));
481   // arg 8 is the return value in ubasic. We
482   // ignore it here. - AUJ
483  int clipping_region_mode = (luaL_optnumber(L,9,0));
484  int clipping_region_column1 = (luaL_optnumber(L,10,0));
485  int clipping_region_row1 = (luaL_optnumber(L,11,0));
486  int clipping_region_column2 = (luaL_optnumber(L,12,0));
487  int clipping_region_row2 = (luaL_optnumber(L,13,0));
488  int parameters = (luaL_optnumber(L,14,1));
489  int pixels_step = (luaL_optnumber(L,15,6));
490  int msecs_before_trigger = (luaL_optnumber(L,16,0));
491  ubasic_set_variable(0, 0);
492  if(md_init_motion_detector(
493    columns, rows, pixel_measure_mode, detection_timeout,
494    measure_interval, threshold, draw_grid, 0,
495    clipping_region_mode,
496    clipping_region_column1, clipping_region_row1,
497    clipping_region_column2, clipping_region_row2,
498    parameters, pixels_step, msecs_before_trigger
499  ))
500    return lua_yield(L, 0);
501  else
502    return luaL_error( L, "md_init_motion_detector failed" );
503}
504
505static int luaCB_autostarted( lua_State* L )
506{
507  lua_pushboolean( L, ubasic_camera_script_autostart() );
508  return 1;
509}
510
511static int luaCB_get_autostart( lua_State* L )
512{
513  lua_pushnumber( L, conf.script_startup );
514  return 1;
515}
516
517static int luaCB_set_autostart( lua_State* L )
518{
519  int to;
520  to = luaL_checknumber( L, 1 );
521  if ( to >= 0 && to <= 2 ) conf.script_startup = to;
522  conf_save();
523  return 0;
524}
525
526static int luaCB_get_usb_power( lua_State* L )
527{
528  if (luaL_optnumber( L, 1, 0 )) lua_pushnumber( L, get_usb_power(1) );
529  else lua_pushnumber( L, get_usb_power(0) );
530  return 1;
531}
532
533static int luaCB_exit_alt( lua_State* L )
534{
535  exit_alt();
536  return 0;
537}
538
539static int luaCB_shut_down( lua_State* L )
540{
541  camera_shutdown_in_a_second();
542  return 0;
543}
544
545static int luaCB_print_screen( lua_State* L )
546{
547 
548  if (lua_isboolean( L, 1 ))
549    script_print_screen_statement( lua_toboolean( L, 1 ) );
550  else
551    script_print_screen_statement( luaL_checknumber( L, 1 )+10000 );
552  return 0;
553}
554
555static int luaCB_get_movie_status( lua_State* L )
556{
557  lua_pushnumber( L, movie_status );
558  return 1;
559}
560
561static int luaCB_set_movie_status( lua_State* L )
562{
563  int to;
564  switch(luaL_checknumber( L, 1 )) {
565    case 1:
566      if (movie_status == 4) {
567        movie_status = 1;
568      }
569    break;
570    case 2:
571      if (movie_status == 1) {
572        movie_status = 4;
573      }
574    break;
575    case 3:
576      if (movie_status == 1 || movie_status == 4) {
577        movie_status = 5;
578      }
579    break;
580  }
581  return 0;
582}
583
584static int luaCB_get_drive_mode( lua_State* L )
585{
586  lua_pushnumber( L, shooting_get_drive_mode() );
587  return 1;
588}
589
590static int luaCB_get_focus_mode( lua_State* L )
591{
592  lua_pushnumber( L, shooting_get_prop(PROPCASE_FOCUS_MODE) );
593  return 1;
594}
595
596static int luaCB_get_flash_mode( lua_State* L )
597{
598  lua_pushnumber( L, shooting_get_prop(PROPCASE_FLASH_MODE) );
599  return 1;
600}
601
602static int luaCB_get_shooting( lua_State* L )
603{
604  lua_pushboolean( L, shooting_get_prop(PROPCASE_SHOOTING) );
605  return 1;
606}
607
608static int luaCB_get_flash_ready( lua_State* L )
609{
610  lua_pushboolean( L, shooting_get_prop(PROPCASE_IS_FLASH_READY) );
611  return 1;
612}
613
614static int luaCB_get_IS_mode( lua_State* L )
615{
616  lua_pushnumber( L, shooting_get_prop(PROPCASE_IS_MODE) );
617  return 1;
618}
619
620static int luaCB_get_orientation_sensor( lua_State* L )
621{
622  lua_pushnumber( L, shooting_get_prop(PROPCASE_ORIENTATION_SENSOR) );
623  return 1;
624}
625
626static int luaCB_get_zoom_steps( lua_State* L )
627{
628  lua_pushnumber( L, zoom_points );
629  return 1;
630}
631
632static int luaCB_get_nd_present( lua_State* L )
633{
634  int to;
635  #if !CAM_HAS_ND_FILTER
636  to = 0;
637  #endif
638  #if CAM_HAS_ND_FILTER && !CAM_HAS_IRIS_DIAPHRAGM
639  to = 1;
640  #endif
641  #if CAM_HAS_ND_FILTER && CAM_HAS_IRIS_DIAPHRAGM
642  to = 2;
643  #endif
644  lua_pushnumber( L, to );
645  return 1;
646}
647
648static int luaCB_get_propset( lua_State* L )
649{
650  lua_pushnumber( L, CAM_PROPSET );
651  return 1;
652}
653
654static int luaCB_get_ev( lua_State* L )
655{
656  lua_pushnumber( L, shooting_get_prop(PROPCASE_EV_CORRECTION_1) );
657  return 1;
658}
659
660static int luaCB_set_ev( lua_State* L )
661{
662  int to;
663  to = luaL_checknumber( L, 1 );
664  shooting_set_prop(PROPCASE_EV_CORRECTION_1, to);
665  shooting_set_prop(PROPCASE_EV_CORRECTION_2, to);
666  return 0;
667}
668
669static int luaCB_get_histo_range( lua_State* L )
670{
671  int from = (luaL_checknumber(L,1));
672  int to = (luaL_checknumber(L,2));
673  if (shot_histogram_isenabled()) lua_pushnumber( L, shot_histogram_get_range(from, to) );
674  else lua_pushnumber( L, -1 ); // TODO should probably return nil
675  return 1;
676}
677
678static int luaCB_shot_histo_enable( lua_State* L )
679{
680  shot_histogram_set(luaL_checknumber( L, 1 ));
681  return 0;
682}
683
684static int luaCB_play_sound( lua_State* L )
685{
686  play_sound(luaL_checknumber( L, 1 ));
687  return 0;
688}
689
690static int luaCB_get_temperature( lua_State* L )
691{
692  int which = (luaL_checknumber( L, 1 ));
693  int temp = -100; // do something insane if users passes bad value
694  switch (which)
695  {
696    case 0:
697      temp = get_optical_temp();
698      break;
699    case 1:
700      temp = get_ccd_temp();
701      break;
702    case 2:
703      temp = get_battery_temp();
704      break;
705  }
706  lua_pushnumber( L, temp );
707  return 1;
708}
709
710static int luaCB_get_time( lua_State* L )
711{
712  int r = -1;
713  unsigned long t2 = time(NULL);
714  static struct tm *ttm;
715  ttm = localtime(&t2);
716  const char *t = luaL_checkstring( L, 1 );
717  if (strncmp("s", t, 1)==0) r = ( L, ttm->tm_sec );
718  else if (strncmp("m", t, 1)==0) r = ( L, ttm->tm_min );
719  else if (strncmp("h", t, 1)==0) r = ( L, ttm->tm_hour );
720  else if (strncmp("D", t, 1)==0) r = ( L, ttm->tm_mday );
721  else if (strncmp("M", t, 1)==0) r = ( L, ttm->tm_mon+1 );
722  else if (strncmp("Y", t, 1)==0) r = ( L, 1900+ttm->tm_year );
723  lua_pushnumber( L, r );
724  return 1;
725}
726
727static int luaCB_peek( lua_State* L )
728{
729  int addr = (luaL_checknumber(L,1));
730  // must be alligned
731  if (addr & 0x3) {
732        lua_pushnil(L);
733  }
734  else {
735    lua_pushnumber( L, *(unsigned *)(addr) );
736  }
737  return 1;
738}
739
740static int luaCB_poke( lua_State* L )
741{
742  int addr = (luaL_checknumber(L,1));
743  int val = (luaL_checknumber(L,2));
744  if (addr & 0x3) {
745        lua_pushnil(L);
746  }
747  else {
748    *(unsigned *)(addr) = val;
749    lua_pushboolean(L,1);
750  }
751  return 1;
752}
753
754static int luaCB_bitand( lua_State* L )
755{
756  int v1 = (luaL_checknumber(L,1));
757  int v2 = (luaL_checknumber(L,2));
758  lua_pushnumber( L, v1 & v2 );
759  return 1;
760}
761
762static int luaCB_bitor( lua_State* L )
763{
764  int v1 = (luaL_checknumber(L,1));
765  int v2 = (luaL_checknumber(L,2));
766  lua_pushnumber( L, v1 | v2 );
767  return 1;
768}
769
770static int luaCB_bitxor( lua_State* L )
771{
772  int v1 = (luaL_checknumber(L,1));
773  int v2 = (luaL_checknumber(L,2));
774  lua_pushnumber( L, v1 ^ v2 );
775  return 1;
776}
777
778static int luaCB_bitshl( lua_State* L )
779{
780  int val = (luaL_checknumber(L,1));
781  unsigned shift = (luaL_checknumber(L,2));
782  lua_pushnumber( L, val << shift );
783  return 1;
784}
785
786static int luaCB_bitshri( lua_State* L )
787{
788  int val = (luaL_checknumber(L,1));
789  unsigned shift = (luaL_checknumber(L,2));
790  lua_pushnumber( L, val >> shift );
791  return 1;
792}
793
794static int luaCB_bitshru( lua_State* L )
795{
796  unsigned val = (luaL_checknumber(L,1));
797  unsigned shift = (luaL_checknumber(L,2));
798  lua_pushnumber( L, val >> shift );
799  return 1;
800}
801
802static int luaCB_bitnot( lua_State* L )
803{
804  unsigned val = (luaL_checknumber(L,1));
805  lua_pushnumber( L, ~val );
806  return 1;
807}
808
809static void set_string_field(lua_State* L, const char *key, const char *val)
810{
811  lua_pushstring(L, val);
812  lua_setfield(L, -2, key);
813}
814
815static int luaCB_get_buildinfo( lua_State* L )
816{
817  lua_createtable(L, 0, 8);
818  set_string_field( L,"platform", PLATFORM );
819  set_string_field( L,"platsub", PLATFORMSUB );
820  set_string_field( L,"version", HDK_VERSION );
821  set_string_field( L,"build_number", BUILD_NUMBER );
822  set_string_field( L,"build_date", __DATE__ );
823  set_string_field( L,"build_time", __TIME__ );
824#ifndef CAM_DRYOS
825  set_string_field( L,"os", "vxworks" );
826#else
827  set_string_field( L,"os", "dryos" );
828#endif
829  lua_pushnumber( L, PLATFORMID );
830  lua_setfield(L, -2, "platformid");
831  return 1;
832}
833
834static int luaCB_get_mode( lua_State* L )
835{
836  int m = mode_get();
837  lua_pushboolean( L, (m&MODE_MASK) != MODE_PLAY );
838  lua_pushboolean( L, MODE_IS_VIDEO(m) );
839  lua_pushnumber( L, m );
840  return 3;
841}
842
843// TODO sanity check file ?
844static int luaCB_set_raw_develop( lua_State* L )
845{
846  raw_prepare_develop(luaL_optstring( L, 1, NULL ));
847  return 0;
848}
849
850static int luaCB_raw_merge_start( lua_State* L )
851{
852  int op = luaL_checknumber(L,1);
853  if (op == RAW_OPERATION_SUM || op == RAW_OPERATION_AVERAGE) {
854    raw_merge_start(op);
855  }
856  else {
857    return luaL_argerror(L,1,"invalid raw merge op");
858  }
859  return 0;
860}
861
862// TODO sanity check file ?
863static int luaCB_raw_merge_add_file( lua_State* L )
864{
865  raw_merge_add_file(luaL_checkstring( L, 1 ));
866  return 0;
867}
868
869static int luaCB_raw_merge_end( lua_State* L )
870{
871  raw_merge_end();
872  return 0;
873}
874
875// Enable/disable LCD back light (input argument 1/0)
876static int luaCB_set_backlight( lua_State* L )
877{
878  int val = (luaL_checknumber(L,1));
879
880  if (val > 0) TurnOnBackLight();
881  else TurnOffBackLight();
882  return 0;
883}
884
885// get the string or number passed in index and return it as an event id
886static unsigned levent_id_from_lua_arg( lua_State* L, int index)
887{
888  unsigned event_id;
889  if (lua_type(L, index) == LUA_TSTRING) {
890    const char *ev_name = lua_tostring(L, index);
891        event_id = levent_id_for_name(ev_name);
892    if (event_id == 0) {
893        return luaL_error( L, "bad event name '%s'", ev_name );
894    }
895  }
896  // could check here if it is in the table, but even valid ones can crash
897  // so we avoid searching the table if given a number
898  else if (lua_type(L,index) == LUA_TNUMBER){
899        event_id = lua_tonumber(L,index);
900  }
901  else {
902    return luaL_error( L, "expected event name or id" );
903  }
904  return event_id;
905}
906
907/*
908  get a value where boolean or 0/!0 are accepted for on/off.
909  normal lua toboolean will convert 0 to true, but ubasic and c users
910  will expect 0 to be off
911  intentional HACK: numbers greater than 1 are returned as is
912*/
913static unsigned on_off_value_from_lua_arg( lua_State* L, int index)
914{
915  if( lua_isboolean(L,index) ) {
916        return lua_toboolean(L,index);
917  }
918  else {
919        return luaL_checknumber(L,index);
920  }
921}
922
923/*
924  return the index of an event, given it's name or event id
925*/
926static unsigned levent_index_from_id_lua_arg( lua_State* L, int index )
927{
928  if (lua_type(L, index) == LUA_TSTRING) {
929        return levent_index_for_name(lua_tostring(L, index));
930  }
931  else if (lua_type(L,index) == LUA_TNUMBER){
932        return levent_index_for_id(lua_tonumber(L,index));
933  }
934  else {
935    return luaL_error( L, "expected string or number" );
936  }
937}
938
939/*
940  name,id,param = get_levent_def(event)
941  event is an event id (number) or name (string)
942  returns nil if event is not found
943*/
944static int luaCB_get_levent_def( lua_State* L )
945{
946  unsigned event_index = levent_index_from_id_lua_arg(L,1);
947  if (event_index == LEVENT_INVALID_INDEX) {
948    lua_pushnil(L);
949    return 1;
950  }
951  lua_pushstring(L, levent_table[event_index].name);
952  lua_pushnumber(L, levent_table[event_index].id);
953  lua_pushnumber(L, levent_table[event_index].param);
954  return 3;
955}
956
957/*
958  index=get_levent_index(event)
959  event is an event id (number) or name (string)
960  returns index or nil if not found
961*/
962static int luaCB_get_levent_index( lua_State* L )
963{
964  unsigned event_index = levent_index_from_id_lua_arg(L,1);
965  if (event_index == LEVENT_INVALID_INDEX) {
966    lua_pushnil(L);
967  }
968  else {
969    lua_pushnumber(L, event_index);
970  }
971  return 1;
972}
973
974/*
975  name,id,param = get_levent_def_by_index(event_index)
976  event_index is number index into the event table
977  returns nil if event is not found
978*/
979static int luaCB_get_levent_def_by_index( lua_State* L )
980{
981  unsigned i = luaL_checknumber(L,1);
982  if(i >= levent_count()) {
983        lua_pushnil(L);
984    return 1;
985  }
986  lua_pushstring(L, levent_table[i].name);
987  lua_pushnumber(L, levent_table[i].id);
988  lua_pushnumber(L, levent_table[i].param);
989  return 3;
990}
991
992/*
993  post_levent_*(event[,unk])
994  post the event with PostLogicalEventToUI or PostLogicaEventForNotPowerType
995  This sends the event. The difference between functions isn't clear.
996  event is an event id (number) or name (string).
997  unk is an optional number whose meaning is unknown, defaults to zero.
998    Based on code, other values would probably be a pointer.
999        This is NOT the 3rd item in the event table.
1000*/
1001static int luaCB_post_levent_to_ui( lua_State* L )
1002{
1003  unsigned event_id,arg;
1004
1005  event_id = levent_id_from_lua_arg(L,1);
1006  arg = luaL_optnumber(L, 2, 0);
1007  PostLogicalEventToUI(event_id,arg);
1008  return 0;
1009}
1010
1011static int luaCB_post_levent_for_npt( lua_State* L )
1012{
1013  unsigned event_id,arg;
1014
1015  event_id = levent_id_from_lua_arg(L,1);
1016  arg = luaL_optnumber(L, 2, 0);
1017  PostLogicalEventForNotPowerType(event_id,arg);
1018  return 0;
1019}
1020
1021/*
1022  set_levent_active(event,state)
1023  event is an event id (number) or name (string)
1024  state is a numeric or boolean state. true or non zero numbers turn on zero, false or nil turn off
1025  exact meaning is unknown, but it has something to do with the delivery of the specified event.
1026*/
1027static int luaCB_set_levent_active( lua_State* L )
1028{
1029  unsigned event_id;
1030  unsigned state;
1031
1032  event_id = levent_id_from_lua_arg(L,1);
1033  state = on_off_value_from_lua_arg(L,2);
1034  SetLogicalEventActive(event_id,state);
1035  return 0;
1036}
1037
1038/*
1039  set_levent_script_mode(state)
1040  state is numeric or boolean state. true or non zero numbers turn on zero, false or nil turn off
1041  exact meaning is unknown, but it has something to do with the behavior of events and/or SetLogicalEventActive.
1042*/
1043static int luaCB_set_levent_script_mode( lua_State* L )
1044{
1045  SetScriptMode(on_off_value_from_lua_arg(L,1));
1046  return 0;
1047}
1048
1049#if 0
1050static int luaCB_set_capture_mode_type( lua_State* L )
1051{
1052  unsigned i = luaL_checknumber(L,1);
1053  // TODO check against modemap as valid mode!
1054  SetCurrentCaptureModeType(i);
1055  return 0;
1056}
1057#endif
1058
1059/*
1060  set_record(state)
1061  if state is 0 (or false) the camera is set to play mode. If 1 or true, the camera is set to record mode.
1062*/
1063static int luaCB_set_record( lua_State* L )
1064{
1065  if(on_off_value_from_lua_arg(L,1)) {
1066    levent_set_record();
1067  }
1068  else {
1069    levent_set_play();
1070  }
1071  return 0;
1072}
1073
1074void register_lua_funcs( lua_State* L )
1075{
1076#define FUNC( X )                       \
1077  lua_pushcfunction( L, luaCB_##X );    \
1078  lua_setglobal( L, #X )
1079
1080  FUNC(shoot);
1081  FUNC(sleep);
1082  FUNC(cls);
1083
1084  lua_pushlightuserdata( L, kbd_sched_click );
1085  lua_pushcclosure( L, luaCB_keyfunc, 1 );
1086  lua_setglobal( L, "click" );
1087
1088  lua_pushlightuserdata( L, kbd_sched_press );
1089  lua_pushcclosure( L, luaCB_keyfunc, 1 );
1090  lua_setglobal( L, "press" );
1091
1092  lua_pushlightuserdata( L, kbd_sched_release );
1093  lua_pushcclosure( L, luaCB_keyfunc, 1 );
1094  lua_setglobal( L, "release" );
1095
1096  FUNC(get_av96);
1097  FUNC(get_av96);
1098  FUNC(get_bv96);
1099  FUNC(get_day_seconds);
1100  FUNC(get_disk_size);
1101  FUNC(get_dof);
1102  FUNC(get_far_limit);
1103  FUNC(get_free_disk_space);
1104  FUNC(get_focus);
1105  FUNC(get_hyp_dist);
1106  FUNC(get_iso_market);
1107  FUNC(get_iso_mode);
1108  FUNC(get_iso_real);
1109  FUNC(get_jpg_count);
1110  FUNC(get_near_limit);
1111  FUNC(get_prop);
1112  FUNC(get_raw_count);
1113  FUNC(get_raw_nr);
1114  FUNC(get_raw);
1115  FUNC(get_sv96);
1116  FUNC(get_tick_count);
1117  FUNC(get_tv96);
1118  FUNC(get_user_av_id);
1119  FUNC(get_user_av96);
1120  FUNC(get_user_tv_id);
1121  FUNC(get_user_tv96);
1122  FUNC(get_vbatt);
1123  FUNC(get_zoom);
1124  FUNC(get_exp_count);
1125  FUNC(get_flash_params_count);
1126  FUNC(get_parameter_data);
1127
1128  FUNC(set_av96_direct);
1129  FUNC(set_av96);
1130  FUNC(set_focus);
1131  FUNC(set_iso_mode);
1132  FUNC(set_iso_real);
1133  FUNC(set_led);
1134  FUNC(set_nd_filter);
1135  FUNC(set_prop);
1136  FUNC(set_raw_nr);
1137  FUNC(set_raw);
1138  FUNC(set_sv96);
1139  FUNC(set_tv96_direct);
1140  FUNC(set_tv96);
1141  FUNC(set_user_av_by_id_rel);
1142  FUNC(set_user_av_by_id);
1143  FUNC(set_user_av96);
1144  FUNC(set_user_tv_by_id_rel);
1145  FUNC(set_user_tv_by_id);
1146  FUNC(set_user_tv96);
1147  FUNC(set_zoom_speed);
1148  FUNC(set_zoom_rel);
1149  FUNC(set_zoom);
1150
1151  FUNC(wait_click);
1152  FUNC(is_pressed);
1153  FUNC(is_key);
1154#ifdef CAM_HAS_JOGDIAL
1155  FUNC(wheel_right);
1156  FUNC(wheel_left);
1157#endif
1158  FUNC(md_get_cell_diff);
1159  FUNC(md_detect_motion);
1160  FUNC(autostarted);
1161  FUNC(get_autostart);
1162  FUNC(set_autostart);
1163  FUNC(get_usb_power);
1164  FUNC(exit_alt);
1165  FUNC(shut_down);
1166  FUNC(print_screen);
1167
1168  FUNC(get_focus_mode);
1169  FUNC(get_propset);
1170  FUNC(get_zoom_steps);
1171  FUNC(get_drive_mode);
1172  FUNC(get_flash_mode);
1173  FUNC(get_shooting);
1174  FUNC(get_flash_ready);
1175  FUNC(get_IS_mode);
1176  FUNC(set_ev);
1177  FUNC(get_ev);
1178  FUNC(get_orientation_sensor);
1179  FUNC(get_nd_present);
1180  FUNC(get_movie_status);
1181  FUNC(set_movie_status);
1182 
1183  FUNC(get_histo_range);
1184  FUNC(shot_histo_enable);
1185  FUNC(play_sound);
1186  FUNC(get_temperature);
1187  FUNC(peek);
1188  FUNC(poke);
1189  FUNC(bitand);
1190  FUNC(bitor);
1191  FUNC(bitxor);
1192  FUNC(bitshl);
1193  FUNC(bitshri);
1194  FUNC(bitshru);
1195  FUNC(bitnot);
1196
1197  FUNC(get_time);
1198
1199  FUNC(get_buildinfo);
1200  FUNC(get_mode);
1201 
1202  FUNC(set_raw_develop);
1203  // NOTE these functions normally run in the spytask.
1204  // called from lua they will run from kbd task instead
1205  FUNC(raw_merge_start);
1206  FUNC(raw_merge_add_file);
1207  FUNC(raw_merge_end);
1208  FUNC(set_backlight);
1209   FUNC(set_aflock);
1210#ifdef OPT_CURVES
1211   FUNC(set_curve_state);
1212#endif
1213// get levent definition by name or id, nil if not found
1214   FUNC(get_levent_def);
1215// get levent definition by index, nil if out of range
1216   FUNC(get_levent_def_by_index);
1217// get levent index from name or ID
1218   FUNC(get_levent_index);
1219   FUNC(post_levent_to_ui);
1220   FUNC(post_levent_for_npt);
1221   FUNC(set_levent_active);
1222   FUNC(set_levent_script_mode);
1223//   FUNC(set_capture_mode_type);
1224
1225   FUNC(set_record);
1226}
Note: See TracBrowser for help on using the repository browser.