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

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

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