root/core/main.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. schedule_memdump
  2. dump_memory
  3. core_get_free_memory
  4. core_rawdata_available
  5. core_spytask_can_start
  6. script_autostart
  7. core_spytask
  8. ftell

   1 #include "platform.h"
   2 #include "stdlib.h"
   3 #include "core.h"
   4 #include "conf.h"
   5 #include "gui.h"
   6 #include "gui_draw.h"
   7 #include "histogram.h"
   8 #include "raw.h"
   9 #include "console.h"
  10 #include "shooting.h"
  11 
  12 #include "edgeoverlay.h"
  13 #include "module_load.h"
  14 
  15 #ifdef CAM_HAS_GPS
  16   #include "gps.h"
  17 #endif
  18 
  19 #include "kbd_common.h"
  20 
  21 //==========================================================
  22 
  23 static char osd_buf[50];
  24 
  25 //==========================================================
  26 
  27 volatile int chdk_started_flag=0;
  28 
  29 int no_modules_flag;
  30 
  31 static volatile int spytask_can_start;
  32 
  33 static unsigned int memdmptick = 0;
  34 volatile int memdmp_delay = 0; // delay in seconds
  35 
  36 void schedule_memdump()
  37 {
  38     memdmptick = get_tick_count() + memdmp_delay * 1000;
  39 }
  40 
  41 void dump_memory()
  42 {
  43     int fd;
  44     static int cnt=1;
  45     static char fn[32];
  46 
  47     // zero size means all RAM
  48     if (conf.memdmp_size == 0) conf.memdmp_size = (unsigned int)camera_info.maxramaddr + 1;
  49     // enforce RAM area
  50     if ((unsigned int)conf.memdmp_start > (unsigned int)camera_info.maxramaddr)
  51         conf.memdmp_start = 0;
  52     if ( (unsigned int)conf.memdmp_start + (unsigned int)conf.memdmp_size > ((unsigned int)camera_info.maxramaddr+1) )
  53         conf.memdmp_size = (unsigned int)camera_info.maxramaddr + 1 - (unsigned int)conf.memdmp_start;
  54 
  55     started();
  56     // try to avoid hanging the camera
  57     if ( !is_video_recording() ) {
  58         mkdir("A/DCIM");
  59         mkdir("A/DCIM/100CANON");
  60         fd = -1;
  61         do {
  62             sprintf(fn, "A/DCIM/100CANON/CRW_%04d.JPG", cnt++);
  63             if (stat(fn,0) != 0) {
  64                 fd = open(fn, O_WRONLY|O_CREAT, 0777);
  65                 break;
  66             }
  67         } while(cnt<9999);
  68         if (fd>=0) {
  69             if ( conf.memdmp_start == 0 ) {
  70                 long val0 = *((long*)(0|CAM_UNCACHED_BIT));
  71                 write(fd, &val0, 4);
  72                 if  (conf.memdmp_size > 4) {
  73                     write(fd, (void*)4, conf.memdmp_size - 4);
  74                 }
  75             }
  76             else {
  77                 write(fd, (void*)conf.memdmp_start, conf.memdmp_size);
  78             }
  79             close(fd);
  80         }
  81         vid_bitmap_refresh();
  82     }
  83     finished();
  84 }
  85 
  86 int core_get_free_memory()
  87 {
  88     cam_meminfo camera_meminfo;
  89     GetCombinedMemInfo(&camera_meminfo);
  90     return camera_meminfo.free_block_max_size;
  91 }
  92 
  93 static volatile long raw_data_available;
  94 
  95 /* called from another process */
  96 void core_rawdata_available()
  97 {
  98     raw_data_available = 1;
  99 }
 100 
 101 void core_spytask_can_start() {
 102     spytask_can_start = 1;
 103 }
 104 
 105 // remote autostart
 106 void script_autostart()
 107 {
 108     // Tell keyboard task we are in <ALT> mode
 109     enter_alt();
 110     // We were called from the GUI task so switch to <ALT> mode before switching to Script mode
 111     gui_activate_alt_mode();
 112     // Switch to script mode and start the script running
 113     script_start_gui( 1 );
 114 }
 115 
 116 void core_spytask()
 117 {
 118     int cnt = 1;
 119     int i=0;
 120 #ifdef CAM_HAS_GPS
 121     int gps_delay_timer = 200 ;
 122     int gps_state = -1 ;
 123 #endif
 124 #if (OPT_DISABLE_CAM_ERROR)
 125     extern void DisableCamError();
 126     int dce_cnt=0;
 127     int dce_prevmode=0;
 128     int dce_nowmode;
 129 #endif
 130     
 131     parse_version(&chdk_version, BUILD_NUMBER, BUILD_SVNREV);
 132 
 133     // Init camera_info bits that can't be done statically
 134     camera_info_init();
 135 
 136     spytask_can_start=0;
 137 
 138 #if !defined(CAM_DRYOS)
 139 // create semaphore to protect Canon memory malloc/free/memPartInfo
 140 // on VxWorks, spytask should start before any other CHDK tasks
 141     extern void canon_malloc_init(void);
 142     canon_malloc_init();
 143 #endif
 144 
 145     extern void aram_malloc_init(void);
 146     aram_malloc_init();
 147 
 148     extern void exmem_malloc_init(void);
 149     exmem_malloc_init();
 150 
 151 #ifdef CAM_CHDK_PTP
 152     extern void init_chdk_ptp_task();
 153     init_chdk_ptp_task();
 154 #endif
 155 
 156     while((i++<1000) && !spytask_can_start) msleep(10);
 157 
 158     started();
 159     msleep(50);
 160     finished();
 161 
 162 #if !CAM_DRYOS
 163     drv_self_unhide();
 164 #endif
 165 
 166     conf_restore();
 167 
 168     extern void gui_init();
 169     gui_init();
 170 
 171 #if CAM_CONSOLE_LOG_ENABLED
 172     extern void cam_console_init();
 173     cam_console_init();
 174 #endif
 175 
 176     static char *chdk_dirs[] =
 177     {
 178         "A/CHDK",
 179         "A/CHDK/FONTS",
 180         "A/CHDK/SYMBOLS",
 181         "A/CHDK/SCRIPTS",
 182         "A/CHDK/LANG",
 183         "A/CHDK/BOOKS",
 184         "A/CHDK/MODULES",
 185         "A/CHDK/MODULES/CFG",
 186         "A/CHDK/GRIDS",
 187         "A/CHDK/CURVES",
 188         "A/CHDK/DATA",
 189         "A/CHDK/LOGS",
 190         "A/CHDK/EDGE",
 191     };
 192     for (i = 0; i < sizeof(chdk_dirs) / sizeof(char*); i++)
 193         mkdir_if_not_exist(chdk_dirs[i]);
 194 
 195     no_modules_flag = stat("A/CHDK/MODULES/FSELECT.FLT",0) ? 1 : 0 ;
 196 
 197     // Calculate the value of get_tick_count() when the clock ticks over to the next second
 198     // Used to calculate the SubSecondTime value when saving DNG files.
 199     long t1, t2;
 200     t2 = time(0);
 201     do
 202     {
 203         t1 = t2;
 204         camera_info.tick_count_offset = get_tick_count();
 205         t2 = time(0);
 206         msleep(10);
 207     } while (t1 != t2);
 208     camera_info.tick_count_offset = camera_info.tick_count_offset % 1000;
 209 
 210     // remote autostart
 211     if (conf.script_startup==SCRIPT_AUTOSTART_ALWAYS)
 212     {
 213         script_autostart();
 214     }
 215     else if (conf.script_startup==SCRIPT_AUTOSTART_ONCE)
 216     {
 217         conf.script_startup=SCRIPT_AUTOSTART_NONE;
 218         conf_save();
 219         script_autostart();
 220     }
 221 
 222     shooting_init();
 223 
 224     // if starting  with HDMI connected, suspend drawing for 3s to avoid crashes
 225 #ifdef HDMI_HPD_FLAG
 226     if(get_hdmi_hpd_physw_mod()) {
 227         draw_suspend(3000);
 228     }
 229 #endif
 230 
 231     if(conf.check_firmware_crc) {
 232         module_run("fwcrc.flt");
 233     }
 234 
 235     while (1)
 236     {
 237         // Set up camera mode & state variables
 238         mode_get();
 239 
 240 #ifdef CAM_CLEAN_OVERLAY
 241         extern void handle_clean_overlay();
 242         handle_clean_overlay();
 243 #endif
 244 
 245 #ifdef  CAM_UNLOCK_ANALOG_AV_IN_REC
 246         extern void handle_analog_av_in_rec();
 247         handle_analog_av_in_rec();
 248 #endif
 249         // update HDMI power override based on mode and remote settings
 250 #ifdef CAM_REMOTE_HDMI_POWER_OVERRIDE
 251         extern void update_hdmi_power_override(void);
 252         update_hdmi_power_override();
 253 #endif
 254 
 255         extern void set_palette();
 256         set_palette();
 257 
 258 #if (OPT_DISABLE_CAM_ERROR)
 259         dce_nowmode = camera_info.state.mode_play;
 260         if (dce_prevmode==dce_nowmode)
 261         {                       //no mode change
 262             dce_cnt++;          // overflow is not a concern here
 263         }
 264         else
 265         {                       //mode has changed
 266             dce_cnt=0;
 267         }
 268         if (dce_cnt==100)
 269         {                       // 1..2s past play <-> rec mode change
 270             DisableCamError();
 271         }
 272         dce_prevmode=dce_nowmode;
 273 #endif
 274 
 275         if ( memdmptick && (get_tick_count() >= memdmptick) )
 276         {
 277             memdmptick = 0;
 278             dump_memory();
 279         }
 280 
 281 #ifdef CAM_HAS_GPS
 282         if ( --gps_delay_timer == 0 )
 283         {
 284             gps_delay_timer = 50 ;
 285             if ( gps_state != (int)conf.gps_on_off )
 286             {
 287                 gps_state = (int)conf.gps_on_off ;
 288                 init_gps_startup(!gps_state) ; 
 289             }
 290         }
 291 #endif        
 292         
 293         // Change ALT mode if the KBD task has flagged a state change
 294         gui_activate_alt_mode();
 295 
 296 #ifdef  CAM_LOAD_CUSTOM_COLORS
 297         // Color palette function
 298         extern void load_chdk_palette();
 299         load_chdk_palette();
 300 #endif
 301 
 302         if (raw_data_available)
 303         {
 304             raw_process();
 305             extern void hook_raw_save_complete();
 306             hook_raw_save_complete();
 307             raw_data_available = 0;
 308 #ifdef CAM_HAS_GPS
 309             if (((int)conf.gps_on_off == 1) && ((int)conf.gps_waypoint_save == 1)) gps_waypoint();
 310 #endif
 311 #if defined(CAM_CALC_BLACK_LEVEL)
 312             // Reset to default in case used by non-RAW process code (e.g. raw merge)
 313             camera_sensor.black_level = CAM_BLACK_LEVEL;
 314 #endif
 315             continue;
 316         }
 317 
 318         if ((camera_info.state.state_shooting_progress != SHOOTING_PROGRESS_PROCESSING) || recreview_hold)
 319         {
 320 #ifdef CAM_DETECT_BITMAP_UPDATE
 321             extern int check_gui_needs_redraw();
 322             int gui_force_redraw = check_gui_needs_redraw();
 323 #else
 324             int gui_force_redraw = 0;
 325 #endif
 326             if (gui_force_redraw || (((cnt++) & 3) == 0))
 327                 gui_redraw(gui_force_redraw);
 328         }
 329 
 330         if (camera_info.state.state_shooting_progress != SHOOTING_PROGRESS_PROCESSING)
 331         {
 332             if (conf.show_histo)
 333                 libhisto->histogram_process();
 334 
 335             if ((camera_info.state.gui_mode_none || camera_info.state.gui_mode_alt) && conf.edge_overlay_thresh && conf.edge_overlay_enable)
 336             {
 337                 // We need to skip first tick because stability
 338                 if (chdk_started_flag)
 339                 {
 340                     libedgeovr->edge_overlay();
 341                 }
 342             }
 343         }
 344 
 345         if ((camera_info.state.state_shooting_progress == SHOOTING_PROGRESS_PROCESSING) && (!shooting_in_progress()))
 346         {
 347             camera_info.state.state_shooting_progress = SHOOTING_PROGRESS_DONE;
 348         }
 349 
 350         i = 0;
 351 
 352 #ifdef DEBUG_PRINT_TO_LCD
 353         sprintf(osd_buf, "%d", cnt );   // modify cnt to what you want to display
 354         draw_txt_string(1, i++, osd_buf, user_color(conf.osd_color));
 355 #endif
 356 #if defined(OPT_FILEIO_STATS)
 357         sprintf(osd_buf, "%3d %3d %3d %3d %3d %3d %3d %4d",
 358                 camera_info.fileio_stats.fileio_semaphore_errors, camera_info.fileio_stats.close_badfile_count,
 359                 camera_info.fileio_stats.write_badfile_count, camera_info.fileio_stats.open_count,
 360                 camera_info.fileio_stats.close_count, camera_info.fileio_stats.open_fail_count,
 361                 camera_info.fileio_stats.close_fail_count, camera_info.fileio_stats.max_semaphore_timeout);
 362         draw_txt_string(1, i++, osd_buf,user_color( conf.osd_color));
 363 #endif
 364 
 365         if (camera_info.perf.md_af_tuning)
 366         {
 367             sprintf(osd_buf, "MD last %-4d min %-4d max %-4d avg %-4d", 
 368                 camera_info.perf.af_led.last, camera_info.perf.af_led.min, camera_info.perf.af_led.max, 
 369                 (camera_info.perf.af_led.count>0)?camera_info.perf.af_led.sum/camera_info.perf.af_led.count:0);
 370             draw_txt_string(1, i++, osd_buf, user_color(conf.osd_color));
 371         }
 372 
 373         // Process async module unload requests
 374         module_tick_unloader();
 375 
 376         msleep(20);
 377         chdk_started_flag=1;
 378     }
 379 }
 380 
 381 long ftell(FILE *file) {
 382     if(!file) return -1;
 383     return file->pos;
 384 }

/* [<][>][^][v][top][bottom][index][help] */