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

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