-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelphel_php.c.353
2301 lines (2140 loc) · 98.3 KB
/
elphel_php.c.353
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
/**
* \file elphel_php.c
* \brief Implementation of elphel extension module for PHP to support camera functionality
* \date 2008
*/
/*!***************************************************************************
*! FILE NAME : elphel_php.c
*! DESCRIPTION: Implementation of elphel extension module for PHP
*! Copyright (C) 2008 Elphel, Inc.
*! -----------------------------------------------------------------------------**
*! This program is free software: you can redistribute it and/or modify
*! it under the terms of the GNU General Public License as published by
*! the Free Software Foundation, either version 3 of the License, or
*! (at your option) any later version.
*!
*! This program is distributed in the hope that it will be useful,
*! but WITHOUT ANY WARRANTY; without even the implied warranty of
*! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
*! GNU General Public License for more details.
*!
*! You should have received a copy of the GNU General Public License
*! along with this program. If not, see <http://www.gnu.org/licenses/>.
*! -----------------------------------------------------------------------------**
*! $Log: elphel_php.c,v $
*! Revision 1.8 2012/04/20 00:25:26 elphel
*! fixed bug in reading Exif data from PHP that was introduced while expanding MakerNote to include temperatures
*!
*! Revision 1.7 2012/04/08 04:11:28 elphel
*! 8.2.2 changes related to temperatures measurement and embedding in the Exif MakerNote
*!
*! Revision 1.6 2010/08/10 21:14:31 elphel
*! 8.0.8.39 - added EEPROM support for multiplexor and sensor boards, so autocampars.php uses application-specific defaults. Exif Orientation tag support, camera Model reflects application and optional mode (i.e. camera number in Eyesis)
*!
*! Revision 1.5 2010/07/20 20:13:34 elphel
*! 8.0.8.33 - added MakerNote info for composite images made with multisensor cameras (with 10359 board)
*!
*! Revision 1.4 2010/05/13 03:39:31 elphel
*! 8.0.8.12 - drivers modified for multi-sensor operation
*!
*! Revision 1.3 2010/03/04 06:41:40 elphel
*! 8.0.7.3 - more data to makerNote
*!
*! Revision 1.2 2009/12/28 06:24:17 elphel
*! 8.0.6.6 - added MakerNote to Exif, it icludes channels gains and gammas/black levels
*!
*! Revision 1.1 2009/04/03 23:18:59 elphel
*! rev 8.0.4.1 - upgraded PHP to 5.2.9 - directories/files added/removed
*!
*! Revision 1.5 2009/02/18 06:25:41 elphel
*! fixed unterminated string of 1 character (GPS mode - 2/3)
*!
*! Revision 1.4 2008/12/08 08:17:50 elphel
*! bug fix in elphel_get_P_value() - it did not handle bit filed for non-global variables ( elphel_get_P_arr() was OK)
*!
*! Revision 1.3 2008/11/30 05:04:46 elphel
*! added Doxygen file data
*!
*! Revision 1.2 2008/11/28 08:17:09 elphel
*! keeping Doxygen a little happier
*!
*! Revision 1.1.1.1 2008/11/27 20:04:02 elphel
*!
*!
*! Revision 1.28 2008/11/22 05:56:40 elphel
*! elphel_set_P_arr(), elphel_set_P_value() now return frame number parameters were written to
*!
*! Revision 1.27 2008/11/20 07:06:04 elphel
*! just touched to overcome dependency (or lack of its support) to make it notice updated c313a.h
*!
*! Revision 1.26 2008/11/15 07:04:27 elphel
*! new parameters to modify analog gains while white balancing
*!
*! Revision 1.25 2008/11/13 05:40:45 elphel
*! 8.0.alpha16 - modified histogram storage, profiling
*!
*! Revision 1.24 2008/11/05 02:01:25 elphel
*! Added bit field manipulation in parameters
*!
*! Revision 1.23 2008/11/04 17:41:43 elphel
*! added elphel_gamma(), elphel_reverse_gamma(), elphel_histogram(), elphel_reverse_histogram() functions
*!
*! Revision 1.22 2008/11/02 07:24:20 elphel
*! added support for some legacy functions
*!
*! Revision 1.21 2008/11/02 00:33:02 elphel
*! Added TODO
*!
*! Revision 1.20 2008/11/01 06:25:46 elphel
*! elphel_get_circbuf_pointers() now returns framenumber too
*!
*! Revision 1.19 2008/10/31 18:26:32 elphel
*! Adding support for constants like SENSOR_REGS32 (defined constant plus 32 to simplify referencing sensor registers from PHP
*!
*! Revision 1.18 2008/10/29 04:18:28 elphel
*! v.8.0.alpha10 made a separate structure for global parameters (not related to particular frames in a frame queue)
*!
*! Revision 1.17 2008/10/28 07:05:49 elphel
*! touched
*!
*! Revision 1.16 2008/10/25 19:51:06 elphel
*! Changed word order in writes to gamma tables driver
*!
*! Revision 1.15 2008/10/23 18:26:14 elphel
*! Fixed percentile calculations in histograms
*!
*! Revision 1.14 2008/10/23 08:11:38 elphel
*! updated for histograms wait queues
*!
*! Revision 1.13 2008/10/19 06:56:05 elphel
*! elphel_wait_frame() now works only for compressed frames, new elphel_skip_frames() and elphel_wait_frame_abs() wait sequencer frames (all sensor frames, even those that are not compressed)
*!
*! Revision 1.12 2008/10/15 22:28:56 elphel
*! snapshot 8.0.alpha2
*!
*! Revision 1.11 2008/10/13 16:56:20 elphel
*! just touched to force recompile
*!
*! Revision 1.10 2008/10/12 06:13:10 elphel
*! snapshot
*!
*! Revision 1.9 2008/10/08 21:26:25 elphel
*! snapsot 7.2.0.pre4 - first images (actually - second)
*!
*! Revision 1.8 2008/10/05 05:13:33 elphel
*! snapshot003
*!
*! Revision 1.7 2008/10/04 16:10:12 elphel
*! snapshot
*!
*! Revision 1.6 2008/09/25 00:58:11 elphel
*! snapshot
*!
*! Revision 1.5 2008/09/19 18:06:38 elphel
*! snapshot
*!
*! Revision 1.4 2008/09/19 04:37:24 elphel
*! snapshot
*!
*! Revision 1.3 2008/09/07 19:48:08 elphel
*! snapshot
*!
*! Revision 1.2 2008/06/04 20:07:08 elphel
*! adding support for multiple frames
*!
*! Revision 1.21 2008/05/02 15:12:09 elphel
*! minor comment edit
*!
*! Revision 1.20 2008/05/01 01:32:04 elphel
*! support for the frame number - combining hardware frame counter used by i2c (3-bit) and software one
*!
*! Revision 1.19 2008/04/25 21:31:35 elphel
*! Added Exif_Photo_ExposureTime to be returned by elphel_get_exif_elphel()
*!
*! Revision 1.18 2008/04/24 18:16:40 elphel
*! New function to retrieve circbuf structure (frame pointers and Exif pointers)
*!
*! Revision 1.17 2008/04/22 22:14:08 elphel
*! Added functions related to Exif data
*!
*! Revision 1.16 2008/04/20 06:49:04 elphel
*! Added histogram related functions
*!
*! Revision 1.15 2008/04/17 22:36:07 elphel
*! Bug fix, new function added - elphel_is_compressor_idle()
*!
*! Revision 1.14 2008/03/20 22:25:09 elphel
*! elphel_trigger - programming synchronization parameters
*!
*! Revision 1.13 2008/03/15 23:04:21 elphel
*! added FPGA registers R/W
*!
*! Revision 1.12 2008/01/27 06:23:17 elphel
*! New function - elphel_wait_frame (wait fro the next frame to be compressed)
*!
*! Revision 1.11 2008/01/12 06:53:23 elphel
*! 7.1.7.2 - added elphel_autoexposure_get() function to elphel php extension
*!
*! Revision 1.10 2008/01/11 07:47:44 elphel
*! added elphel_autoexposure_set() function
*!
*! Revision 1.9 2008/01/10 02:43:37 elphel
*! Added balance of 2 greens in Bayer mosaic
*!
*! Revision 1.8 2008/01/09 10:22:02 elphel
*! Implemented elphel_white_balance() function
*!
*! Revision 1.7 2007/12/14 22:38:53 elphel
*! cleaned up after fixing cache bug
*!
*! Revision 1.6 2007/12/06 19:05:33 elphel
*! 2 new functions - arbitrary compressor command (elphel_compressor_cmd) and elphel_compressor_reset
*!
*! Revision 1.5 2007/12/05 23:41:04 elphel
*! Fixing minor in bugs in the sensor reset code
*!
*! Revision 1.4 2007/12/05 22:01:19 elphel
*! added handling etrax fs mmap cach problem when communicating between PHP extension and the driver
*!
*! Revision 1.3 2007/12/04 06:41:11 elphel
*! Implementation of 2 additional functions: elphel_get_P_arr, elphel_set_P_arr
*!
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <sys/mman.h> /* mmap */
#include <sys/ioctl.h>
#include <fcntl.h> /* (O_RDWR) */
#include <asm/byteorder.h>
#include <errno.h>
#include "php.h"
#include "php_ini.h" /* for php.ini processing */
#include "elphel_php.h"
ZEND_DECLARE_MODULE_GLOBALS(elphel)
static function_entry elphel_functions[] = {
PHP_FE(elphel_get_frame, NULL)
PHP_FE(elphel_skip_frames, NULL)
PHP_FE(elphel_wait_frame_abs, NULL)
PHP_FE(elphel_framepars_get_raw, NULL)
PHP_FE(elphel_parse_P_name, NULL)
PHP_FE(elphel_is_global_par, NULL)
PHP_FE(elphel_is_frame_par, NULL)
PHP_FE(elphel_get_P_value, NULL)
PHP_FE(elphel_set_P_value, NULL)
PHP_FE(elphel_get_P_arr, NULL)
PHP_FE(elphel_set_P_arr, NULL)
PHP_FE(elphel_gamma_add, NULL)
PHP_FE(elphel_gamma_add_custom, NULL)
PHP_FE(elphel_gamma_get, NULL)
PHP_FE(elphel_gamma_get_index, NULL)
PHP_FE(elphel_gamma_get_raw, NULL)
PHP_FE(elphel_histogram_get_raw, NULL)
PHP_FE(elphel_histogram_get, NULL)
PHP_FE(elphel_get_state, NULL)
PHP_FE(elphel_compressor_reset, NULL)
PHP_FE(elphel_compressor_run, NULL)
PHP_FE(elphel_compressor_stop, NULL)
PHP_FE(elphel_compressor_frame, NULL)
PHP_FE(elphel_reset_sensor, NULL)
PHP_FE(elphel_set_fpga_time, NULL)
PHP_FE(elphel_get_fpga_time, NULL)
PHP_FE(elphel_wait_frame, NULL)
PHP_FE(elphel_fpga_read, NULL)
PHP_FE(elphel_fpga_write, NULL)
PHP_FE(elphel_gamma, NULL)
PHP_FE(elphel_reverse_gamma, NULL)
PHP_FE(elphel_histogram, NULL)
PHP_FE(elphel_reverse_histogram, NULL)
PHP_FE(elphel_get_exif_field, NULL)
PHP_FE(elphel_set_exif_field, NULL)
PHP_FE(elphel_get_interframe_meta, NULL)
PHP_FE(elphel_get_exif_elphel, NULL)
PHP_FE(elphel_update_exif, NULL)
PHP_FE(elphel_get_circbuf_pointers, NULL)
{NULL, NULL, NULL}
};
zend_module_entry elphel_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
PHP_ELPHEL_EXTNAME,
elphel_functions,
PHP_MINIT(elphel),
PHP_MSHUTDOWN(elphel),
PHP_RINIT(elphel),
NULL,
PHP_MINFO(elphel),
#if ZEND_MODULE_API_NO >= 20010901
PHP_ELPHEL_VERSION,
#endif
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_ELPHEL
ZEND_GET_MODULE(elphel)
#endif
PHP_INI_BEGIN()
//! read ini entries here
PHP_INI_END()
//!call before any access to sensor parameters/functions
static void init_sens() {
}
/**
* @brief See if the name ends with __A,__B,__c,__a,__b.__c
* @param name constant name, possibly ending with a suffix __A,__B,__c,__a,__b or __c
* @return <0 - no suffix, >=0 channel (0..2) | 4 for strict (lower case)
*/
//multiSensIndex
int parseMultiSens(char * name) {
int len= strlen(name);
int nIndex;
int d=0;
int dp=1;
int bfields;
int rslt=-1;
if ((len>3) && (name[len-2]=='_') && (name[len-3]=='_')) {
switch (name[len-1]) {
case 'A':rslt=0; break;
case 'B':rslt=1; break;
case 'C':rslt=2; break;
case 'a':rslt=256; break;
case 'b':rslt=257; break;
case 'c':rslt=258; break;
}
}
if (rslt >=0) name[len-3]='\0';
return rslt;
}
/**
* @brief apply
* @param parNum parameter composite index
* @param multiMod bit 8 - strict, lower bits - channel index
* @return -1 - failure, otherwise - parameter index with allcontrol bits preserved
*/
//multiSensIndex P_MAX_PAR MAX_SENSORS=3
int applyMultiSens(long parNum, long multiMod) {
unsigned long * multiSensIndex = ((unsigned long *) (ELPHEL_G (multiSensIndex)));
long parIndex= parNum &0xffff;
if (parIndex >= P_MAX_PAR) return -1;
if (multiSensIndex[parIndex]==0) {
if (multiMod & 0x100) return -1;
return parNum; /// No individual parameters available, return the original one as 'strict' bit is not set.
}
multiMod &= 0xff;
if (multiMod >= MAX_SENSORS) return -1;
parIndex= (multiSensIndex[parIndex]+multiMod) & 0xffff;
if (parIndex >= P_MAX_PAR) return -1; /// multiSensIndex seems to be corrupted - add error message?
return (parNum & 0xffff0000) | parIndex; /// preserve original modifiers
}
/**
* @brief See if the name ends with number, truncate name to remove number and return number value
* @param name constant name, possibly ending with a number (decimal)
* if constant includes "__" (double "_") and has 4 decimals after __WWBB, those decimals are treated as
* bit field specs (WW 1..31 - width, BB - 0..31 - start bit ), i.e. "__0816" is the 3-rd byte of 4 in the 32-bit parameter
* These bit-filed modifiers are stored in bits 16..20 (bit) and 21..25 (width) of parameter address (number)
* @return number value (to be added to the constant value) or -1 if name does not end with a number
*/
//multiSensIndex
int splitConstantName(char * name) {
int len= strlen(name);
int nIndex;
int d=0;
int dp=1;
int bfields;
int success=0;
if ((len>6) && (name[len-6]=='_') && (name[len-5]=='_')) {
d= FRAMEPAIR_FRAME_BITS(((name[len-3] - '0') + ((name[len-4] - '0') * 10)), ((name[len-1] - '0') + ((name[len-2] - '0') * 10)));
name[len-6]='\0';
len-=6;
success=1;
}
nIndex= len-1;
while ((nIndex>0) && (name[nIndex]>='0') && (name[nIndex] <='9')){
d+=dp*(name[nIndex]-'0');
dp*=10;
nIndex--;
success=1;
}
nIndex++;
if (success) {
name[nIndex]='\0'; /// terminate name with '\0'
return d;
} else return -1;
}
/// Get current frame number
PHP_FUNCTION(elphel_get_frame)
{
RETURN_LONG( ELPHEL_GLOBALPARS(G_THIS_FRAME));
}
PHP_FUNCTION(elphel_skip_frames)
{
long skip=1;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &skip) == FAILURE) {
RETURN_NULL();
}
long target_frame=lseek((int) ELPHEL_G( fd_fparmsall), 0, SEEK_CUR )+skip;
if ((target_frame<0) || (target_frame > 0x7ffffdff)) RETURN_NULL(); /// Out of limit for skip frames
RETURN_LONG(lseek((int) ELPHEL_G( fd_fparmsall), target_frame + LSEEK_FRAME_WAIT_ABS, SEEK_END ));
}
PHP_FUNCTION(elphel_wait_frame_abs)
{
long target_frame;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &target_frame) == FAILURE) {
RETURN_NULL();
}
if ((target_frame<0) || (target_frame > 0x7ffffdff)) RETURN_NULL(); /// Out of limit for skip frames
RETURN_LONG(lseek((int) ELPHEL_G( fd_fparmsall), target_frame + LSEEK_FRAME_WAIT_ABS, SEEK_END ));
}
/**
* @brief Parse P_* /G_* parameter name with modifiers
* @param name - constant name (w/o leading "ELPHEL_")
* @return full address/number or NULL if it does not exist
*/
PHP_FUNCTION(elphel_parse_P_name)
{
char full_constant_name[256];
char *name;
int name_len;
long full_addr =-1;
zval const_value;
long constAddNumber;
long multiMod;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) {
RETURN_NULL();
}
if (strlen(name)>(sizeof(full_constant_name)-8)) RETURN_NULL();
sprintf (full_constant_name,"ELPHEL_%s",name);
if (zend_get_constant(full_constant_name, strlen(full_constant_name), &const_value TSRMLS_CC)) { /// found the constant as is
full_addr= Z_LVAL(const_value);
} else {
multiMod=parseMultiSens(full_constant_name); /// will truncate full_constant_name if sensor number suffix found
constAddNumber=splitConstantName(full_constant_name);
if ((constAddNumber>=0) && (zend_get_constant(full_constant_name, strlen(full_constant_name), &const_value TSRMLS_CC))) {
full_addr= (Z_LVAL(const_value) & ~FRAMEPAIR_MASK_BYTES)+constAddNumber; /// FRAMEPAIR_MASK_BYTES to prevent bit-field modifier addition to constants that already have them
if ((multiMod>=0) && (full_addr!=-1)) full_addr=applyMultiSens(full_addr,multiMod);
}
}
if (full_addr!=-1) {
RETURN_LONG (full_addr);
zval_dtor(&const_value);
}
RETURN_NULL();
}
/**
* @brief Strips integer constant of optional modifiers and finds if it is a valid global parameter number
* @param full parameter address
* @return true if this address is a valid address of a global parameter
*/
PHP_FUNCTION(elphel_is_global_par)
{
long addr;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &addr) == FAILURE) {
RETURN_NULL();
}
addr &= 0xffff; /// remove any possible flags
if ((addr >= FRAMEPAR_GLOBALS) && (addr < (FRAMEPAR_GLOBALS+P_MAX_GPAR))) RETURN_TRUE;
RETURN_FALSE;
}
/**
* @brief Strips integer constant of optional modifiers and finds if it is a valid frame parameter number
* @param full parameter address
* @return true if this address is a valid address of a frame parameter
*/
PHP_FUNCTION(elphel_is_frame_par)
{
long addr;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &addr) == FAILURE) {
RETURN_NULL();
}
addr &= 0xffff; /// remove any possible flags
if (addr < (sizeof (struct framepars_t) >>2)) RETURN_TRUE;
RETURN_FALSE;
}
//! Read value from the sensor/compressor parameters ("read" parameters, verified by the driver), see asm/elphel/c313a.h
///TODO: Make it read pastPars also?
PHP_FUNCTION(elphel_get_P_value)
{
long addr, full_addr;
long frame=-1;
long frame_index=-1;
int frame_stored;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|l", &full_addr,&frame) == FAILURE) {
RETURN_NULL();
}
if (frame <0) {
frame=ELPHEL_GLOBALPARS(G_THIS_FRAME);
}
addr = full_addr & 0xffff; /// remove any possible flags
/// is it a global parameter?
if (addr >= FRAMEPAR_GLOBALS) { /// these globals can be written just through mmap
if (addr >= (FRAMEPAR_GLOBALS+P_MAX_GPAR)) {
RETURN_NULL();
}
if (full_addr & FRAMEPAIR_MASK_BYTES) {
RETURN_LONG(FRAMEPAIR_FRAME_FIELD(full_addr,ELPHEL_GLOBALPARS(addr)));
} else {
RETURN_LONG(ELPHEL_GLOBALPARS(addr));
}
}
/// processing dynamic frame parameters
if ((addr<0) ||(addr>= (sizeof (struct framepars_t) >>2))) {
RETURN_NULL();
}
if (frame <0) {
// frame=ELPHEL_GLOBALPARS(G_THIS_FRAME) + FRAME_DEAFAULT_AHEAD;
frame=ELPHEL_GLOBALPARS(G_THIS_FRAME); /// read current (most recent) frame - different from _set_
}
///try framePars
frame_index = frame & PARS_FRAMES_MASK;
frame_stored= ((struct framepars_t *) ELPHEL_G(framePars))[frame_index].pars[P_FRAME];
if (frame_stored == frame) { /// This is what we are looking for
if (full_addr & FRAMEPAIR_MASK_BYTES) {
RETURN_LONG( FRAMEPAIR_FRAME_FIELD(full_addr,((struct framepars_t *) ELPHEL_G(framePars))[frame_index].pars[addr]));
} else {
RETURN_LONG( ((struct framepars_t *) ELPHEL_G(framePars))[frame_index].pars[addr]);
}
}
if (frame_stored <frame) { /// too early for the frame number specified
RETURN_NULL();
}
/// too late for framePars - let's try pastPars
frame_index = frame & PASTPARS_SAVE_ENTRIES_MASK;
frame_stored= ((struct framepars_past_t *) ELPHEL_G(pastPars))[frame_index].past_pars[P_FRAME-PARS_SAVE_FROM];
if (frame_stored == frame) { /// This is what we are looking for
addr-=PARS_SAVE_FROM;
if ((addr<0) ||(addr>= (sizeof (struct framepars_past_t) >>2))) {
RETURN_NULL();
}
if (full_addr & FRAMEPAIR_MASK_BYTES) {
RETURN_LONG(FRAMEPAIR_FRAME_FIELD(full_addr,((struct framepars_past_t *) ELPHEL_G(pastPars))[frame_index].past_pars[addr]));
} else {
RETURN_LONG( ((struct framepars_past_t *) ELPHEL_G(pastPars))[frame_index].past_pars[addr]);
}
}
/// too late, probably
RETURN_NULL();
}
///NOTE: Just for compatibility with older code
//!Get sensor state, usually 7- sensor is running, compressor is stopped, 8 - compressor is runing
//!program sensor/compressor according to the parameters specified. Argument==0 - restart the sensor, 1 - do it "on the fly"
PHP_FUNCTION(elphel_get_state)
{
long frame8=ELPHEL_GLOBALPARS(G_THIS_FRAME) & PARS_FRAMES_MASK;
long compressor_state= ((struct framepars_t *) ELPHEL_G(framePars))[frame8].pars[P_COMPRESSOR_RUN];
long sensor_state= ((struct framepars_t *) ELPHEL_G(framePars))[frame8].pars[P_SENSOR_RUN];
long result=(compressor_state==COMPRESSOR_RUN_CONT)?0x8:
((compressor_state==COMPRESSOR_RUN_SINGLE)?0xa:
((sensor_state==SENSOR_RUN_CONT)?0x7:0));
RETURN_LONG(result);
}
/**
* @brief return selected (by integer index) framepars structure (struct framepars_t) as a binary string
* @param index - frame index (0..7). -1 - return func2call page instead, -2 - globalPars
* @return NULL - error, otherwise a string with (struct framepars_t)
*
*/
PHP_FUNCTION(elphel_framepars_get_raw)
{
char * packed_framepars_structure;
long index=0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &index) == FAILURE) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Wrong index");
RETURN_NULL ();
}
packed_framepars_structure= (char*) emalloc (sizeof (struct framepars_t));
if (packed_framepars_structure) {
/// use gamma_cache_index to retrieve table from cache
if (index>=0) {
memcpy(packed_framepars_structure, &(((struct framepars_t *) ELPHEL_G(framePars))[index & PARS_FRAMES_MASK]),sizeof(struct framepars_t));
} else if (index == -1) {
memcpy(packed_framepars_structure, ((struct framepars_t *) ELPHEL_G(funcs2call)),sizeof(struct framepars_t));
} else if (index == -2) {
memcpy(packed_framepars_structure, ((struct framepars_t *) ELPHEL_G(globalPars)),sizeof(struct framepars_t));
} else RETURN_NULL ();
RETURN_STRINGL (packed_framepars_structure, sizeof(struct framepars_t), 0);
}
php_error_docref(NULL TSRMLS_CC, E_ERROR, "emalloc error");
RETURN_NULL ();
}
//! This function reads associative array and uses the keys as a template for the result array.
//! If mey is one of the defined P_VALUE names (same as global constant but w/o "ELPHEL_" prefix)
//! then the result array will have element with the same key and the value equal to the value
//! of the camera parameter
/// TODO:Make it per frame (absolute), combine with past frames
/// frame absent - "current frame"
/// frame ==0 - frame zero (static?)
PHP_FUNCTION(elphel_get_P_arr)
{
long frame=-1;
long frame_index=-1;
int future=1;
int frame_stored;
long addr,full_addr,val;
char full_constant_name[256];
zval *arr, **data;
HashTable *arr_hash;
HashPosition pointer;
char *key;
int key_len;
long index;
long constAddNumber; /// number to add to the ELPHEL_* constant value
zval const_value;
long multiMod;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|l", &arr, &frame) == FAILURE) {
RETURN_NULL();
}
// init_sens();
/// first see if this frame is either in the future, past or is frame zero (parameters that are not related to frames)
if (frame < 0) { /// frame number not provided - use latest
frame=ELPHEL_GLOBALPARS(G_THIS_FRAME);
}
/// try future first
frame_index = frame & PARS_FRAMES_MASK;
frame_stored= ((struct framepars_t *) ELPHEL_G(framePars))[frame_index].pars[P_FRAME];
if (frame_stored <frame) { /// too early for the frame number specified
future=-1 ; /// not available - only global parameters could be retrieved
}
if (frame_stored > frame) { /// Maybe it is in the past frames (only subset of parameters preserved)
frame_index = frame & PASTPARS_SAVE_ENTRIES_MASK;
frame_stored= ((struct framepars_past_t *) ELPHEL_G(pastPars))[frame_index].past_pars[P_FRAME-PARS_SAVE_FROM];
if (frame_stored != frame) { /// Too late, probably - all the records are gone by now
future=-1 ;// not available - only global parameters could be retrieved
} else future=0; /// should be there, but in the past
}
array_init(return_value);
arr_hash = Z_ARRVAL_P(arr);
for(zend_hash_internal_pointer_reset_ex(arr_hash, &pointer);
zend_hash_get_current_data_ex(arr_hash, (void**) &data, &pointer) == SUCCESS;
zend_hash_move_forward_ex(arr_hash, &pointer)) {
if (zend_hash_get_current_key_ex(arr_hash, &key, &key_len, &index, 0, &pointer) == HASH_KEY_IS_STRING) {
if (strlen(key)>(sizeof(full_constant_name)-8)) RETURN_NULL();
sprintf (full_constant_name,"ELPHEL_%s",key);
full_addr =-1;
if (zend_get_constant(full_constant_name, strlen(full_constant_name), &const_value TSRMLS_CC)) { /// found the constant as is
full_addr= Z_LVAL(const_value);
} else {
multiMod=parseMultiSens(full_constant_name); /// will truncate full_constant_name if sensor number suffix found
constAddNumber=splitConstantName(full_constant_name);
// php_printf ("constAddNumber=%d, full_constant_name=%s (length=%d)\n",constAddNumber, full_constant_name,strlen(full_constant_name));
if ((constAddNumber>=0) && (zend_get_constant(full_constant_name, strlen(full_constant_name), &const_value TSRMLS_CC))) { /// Try to remove number from the end
full_addr= (Z_LVAL(const_value) & ~FRAMEPAIR_MASK_BYTES)+constAddNumber; /// FRAMEPAIR_MASK_BYTES to prevent bit-field modifier addition to constants that already have them
if ((multiMod>=0) && (full_addr != -1)) full_addr=applyMultiSens(full_addr,multiMod);
}
}
if (full_addr!=-1) {
addr=full_addr & 0xffff;
/// is it a global parameter?
if (addr >= FRAMEPAR_GLOBALS) {
if (addr < (FRAMEPAR_GLOBALS+P_MAX_GPAR)) {
if (full_addr & FRAMEPAIR_MASK_BYTES) {
add_assoc_long(return_value, key, FRAMEPAIR_FRAME_FIELD(full_addr,ELPHEL_GLOBALPARS(addr)));
} else {
add_assoc_long(return_value, key, ELPHEL_GLOBALPARS(addr));
}
}
/// is it in the future/latest?
} else if (future>0) {
if ((addr >=0) && (addr < (sizeof (struct framepars_t) >>2))) {
if (full_addr & FRAMEPAIR_MASK_BYTES) {
add_assoc_long(return_value, key, FRAMEPAIR_FRAME_FIELD(full_addr,((struct framepars_t *) ELPHEL_G(framePars))[frame_index].pars[addr]));
} else {
add_assoc_long(return_value, key, ((struct framepars_t *) ELPHEL_G(framePars))[frame_index].pars[addr]);
}
}
/// is it saved in past parameters?
} else if (future==0){
addr-=PARS_SAVE_FROM;
if ((addr >=0) && (addr <(sizeof (struct framepars_past_t) >>2))) {
if (full_addr & FRAMEPAIR_MASK_BYTES) {
add_assoc_long(return_value, key, FRAMEPAIR_FRAME_FIELD(full_addr,((struct framepars_past_t *) ELPHEL_G(pastPars))[frame_index].past_pars[addr]));
} else {
add_assoc_long(return_value, key, ((struct framepars_past_t *) ELPHEL_G(pastPars))[frame_index].past_pars[addr]);
}
}
}
zval_dtor(&const_value);
}
}
}
}
/**
* @brief common part of elphel_set_P_value() and elphel_compressor_*()
* @param addr register address (with possible flags)
* @param data data to write
* @param frame frame to write (<0) - use current + FRAME_DEAFAULT_AHEAD
* @param flags additional flags (0) - none
* @return <0 - -errno ( error), otherwise frame used
*/
long elphel_set_P_value_common(long addr, long data, long frame, long flags) {
unsigned long write_data[4];
long maddr;
///shortcut for global parameters - directly mmaped
maddr=addr & 0xffff;
if (( (addr & 0xff00) != 0xff00 ) && (maddr >= FRAMEPAR_GLOBALS)) { /// these globals can be written just through mmap
if (maddr >= (FRAMEPAR_GLOBALS+P_MAX_GPAR)) {
return -1;
}
ELPHEL_GLOBALPARS(maddr)=data;
return 0;
}
if (frame <0) {
frame=ELPHEL_GLOBALPARS(G_THIS_FRAME) + FRAME_DEAFAULT_AHEAD;
}
flags |= (flags << 16); /// will accept flags both shifted and not shifted
flags &=0xffff0000;
if ((addr<0) ||((maddr >= (sizeof (struct framepars_t) >>2)) && ( (addr & 0xff00) != 0xff00 ) )) {
return -1;
}
write_data[0]=FRAMEPARS_SETFRAME;
write_data[1]=frame;
write_data[2]= addr | flags;
write_data[3]= data;
long rslt=write(ELPHEL_G(fd_fparmsall), write_data, sizeof(write_data));
// if (rslt<0) rslt =-errno;
if (rslt<0) return -errno;
if (rslt == sizeof( write_data )) return frame;
return -1;
}
//! Set acquisition/compression parameters.
/// addr may include flags - addr|=(flags>>16)
/// UPDATE: return frame number to which parameters was set
PHP_FUNCTION(elphel_set_P_value)
{
long addr;
long data;
long frame=-1;
unsigned long flags=0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll|ll", &addr,&data,&frame,&flags) == FAILURE) {
RETURN_NULL();
}
if (((frame=elphel_set_P_value_common (addr, data, frame, flags))) <0) {
RETURN_NULL();
}
RETURN_LONG(frame);
}
//! Reset FPGA compressor - set to the same as elphel_compressor_stop()
PHP_FUNCTION(elphel_compressor_reset)
{
long frame=-1;
unsigned long flags=-1;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ll", &frame,&flags) == FAILURE) {
RETURN_NULL();
}
if (flags<0) flags=FRAMEPAIR_FORCE_NEWPROC;
if (((frame=elphel_set_P_value_common (P_COMPRESSOR_RUN, COMPRESSOR_RUN_STOP, frame, flags)))<0) {
RETURN_NULL();
}
RETURN_LONG(frame);
}
//! Start FPGA compressor
PHP_FUNCTION(elphel_compressor_run)
{
long frame=-1;
unsigned long flags=-1;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ll", &frame,&flags) == FAILURE) {
RETURN_NULL();
}
if (flags<0) flags=FRAMEPAIR_FORCE_NEWPROC;
if (((frame=elphel_set_P_value_common (P_COMPRESSOR_RUN, COMPRESSOR_RUN_CONT, frame, flags)))<0) {
RETURN_NULL();
}
RETURN_LONG(frame);
}
//! Stop FPGA compressor
PHP_FUNCTION(elphel_compressor_stop)
{
long frame=-1;
unsigned long flags=-1;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ll", &frame,&flags) == FAILURE) {
RETURN_NULL();
}
if (flags<0) flags=FRAMEPAIR_FORCE_NEWPROC;
if (((frame=elphel_set_P_value_common (P_COMPRESSOR_RUN, COMPRESSOR_RUN_STOP, frame, flags)))<0) {
RETURN_NULL();
}
RETURN_LONG(frame);
}
//! Acquire one frame to the buffer
PHP_FUNCTION(elphel_compressor_frame)
{
long frame=-1;
unsigned long flags=-1;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ll", &frame,&flags) == FAILURE) {
RETURN_NULL();
}
if (flags<0) flags=FRAMEPAIR_JUST_THIS;
if (((frame=elphel_set_P_value_common (P_COMPRESSOR_RUN, COMPRESSOR_RUN_SINGLE, frame, flags)))<0) {
RETURN_NULL();
}
RETURN_LONG(frame);
}
//!reset sensor, and re-initialize it
PHP_FUNCTION(elphel_reset_sensor) {
lseek((int) ELPHEL_G(fd_fparmsall), LSEEK_FRAMEPARS_INIT, SEEK_END ); /// reset all framepars and globalPars
elphel_set_P_value_common (P_SENSOR, 0, 0, -1);
RETURN_NULL();
}
//! This function reads associative array and writes values to the camera registers, using "ELPHEL_* constants"
//! to determine register address from the provided key in each key/value pair
//! All non-numerical values are ignored
//! Returns number of values written
///UPDATE:retuns frame number to which parameters were written
PHP_FUNCTION(elphel_set_P_arr)
{
char full_constant_name[256];
zval *arr, **data;
HashTable *arr_hash;
HashPosition pointer;
char *key;
int key_len;
long index;
zval const_value;
int array_count;
unsigned long * write_data=NULL;
long frame=-1;
long flags=0;
int num_written=0;
int num_mmap_written=0;
int reg_addr, reg_data, constAddNumber;
long multiMod;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|ll", &arr,&frame,&flags) == FAILURE) {
RETURN_LONG(num_written);
}
if (frame <0) {
frame=ELPHEL_GLOBALPARS(G_THIS_FRAME) + FRAME_DEAFAULT_AHEAD;
}
flags |= (flags << 16); /// will accept flags both shifted and not shifted
flags &=0xffff0000;
init_sens();
arr_hash = Z_ARRVAL_P(arr);
array_count = zend_hash_num_elements(arr_hash);
///allocate array to be written (8 bytes per value + 8)
write_data=(unsigned long *) emalloc ((array_count+1)<<3);
if (!write_data) RETURN_NULL(); /// emalloc failed
write_data[0]=FRAMEPARS_SETFRAME;
write_data[1]=frame;
for(zend_hash_internal_pointer_reset_ex(arr_hash, &pointer);
zend_hash_get_current_data_ex(arr_hash, (void**) &data, &pointer) == SUCCESS;
zend_hash_move_forward_ex(arr_hash, &pointer)) {
if ((zend_hash_get_current_key_ex(arr_hash, &key, &key_len, &index, 0, &pointer) == HASH_KEY_IS_STRING) &&
(Z_TYPE_PP(data) == IS_LONG)) {
reg_data=Z_LVAL_PP(data);
if (strlen(key)>(sizeof(full_constant_name)-8)) RETURN_NULL();
sprintf (full_constant_name,"ELPHEL_%s",key);
reg_addr =-1;
if (zend_get_constant(full_constant_name, strlen(full_constant_name), &const_value TSRMLS_CC)) { /// found the constant as is
reg_addr= Z_LVAL(const_value);
} else {
multiMod=parseMultiSens(full_constant_name); /// will truncate full_constant_name if sensor number suffix found
constAddNumber=splitConstantName(full_constant_name);
if ((constAddNumber>=0) && (zend_get_constant(full_constant_name, strlen(full_constant_name), &const_value TSRMLS_CC))) { /// Try to remove number from the end
reg_addr= (Z_LVAL(const_value) & ~FRAMEPAIR_MASK_BYTES)+constAddNumber; /// FRAMEPAIR_MASK_BYTES to prevent bit-field modifier addition to constants that already have them
if ((multiMod>=0) && (reg_addr != -1)) reg_addr=applyMultiSens(reg_addr,multiMod);
}
}
if (reg_addr>=0) {
/// is it a global parameter?
zval_dtor(&const_value); /// free resources used for constant value
if (((reg_addr & 0xff00) != 0xff00 ) && ((reg_addr & 0xffff) >= FRAMEPAR_GLOBALS)) { /// these globals can be written just through mmap
if ((reg_addr & 0xffff) < (FRAMEPAR_GLOBALS+P_MAX_GPAR)) { /// Fits in the range of the global parameters
if ((reg_addr & FRAMEPAIR_MASK_BYTES) ==0) { /// Full 32-bit writes - use mmap
ELPHEL_GLOBALPARS(reg_addr & 0xffff)=reg_data;
num_mmap_written++;
} else { /// only some bitfield is modified - use (slower) write to have it atomic, no need to do bit field combining here
write_data[(num_written<<1) + 2]= reg_addr | flags;
write_data[(num_written<<1) + 3]= reg_data;
num_written++;
}
}
} else if ((reg_addr>=0) && (((reg_addr & 0xffff) < (sizeof (struct framepars_t) >>2)) || ( (reg_addr & 0xff00) == 0xff00 ) )) {
write_data[(num_written<<1) + 2]= reg_addr | flags;
write_data[(num_written<<1) + 3]= reg_data;
num_written++;
}
}
}
}
if (num_written) {
long rslt=write(ELPHEL_G(fd_fparmsall), write_data, (num_written+1)<<3);
efree(write_data);
if (rslt<0) RETURN_LONG(-errno);
num_written=(rslt>>3) -1 ; ///actually written to driver
}
/// RETURN_LONG(num_mmap_written+num_written);
RETURN_LONG(frame);
}
/**
* @brief Calculate gamma table (as array of 257 unsigned short values)
* @param gamma - gamma value (1.0 - linear)
* @param black - black level, 1.0 corresponds to 256 for 8bit values
* @param gtable - gamma array reference (allocated by the caller)
* @return 0 - OK, <0 - error
*/
int gamma_calc (double gamma, double black, unsigned short * gtable) {
int i;
double x, black256,k;
int ig;
black256=black*256.0;
if (k>=1.0) k= k/256.0 ; /// just in case k is provided as a fraction of 256, not 1.0
if (k>0.99) k=0.99;
k=1.0/(256.0-black256);
if (!gtable) return -1;
///Same 0.13 <= gamma <= 10.0 limits for gamma as used earlier
if (gamma < 0.13) gamma=0.13;
if (gamma >10.0) gamma=10.0;
for (i=0; i<257; i++) {
x=k*(i-black256);
if (x < 0.0 ) x=0.0;
ig= 0.5+65535.0*pow(x,gamma);
if (ig > 0xffff) ig=0xffff;
gtable[i]=ig;
}
return 0;
}
/**
* @brief Calculate new gamma table (specified by gamma value and black level) and put it into gamma cache
* Gamma cache will be used to program gamma tables to FPGA, calculate derivative tables
* Gamma tables should be loaded before used (gamma/black level/scale) specified as frame parameters
* gamma - floating point, <=1.0, will be rounded to 0.01. Larger gammas are reseved for custom tables
* black - floating point, <=1.0 or integer>1 (1..255) - level to subtract from sensor value
* @return hash16 - ((gamma *100) & 0xff) | (((black * 256) 0xff) << 8)
*/
PHP_FUNCTION(elphel_gamma_add)
{
unsigned short data_to_write[260];
double gamma,black;
int igamma, iblack, hash16;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "dd", &gamma, &black ) == FAILURE) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Wrong arguments");
RETURN_NULL ();
}
igamma=100*gamma+0.5;
if (igamma < 0) igamma= 0;
if (igamma > 255) igamma=255;
gamma=0.01*igamma;
iblack= (black>=1.0)?black:(256*black+0.5);
if (iblack < 0) iblack= 0;
if (iblack > 254) iblack=254; /// don't use 255 - reserve it for custom tables
black= (1.0/256.0) * iblack;
hash16= igamma | (iblack<<8);
data_to_write[0]= GAMMA_SCLALE_1; /// 1.0
data_to_write[1]= hash16;
///- next 1 byte [4] - mode (1 - not_nice, 2 - need reverse, 4 - hardware)
///- next 1 byte [5] - color ( only if hardware bit in mode is set)
data_to_write[2]=0;
gamma_calc (gamma, black, &data_to_write[3]);
long rslt=write(ELPHEL_G(fd_gamma_cache), data_to_write, sizeof(data_to_write));
if (rslt<0) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Write to fd_gamma_cache returned errno=%d",errno);
RETURN_LONG(-errno);
}
RETURN_LONG (hash16);
}
/**
* @brief Load custom gamma table, tag it with hash16 (unsigned short).
* Using lower byte of hash16 larger than 0x64 (100) or high byte of 0xff will prevent hash16 used by gamma/blacklevel pairs
* @param hash16 - hash 16 that will be used to identify the table when setting parameters
* @param zarray - array of 257 elements indexed as 0..256, in the range of 0.. 1.0 to be used for a table
* @return -1.. -998 - errno when writing to driver
* -998 - wrong arguments
* -999 - array length is not 257
* -1000..-1256 - missing element 0..256
* -2000..-2256 - non-numeric element 0..256
* >=0 - hash16
*/
PHP_FUNCTION(elphel_gamma_add_custom)