parsedit.php 74.2 KB
Newer Older
1
#!/usr/bin/env php -q
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
<?php
/*!
*! PHP script
*! FILE NAME  : parsedit.php
*! DESCRIPTION: 
*! AUTHOR     : Elphel, Inc.
*! 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: parsedit.php,v $
*!  Revision 1.12  2012/01/13 23:49:51  elphel
*!  no waits in GET mode
*!
*!  Revision 1.11  2012/01/12 08:22:47  elphel
*!  removed final waiting in HTTP GET mode
*!
*!  Revision 1.9  2010/06/08 19:40:22  elphel
*!  using bimg instead of img in imgsrv (configurable)
*!
*!  Revision 1.8  2010/06/01 08:30:36  elphel
*!  support for the FPGA code 03534022  with optional master time stamp over the inter-camera sync line(s)
*!
*!  Revision 1.7  2010/04/30 00:29:33  elphel
*!  new urls, example with explanations
*!
*!  Revision 1.6  2010/04/28 02:27:13  elphel
*!  Added link to control "photofinish" (line scan) modes
*!
*!  Revision 1.5  2010/02/20 20:34:50  elphel
*!  Added color gains control panel, with nothing else
*!
*!  Revision 1.4  2008/12/05 03:32:31  elphel
*!  minor  circbuf format change, support fro COMPRESSOR_SINGLE
*!
*!  Revision 1.3  2008/11/30 05:32:59  elphel
*!  Added WB_EN to be able to turn white balance on/off independently of the WB_MASK
*!
*!  Revision 1.2  2008/11/30 05:01:03  elphel
*!  Changing gains/scales behavior
*!
*!  Revision 1.1.1.1  2008/11/27 20:04:03  elphel
*!
*!
*!  Revision 1.20  2008/11/20 23:23:19  elphel
*!  Now shows descriptions for composite names - bit selections and indexed
*!
*!  Revision 1.19  2008/11/20 07:04:15  elphel
*!  support for parameter descriptions
*!
*!  Revision 1.18  2008/11/18 07:36:27  elphel
*!  Added "Select All" button
*!
*!  Revision 1.17  2008/11/17 23:42:04  elphel
*!  changed myval() to accept numbers in ""
*!
*!  Revision 1.16  2008/11/16 17:34:32  elphel
*!  default settings
*!
*!  Revision 1.15  2008/11/16 00:24:21  elphel
*!  Added "Uncheck All" button
*!
*!  Revision 1.14  2008/11/15 07:04:27  elphel
*!  new parameters to modify analog gains while white balancing
*!
*!  Revision 1.13  2008/11/15 03:10:13  elphel
*!  Some parameters renamed, reassigned.
*!
*!  Revision 1.12  2008/11/13 05:40:45  elphel
*!  8.0.alpha16 - modified histogram storage, profiling
*!
*!  Revision 1.11  2008/11/08 05:53:33  elphel
*!  removed commented out code
*!
*!  Revision 1.10  2008/11/05 02:01:25  elphel
*!  Added bit field manipulation in parameters
*!
*!  Revision 1.9  2008/11/03 19:21:03  elphel
*!  wrong value for TRIG_PERIOD (it is in pixel clocks, not usec)
*!
*!  Revision 1.8  2008/11/02 00:55:10  elphel
*!  added analog gain control (in white balance page)
*!
*!  Revision 1.7  2008/11/02 00:34:55  elphel
*!  added initialization of the camera, multiple demo links on default page
*!
*!  Revision 1.6  2008/11/01 06:37:59  elphel
*!  minor bug fix
*!
*!  Revision 1.5  2008/11/01 06:26:12  elphel
*!  added optional image to the control page
*!
*!  Revision 1.4  2008/10/31 23:16:28  elphel
*!  Now generates last acquired images, annotating them with sequencer commands
*!
*!  Revision 1.3  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.2  2008/10/29 05:29:20  elphel
*!  snapshot
*!
*!  Revision 1.1  2008/10/29 04:29:29  elphel
*!  started a new script to edit arbitrary camera parameters through web interface
*!
*/

//main()
///globals
Andrey Filippov's avatar
Andrey Filippov committed
122 123 124
    $PARS_FRAMES=16;
    $PARS_FRAMES_MASK = $PARS_FRAMES - 1;
    $sensor_port=0; /// TODO: NC393 - add sensor port control, initially will use $sensor_port=0 for all php functions that require it    
125
    $autocampars='/www/pages/autocampars.php';
126
    $descriptions=getParDescriptions($autocampars);
127
    $default_ahead= 3;
128
    $maxahead=6; /// maximal ahead of the current frame that tasks can currently be set to driver; //NC393 - is it the same?
129 130 131
    $minahead=4; /// skip to frame $minahead from the soonest next task before programming
    $brief=true;
    $ahead_separator='*';
132
    $port_mask_separator='!';
133 134 135 136 137 138
    $refreshSig="refresh";
    $testMode=-1; /// don't even show
    $showSeqMode=-1;/// don't even show
    $testBefore=2;/// Start compressor $testBefore frames before first task
    $testAfter=2; /// Stop compressor $testAfter frames after the last task
    $framesBeforeStart=2; ///In test mode - compressor will be started theis frames after "now"
139 140 141 142 143
//    $imgsrv="http://".$_SERVER['SERVER_ADDR'].":8081";
    $imgsrv_base="http://".$_SERVER['SERVER_ADDR'].":";
    $imgsrv_ports= array ("2323","2324","2325","2326");
    
    /// $imglink="img"; /// use this for faster output (and safer in simultaneous accesses from multiple hosts
144 145 146
    $imglink="bimg"; ///It was "img" - faster, but image may get corrupted if buffer is overrun before it is trasferred (network congestion)
    $defaultImgScale=0.2; /// 20% image size
    $defaultImagesPerRow=3;
147
    $defaultImagesNumber= 9; // 12
148 149 150 151 152 153 154 155 156 157 158 159 160
    $isPost=$_SERVER["REQUEST_METHOD"]=="POST";
    $ignoreVals=$isPost;
    $imagesNumber=$defaultImagesNumber;
    $imagesPerRow=$defaultImagesPerRow;
    $imgScale=$defaultImgScale;
    $embedImageScale=0;
    $defaultEmbedImageScale=0.3; 
    $encoded_todo="";
    if ($isPost) {
      parsePost();
      $todo=preparePost();
    }

161 162 163 164 165
    if (array_key_exists ( 'sensor_port', $_GET )) {
    	$GLOBALS [sensor_port] = (myval($_GET ['sensor_port'])) & 3;
    }
    $page_title="Default control/test page for the NC393L camera, sensor port ".$GLOBALS [sensor_port];
    if ((count($_GET)==0) || ((count($_GET)==1) && array_key_exists ( 'sensor_port', $_GET ))) {
166 167 168 169 170
         startPage($page_title, "");
         printDefaultPage();
         endPage();
         exit (0);
    }
171 172
    
    $imgsrv = $imgsrv_base.$imgsrv_ports[$GLOBALS [sensor_port]];
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
    $elp_const=get_defined_constants(true);
    $elp_const=$elp_const["elphel"];
    $immediateMode=parseGetNames(); 
    if ($immediateMode){
      convertImmediateMode();
      $todo=preparePost();
      if ($showSeqMode>0) { // use regular html page
        startPage($page_title, "");
        echo "<!--\n";
        echo "global_params:\n";
        var_dump($global_params);
        echo "frame_params:\n";
        var_dump($frame_params);
        echo "posted_params:\n";
        var_dump($posted_params);
        echo "todo:\n";
        var_dump($todo);

        echo "\n-->\n";
        showSequence($todo,0);
        addGammas($todo);
        applyPost($todo,true); // no final wait
        showSequence($todo,$frame_zero);
        endPage();
        exit(0); // that's all
      } else { // return XML page with specified parameters values
        addGammas($todo);
        $names=array_merge(extractNames($global_params),extractNames($frame_params));
Andrey Filippov's avatar
Andrey Filippov committed
201
        $currentParameters=elphel_get_P_arr($GLOBALS [sensor_port], $names);
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
        applyPost($todo,true); // no final wait
        $msg="<?xml version=\"1.0\"?>\n<parameters>\n";
        foreach ($currentParameters as $key=>$value) {
          $msg.=sprintf("  <%s>%d</%s>\n",$key,$value,$key);
        }
        $msg.="</parameters>\n";
        header("Content-Type: text/xml");
        header("Content-Length: ".strlen($msg)."\n");
        header("Pragma: no-cache\n");
        printf($msg);
        exit(0); // that's all
      }
    }
//    startPage();
    startPage($page_title, mainJavascript($refreshSig));

    if ($isPost) {
      if ($showSeqMode>0) showSequence($todo,0);
      addGammas($todo);
      applyPost($todo);
      if ($showSeqMode>0) showSequence($todo,$frame_zero);
      $encoded_todo=encodeTodo($todo,$frame_zero); /// do it in all modes

    }
    readCurrentParameterValues();
//   echo "<pre>";
//    print_r($frame_params);
//   print_r($posted_params);
//    print_r($global_params);
//   var_dump($todo);
//   echo "</pre>";
    printPage($encoded_todo);
    endPage();

    exit (0);
/**
 * @brief Print the default page that has some demo links
 */

function printDefaultPage() {
242
   $url_port = "sensor_port=".$GLOBALS [sensor_port]."&";	
243
//   $imgsrv="http://".$_SERVER['SERVER_ADDR'].":8081";
244
   $url_init=  $url_port."embed&test&showseq&title=Camera+initialization+parameters"
245 246 247 248 249 250
              ."&TASKLET_CTL=0*0"          /// at least ELPHEL_CONST_TASKLET_CTL_NOSAME bit should be 0 so initialization will not wait for the frame sync
              ."&MAXAHEAD=2"               /// When servicing interrupts, try programming up to 2 frames ahead of the due to program time)
              ."&FPGA_XTRA=1000"           /// Extra clock cycles needed to compress a frame (in addition to per macroblocs time)
              ."&EXTERN_TIMESTAMP=1"       /// Use external timestamp when available
              ."&BITS=8"                   /// 8 bit data mode 
              ."&QUALITY=80"               /// 80 percent JPEG image quality
251
              ."&COLOR=0"                  /// regular color mode (not mono or JP4 flavors)
252 253 254 255 256 257 258 259 260 261 262
              ."&COLOR_SATURATION_BLUE=200"/// 2.0 (200% blue/green color saturation (to compensate for effect of non-unity gamma)
              ."&COLOR_SATURATION_RED=200" /// 2.0 (200% blue/green color saturation
              ."&BAYER=0"                  /// No bayer shift
              ."&SENSOR_RUN=".    ELPHEL_CONST_SENSOR_RUN_CONT     /// turn on sensor in continuous mode
              ."&COMPRESSOR_RUN=".ELPHEL_CONST_COMPRESSOR_RUN_CONT /// run compressor in continuous mode
              ."&GTAB_R=0x0a390400"        /// red component: black level=10, gamma=0.57, scale=1.0 (will force calculation gamma table)
              ."&GTAB_G=0x0a390400"        /// same for green (main one, red row)
              ."&GTAB_GB=0x0a390400"       /// same for green (second, blue row)
              ."&GTAB_B=0x0a390400"        /// same for blue
              ."&SENSOR=0*0"               /// setting sensor to zero will initiate sensor detection attempt (should be frame 0)
              ;
263
   $url_debug= $url_port."test&showseq&title=Debug control"
264 265 266 267 268 269 270 271 272 273 274
              ."&DEBUG=0x3fc"
              ."&TASKLET_CTL"
              ."&PROFILING_EN"
              ."&HISTMODE_C"
              ."&HISTMODE_Y"
              ."&HIST_LAST_INDEX=@"
              ."&THIS_FRAME=@"
              ."&HIST_Y_FRAME=@"
              ."&HIST_C_FRAME=@"
              ."&CIRCBUFWP=@"
              ;
275
   $url_woi_control=  $url_port."embed&test&showseq&title=Camera+WOI+Controls".
276
                      "&SENSOR_RUN&COMPRESSOR_RUN&WOI_LEFT&WOI_TOP&WOI_WIDTH&WOI_HEIGHT&FLIPH&FLIPV&DCM_HOR&DCM_VERT&THIS_FRAME=@&CIRCBUFWP=@";
277
   $url_woi_control_test=$url_port."embed=.4&test=1&showseq=1&title=Camera+WOI+Controls+(test+mode)".
278
                      "&SENSOR_RUN&COMPRESSOR_RUN&WOI_LEFT&WOI_TOP&WOI_WIDTH&WOI_HEIGHT&FLIPH=1*3&FLIPV=1*5&DCM_HOR&DCM_VERT&THIS_FRAME=@&CIRCBUFWP=@";
279 280
   $url_images=       $url_port."images=9:3:.2";
$url_aexp_all= $url_port."embed=0.1&title=Autoexposure/White+Balance/HDR+controls+(full)"
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
              ."&COMPRESSOR_RUN=".ELPHEL_CONST_COMPRESSOR_RUN_CONT /// run compressor in continuous mode
              ."&DAEMON_EN=1"         /// Daemons are controlled by bits in this register. Autoexposure bit is 0 (1<<0 == 1)
              ."&AUTOEXP_ON=1"        /// setting it to 0 will only disable autoexposure, but not HDR modes or white balancing
              ."&AEXP_FRACPIX=0xff80" /// Fraction of all pixels that should be below P_AEXP_LEVEL (16.16 - 0x10000 - all pixels)
              ."&AEXP_LEVEL=0xf800"   /// Target output level:  [AEXP_FRACPIX]/0x10000 of all pixels should have value below it (0x10000 - full output scale)
              ."&AE_PERIOD=4"         /// Autoexposure period (will be increased if below the latency)
              ."&AE_THRESH=500"       /// AE error (logariphmic exposures) is integrated between frame and corrections are scaled when error is below thershold (500)
              ."&THIS_FRAME=@"        /// Current frame
              ."&NEXT_AE_FRAME=@"     /// Next frame to be processed by autoexposure
              ."&NEXT_WB_FRAME=@"     /// Next frame to be processed by the white balance
              ."&VEXPOS"              /// exposure measured in sensor scanlines (autoexposure modifies number of exposure lines)
              ."&EXPOS"               /// exposure in microseconds
              ."&NEXT_WB_FRAME=@"     /// Next frame to be processed by the white balance
              ."&HIST_DIM_01=0x0a000a00" /// Percentiles measured for colors 0 (lower 16 bits) and 1 (high 16 bits) for  VEXPOS=1 (darkest)
                                      /// Setting it to 0xffffffff will initiate dark levels re-calibration (making 2 dark drames)
              ."&HIST_DIM_23=0x0a000a00" /// Same for colors 2 and 3
              ."&WB_EN=0x1"           /// 1 - enable, 0 - disable white balance
              ."&WB_MASK=0xd"         /// bitmask - which colors to correct (1 - correct, 0 - ignore).
              ."&WB_PERIOD=16"        /// White balance period (will be increased if below the latency)
              ."&WB_WHITELEV=0xfae1"  /// White balance level of white (16.16 - 0x10000 is full scale, 0xfae1 - 98%, default)
              ."&WB_WHITEFRAC=0x028f" /// White balance fraction (16.16) of all pixels that have level above [P_WB_WHITELEV] for the brightest color
                                      /// locally [WB_WHITELEV] will be decreased if needed to satisfy [WB_WHITELEV]. default is 1% (0x028f)

              ."&WB_SCALE_R=0x10000"  /// additional correction for R from calulated by white balance (16.16)
              ."&WB_SCALE_GB=0x10000" /// additional correction for GB (second green) from calulated by white balance (16.16)
              ."&WB_SCALE_B=0x10000"  /// additional correction for B from calulated by white balance (16.16)
              ."&WB_THRESH=0"         /// How many frames the white balance correction has to be the same sign before it
                                      /// will be applied (<128,for each color independently)
              ."&GTAB_R"              /// red component: black level=10, gamma=0.57, scale=1.0 (will force calculation gamma table)
              ."&GTAB_G"              /// same for green (main one, red row)
              ."&GTAB_GB"             /// same for green (second, blue row)
              ."&GTAB_B"              /// same for blue
              ."&HDR_DUR=0"           /// 0 - HDR 0ff, >1 - duration of same exposure (currently 1 or 2 - for free running)
              ."&HDR_VEXPOS=0x40000"  /// if less than 0x10000 - number of lines of exposure, >=10000 - relative to "normal" exposure
              ."&EXP_AHEAD=3"         /// How many frames ahead of the current frame write exposure to the sensor
              ."&AE_INTEGERR=@"       ///  current integrated error in the AE loop
              ."&HISTWND_RWIDTH"      /// Relative histogram window width (0x10000 - 100%)
              ."&HISTWND_RHEIGHT"     /// Relative histogram window height (0x10000 - 100%)
              ."&HISTWND_RLEFT"       /// Relative histogram window left   (0x10000 - 100%)
              ."&HISTWND_RTOP"        /// Relative histogram window top    (0x10000 - 100%)
              ."&HISTWND_WIDTH=@"     /// Absolute (as written to FPGA) histogram window width
              ."&HISTWND_HEIGHT=@"    /// Absolute (as written to FPGA) histogram window height
              ."&HISTWND_LEFT=@"      /// Absolute (as written to FPGA) histogram window left (counted from the left of WOI margin)
              ."&HISTWND_TOP=@"       /// Absolute (as written to FPGA) histogram window top  (counted from the top of WOI)
              ;

327
$url_aexp_only= $url_port."embed=0.1&title=Autoexposure+controls"
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
              ."&COMPRESSOR_RUN=".ELPHEL_CONST_COMPRESSOR_RUN_CONT /// run compressor in continuous mode
              ."&DAEMON_EN=1"         /// Daemons are controlled by bits in this register. Autoexposure bit is 0 (1<<0 == 1)
              ."&AUTOEXP_ON=1"        /// setting it to 0 will only disable autoexposure, but not HDR modes or white balancing
              ."&AEXP_FRACPIX=0xff80" /// Fraction of all pixels that should be below P_AEXP_LEVEL (16.16 - 0x10000 - all pixels)
              ."&AEXP_LEVEL=0xf800"   /// Target output level:  [AEXP_FRACPIX]/0x10000 of all pixels should have value below it (0x10000 - full output scale)
              ."&AE_PERIOD=4"         /// Autoexposure period (will be increased if below the latency)
              ."&AE_THRESH=500"       /// AE error (logariphmic exposures) is integrated between frame and corrections are scaled when error is below thershold (500)
              ."&THIS_FRAME=@"        /// Current frame
              ."&NEXT_AE_FRAME=@"     /// Next frame to be processed by autoexposure
              ."&VEXPOS"              /// exposure measured in sensor scanlines (autoexposure modifies number of exposure lines)
              ."&EXPOS"               /// exposure in microseconds
              ."&HIST_DIM_01=0x0a000a00" /// Percentiles measured for colors 0 (lower 16 bits) and 1 (high 16 bits) for  VEXPOS=1 (darkest)
                                      /// Setting it to 0xffffffff will initiate dark levels re-calibration (making 2 dark drames)
              ."&HIST_DIM_23=0x0a000a00" /// Same for colors 2 and 3
              ."&EXP_AHEAD=3"         /// How many frames ahead of the current frame write exposure to the sensor
              ."&HISTWND_RWIDTH"      /// Relative histogram window width (0x10000 - 100%)
              ."&HISTWND_RHEIGHT"     /// Relative histogram window height (0x10000 - 100%)
              ."&HISTWND_RLEFT"       /// Relative histogram window left   (0x10000 - 100%)
              ."&HISTWND_RTOP"        /// Relative histogram window top    (0x10000 - 100%)
              ."&HISTWND_WIDTH=@"     /// Absolute (as written to FPGA) histogram window width
              ."&HISTWND_HEIGHT=@"    /// Absolute (as written to FPGA) histogram window height
              ."&HISTWND_LEFT=@"      /// Absolute (as written to FPGA) histogram window left (counted from the left of WOI margin)
              ."&HISTWND_TOP=@"       /// Absolute (as written to FPGA) histogram window top  (counted from the top of WOI)
              ."&AE_INTEGERR=@"       /// Current integrated error in the AE loop
              ;
353
$url_wb_only= $url_port."embed=0.1&title=White+Balance+controls"
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
              ."&COMPRESSOR_RUN=".ELPHEL_CONST_COMPRESSOR_RUN_CONT /// run compressor in continuous mode
              ."&DAEMON_EN=1"         /// Daemons are controlled by bits in this register. Autoexposure bit is 0 (1<<0 == 1)
              ."&THIS_FRAME=@"        /// Current frame
              ."&NEXT_WB_FRAME=@"     /// Next frame to be processed by the white balance
              ."&NEXT_WB_FRAME=@"     /// Next frame to be processed by the white balance
              ."&WB_EN=0x1"           /// 1 - enable, 0 - disable white balance
              ."&WB_MASK=0xd"         /// bitmask - which colors to correct (1 - correct, 0 - ignore).
              ."&WB_PERIOD=16"        /// White balance period (will be increased if below the latency)
              ."&WB_WHITELEV=0xfae1"  /// White balance level of white (16.16 - 0x10000 is full scale, 0xfae1 - 98%, default)
              ."&WB_WHITEFRAC=0x028f" /// White balance fraction (16.16) of all pixels that have level above [P_WB_WHITELEV] for the brightest color
                                      /// locally [WB_WHITELEV] will be decreased if needed to satisfy [WB_WHITELEV]. default is 1% (0x028f)
              ."&WB_SCALE_R=0x10000"  /// additional correction for R from calulated by white balance (16.16)
              ."&WB_SCALE_GB=0x10000" /// additional correction for GB (second green) from calulated by white balance (16.16)
              ."&WB_SCALE_B=0x10000"  /// additional correction for B from calulated by white balance (16.16)
              ."&WB_THRESH=500"       /// WB errors are integrated between frame and corrections are scaled when error is below thershold (500)
369 370
              ."&GAIN_MIN=0x18000"    /// minimal sensor analog gain (0x100 - 1.0)
              ."&GAIN_MAX=0xfc000"    /// maximal sensor analog gain (0x100 - 1.0)
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
//              ."&GAIN_STEP=0x20"      /// minimal correction to be applied to the analog gain (should be set larger that sensor
//                                      /// actual gain step to prevent oscillations (0x100 - 1.0, 0x40 - 1/8)
              ."&ANA_GAIN_ENABLE=1"       /// Enable analog gain controls in white balancing
              ."&GTAB_R"              /// red component: black level=10, gamma=0.57, scale=1.0 (will force calculation gamma table)
              ."&GTAB_G"              /// same for green (main one, red row)
              ."&GTAB_GB"             /// same for green (second, blue row)
              ."&GTAB_B"              /// same for blue
/// Analog gains
              ."&GAINR=0x10000"         /// R  channel gain (mono gain)  8.8 0x100 - 1.0
              ."&GAING=0x10000"         /// G  channel gain (mono gain)  8.8 0x100 - 1.0
              ."&GAINGB=0x10000"        /// GB channel gain (mono gain)  8.8 0x100 - 1.0
              ."&GAINB=0x10000"         /// B  channel gain (mono gain)  8.8 0x100 - 1.0
              ."&SENSOR_REGS45=@"     /// Sensor register gain R
              ."&SENSOR_REGS43=@"     /// Sensor register gain G
              ."&SENSOR_REGS46=@"     /// Sensor register gain GB
              ."&SENSOR_REGS44=@"     /// Sensor register gain B
              ."&WB_INTEGERR=@"       /// current integrated error in the WB loop
              ;

390
$url_colors_only= $url_port."embed=0.1&title=Color+gains+controls"
391 392 393 394 395 396 397 398 399 400 401
/// Analog gains
              ."&GAINR=0x10000"         /// R  channel gain (mono gain)  8.8 0x100 - 1.0
              ."&GAING=0x10000"         /// G  channel gain (mono gain)  8.8 0x100 - 1.0
              ."&GAINGB=0x10000"        /// GB channel gain (mono gain)  8.8 0x100 - 1.0
              ."&GAINB=0x10000"         /// B  channel gain (mono gain)  8.8 0x100 - 1.0
              ."&SENSOR_REGS45=@"     /// Sensor register gain R
              ."&SENSOR_REGS43=@"     /// Sensor register gain G
              ."&SENSOR_REGS46=@"     /// Sensor register gain GB
              ."&SENSOR_REGS44=@"     /// Sensor register gain B
              ;

402
$url_hdr_exp= $url_port."embed=0.1&title=HDR+exposure+controls"
403 404 405 406 407 408 409 410 411
              ."&COMPRESSOR_RUN=".ELPHEL_CONST_COMPRESSOR_RUN_CONT /// run compressor in continuous mode
              ."&DAEMON_EN=1"         /// Daemons are controlled by bits in this register. Autoexposure bit is 0 (1<<0 == 1)
              ."&AUTOEXP_ON=1"        /// setting it to 0 will only disable autoexposure, but not HDR modes or white balancing
              ."&VEXPOS"              /// exposure measured in sensor scanlines (autoexposure modifies number of exposure lines)
              ."&EXPOS"               /// exposure in microseconds
              ."&HDR_DUR=0"           /// 0 - HDR 0ff, >1 - duration of same exposure (currently 1 or 2 - for free running)
              ."&HDR_VEXPOS=0x40000"  /// if less than 0x10000 - number of lines of exposure, >=10000 - relative to "normal" exposure
              ."&THIS_FRAME=@"        /// Current frame
              ;
412
$url_ext_trigger=$url_port."embed=0.1&title=External+trigger+controls"
413
              ."&TRIG=4*5"            ///  External trigger mode (should be set after dealys are set?)
414 415 416 417
                                      /// bit 0  - "old" external mode (0- internal, 1 - external )
                                      /// bit 1 - enable(1) or disable(0) external trigger to stop clip
                                      /// bit 2 - async (snapshot, ext trigger) mode, 0 - continuous NOTE: Only this bit is used now !
                                      /// bit 3 - no overlap,  single frames: program - acquire/compress same frame
418
                                      /// bit 4 - Global reset release mode
419

420
              ."&TRIG_PERIOD=25000000"/// 0.25 sec @100MHz output sync period (32 bits, in pixel clocks)
421 422 423 424
                                      /// >=256 repetitive with specified period.
                                      /// NOTE: Currently there is no verification that period is longer than sensor/compressor can handle
              ."&TRIG_DELAY"          /// trigger delay, 32 bits in pixel clocks (needed when multiple cameras are synchronized)
              ."&EXTERN_TIMESTAMP=1"  /// Use external timestamp if available
425
              ."&TRIG_BITLENGTH=31"   /// bit lengh minus 1 in pixel clocks (when sending timestamp over sync line)
426 427 428 429
              ."&XMIT_TIMESTAMP=1"    /// transmit timestamp when sending sync
              ."&THIS_FRAME=@"        /// Current frame
///Next parameters are non-zero only for external connections and should match particular I/O boards/connectors
              ."&TRIG_CONDITION=0"    /// trigger condition, 0 - internal, else dibits ((use<<1) | level) for each GPIO[11:0] pin
430 431 432 433
		                              /// 0x0 - from FPGA, 0x80000 - ext, 0x8000 - int, 0x88000 - any, 0x95555 - add ext, 0x59999 - add int 
              ."&TRIG_OUT=0x65555"    /// trigger output to GPIO, dibits ((use << 1) | level_when_active). Bit 24 - test mode,
                                      ///  when GPIO[11:10] are controlled by other internal signals
                                      /// 0x56555 - ext connector, 0x65555  - internal connector 0x66555 - both, 0x55555 - none
434 435
              ."&SENSOR_REGS30=@"     /// Sensor register MODE1 (trigger bit)
              ;
436
$url_cable_delay=$url_port."embed=0.3&title=Cable+delay+/+Sensor+phase+adjustment"
437 438 439 440
              ."&SENSOR_PHASE"        /// Sensor phase - use | 0x80000 to reset DCM (needed if phase went too far)
              ."&DAEMON_EN=0"         /// disable autoexposure/color balance
              ."&TESTSENSOR=0x10008"  /// 0x10008 - color bars
              ;
441 442
$url_sample=   $url_port
			  ."embed=0.3&title=Sample+camera+control+page"
443 444 445 446 447 448 449
              ."&SENSOR_REGS32=@"
              ."&SENSOR_REGS32__0106"
              ."&SENSOR_REGS160=@"
              ."&SENSOR_REGS160__0403"
              ."&TESTSENSOR=0x10008"
              ."&FPGATEST"
              ;
450
$url_ext_photofinish=$url_port."embed=0.2&&title=Photofinish+and+ oversize+controls"
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
              ."&WOI_TOP"             /// WOI top - here used to adjust the acquisition line to the center of the screen 
              ."&WOI_HEIGHT"          /// Total WOI height (composite image),<16384
              ."&WOI_WIDTH"           /// Just WOI width (image height in photofinish mode)
              ."&PF_HEIGHT"           /// Photofinish stripe height - minimal 2. Set to zero to turn photofinish mode off
              ."&OVERSIZE"            /// include black pixels in the output. WOI_WIDTH and WOI_HEIGHT are not verified in this mode
              ."&DAEMON_EN"           /// bitwise enabling daemons (+1 - autoexposure, +2 - streamer)
              ."&EXPOS"               /// exposure time in microseconds
              ."&GAINR"               /// Red gain (0x10000 corresponds to unity gain)
              ."&GAING"               /// Green in red line gain (0x10000 corresponds to unity gain)
              ."&GAINGB"              /// Green in blue line gain (0x10000 corresponds to unity gain)
              ."&GAINB"               /// Blue gain (0x10000 corresponds to unity gain)
              ."&VIRT_HEIGHT"         /// height of the single photofinish strip (use to slow down line scanning rate - minimal is calculated automatically)
              ."&VIRT_KEEP"           /// 1 - use specified value of VIRT_HEIGHT, 0 - use minimal possible
              ."&ACTUAL_HEIGHT=@"     /// actual height of the composite image, readonly
              ."&SENSOR_REGS6=@"      /// vertical blank in each strip, readonly
              ."&refresh"
              ;
$prefix_url='http://'.$_SERVER['SERVER_ADDR'].$_SERVER['SCRIPT_NAME'];
         echo <<<USAGE
   <h4> Control links:</h4>
   <ul>
   <li><a href="?$url_init">Initialization Parameters (i.e. after powerup)</a></li>
   <li><a href="?$url_colors_only">Control of color channel gains</a></li>
   <li><a href="?$url_woi_control">Demo to change image size, position and mirroring</a></li>
   <li><a href="?$url_woi_control_test">Demo to change image size, position and mirroring - test mode</a></li>
   <li><a href="?$url_aexp_only">Controls related to autoexposure</a></li>
   <li><a href="?$url_wb_only">Controls related to white balance</a></li>
   <li><a href="?$url_hdr_exp">Controls related to HDR exposure - all</a></li>
   <li><a href="?$url_aexp_all">Controls related to autoexposure/white balance/HDR exposure - all</a></li>
   <li><a href="?$url_ext_trigger">External trigger controls</a></li>
   <li><a href="?$url_ext_photofinish">Photofinish and oversize (with black pixels around actiove pixels) modes controls</a></li>
   <li><a href="?$url_cable_delay">Cable delay / sensor phase adjustment. low 16 bits - signed number of ~22ps steps, bits 17:16 - 90 degree steps. Add 0x80000 to reset delays (needed if small delay was set too far for the internal FPGA DCM - phase adjustment limits are frequency dependent, but less than +/-255).</a></li>
   <li><a href="?$url_images">last 9 images, 3 per row, scale=0.2</a></li>
   <li><a href="?$url_debug">Debug on/off control (usually requires to redirect printk() output with 'printk_mod &'</a></li>
   </ul>
   <br/>
   <h2> You may construct custom URLs that combine parameters you need to control or monitor using the included links URLs as examples.</h2>
   </p>Just including parameter (&SOME_PARAMETER) will create form to edit it, adding "&SOME_PARAMETER=1234" will put "1234" in the value filed, &SOME_PARAMETER=@" creates a read-only field to monitor parameter value. You may view/modify sensor registers using meta names SENSOR_REGS with the register number, you may also view edit particular bit fileds of the parameters, adding "__" (double underscore), then WW (two decimals specifying the bit width of the fields) followed by BB - (two decimal numbers specifying the lowest bit in the field (0..31) . In the following example</p>
  <p><a href="?$url_sample">$prefix_url?$url_sample</a></p>
   <ul>
     <li><b>embed=0.3</b> - embedd the camera image scaled down to 30% of the original size</li>
     <li<b>title=Sample+camera+control+page</b> - page title with spaces replaced by "+" signs</li>
     <li<b>SENSOR_REGS32=@</b> - show sensor register 32 in read-only mode</li>
     <li<b>SENSOR_REGS32__0106</b> - show sensor register 32 selected bit [6] (in MT9P031 it controls black level calibration, should be off for the test patterns), allow editing</li>
     <li<b>SENSOR_REGS160=@</b> - show sensor register 160 in read-only mode</li>
     <li<b>SENSOR_REGS160__0403</b> - show sensor register 160 selected bits [6:3] (in MT9P031 they control sensor test pattern), allow editing</li>
     <li<b>TESTSENSOR=0x10008</b> - preffered (register-independent) way to select sensor test pattern, suggest particular value. 0x10000 turns the mode on, 8 selects color bars pattern.</li>
     <li<b>FPGATEST</b> - Add field for FPGA internally generated image (currently just two values - 0 - off, 1 - gray gradient columns).</li>
   </ul>
   <p>Each parameter on a generated page has a "tooltip" - if  you hover a mouse pointer over it's name it will show short description of the parameter.</p>
   <h4> Changing parameters with the HTTP GET method</h4>
   <p>If "&immediate" is added the the URL, the program will apply parameteres (those that have values specified) without opening any forms. If the parameter <b>showseq</b> is specified and positive, the HTML page will show the sequence of parameter application, if it is ommitted or less or equal to 0, the request will generate an XML response with the previous parameter values (before application of the new ones). You may specify different program-ahead values and/or multiple settings for the same parameters - program-ahead values are separated from the parameter values with "*", multiple value/program-ahead pairs for the same parameter are separated with ":". Non-specified program-ahead is currently set to 3.</p>

USAGE;
}


/// show table with last acquired images and meta data
509
/// TODO: show changed parameters for those frames? ***
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
function showImgData($meta,$skipped,$prev,$imgScale,$done) {
  global $imgsrv,$imglink;
  $width= $meta['meta']['width'];
  $height=$meta['meta']['height'];
  $title="";
  $frame=$meta['Exif']['FrameNumber'];
  foreach ($meta['Exif'] as $key=>$value) {
    $title.=sprintf("%s=%s    \n",$key,$value);
  }
/*
echo "<pre>";
 print_r($done);
echo "</pre>";
*/
  printf ("<table border='1'>");
  printf ("<tr>");
    printf ("<td colspan=4>");
  printf("<img src='$imgsrv/%d/$imglink' style='width:%dpx; height:%dpx;' title='%s'/>\n",
   $meta['circbuf_pointer'],
   floor($width*$imgScale),
   floor($height*$imgScale),
   $title);
    printf ("</td>");
  printf ("</tr>");
  printf ("<tr>");
    printf ("<td>%s</td><td>%d (0x%x)</td>",'FrameNumber',$frame,$frame);
    printf ("<td>%s</td><td>%d (0x%x)</td>",'width',$meta['meta']['width'],$meta['meta']['width']);

  printf ("</tr>");
  printf ("<tr>");
      $just_time=end(explode(" ", trim($meta['Exif']['DateTimeOriginal'])));
/// just seconds
      $just_time=end(explode(":", $just_time));
    if ($skipped)
      printf ("<td><b>Skipped</b></td><td><b>%d</b></td>",$skipped);
    else
      printf ("<td>&nbsp;</td><td>&nbsp;</td>");
    printf ("<td>%s</td><td>%d (0x%x)</td>",'height',$meta['meta']['height'],$meta['meta']['height']);
  printf ("</tr>");
  printf ("<tr>");
    printf ("<td>%s</td><td>%s</td>",'TimeOriginal',$just_time);
    printf ("<td>%s</td><td>%d (0x%x)</td>",'quality2',$meta['meta']['quality2'],$meta['meta']['quality2']);
  printf ("</tr>");
  printf ("<tr>");
    printf ("<td>%s</td><td>%s</td>",'ExposureTime',trim($meta['Exif']['ExposureTime']));
    printf ("<td>%s</td><td>%d</td>",'color',$meta['meta']['color']);
  printf ("</tr>");
  if ($done) {
    printf ("<tr>");
    printf ("<td colspan='4'>");

    printf   ("<table border='1' style='width:100%s'>\n",'%');
    foreach ($done as $action_frame=>$actions) {
      $firstRow=true;
       printf("<tr><td rowspan='%d'>%s</td>",count($actions),($action_frame==$frame)?"now":("prev ".($frame-$action_frame)));
       foreach ($actions as $key=>$value) {
         if (!$firstRow) printf ("<tr>");
         else $firstRow=false;
         printf("<td>%s</td><td style='text-align:right'>%d</td><td>0x%x</td>",$key,$value,$value);
         printf ("</tr>\n");
       }
    }
    printf ("</table>");

//print_r($done);
    printf ("</td>");
    printf ("</tr>");
  }
  printf ("</table>");

//  print_r($meta['Exif']);
}

///TODO:if $todo is provided in $_GET - try to find the correct images even if they are not the latest

585
function showLastImages($numImg, $imagesPerRow, $imgScale) {
586
//	elphel_update_exif(); // just for testing
587 588 589 590 591
	$done = decodeTodo ( $_GET ['done'] );
	// $this_exif=elphel_get_exif_elphel(0);
	$circbuf_pointers = elphel_get_circbuf_pointers ( $GLOBALS [sensor_port], 1 );
	echo "<!--";
	var_dump($circbuf_pointers);
592 593
	var_dump($done);
	var_dump($numImg);
594 595 596 597 598 599 600
	echo "-->";
	$framesAgo = 0;
	// echo "<pre>\n";
	end ( $circbuf_pointers );
	if ($done) {
		end ( $done );
		$lastFrameNumber = key ( $done );
601
//		$lastFrameNumber = key ( $done ) + 9; // NC393 debugging
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
		$cur_ptr = current ( $circbuf_pointers );
		while ( $cur_ptr ['frame'] > $lastFrameNumber ) {
			if (! prev ( $circbuf_pointers )) { // / failed to find the right frame in circbuf - probably overwritten
				end ( $circbuf_pointers );
				break;
			}
			$cur_ptr = current ( $circbuf_pointers );
			$framesAgo ++;
		}
	}
	// /TODO: If all changes were later than the images shown - disregard $todo
	// print_r($circbuf_pointers);
	// print_r($done);
	
	// echo "</pre>\n";
	$meta = array ();
	// end($circbuf_pointers);
	$lastFrameIndex = key ( $circbuf_pointers );
	for($i = 0; $i <= min ( ($numImg - 1), $lastFrameIndex ); $i ++) {
		$meta [$i] = array (
				'circbuf_pointer' => $circbuf_pointers [$lastFrameIndex - ($numImg - 1) + $i] ['circbuf_pointer'],
				'meta' => elphel_get_interframe_meta (
						$GLOBALS [sensor_port],
						$circbuf_pointers [$lastFrameIndex - ($numImg - 1) + $i] ['circbuf_pointer'] ),
				'Exif' => elphel_get_exif_elphel (
						$GLOBALS [sensor_port],
						$circbuf_pointers [$lastFrameIndex - ($numImg - 1) + $i] ['exif_pointer'] ) 
		);
		$lastFrameNumber = $circbuf_pointers [$lastFrameIndex - ($numImg - 1) + $i] ['frame'];
	}
	echo "<!--";
	var_dump($meta);
	echo "-->";
	
	$running = (elphel_get_P_value ( $GLOBALS [sensor_port], ELPHEL_COMPRESSOR_RUN ) == ELPHEL_CONST_COMPRESSOR_RUN_CONT) && (elphel_get_P_value ( $GLOBALS [sensor_port], ELPHEL_SENSOR_RUN ) == ELPHEL_CONST_SENSOR_RUN_CONT);
	$page_title = sprintf ( "%s %d images acquired to the circular buffer (circbuf). Acquisition is %s. Last frame is %d", $framesAgo ? "$framesAgo frames (stored) ago" : "Latest", $numImg, $running ? "on - these frames are/will be overwritten in the camera memory" : "off", $lastFrameNumber );
	/*
	 * $page_title=sprintf("%d: %s %d images acquired to the circular buffer (circbuf). Acquisition is %s"
	 * ,$lastFrameNumber
	 * ,$framesAgo?"$framesAgo frames ago":"Latest"
	 * ,$numImg
	 * ,$running?"on - these frames are/will be overwritten in the camera memory":"off"
	 * );
	 */
	startPage ( $page_title, "" );
	printf ( "<h4>%s</h4>\n", $page_title );
	printf ( "<table>\n" );
	$rowOpen = $false;
	$lastFrame = 0;
	$done_left = count ( $done );
	reset ( $done );
	$slice_start = 0;
	$slice_count = 0;
	for($i = 0; $i < $numImg; $i ++) {
		$slice_start += $slice_count;
		$slice_count = 0;
		$frame = $meta [$i] ['Exif'] ['FrameNumber'];
		while ( $done_left && (key ( $done ) <= $frame) ) {
			$slice_count ++;
			$done_left --;
			next ( $done );
		}
		// /$done per image
		$this_done = array_slice ( $done, $slice_start, $slice_count, true );
		if (! ($i % $imagesPerRow)) {
			if ($rowOpen) {
				printf ( "</tr>\n" );
			}
			printf ( "<tr>\n" );
			$rowOpen = true;
		}
		printf ( "<td style='vertical-align: top;'>" );
		$skipped = ($i > 0) ? ($frame - $lastFrame - 1) : 0;
		$lastFrame = $frame;
		showImgData ( $meta [$i], $skipped, $numImg - $i - 1, $imgScale, $this_done );
		printf ( "</td>\n" );
	}
	while ( $i ++ % $imagesPerRow ) {
		printf ( "<td>&nbsp;</td>\n" );
	}
	printf ( "</tr>\n" );
	printf ( "</table>\n" );
	endPage ();
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
}

/**
 * @brief Encode $todo to a string that can be passed in GET HTTP request
 * @param $todo - array of arrays of parameter chnages
 * @param $frame_zero - sequence start frame number that should be added to keys in $todo to get absolute frame numbers
 * @return string representation of $todo
 */
function encodeTodo($todo,$frame_zero) {
   $result="";
   foreach ($todo as $frame=>$actions) {
     $result.=sprintf("%d/",$frame+$frame_zero);
     foreach ($actions as $par=>$value) {
       $result.=sprintf("%s:%d/",$par,$value);
     }
   }
  return $result;
}

/**
 * @brief Reverse endodeTodo() - create $todo array from the encoded string
 * @param $encoded_todo - string representation of todo array
 * @return array representation of $encoded_todo
 */
function decodeTodo ($encoded_todo) {
  $todo=array();
  $frame=0;
  if (!$encoded_todo) return $todo;
  $done=explode("/",rtrim($encoded_todo,'/'));
  foreach ($done as $term) {
    $term=explode(":",$term);
    if (count($term)==1) {
      $frame=$term[0];
      $todo[$frame]=array();
    } else {
      $todo[$frame][$term[0]]=(int)$term[1];
    }
  }
  return $todo;
}


function  showSequence($todo,$frame_zero) {
   printf("<h4>Command Sequence (%s)</h4>\n",$frame_zero?"absolute frame numbers":"relative frame numbers");
   printf   ("<table border='1' style='font-family: Courier, monospace;'>\n");
   printf   ("<tr style='text-align:center'>".
             "<td colspan=2>Frame</td>".
             "<td rowspan=2>Parameter name</td>".
             "<td colspan=2>Value</td>".
734 735
   		     "<td rowspan=2>Broad-<br/>cast</td>".
   		 "</tr>\n");
736 737 738 739 740 741
   printf   ("<tr style='text-align:center'>".
             "<td>dec</td>".
             "<td>hex</td>".
             "<td>dec</td>".
             "<td>hex</td>".
             "</tr>\n");
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
	foreach ( $todo as $frame => $actions ) {
		$first_act = true;
//		printf ( "<tr style='text-align:right'>\n" );
		foreach ( $actions as $name => $value ) {
			if ($name[0] != '*') {
				printf ( "<tr style='text-align:right'>\n" );
				if ($first_act) {
					printf ( "<td rowspan=%d>%d</td><td rowspan=%d>0x%x</td>", count ( $actions ), ($frame + $frame_zero), count ( $actions ), ($frame + $frame_zero) );
				}
				if (array_key_exists('*'.$name, $actions)){
					printf ( "<td style='text-align:left'>%s</td>" . "<td>%d</td>" . "<td>0x%x</td>" . "<td>0x%x</td>" . "</tr>\n",
							$name, $value, $value, $actions['*'.$name]);
				} else {
					printf ( "<td style='text-align:left'>%s</td>" . "<td>%d</td>" . "<td>0x%x</td>" . "<td>&nbsp;</td>" . "</tr>\n", $name, $value, $value );
				}
				$first_act = false;
			}
		}
	}
	
	printf ( "</table>\n" );
763 764
}

765
function  applyPost_debug($todo,$noFinalWait=false) {
766 767 768 769 770 771 772 773 774 775 776 777
   global $maxahead,$minahead,$frame_zero,$showSeqMode;
   if ($showSeqMode>0) {
     printf("<h4>Running sequence...</h4>\n");
     echo "<pre>";
   }
//   print_r($_POST);
//   print_r($posted_params);
//   var_dump($todo);
//   print_r($todo);

/// Skip to the next frame (so more deterministic phase - maximal time till next frame)
   $waitingEnabled=true;
778
   /*
779 780 781 782
   foreach ($todo as $pars) if (array_key_exists('SENSOR', $pars)) {
     $waitingEnabled=false;
     break;
   }
783
   */
Andrey Filippov's avatar
Andrey Filippov committed
784 785
   if (elphel_get_frame($GLOBALS [sensor_port])< 8) $waitingEnabled=false; /// or is "==0" enough?
   if ($waitingEnabled && !$noFinalWait) elphel_skip_frames($GLOBALS [sensor_port],1); // in GET mode, do not skip any frames
786
/// store the current frame number as reference for all actions delays
Andrey Filippov's avatar
Andrey Filippov committed
787
   $frame_zero=elphel_get_frame($GLOBALS [sensor_port]);
788 789 790 791 792 793 794
   $frame_since=0;
   $frame_now=$frame_zero;
///Iterate through $todo array, programming the parameter changes
   foreach ($todo as $since=>$pgmpars) {
     if (($since-$maxahead) >$frame_since ) { /// too early to program, need to wait
       $frame_since=$since-$minahead;
       $frame_now=$frame_since+$frame_zero;
795 796 797 798 799 800 801 802 803 804 805 806 807 808
			if ($waitingEnabled) {
				if ($showSeqMode > 0) {
					printf ( "waiting frame %d (0x%x) ...s=%d, fs=%d noFinalWait=",
							$frame_now, $frame_now, $since, $frame_since, $noFinalWait);
					ob_flush ();
					flush ();
				}
				elphel_wait_frame_abs ( $GLOBALS [sensor_port], $frame_now );
				if ($showSeqMode > 0) {
					printf ( "done\n" );
					ob_flush ();
					flush ();
				}
			}
809
     }
810
     $broadcast_sorted=split_broadcast($pgmpars);
811
          
812
     ksort($broadcast_sorted);
813 814 815 816
     $frame_to_apply = $frame_zero+$since;
     if ($since<0){
     	$frame_to_apply = ELPHEL_CONST_FRAME_IMMED;
     }
817 818
     foreach($broadcast_sorted as $bcast=>$params){
     	if ($bcast){
819
     		elphel_set_P_arr ($GLOBALS [sensor_port], $pgmpars,$frame_to_apply, ELPHEL_CONST_FRAMEPAIR_FORCE_NEWPROC, $bcast);
820
     	} else {
821
     		elphel_set_P_arr ($GLOBALS [sensor_port], $pgmpars,$frame_to_apply, ELPHEL_CONST_FRAMEPAIR_FORCE_NEWPROC); /// Are these flags needed?
822 823
     	}
     }
824 825 826
   }
   if (!$noFinalWait) {
     $frame_now=$since+$frame_zero+1; /// wait just 1 frame longer that the target of the last command in $todo
827 828
     //   echo "since=$since\n"; ob_flush();  flush();
     if ($showSeqMode>0) {printf ("(final) waiting frame %d (0x%x) ... ",$frame_now,$frame_now);  ob_flush();  flush();}
829
     if ($waitingEnabled) {
830
        $rslt = elphel_wait_frame_abs($GLOBALS [sensor_port], $frame_now);
831 832 833 834
     } else {
       $timeout_step=  100000; /// 0.1 sec
       $timeout=      3000000; /// 3.0sec
       for ($i=0 ; $i < $timeout; $i+=$timeout_step) {
Andrey Filippov's avatar
Andrey Filippov committed
835
        if (elphel_get_frame($GLOBALS [sensor_port])>=$frame_now) break;
836 837 838 839
        usleep($timeout_step);
       }
     }
   }
840 841 842 843 844
    
   if ($showSeqMode>0) {printf ("done, frame is %d, rslt=%d, frame_now was %d, waitingEnabled=%d\n",
   		elphel_get_frame($GLOBALS [sensor_port]), $rslt, $frame_now, $waitingEnabled);
   		ob_flush();  flush();
   }
845 846 847 848
   if ($showSeqMode>0) echo "</pre>";
//   exit (0);
}

849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
function  applyPost($todo,$noFinalWait=false) {
	global $maxahead,$minahead,$frame_zero,$showSeqMode;
	if ($showSeqMode>0) {
		printf("<h4>Running sequence...</h4>\n");
		echo "<pre>";
	}
	//   print_r($_POST);
	//   print_r($posted_params);
	//   var_dump($todo);
	//   print_r($todo);
	/// Skip to the next frame (so more deterministic phase - maximal time till next frame)
	$waitingEnabled=true;
	foreach ($todo as $pars) if (array_key_exists('SENSOR', $pars)) {
		$waitingEnabled=false;
		break;
	}
	if (elphel_get_frame($GLOBALS [sensor_port])< 8) $waitingEnabled=false; /// or is "==0" enough?
	if ($waitingEnabled && !$noFinalWait) elphel_skip_frames($GLOBALS [sensor_port],1); // in GET mode, do not skip any frames
	/// store the current frame number as reference for all actions delays
	$frame_zero=elphel_get_frame($GLOBALS [sensor_port]);
	$frame_since=0;
	$frame_now=$frame_zero;
	///Iterate through $todo array, programming the parameter changes
	foreach ($todo as $since=>$pgmpars) {
873 874 875 876
//		echo "<!--";
//		echo "since=".$since."\n";
//		var_dump(split_broadcast($pgmpars));
//		echo "-->";
877 878 879 880 881
		if (($since-$maxahead) >$frame_since ) { /// too early to program, need to wait
			$frame_since=$since-$minahead;
			$frame_now=$frame_since+$frame_zero;
//			$frame_now=$frame_since+$frame_zero+2; // NC393: adding 2 extra
			if ($waitingEnabled) {
882
//			$frame_now=$frame_since+$frame_zero;
883 884 885 886 887
				if ($showSeqMode>0) {printf ("waiting frame %d (0x%x) ... ",$frame_now,$frame_now);  ob_flush();  flush();}
				elphel_wait_frame_abs($GLOBALS [sensor_port], $frame_now);
				if ($showSeqMode>0) {printf ("done (%d)\n", elphel_get_frame($GLOBALS [sensor_port]));   ob_flush();  flush();}
			}
		}
888 889
		$broadcast_sorted=split_broadcast($pgmpars);
		ksort($broadcast_sorted);
890 891
//		echo "<!--";
//		print_r($broadcast_sorted);
892 893 894
		foreach ( $broadcast_sorted as $bcast => $params ) {
			if (count ( $params ) > 0) {
				if ($bcast) {
895
//					echo "elphel_set_P_arr(" . $GLOBALS [sensor_port] . ", " . print_r ( $params, 1 ) . ", " . ($frame_zero + $since) . ", ELPHEL_CONST_FRAMEPAIR_FORCE_NEWPROC, " . $bcast . ")\n";
896 897
					elphel_set_P_arr ( $GLOBALS [sensor_port], $params, $frame_zero + $since, ELPHEL_CONST_FRAMEPAIR_FORCE_NEWPROC, $bcast );
				} else {
898
//					echo "elphel_set_P_arr(" . $GLOBALS [sensor_port] . ", " . print_r ( $params, 1 ) . ", " . ($frame_zero + $since) . ", ELPHEL_CONST_FRAMEPAIR_FORCE_NEWPROC)\n";
899 900 901 902
					elphel_set_P_arr ( $GLOBALS [sensor_port], $params, $frame_zero + $since, ELPHEL_CONST_FRAMEPAIR_FORCE_NEWPROC ); // / Are these flags needed?
				}
			}
		}
903
//		echo "-->";
904 905
	}
	if (!$noFinalWait) {
906 907
		$frame_now=$since+$frame_zero+1; /// wait just 1 frame longer that the target of the last command in $todo
//		$frame_now=$since+$frame_zero+4; /// wait just 1 frame longer that the target of the last command in $todo
908 909 910 911 912 913 914 915 916 917 918 919
		//   echo "since=$since\n"; ob_flush();  flush();
		if ($showSeqMode>0) {printf ("waiting frame %d (0x%x) ... ",$frame_now,$frame_now);  ob_flush();  flush();}
		if ($waitingEnabled) {
			elphel_wait_frame_abs($GLOBALS [sensor_port], $frame_now);
		} else {
			$timeout_step=  100000; /// 0.1 sec
			$timeout=      3000000; /// 3.0sec
			for ($i=0 ; $i < $timeout; $i+=$timeout_step) {
				if (elphel_get_frame($GLOBALS [sensor_port])>=$frame_now) break;
				usleep($timeout_step);
			}
		}
920 921
//	} else { // just debugging - still waiting
//		elphel_skip_frames($GLOBALS [sensor_port],8); // skip 8 frames - debugging NC393 - no differnce!
922 923 924 925 926 927
	}
	if ($showSeqMode>0) {printf ("done (%d, 0x%x)\n", elphel_get_frame($GLOBALS [sensor_port]), elphel_get_frame($GLOBALS [sensor_port]));   ob_flush();  flush();}
	if ($showSeqMode>0) echo "</pre>";
	//   exit (0);
}

928 929 930 931 932 933 934 935
function split_broadcast($pgmpars){
	$port_masks=array();
	foreach ($pgmpars as $name=>$value) {
		if ($name[0] == '*'){
			$port_masks[substr($name,1)] = $value;
			unset ($pgmpars[$name]);
		}
	}
936
	
937 938 939 940 941 942 943 944 945 946 947 948
	$broadcast=array(0=>array());
	foreach ($port_masks as $key=>$value){
//		if (!in_array($value,$broadcast)) $broadcast[] = $value;
		if (!array_key_exists($value,$broadcast)) $broadcast[$value] = array();
	}
	foreach ($pgmpars as $name=>$value) {
		if (array_key_exists($name,$port_masks)) $broadcast[$port_masks[$name]][$name] = $value;
		else $broadcast[0][$name] = $value;
	}
	return $broadcast;
}

949

950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
function  parsePost() {
   global $_POST,$testMode,$showSeqMode,$posted_params;
   $posted_params=array();
/// parse to an array of parameters/changes
   $testMode=$_POST['test_mode'];
   $showSeqMode=$_POST['show_seq'];
   foreach ($_POST as $post=>$value) {
     if (($post=="test_mode") || ($post=="show_seq")) {
// nop;
     } else {
       list ($name,$index)=explode('_',$post);
       $index = (int) $index;
       if (!$posted_params[$index]) $posted_params[$index]=array();
       $posted_params[$index][$name]=$value;
     }
965
   } 
966 967 968
}
/// Simulate POST from URL parameters if 'immediate' is present in the URL
function convertImmediateMode(){
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
	global $posted_params,$global_params,$frame_params;
	$posted_params=array();
	$index=0;
	foreach ($global_params as $param) {   
    	$posted_params[$index++]=array(
        	'name'   =>   $param['name'],
            'oldval' =>   $param['cur_value'],
            'paramdec' => $param['value'], /// 'paramhex' is not used
            'delay'=>     $param['ahead'],
    		'broadcast'=> $param['port_mask'],
            'apply' =>    $param['write_en'] && $param['modified']
        );
    }
   	foreach ($frame_params as $param) {
		$posted_params[$index++]=array(
            'name'   =>   $param['name'],
            'oldval' =>   $param['cur_value'],
            'paramdec' => $param['value'], /// 'paramhex' is not used
            'delay'=>     $param['ahead'],
			'broadcast'=> $param['port_mask'],
            'apply' =>    $param['write_en'] && $param['modified']
        );
    }
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
}
function extractNames($arr){
  $names=array();
  foreach ($arr as $element) $names[$element['name']]=0; // values will not be used;
  return $names;
}

function  preparePost() {
   global $testMode,$testBefore,$testAfter,$framesBeforeStart,$posted_params;
   $todo=array();
/// Now need to find minimal index in $todo and add compressor stop/copmpressor start
/// Extract data about actual actions, organize them in frames ahead (all globals will have ahead=0)
   foreach ($posted_params as $par) if ($par['apply']){
     if (array_key_exists('delay',$par)) {
       $delay = (int) $par['delay']  + (($testMode>0)?($testBefore+$framesBeforeStart):0);
     } else $delay=0;
     if (!$todo[$delay]) $todo[$delay]=array();
     $todo[$delay][$par['name']]=(int) $par['paramdec'];
1010 1011 1012 1013
     $broadcast=intval($par['broadcast'],16);
     if ($broadcast != 0){
     	$todo[$delay]['*'.$par['name']]=$broadcast;
     }
1014 1015 1016 1017 1018 1019 1020 1021 1022
   }
/// sort $todo using time (key)
   ksort($todo); /// needed to process parameters in temporal sequence
   if ($testMode>0) {
     end($todo);
     $todo[key($todo)+$testAfter]=array("COMPRESSOR_RUN"=>ELPHEL_CONST_COMPRESSOR_RUN_STOP);
     $todo[$framesBeforeStart]=array("COMPRESSOR_RUN"=>ELPHEL_CONST_COMPRESSOR_RUN_CONT);
     ksort($todo); /// re-sort it after adding start/stop
   }
1023
//   file_put_contents("/tmp/todo.log",print_r($todo,1));
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
   return $todo;
}

//   global $_POST,$testMode,$showSeqMode,$testBefore,$testAfter,$framesBeforeStart,$posted_params;


/**
 * @brief Scan commands for possible changing gamma tables, calculate them in advance
 * (driver can only scale gamma, not calculate prototypes)
 * @param todo - array of arrays of parameter chnages
 */

function  addGammas($todo) {
   global $showSeqMode;
   $gammas=array();
   foreach ($todo as $pars) {
     if (array_key_exists('GTAB_R', $pars)) $gammas[$pars['GTAB_R' ]>>16]=1; /// duplicates will be eliminated
     if (array_key_exists('GTAB_G', $pars)) $gammas[$pars['GTAB_G' ]>>16]=1;
     if (array_key_exists('GTAB_GB',$pars)) $gammas[$pars['GTAB_GB']>>16]=1;
     if (array_key_exists('GTAB_B', $pars)) $gammas[$pars['GTAB_B' ]>>16]=1;
   }
//   var_dump($gammas);
   foreach ($gammas as $gamma_black=>$whatever) {
     $black=($gamma_black>>8) & 0xff;
     $gamma=($gamma_black & 0xff)*0.01;
     if ($showSeqMode>0) printf("<pre>Adding gamma table (gamma=%f, black level=%d)\n</pre>\n",$gamma,$black);
Andrey Filippov's avatar
Andrey Filippov committed
1050
     elphel_gamma_add ($gamma, $black); // does not need $GLOBALS [sensor_port]
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
   }
}

function myval ($s) {
  $s=trim($s,"\" ");
  if (strtoupper(substr($s,0,2))=="0X")   return intval(hexdec($s));
  else return intval($s);
}


function parseGetNames() {
   global $_GET,$_POST,$isPost,$elp_const,$frame_params,$global_params,
1063
          $page_title,$default_ahead,$maxahead,$brief,$ahead_separator, $port_mask_separator,
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
          $refreshSig,$ignoreVals,$testMode,$showSeqMode,$posted_params,$defaultImgScale,$defaultImagesPerRow,$defaultImagesNumber,
          $imagesNumber,$imagesPerRow,$imgScale,$embedImageScale, $defaultEmbedImageScale;
   $index=0;
   $frame_params=array();
   $global_params=array();
   $immediateMode=false; /// if true - no dialog, just apply parameters from the URL
   foreach ($_GET as $key=>$value) {
      if ($key=="immediate") {
        $immediateMode=true;
      } else if ($key=="title") {
        $page_title=$value;
      } else if ($key=="shownumbers") {
        $brief=false;
      } else if ($key==$refreshSig) {
        $ignoreVals=true;
      } else if ($key=='test') {
        $testMode=   myval($isPost?$_POST['test_mode']:$value);
      } else if ($key=='showseq') {
        $showSeqMode=myval($isPost?$_POST['show_seq']:$value);
      } else if (($key=='images') || ($key=='link_images')) {
        if ($value) $value=explode(':',$value);
        else $value=array();
        $imagesNumber=$value[0]?$value[0]:$defaultImagesNumber;
        $imagesPerRow=$value[1]?$value[1]:$defaultImagesPerRow;
        $imgScale=    $value[2]?$value[2]:$defaultImgScale;
        if ($key=='images') {
          showLastImages($imagesNumber,$imagesPerRow,$imgScale);
          exit(0);
        }
      } else if ($key=='embed') {
        $embedImageScale=   $value;
        if (!$embedImageScale) $embedImageScale=$defaultEmbedImageScale;
      } else if ($key=='_time') {
Andrey Filippov's avatar
Andrey Filippov committed
1097
      } else if ($key=='sensor_port') {
1098 1099
      } else if ($key[0]=='*') {

1100 1101 1102 1103
      } else {
/// locate $key among constants, accept numeric values also
        $address=myval ($key);
        if (($address==0) && (strlen($key)>3)) { /// suspect constant
Andrey Filippov's avatar
Andrey Filippov committed
1104
          $address=elphel_parse_P_name($key); // does not need $GLOBALS [sensor_port],
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
        }
        if ($address==0) {
          $xml = new SimpleXMLElement("<?xml version='1.0'?><framepars/>");
          $xml->addChild ('ERROR','"Wrong address==0, probably misspelled constant: \''.$key.'\'"' );
          $rslt=$xml->asXML();
          header("Content-Type: text/xml");
          header("Content-Length: ".strlen($rslt)."\n");
          header("Pragma: no-cache\n");
          printf($rslt);
          exit (0);
        }
1116 1117 1118 1119 1120 1121
        $port_mask = 0;
        if (strpos($value,"!")!==FALSE) {
        	$port_mask = myval(substr($value,strpos($value,"!")+1),16);
        	$value = substr($value,strpos($value,"!")+1);
        }

1122 1123 1124 1125
        if (isset($_GET["*$key"])) {
        	$port_mask = myval($_GET["*$key"]);
        }
        
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
        $write_en=(!($value[0]=="@"));
        if (!$write_en) {
          if (strlen($value)==1) $value="";
          else $value=substr($value,1);
        }
        $values=explode(":", $value);
        foreach ($values as $value) {
          $modified=false;
          $value=explode($ahead_separator,$value);
          if (count($value)>1) {
            $ahead=myval($value[1]);
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
1137
            //if ($ahead<0) $ahead=0;
1138 1139 1140 1141 1142 1143 1144 1145
          } else {
            $ahead=$default_ahead;
          }
          $modified=(strlen($value[0])>0);
          $value=myval($value[0]);
          if ($isPost) {
            $ahead=$posted_params[$index]['delay'];
          }
Andrey Filippov's avatar
Andrey Filippov committed
1146
          if (elphel_is_global_par($address)) { // does not need $GLOBALS [sensor_port],
1147 1148 1149 1150 1151 1152
            $global_params[$index++]=array("number"=>$address,
                                           "name"=>$key,
                                           "value"=>$value,
                                           "write_en"=>$write_en,
                                           "cur_value"=>"",
                                           'modified'=>$modified,
1153 1154
                                           'ahead'=>$ahead,
                                           'port_mask'=>$port_mask);
1155 1156 1157 1158 1159 1160 1161
          } else {
            $frame_params [$index++]=array("number"=>$address,
                                           "name"=>$key,
                                           "value"=>$value,
                                           "write_en"=>$write_en,
                                           "cur_value"=>"",
                                           'modified'=>$modified,
1162 1163
                                           'ahead'=>$ahead,
                                           'port_mask'=>$port_mask);
1164 1165 1166 1167
          }
       }
     }
   }
1168
   $page_title.=': port '.$GLOBALS [sensor_port];
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
   return  $immediateMode;
}
function readCurrentParameterValues() {
   global $frame_params,$global_params,$page_title,$ignoreVals;
   $pars=array();
   foreach ($frame_params as $par) {
      $pars[$par['name']]=0;                /// may be duplicates (they will use the same $pars[] element
   }
   foreach ($global_params as $par) {
      $pars[$par['name']]=0;
   }
// echo "<pre>";
//print_r($pars);
Andrey Filippov's avatar
Andrey Filippov committed
1182
   $pars=elphel_get_P_arr($GLOBALS [sensor_port], $pars); /// next2 frame/globals
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
//print_r($pars);
   foreach ($frame_params as $key=>$par) {
      $frame_params[$key]['cur_value']=$pars[$par['name']];
      $par['cur_value']=$pars[$par['name']];
      if (!$par['modified'] || $ignoreVals) $frame_params[$key]['value']=$par['cur_value'];
//echo "pars[par['name']]=".$pars[$par['name']]."\n";
//print_r($par);
   }
   foreach ($global_params as $key=>$par) {
      $global_params[$key]['cur_value']=$pars[$par['name']];
      $par['cur_value']=$pars[$par['name']];
      if (!$par['modified'] || $ignoreVals) $global_params[$key]['value']=$par['cur_value'];
//print_r($par);
   }
}
///============
function mainJavascript($refreshSig) {
   global $refreshSig,$frame_params,$global_params;
   $checkboxNumbers="";
   foreach ($frame_params as $num=>$par) {
     $checkboxNumbers.=$num.",";
   }
   foreach ($global_params as $num=>$par) {
     $checkboxNumbers.=$num.",";
   }
   $checkboxNumbers=rtrim ($checkboxNumbers,",");

return <<<JAVASCRIPT
function hex2dec(h) {
 var dec=0;
 var d;
 h=h.toUpperCase();
 while (h.length>0) {
   d=h.charCodeAt(0)-"0".charCodeAt(0);
   if (d>9) d= (h.charCodeAt(0))-("A".charCodeAt(0))+10;
   dec=dec*16+d;
   h=h.substring(1,h.length);
 }
 return dec;
}
function dec2hex(d) {
 var hex="";
 var d0,d1;
 if (d<0) d+=4294967296;
 if ((d<0) || ( d>4294967296)) return "NaN";
 while (d>0) {
  d1=Math.floor(d/16);
  d0=d-16*d1;
  d=d1;
  hex=("0123456789abcdef".charAt(d0))+hex;
 }
 return hex;
}
function onchangeDec(elem,id_hex,id_apply) {
//  alert ("onchangeDec("+elem.id+","+id_hex+","+id_apply+")");
  document.getElementById(id_hex).value=dec2hex(document.getElementById(elem.id).value);
  document.getElementById(id_apply).checked=true;
}
function onchangeHex(elem,id_dec,id_apply) {
//  alert ("onchangeHex("+elem.id+","+id_dec+","+id_apply+")");
  document.getElementById(id_dec).value=hex2dec(document.getElementById(elem.id).value);
  document.getElementById(id_apply).checked=true;
}
function onchangeDelay(elem,id_apply) {
//  alert ("onchangeDelay("+elem.id+","+id_apply+")");
  var d=document.getElementById(elem.id).value;
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
1249
  //if (d<0) document.getElementById(elem.id).value=0;
1250 1251
  document.getElementById(id_apply).checked=true;
}
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
function onchangeBroadcast(elem,id_apply) {
//  alert ("onchangeBroadcast("+elem.id+","+id_apply+")");
  var d=hex2dec(document.getElementById(elem.id).value);
  if ((d<0) || (d>15)){
    if (d<0) document.getElementById(elem.id).value=0;
    if (d>15) document.getElementById(elem.id).value=15;
    document.getElementById(elem.id).value=hex2dec(d);
  }
  document.getElementById(id_apply).checked=true;
}
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
function refreshPage(mode) {
  var url=window.location.href;
  var refreshSign="&"+"$refreshSig";
  var refIndex,index1;
///remove &$refreshSig from the url (if any)
  while (url.indexOf(refreshSign)>=0) {
    refIndex=url.indexOf(refreshSign);
    url=url.substr(0,refIndex)+url.substr(refIndex+refreshSign.length);
  }
  if (mode) url+=refreshSign;
///Preserve test_mode through GET
  var testModeName="test";
  refIndex=url.indexOf("&"+testModeName);
  if (refIndex<0)refIndex=url.indexOf("?"+testModeName);
  if (refIndex>=0){ // test should be in the URL or it will not be shown on a page
    index1=url.indexOf("&",refIndex+testModeName.length);
    if (index1<0) index1= url.length;
    refIndex+=testModeName.length+1;
    url=url.substr(0,refIndex)+"="+(document.getElementById("id_test_mode").checked?1:0)+url.substr(index1);
  }
//  alert ("url="+url);
///Preserve show sequence through GET
  var showSeqName="showseq";
  refIndex=url.indexOf("&"+showSeqName);
  if (refIndex<0)refIndex=url.indexOf("?"+showSeqName);
  if (refIndex>=0){ // test should be in the URL or it will not be shown on a page
    index1=url.indexOf("&",refIndex+showSeqName.length);
    if (index1<0) index1= url.length;
    refIndex+=showSeqName.length+1;
    url=url.substr(0,refIndex)+"="+(document.getElementById("id_show_seq").checked?1:0)+url.substr(index1);
  }
//  alert ("url="+url);
  window.location.href=url;
}
function unCheckAll() {
  checkboxes=Array($checkboxNumbers);
  for (i in checkboxes) {
    if (((cb=document.getElementById('id_apply_'+checkboxes[i])))) {
      cb.checked=false;
    }
  }
}
function checkAll() {
  checkboxes=Array($checkboxNumbers);
  for (i in checkboxes) {
    if (((cb=document.getElementById('id_apply_'+checkboxes[i])))) {
      cb.checked=true;
    }
  }
}
JAVASCRIPT;
}



function startPage($page_title, $javascript) {
echo <<<HEAD
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
 <title>$page_title</title>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script type="text/javascript"><!--
$javascript
//-->
</script>
</head>
<body>
HEAD;
}
function  endPage(){
  echo "\n</body></html>\n";
}

/// Called twice from printPage($encoded_todo)
function showControlButtonsRow($table_width,$readonly,$testMode,$showSeqMode,$encoded_todo,$imagesNumber,$imagesPerRow,$imgScale,$checkNotUncheck) {
1339
   $url_port = "sensor_port=".$GLOBALS [sensor_port]."&";
1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
   printf ("<tr style=''><td colspan=%d>",$table_width-($readonly?0:1));
   if (!$readonly) {
     if ($testMode>=0) {
       printf ("<label style='border: 1px solid #000;' for='id_test_mode' title='Add compressor start (before) and compressor stop (after) the sequence'>".
               "Test".
               "<input type='checkbox' name='test_mode' id='id_test_mode' value='1' %s".
               " />".
               "</label>",
               ($testMode>0)?'checked':'');
     }
     if ($showSeqMode>=0) {
       printf ("<label style='border: 1px solid #000;' for='id_show_seq' title='Show command sequence and the sequence progress'>".
               "ShowSeq".
               "<input type='checkbox' name='show_seq' id='id_show_seq' value='1' %s".
               " />".
               "</label>",
               ($showSeqMode>0)?'checked':'');
     }
     printf ("<input type='submit' value='Apply' title='Program the parameters (selected by \"Apply\" checkboxes) to the camera'/>\n");
   }
   printf ("<input type='button' value='Refresh'onclick='refreshPage(1);' title='Reload the page, keep camera parameters (does not preserve delays yet)'/>\n");
   printf ("<input type='button' value='Restart'onclick= 'refreshPage(0);' title='Reload the page, keep URL parameters'/>\n");
   if ($encoded_todo) { // show link to last images
1363
    printf ("<a href='?".$url_port."images=%d:%d:%f&done=%s' target='new'>Last acquired images</a>\n",$imagesNumber,$imagesPerRow,$imgScale,$encoded_todo);
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
   }
   if (!$readonly) {
     if ($checkNotUncheck)
       printf ("</td><td style='text-align:center'><input type='button' value='&nbsp;Select&nbsp;\nAll'onclick='checkAll();' title='Check all \"Apply\" the checkboxes'/>\n");
     else
       printf ("</td><td style='text-align:center'><input type='button' value='Deselect\nAll'onclick='unCheckAll();' title='Uncheck all \"Apply\" the checkboxes'/>\n");
   }
   printf ("</td></tr>\n");
}

function printPage($encoded_todo) {
   global $frame_params,$global_params,$page_title,$brief,$testMode,$showSeqMode,
                    $imagesNumber,$imagesPerRow,$imgScale,$embedImageScale,$imgsrv,$imglink,$descriptions;

   $readonly=true;
   foreach ($frame_params as $par) if ($par['write_en']) {
      $readonly=false;
      break;
   }
   if ($readonly) foreach ($global_params as $par) if ($par['write_en']) {
      $readonly=false;
      break;
   }
1387 1388 1389
//   $table_width=$readonly?5:9;
   $table_width=$readonly?6:10; // with broadcast mask
    if ($brief) $table_width-=2;
1390 1391 1392 1393 1394 1395
//   printf ("<center><h3>$page_title</h3></center>\n");
///   printf ("<h3>$page_title</h3>\n");
   printf ("<form action=\"$self\" method=\"post\">");
   printf ("<table border='1' style='font-family: Courier, monospace;'>\n");

   if ($embedImageScale) {
1396
     $fd_circ=fopen("/dev/circbuf".strval($GLOBALS['sensor_port']),"r");
1397 1398 1399
     fseek($fd_circ, ELPHEL_LSEEK_CIRC_LAST,SEEK_END);
     $circbuf_pointer=ftell($fd_circ);
     fclose($fd_circ);
1400

1401
     if ($circbuf_pointer>=0) {
Andrey Filippov's avatar
Andrey Filippov committed
1402
       $meta=elphel_get_interframe_meta($GLOBALS [sensor_port], $circbuf_pointer);
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
       $width= floor($meta['width']*$embedImageScale);
       $height=floor($meta['height']*$embedImageScale);
//echo "width=$width, height=$height, embedImageScale=$embedImageScale<br />\n";
       printf ("<tr style='text-align:center'>".
               "<td colspan=$table_width>");
///       printf ("<a href='$imgsrv/%d/$imglink'><img src='$imgsrv/%d/$imglink' style='width:%dpx; height:%dpx;' /></a>",$circbuf_pointer,$circbuf_pointer, $width,$height); 
       printf ("<a href='$imgsrv/$imglink' target='new'><img src='$imgsrv/%d/$imglink' style='width:%dpx; height:%dpx;' title='Click on the image to open the last acquired one - it may be different than this one (if acquisition is going on)' /></a>",$circbuf_pointer, $width,$height); 
       printf ("</td></tr>\n");
     }

   }
   showControlButtonsRow($table_width,$readonly,$testMode,$showSeqMode,$encoded_todo,$imagesNumber,$imagesPerRow,$imgScale,true);

   printf ("<tr style='text-align:center'>".
           "<td rowspan=2>Parameter name</td>");
   if (!$brief) {printf (
           "<td colspan=2>Number</td>");
   }
   printf ("<td colspan=2>Current value</td>");
   if (!$readonly) {printf (
            "<td colspan=2>New value</td>".
            "<td rowspan=2>Program<br/>ahead</td>".
1425 1426
   		    "<td rowspan=2>Broadcast<br/>mask</td>".
   		 "<td rowspan=2>Apply</td></tr>");
1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462
   }
   printf   ("</tr>");
   printf ("<tr style='text-align:center'>");
   if (!$brief) {printf (
           "<td>&nbsp;dec&nbsp;</td>".
           "<td>&nbsp;hex&nbsp;</td>");
   }
   printf ("<td>&nbsp;&nbsp;&nbsp;dec&nbsp;&nbsp;&nbsp;&nbsp;</td>".
           "<td>&nbsp;&nbsp;&nbsp;hex&nbsp;&nbsp;&nbsp;&nbsp;</td>");
   if (!$readonly) {printf (
           "<td>&nbsp;&nbsp;&nbsp;dec&nbsp;&nbsp;&nbsp;&nbsp;</td>".
           "<td>&nbsp;&nbsp;&nbsp;hex&nbsp;&nbsp;&nbsp;&nbsp;</td>");
   }
   printf   ("</tr>");
   if (count($frame_params)) {
     printf ("<tr><td colspan='$table_width' style='text-align:center'>Frame-related parameters</td></tr>\n");
     foreach ($frame_params as $num=>$par) {
//       printf ("<tr title='%s'>",$descriptions[$par['name']]);
       printf ("<tr title='%s'>",getDescription ($par['name'],$descriptions));
       printf ("<td>%s</td>",$par['name']);
   if (!$brief) {
       printf ("<td style='text-align:right'>%d</td>",$num);
       printf ("<td style='text-align:right'>0x%x</td>",$num);
      }
       printf ("<td style='text-align:right'>%d</td>",$par['cur_value']);
       printf ("<td style='text-align:right'>0x%x".
                  "<input type='hidden' name='oldval_%d' value='%d'>".
                  "<input type='hidden' name='name_%d' value='%s'>".
                  "</td>",$par['cur_value'],$num,$par['cur_value'],$num,$par['name']);
       if ($par['write_en']) {
         printf ("<td><input name='paramdec_%d' type='text' size='8' value='%d' id='id_dec_%d' style='text-align:right'".
                 " onchange='onchangeDec(this,\"id_hex_%d\",\"id_apply_%d\");'/></td>",$num,$par['value'],$num,$num,$num);
         printf ("<td  style='white-space:nowrap'>0x<input name='paramhex_%d' type='text' size='8' value='%x' id='id_hex_%d' style='text-align:left'".
                 " onchange='onchangeHex(this,\"id_dec_%d\",\"id_apply_%d\");'/></td>",$num,$par['value'],$num,$num,$num);
         printf ("<td style='text-align:center'><input name='delay_%d' type='text' size='4' value='%d' id='id_delay_%d' style='text-align:right'".
                 " onchange='onchangeDelay(this,\"id_apply_%d\");'/></td>",$num,$par['ahead'],$num,$num);
1463

1464
         printf ("<td style='text-align:center'>0x<input name='broadcast_%d' type='text' size='2' value='%x' id='id_broadcast_%d' style='text-align:center'".
1465 1466
         		" onchange='onchangeBroadcast(this,\"id_apply_%d\");'/></td>",$num,$par['port_mask'],$num,$num);
         
1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508
         printf ("<td style='text-align:center'><input type='checkbox' name='apply_%d' value='1' %s id='id_apply_%d'/></td>",$num,$par['modified']?'checked':'',$num);
       }else {
         if (!$readonly) printf ("<td colspan='4'>&nbsp;</td>");
       }
       printf ("</tr>\n");
     }
   }
   if (count($global_params)) {
     if ($table_width<5) printf ("<tr><td colspan='$table_width' style='text-align:center'>Global parameters</td></tr>\n");
     else                printf ("<tr><td colspan='$table_width' style='text-align:center'>Global (not related to particular frames) parameters</td></tr>\n");
     foreach ($global_params as $num=>$par) {
//       printf ("<tr title='%s'>",$descriptions[$par['name']]);
       printf ("<tr title='%s'>",getDescription ($par['name'],$descriptions));
       printf ("<td>%s</td>",$par['name']);
   if (!$brief) {
       printf ("<td style='text-align:right'>%d</td>",$num);
       printf ("<td style='text-align:right'>0x%x</td>",$num);
      }
       printf ("<td style='text-align:right'>%d</td>",$par['cur_value']);
       printf ("<td style='text-align:right'>0x%x".
                  "<input type='hidden' name='oldval_%d' value='%d'>".
                  "<input type='hidden' name='name_%d' value='%s'>".
                  "</td>",$par['cur_value'],$num,$par['cur_value'],$num,$par['name']);
       if ($par['write_en']) {
         printf ("<td><input name='paramdec_%d' type='text' size='8' value='%d' id='id_dec_%d' style='text-align:right'".
                 " onchange='onchangeDec(this,\"id_hex_%d\",\"id_apply_%d\");'/></td>",$num,$par['value'],$num,$num,$num);
         printf ("<td  colspan='2' style='white-space:nowrap'>0x<input name='paramhex_%d' type='text' size='8' value='%x' id='id_hex_%d'  style='text-align:left'".
                 " onchange='onchangeHex(this,\"id_dec_%d\",\"id_apply_%d\");'/></td>",$num,$par['value'],$num,$num,$num);
         printf ("<td style='text-align:center'><input type=\"checkbox\" name=\"apply_%d\" value=\"1\" %s id='id_apply_%d'/></td>",$num,$par['modified']?'checked':'',$num);
       }else {
         if (!$readonly) printf ("<td colspan='4'>&nbsp;</td>");
       }
       printf ("</tr>\n");
     }
   }
   showControlButtonsRow($table_width,$readonly,$testMode,$showSeqMode,$encoded_todo,$imagesNumber,$imagesPerRow,$imgScale,false);
   printf ("</table>\n");
   printf ("</form>\n");
}
function getDescription ($compositeName,$descriptions){
  if (array_key_exists($compositeName,$descriptions)) return $descriptions[$compositeName];
/// try to find a parent name
Andrey Filippov's avatar
Andrey Filippov committed
1509
  $number=elphel_parse_P_name($compositeName); // does not need $GLOBALS [sensor_port],
1510 1511 1512 1513 1514 1515 1516 1517 1518 1519
  if (!$number) return "Unknown name";
  $prefix="";
// is it a bit field?
  $width= ($number >> 21) & 0x1f;
  $bit=   ($number >> 16) & 0x1f;
  $name=$compositeName;
  if ($bit || $width) {
    $name=substr($compositeName,0,strlen($compositeName)-strlen('__XXYY'));
    if ($width==1) $prefix.= sprintf("This is the bit %d of %s - ",$bit,$name);
    else           $prefix.= sprintf("This is a bit selection - bits %d through %d of %s - ",$bit,($bit+$width-1),$name);
Andrey Filippov's avatar
Andrey Filippov committed
1520
    $number=elphel_parse_P_name($name); // does not need $GLOBALS [sensor_port],
1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548
    if (!$number) {
        echo "Internal error in parsedit.php:Unknown name $name of $compositeName";
        exit (1);
    }
  }

  if (array_key_exists($name,$descriptions)) return $prefix.$descriptions[$name];
  $index="";
/// see if it has displacement from a known name
  while ($name && strpos (" 0123456789", $name[strlen($name)-1])) {
    $index= $name[strlen($name)-1].$index;
    $name=substr($name, 0, strlen($name)-1);
  }
  if ($prefix) $prefix.="offset ".$index." of ".$name." - ";
  else $prefix.="This is the parameter at offset ".$index." of ".$name." - ";
  if (array_key_exists($name,$descriptions)) return $name.": ".$prefix."++".$descriptions[$name];
  return $prefix."no description available";
}

/*
/// compose bit fields to be OR-ed to the parameter number bit 16..25: b - start bit (0..31), w - width 1..32
#define FRAMEPAIR_FRAME_BITS(w,b) ((((w) & 0x1f)<<21) | (((b) & 0x1f)<<16))
/// Shift new data (nd), apply mask and combine with old data (od), taking shift/width information from bits 16..25 of (a)
#define FRAMEPAIR_FRAME_MASK_NEW(a,od,nd) ((((od) ^ ((nd) << (((a)>>16) & 0x1f))) & (((1 << (((a)>>21) & 0x1f))-1) << (((a)>>16) & 0x1f))) ^ (od))


*/
function getParDescriptions($autocampars) {
1549
  /*	
1550 1551
  $file=file($autocampars);
  $path="";
1552
  foreach ($file as $line) if ((strpos($line,'$configPath')!==false ) || ((strpos($line,"'configPath'")!==false)){
1553 1554 1555 1556 1557 1558
    $name= strtok ($line,'"');
    if (strpos ($name,'//')===false) {
      $path=strtok ('"');
      break;
    }
  }
1559
  */
1560
  $path='/etc/elphel393/autocampars.xml'; // link to a master channel config
Andrey Filippov's avatar
Andrey Filippov committed
1561
  $path = str_replace("0.",strval($GLOBALS [sensor_port]).".",$path); // NC393: here it does not really matter
1562 1563 1564 1565 1566 1567 1568 1569 1570
  $xml = simplexml_load_file($path);
  $descriptions=array();
  foreach ($xml->descriptions->children() as $entry) {
    $descriptions[$entry->getName()]=str_replace('`',"\n",trim((string)$entry,"\""));
  }
  return $descriptions;
}
///============
?>