-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathluascript.c
1878 lines (1678 loc) · 45.5 KB
/
luascript.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "luascript.h"
#include "kbd.h"
#include "platform.h"
#include "script.h"
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#include "conf.h"
#include "shot_histogram.h"
#include "stdlib.h"
#include "raw.h"
#include "raw_merge.h"
#include "levent.h"
#include "console.h"
#include "action_stack.h"
#include "motion_detector.h"
#include "ptp.h"
#include "core.h"
#include "../lib/lua/lstate.h" // for L->nCcalls, baseCcalls
lua_State* L;
lua_State* Lt;
static int lua_script_is_ptp;
static int yield_hook_enabled;
static void lua_script_disable_yield_hook(void) {
yield_hook_enabled = 0;
}
static void lua_script_enable_yield_hook(void) {
yield_hook_enabled = 1;
}
#ifdef CAM_CHDK_PTP
// create a ptp message from the given stack index
// incompatible types will return a TYPE_UNSUPPORTED message
static ptp_script_msg *lua_create_usb_msg( lua_State* L, int index, unsigned msgtype) {
// TODO maybe we should just pass the lua type constants
unsigned datatype, datasize = 4;
int ivalue = 0;
void *data = &ivalue;
int ltype = lua_type(L,index);
switch(ltype) {
case LUA_TNONE:
return NULL; // nothing on the stack, no message generated
break;
case LUA_TNIL:
datatype = PTP_CHDK_TYPE_NIL;
break;
case LUA_TBOOLEAN:
datatype = PTP_CHDK_TYPE_BOOLEAN;
ivalue = lua_toboolean(L,index);
break;
case LUA_TNUMBER:
datatype = PTP_CHDK_TYPE_INTEGER;
ivalue = lua_tonumber(L,index);
break;
case LUA_TSTRING:
datatype = PTP_CHDK_TYPE_STRING;
data = (char *)lua_tolstring(L,index,&datasize);
break;
// TODO this uses usb_msg_table_to_string to serialize the table
// the default format is described in
// http://chdk.setepontos.com/index.php?topic=4338.msg62606#msg62606
// other formats can be implemented by overriding this function in your lua code
case LUA_TTABLE: {
int result;
lua_script_disable_yield_hook(); // don't want to yield while converting
lua_getglobal(L, "usb_msg_table_to_string"); // push function
lua_pushvalue(L, index); // copy specified index to top of stack
result = lua_pcall(L,1,1,0); // this will leave an error message as a string on the stack if call fails
lua_script_enable_yield_hook();
if( result ) {
// if called from lua, throw a normal error
if( msgtype == PTP_CHDK_S_MSGTYPE_USER ) {
luaL_error(L,lua_tostring(L,-1));
return NULL; // not reached
} else { // if it's a return, convert the message to an ERR
msgtype = PTP_CHDK_S_MSGTYPE_ERR;
datatype = PTP_CHDK_S_ERRTYPE_RUN;
data = (char *)lua_tolstring(L,-1,&datasize);
break;
}
}
// an empty table is returned as an empty string by default
// a non-string should never show up here
if ( !lua_isstring(L,-1) ) {
return NULL;
}
datatype = PTP_CHDK_TYPE_TABLE;
data = (char *)lua_tolstring(L,-1,&datasize);
lua_pop(L,1);
}
break;
default:
datatype = PTP_CHDK_TYPE_UNSUPPORTED;
data = (char *)lua_typename(L,ltype); // return type name as message data
datasize = strlen(data);
}
return ptp_script_create_msg(msgtype,datatype,datasize,data);
}
void lua_script_error_ptp(int runtime, const char *err) {
if(runtime) {
ptp_script_write_error_msg(PTP_CHDK_S_ERRTYPE_RUN, err);
script_end();
} else {
ptp_script_write_error_msg(PTP_CHDK_S_ERRTYPE_COMPILE, err);
lua_script_reset();
}
}
#endif
void lua_script_reset()
{
lua_close( L );
L = 0;
}
static void lua_count_hook(lua_State *L, lua_Debug *ar)
{
if( L->nCcalls <= L->baseCcalls && yield_hook_enabled )
lua_yield( L, 0 );
}
void lua_script_error(lua_State *Lt,int runtime)
{
const char *err = lua_tostring( Lt, -1 );
script_console_add_line( err );
if(lua_script_is_ptp) {
#ifdef CAM_CHDK_PTP
lua_script_error_ptp(runtime,err);
#endif
} else {
if(runtime) {
if(conf.debug_lua_restart_on_error) {
lua_script_reset();
script_start_gui(0);
} else {
script_wait_and_end();
}
} else {
script_print_screen_end();
script_wait_and_end();
}
}
}
// TODO more stuff from script.c should be moved here
void lua_script_finish(lua_State *L)
{
#ifdef CAM_CHDK_PTP
if(lua_script_is_ptp) {
// send all return values as RET messages
int i,end = lua_gettop(L);
for(i=1;i<=end; i++) {
ptp_script_msg *msg = lua_create_usb_msg(L,i,PTP_CHDK_S_MSGTYPE_RET);
// if the queue is full return values will be silently discarded
// incompatible types will be returned as TYPE_UNSUPPORTED to preserve expected number and order of return values
if(msg) {
ptp_script_write_msg(msg);
// create_usb_msg may convert the message to an error
if(msg->type != PTP_CHDK_S_MSGTYPE_RET) {
break;
}
} else {
ptp_script_write_error_msg(PTP_CHDK_S_ERRTYPE_RUN, "error creating return msg");
break;
}
}
}
#endif
}
int lua_script_start( char const* script, int ptp )
{
lua_script_is_ptp = ptp;
L = lua_open();
luaL_openlibs( L );
register_lua_funcs( L );
Lt = lua_newthread( L );
lua_setfield( L, LUA_REGISTRYINDEX, "Lt" );
if( luaL_loadstring( Lt, script ) != 0 ) {
lua_script_error(Lt,0);
return 0;
}
lua_sethook(Lt, lua_count_hook, LUA_MASKCOUNT, 1000 );
lua_script_enable_yield_hook();
return 1;
}
// run the "restore" function at the end of a script
void lua_run_restore()
{
lua_getglobal(Lt, "restore");
if (lua_isfunction(Lt, -1)) {
if (lua_pcall( Lt, 0, 0, 0 )) {
script_console_add_line( lua_tostring( Lt, -1 ) );
}
}
}
// get key ID of key name at arg, throw error if invalid
static int lua_get_key_arg( lua_State * L, int narg )
{
int k = script_keyid_by_name( luaL_checkstring( L, narg ) );
if(!k)
luaL_error( L, "unknown key" );
return k;
}
#ifdef OPT_CURVES
#include "curves.h"
static int luaCB_set_curve_state( lua_State* L )
{
int value;
value=luaL_checknumber( L, 1 );
curve_set_mode(value);
return 0;
}
#endif
static int luaCB_set_aflock(lua_State* L)
{
int val = luaL_checknumber(L, 1);
if (val>0) DoAFLock(); // 1: enable AFLock
else UnlockAF(); // 0: disable unlock AF
return 0;
}
static int luaCB_shoot( lua_State* L )
{
action_push(AS_SHOOT);
return lua_yield( L, 0 );
}
static int luaCB_sleep( lua_State* L )
{
action_push_delay( luaL_checknumber( L, 1 ) );
return lua_yield( L, 0 );
}
// for press,release and click
static int luaCB_keyfunc( lua_State* L )
{
void* func = lua_touserdata( L, lua_upvalueindex(1) );
((void(*)(long))func)( lua_get_key_arg( L, 1 ) );
return lua_yield( L, 0 );
}
static int luaCB_cls( lua_State* L )
{
console_clear();
return 0;
}
static int luaCB_set_console_layout( lua_State* L )
{
console_set_layout(luaL_checknumber( L, 1 ),luaL_checknumber( L, 2 ),luaL_checknumber( L, 3 ),luaL_checknumber( L, 4 ));
return 0;
}
static int luaCB_set_console_autoredraw( lua_State* L )
{
console_set_autoredraw(luaL_checknumber(L,1));
return 0;
}
static int luaCB_console_redraw( lua_State* L )
{
console_redraw();
return 0;
}
static int luaCB_get_av96( lua_State* L )
{
lua_pushnumber( L, shooting_get_av96() );
return 1;
}
static int luaCB_get_bv96( lua_State* L )
{
lua_pushnumber( L, shooting_get_bv96() );
return 1;
}
static int luaCB_get_day_seconds( lua_State* L )
{
lua_pushnumber( L, shooting_get_day_seconds() );
return 1;
}
static int luaCB_get_disk_size( lua_State* L )
{
lua_pushnumber( L, GetTotalCardSpaceKb() );
return 1;
}
static int luaCB_get_dof( lua_State* L )
{
lua_pushnumber( L, shooting_get_depth_of_field() );
return 1;
}
static int luaCB_get_far_limit( lua_State* L )
{
lua_pushnumber( L, shooting_get_far_limit_of_acceptable_sharpness() );
return 1;
}
static int luaCB_get_free_disk_space( lua_State* L )
{
lua_pushnumber( L, GetFreeCardSpaceKb() );
return 1;
}
static int luaCB_get_focus( lua_State* L )
{
lua_pushnumber( L, shooting_get_subject_distance() );
return 1;
}
static int luaCB_get_hyp_dist( lua_State* L )
{
lua_pushnumber( L, shooting_get_hyperfocal_distance() );
return 1;
}
static int luaCB_get_iso_market( lua_State* L )
{
lua_pushnumber( L, shooting_get_iso_market() );
return 1;
}
static int luaCB_get_iso_mode( lua_State* L )
{
lua_pushnumber( L, shooting_get_iso_mode() );
return 1;
}
static int luaCB_get_iso_real( lua_State* L )
{
lua_pushnumber( L, shooting_get_iso_real() );
return 1;
}
static int luaCB_get_jpg_count( lua_State* L )
{
lua_pushnumber( L, GetJpgCount() );
return 1;
}
static int luaCB_get_near_limit( lua_State* L )
{
lua_pushnumber( L, shooting_get_near_limit_of_acceptable_sharpness() );
return 1;
}
static int luaCB_get_prop( lua_State* L )
{
lua_pushnumber( L, shooting_get_prop( luaL_checknumber( L, 1 ) ) );
return 1;
}
static int luaCB_get_raw_count( lua_State* L )
{
lua_pushnumber( L, GetRawCount() );
return 1;
}
static int luaCB_get_sv96( lua_State* L )
{
lua_pushnumber( L, shooting_get_sv96() );
return 1;
}
static int luaCB_get_tick_count( lua_State* L )
{
lua_pushnumber( L, shooting_get_tick_count() );
return 1;
}
static int luaCB_get_exp_count( lua_State* L )
{
lua_pushnumber( L, get_exposure_counter() );
return 1;
}
static int luaCB_get_tv96( lua_State* L )
{
lua_pushnumber( L, shooting_get_tv96() );
return 1;
}
static int luaCB_get_user_av_id( lua_State* L )
{
lua_pushnumber( L, shooting_get_user_av_id() );
return 1;
}
static int luaCB_get_user_av96( lua_State* L )
{
lua_pushnumber( L, shooting_get_user_av96() );
return 1;
}
static int luaCB_get_user_tv_id( lua_State* L )
{
lua_pushnumber( L, shooting_get_user_tv_id() );
return 1;
}
static int luaCB_get_user_tv96( lua_State* L )
{
lua_pushnumber( L, shooting_get_user_tv96() );
return 1;
}
static int luaCB_get_vbatt( lua_State* L )
{
lua_pushnumber( L, stat_get_vbatt() );
return 1;
}
static int luaCB_get_zoom( lua_State* L )
{
lua_pushnumber( L, shooting_get_zoom() );
return 1;
}
static int luaCB_get_parameter_data( lua_State* L )
{
extern long* FlashParamsTable[];
unsigned size;
unsigned id = luaL_checknumber( L, 1 );
unsigned val;
if (id >= get_flash_params_count()) {
// return nil
return 0;
}
size = FlashParamsTable[id][1]>>16;
if (size == 0) {
// return nil
return 0;
}
if (size >= 1 && size <= 4) {
val = 0;
get_parameter_data( id, &val, size );
lua_pushlstring( L, (char *)&val, size );
// for convenience, params that fit in a number are returned in one as a second result
lua_pushnumber( L, val );
return 2;
}
else {
char *buf = malloc(size);
if(!buf) {
luaL_error( L, "malloc failed in luaCB_get_parameter_data" );
}
get_parameter_data( id, buf, size );
lua_pushlstring( L, buf, size );
free(buf);
return 1;
}
}
static int luaCB_get_flash_params_count( lua_State* L )
{
lua_pushnumber( L, get_flash_params_count() );
return 1;
}
static int luaCB_set_av96_direct( lua_State* L )
{
shooting_set_av96_direct( luaL_checknumber( L, 1 ), SET_LATER );
return 0;
}
static int luaCB_set_av96( lua_State* L )
{
shooting_set_av96( luaL_checknumber( L, 1 ), SET_LATER );
return 0;
}
static int luaCB_set_focus( lua_State* L )
{
int to = luaL_checknumber( L, 1 );
int m=mode_get()&MODE_SHOOTING_MASK;
int mode_video=MODE_IS_VIDEO(m);
#if CAM_HAS_MANUAL_FOCUS
if (shooting_get_focus_mode() || (mode_video)) shooting_set_focus(to, SET_NOW);
else shooting_set_focus(to, SET_LATER);
#else
if (mode_video) shooting_set_focus(to, SET_NOW);
else shooting_set_focus(to, SET_LATER);
#endif
return 0;
}
static int luaCB_set_iso_mode( lua_State* L )
{
shooting_set_iso_mode( luaL_checknumber( L, 1 ) );
return 0;
}
static int luaCB_set_iso_real( lua_State* L )
{
shooting_set_iso_real( luaL_checknumber( L, 1 ), SET_LATER);
return 0;
}
static int luaCB_set_led( lua_State* L )
{
int to, to1, to2;
to = luaL_checknumber( L, 1 );
to1 = luaL_checknumber( L, 2 );
to2 = 200;
if( lua_isnumber( L, 3 ) )
to2 = lua_tonumber( L, 3 );
camera_set_led(to, to1, to2);
return 0;
}
static int luaCB_set_nd_filter( lua_State* L )
{
shooting_set_nd_filter_state( luaL_checknumber( L, 1 ), SET_LATER);
return 0;
}
static int luaCB_set_prop( lua_State* L )
{
int to, to1;
to = luaL_checknumber( L, 1 );
to1 = luaL_checknumber( L, 2 );
shooting_set_prop(to, to1);
return 0;
}
static int luaCB_set_raw_nr( lua_State* L )
{
camera_set_nr(luaL_checknumber( L, 1 ));
return 0;
}
static int luaCB_get_raw_nr( lua_State* L )
{
lua_pushnumber( L, camera_get_nr() );
return 1;
}
static int luaCB_set_raw( lua_State* L )
{
camera_set_raw(luaL_checknumber( L, 1 ));
return 0;
}
static int luaCB_get_raw( lua_State* L )
{
lua_pushnumber( L, conf.save_raw );
return 1;
}
static int luaCB_set_sv96( lua_State* L )
{
shooting_set_sv96(luaL_checknumber( L, 1 ), SET_LATER);
return 0;
}
static int luaCB_set_tv96_direct( lua_State* L )
{
shooting_set_tv96_direct(luaL_checknumber( L, 1 ), SET_LATER);
return 0;
}
static int luaCB_set_tv96( lua_State* L )
{
shooting_set_tv96(luaL_checknumber( L, 1 ), SET_LATER);
return 0;
}
static int luaCB_set_user_av_by_id_rel( lua_State* L )
{
shooting_set_user_av_by_id_rel(luaL_checknumber( L, 1 ));
return 0;
}
static int luaCB_set_user_av_by_id( lua_State* L )
{
shooting_set_user_av_by_id(luaL_checknumber( L, 1 ));
return 0;
}
static int luaCB_set_user_av96( lua_State* L )
{
shooting_set_user_av96(luaL_checknumber( L, 1 ));
return 0;
}
static int luaCB_set_user_tv_by_id_rel( lua_State* L )
{
shooting_set_user_tv_by_id_rel(luaL_checknumber( L, 1 ));
return 0;
}
static int luaCB_set_user_tv_by_id( lua_State* L )
{
shooting_set_user_tv_by_id(luaL_checknumber( L, 1 ));
return 0;
}
static int luaCB_set_user_tv96( lua_State* L )
{
shooting_set_user_tv96(luaL_checknumber( L, 1 ));
return 0;
}
static int luaCB_set_zoom_speed( lua_State* L )
{
shooting_set_zoom_speed(luaL_checknumber( L, 1 ));
return 0;
}
static int luaCB_set_zoom_rel( lua_State* L )
{
shooting_set_zoom_rel(luaL_checknumber( L, 1 ));
return 0;
}
static int luaCB_set_zoom( lua_State* L )
{
shooting_set_zoom(luaL_checknumber( L, 1 ));
return 0;
}
static int luaCB_wait_click( lua_State* L )
{
int timeout = luaL_optnumber( L, 1, 0 );
action_wait_for_click(timeout);
return lua_yield( L, 0 );
}
static int luaCB_is_pressed( lua_State* L )
{
lua_pushboolean( L, script_key_is_pressed(lua_get_key_arg( L, 1 )));
return 1;
}
static int luaCB_is_key( lua_State* L )
{
lua_pushboolean( L, script_key_is_clicked(lua_get_key_arg( L, 1 )));
return 1;
}
#if CAM_HAS_JOGDIAL
static int luaCB_wheel_right( lua_State* L )
{
JogDial_CW();
return 0;
}
static int luaCB_wheel_left( lua_State* L )
{
JogDial_CCW();
return 0;
}
#endif
static int luaCB_md_get_cell_diff( lua_State* L )
{
lua_pushnumber( L, md_get_cell_diff(luaL_checknumber(L,1),
luaL_checknumber(L,2)));
return 1;
}
static int luaCB_md_detect_motion( lua_State* L )
{
int columns = (luaL_optnumber(L,1,6));
int rows = (luaL_optnumber(L,2,4));
int pixel_measure_mode = (luaL_optnumber(L,3,1));
int detection_timeout = (luaL_optnumber(L,4,10000));
int measure_interval = (luaL_optnumber(L,5,7));
int threshold = (luaL_optnumber(L,6,10));
int draw_grid = (luaL_optnumber(L,7,1));
// arg 8 is the return value in ubasic. We
// ignore it here. - AUJ
int clipping_region_mode = (luaL_optnumber(L,9,0));
int clipping_region_column1 = (luaL_optnumber(L,10,0));
int clipping_region_row1 = (luaL_optnumber(L,11,0));
int clipping_region_column2 = (luaL_optnumber(L,12,0));
int clipping_region_row2 = (luaL_optnumber(L,13,0));
int parameters = (luaL_optnumber(L,14,1));
int pixels_step = (luaL_optnumber(L,15,6));
int msecs_before_trigger = (luaL_optnumber(L,16,0));
if(md_init_motion_detector(
columns, rows, pixel_measure_mode, detection_timeout,
measure_interval, threshold, draw_grid,
clipping_region_mode,
clipping_region_column1, clipping_region_row1,
clipping_region_column2, clipping_region_row2,
parameters, pixels_step, msecs_before_trigger
))
return lua_yield(L, 0);
else
return luaL_error( L, "md_init_motion_detector failed" );
}
static int luaCB_autostarted( lua_State* L )
{
lua_pushboolean( L, camera_get_script_autostart() );
return 1;
}
static int luaCB_get_autostart( lua_State* L )
{
lua_pushnumber( L, conf.script_startup );
return 1;
}
static int luaCB_set_autostart( lua_State* L )
{
int to;
to = luaL_checknumber( L, 1 );
if ( to >= 0 && to <= 2 ) conf.script_startup = to;
conf_save();
return 0;
}
static int luaCB_get_usb_power( lua_State* L )
{
if (luaL_optnumber( L, 1, 0 )) lua_pushnumber( L, get_usb_power(1) );
else lua_pushnumber( L, get_usb_power(0) );
return 1;
}
static int luaCB_exit_alt( lua_State* L )
{
exit_alt();
return 0;
}
// optional parameter is 0 for soft shutdown (default) or 1 for hard/immediate
static int luaCB_shut_down( lua_State* L )
{
if ( luaL_optnumber(L,1,0) == 1 )
{
shutdown();
} else {
camera_shutdown_in_a_second();
}
return 0;
}
static int luaCB_print_screen( lua_State* L )
{
if (lua_isboolean( L, 1 ))
script_print_screen_statement( lua_toboolean( L, 1 ) );
else
script_print_screen_statement( luaL_checknumber( L, 1 )+10000 );
return 0;
}
static int luaCB_get_movie_status( lua_State* L )
{
lua_pushnumber( L, movie_status );
return 1;
}
static int luaCB_set_movie_status( lua_State* L )
{
int to;
switch(luaL_checknumber( L, 1 )) {
case 1:
if (movie_status == 4) {
movie_status = 1;
}
break;
case 2:
if (movie_status == 1) {
movie_status = 4;
}
break;
case 3:
if (movie_status == 1 || movie_status == 4) {
movie_status = 5;
}
break;
}
return 0;
}
static int luaCB_get_drive_mode( lua_State* L )
{
lua_pushnumber( L, shooting_get_drive_mode() );
return 1;
}
static int luaCB_get_focus_mode( lua_State* L )
{
lua_pushnumber( L, shooting_get_prop(PROPCASE_FOCUS_MODE) );
return 1;
}
static int luaCB_get_flash_mode( lua_State* L )
{
lua_pushnumber( L, shooting_get_prop(PROPCASE_FLASH_MODE) );
return 1;
}
static int luaCB_get_shooting( lua_State* L )
{
lua_pushboolean( L, shooting_get_prop(PROPCASE_SHOOTING) );
return 1;
}
static int luaCB_get_flash_ready( lua_State* L )
{
lua_pushboolean( L, shooting_get_prop(PROPCASE_IS_FLASH_READY) );
return 1;
}
static int luaCB_get_IS_mode( lua_State* L )
{
lua_pushnumber( L, shooting_get_prop(PROPCASE_IS_MODE) );
return 1;
}
static int luaCB_get_orientation_sensor( lua_State* L )
{
lua_pushnumber( L, shooting_get_prop(PROPCASE_ORIENTATION_SENSOR) );
return 1;
}
static int luaCB_get_zoom_steps( lua_State* L )
{
lua_pushnumber( L, zoom_points );
return 1;
}
static int luaCB_get_nd_present( lua_State* L )
{
int to;
#if !CAM_HAS_ND_FILTER
to = 0;
#endif
#if CAM_HAS_ND_FILTER && !CAM_HAS_IRIS_DIAPHRAGM
to = 1;
#endif
#if CAM_HAS_ND_FILTER && CAM_HAS_IRIS_DIAPHRAGM
to = 2;
#endif
lua_pushnumber( L, to );
return 1;
}
static int luaCB_get_propset( lua_State* L )
{
lua_pushnumber( L, CAM_PROPSET );
return 1;
}
static int luaCB_get_ev( lua_State* L )
{
lua_pushnumber( L, shooting_get_prop(PROPCASE_EV_CORRECTION_1) );
return 1;
}
static int luaCB_set_ev( lua_State* L )
{
int to;
to = luaL_checknumber( L, 1 );
shooting_set_prop(PROPCASE_EV_CORRECTION_1, to);
shooting_set_prop(PROPCASE_EV_CORRECTION_2, to);
return 0;
}
static int luaCB_get_histo_range( lua_State* L )
{
int from = (luaL_checknumber(L,1));
int to = (luaL_checknumber(L,2));
if (shot_histogram_isenabled()) lua_pushnumber( L, shot_histogram_get_range(from, to) );
else lua_pushnumber( L, -1 ); // TODO should probably return nil
return 1;
}
static int luaCB_shot_histo_enable( lua_State* L )
{
shot_histogram_set(luaL_checknumber( L, 1 ));
return 0;
}
static int luaCB_play_sound( lua_State* L )
{
play_sound(luaL_checknumber( L, 1 ));
return 0;
}
static int luaCB_get_temperature( lua_State* L )
{
int which = (luaL_checknumber( L, 1 ));
int temp = -100; // do something insane if users passes bad value
switch (which)
{
case 0:
temp = get_optical_temp();
break;
case 1:
temp = get_ccd_temp();
break;
case 2:
temp = get_battery_temp();
break;
}
lua_pushnumber( L, temp );
return 1;
}
static int luaCB_get_time( lua_State* L )
{
int r = -1;
unsigned long t2 = time(NULL);
static struct tm *ttm;
ttm = localtime(&t2);
const char *t = luaL_checkstring( L, 1 );
if (strncmp("s", t, 1)==0) r = ( L, ttm->tm_sec );
else if (strncmp("m", t, 1)==0) r = ( L, ttm->tm_min );
else if (strncmp("h", t, 1)==0) r = ( L, ttm->tm_hour );
else if (strncmp("D", t, 1)==0) r = ( L, ttm->tm_mday );
else if (strncmp("M", t, 1)==0) r = ( L, ttm->tm_mon+1 );
else if (strncmp("Y", t, 1)==0) r = ( L, 1900+ttm->tm_year );
lua_pushnumber( L, r );
return 1;
}
/*
val=peek(address[,size])
return the value found at address in memory, or nil if address or size is invalid
size is optional 1=byte 2=halfword 4=word. defaults is 4
*/
static int luaCB_peek( lua_State* L )
{
unsigned addr = luaL_checknumber(L,1);
unsigned size = luaL_optnumber(L, 2, 4);
switch(size) {
case 1:
lua_pushnumber( L, *(unsigned char *)(addr) );
break;
case 2:
if (addr & 0x1) {
lua_pushnil(L);
}
else {
lua_pushnumber( L, *(unsigned short *)(addr) );
}
break;
case 4:
if (addr & 0x3) {
lua_pushnil(L);
}
else {
lua_pushnumber( L, *(unsigned *)(addr) );
}
break;
default:
lua_pushnil(L);
}
return 1;
}
/*
status=poke(address,value[,size])
writes value to address in memory
size is optional 1=byte 2=halfword 4=word. defaults is 4
returns true, or nil if address or size is invalid
*/
static int luaCB_poke( lua_State* L )
{
unsigned addr = luaL_checknumber(L,1);
unsigned val = luaL_checknumber(L,2);
unsigned size = luaL_optnumber(L, 3, 4);
int status = 0;
switch(size) {
case 1:
*(unsigned char *)(addr) = (unsigned char)val;
status=1;
break;
case 2:
if (!(addr & 0x1)) {
*(unsigned short *)(addr) = (unsigned short)val;
status=1;
}