# include "dimm.h"

static struct sbig_init info;

int InitCamera (int port) {
  
  int i, state;

  for (i = 0; i < 10; i++) {
    state = sbig_init (port, SBIG_IMAGING_CCD, &info);
    if (state == -6) state = 0;
    if (state ==  0) {
      DumpCameraInfo ();
      return (TRUE);
    }
    fprintf (stderr, "retry...\n");
  }
  
  fprintf (stderr, "failed to init sbig camera on %d\n", port);
  fprintf (stderr, "%s\n", sbig_show_error (state));
  return (FALSE);
}

void DumpCameraInfo () {

      fprintf (stderr, "opened sbig camera:\n");
      fprintf (stderr, "linux_version: %f\n",      info.linux_version);
      fprintf (stderr, "nmbr_bad_columns: %d\n",   info.nmbr_bad_columns);
      fprintf (stderr, "imaging_abg_type: %d\n",   info.imaging_abg_type);
      fprintf (stderr, "serial_number: %s\n",      info.serial_number);
      fprintf (stderr, "firmware_version: %d\n",   info.firmware_version);
      fprintf (stderr, "camera_name: %s\n",        info.camera_name);
      fprintf (stderr, "nmbr_readout_modes: %d\n", info.camera_info[0].nmbr_readout_modes);
      fprintf (stderr, "mode: %d\n",               info.camera_info[0].readout_mode[0].mode);
      fprintf (stderr, "width: %d\n",              info.camera_info[0].readout_mode[0].width);
      fprintf (stderr, "height: %d\n",             info.camera_info[0].readout_mode[0].height);
      fprintf (stderr, "gain: %d\n",               info.camera_info[0].readout_mode[0].gain);
      fprintf (stderr, "pixel_width: %d\n",        info.camera_info[0].readout_mode[0].pixel_width);
      fprintf (stderr, "pixel_height: %d\n",       info.camera_info[0].readout_mode[0].pixel_height);

      fprintf (stderr, "ST5_AD_size: %d\n", info.ST5_AD_size);
      fprintf (stderr, "ST5_filter_type: %d\n", info.ST5_filter_type);
}      

void CameraFullSize (int *x, int *y) {
  *x = info.camera_info[0].readout_mode[0].width;
  *y = info.camera_info[0].readout_mode[0].height;
}

int SetTemperature (double temp) {

  int state;
  struct sbig_cool cool;

  if (temp < -50) return (FALSE);
  if (temp > +20) return (FALSE);

  cool.regulation = SBIG_TEMP_REGULATION_ON;
  cool.temperature = (int) (10.0*temp + 0.5);
  cool.direct_drive = 0;

  state = sbig_set_cooling (&cool);
  if (state < 0) {
    fprintf (stderr, "sbig error: %s\n", sbig_show_error (state));
    return (FALSE);
  }
  return (TRUE);
}

double GetTemperature () {

  int state;
  double temp;
  struct sbig_status status;

  state = sbig_get_status (&status);
  if (state < 0) {
    fprintf (stderr, "sbig error: %s\n", sbig_show_error (state));
    return (-200.0);
  }
  temp = (status.ccd_temperature - 0.5) / 10.0;
  return (temp);
}

int DumpCameraStatus () {

  int state;
  double temp;
  struct sbig_status status;

  state = sbig_get_status (&status);
  if (state < 0) {
    fprintf (stderr, "sbig error: %s\n", sbig_show_error (state));
    return (FALSE);
  }

  fprintf (stderr, "imaging_ccd_status: %d\n", status.imaging_ccd_status);
  fprintf (stderr, "tracking_ccd_status: %d\n", status.tracking_ccd_status);
  fprintf (stderr, "fan_on: %d\n", status.fan_on);
  fprintf (stderr, "shutter_state: %d\n", status.shutter_state);
  fprintf (stderr, "led_state: %d\n", status.led_state);
  fprintf (stderr, "shutter_edge: %d\n", status.shutter_edge);
  fprintf (stderr, "plus_x_relay: %d\n", status.plus_x_relay);
  fprintf (stderr, "minus_x_relay: %d\n", status.minus_x_relay);
  fprintf (stderr, "plus_y_relay: %d\n", status.plus_y_relay);
  fprintf (stderr, "minus_y_relay: %d\n", status.minus_y_relay);
  fprintf (stderr, "pulse_active: %d\n", status.pulse_active);
  fprintf (stderr, "temperature_regulation: %d\n", status.temperature_regulation);
  fprintf (stderr, "temperature_setpoint: %d\n", status.temperature_setpoint);
  fprintf (stderr, "cooling_power: %d\n", status.cooling_power);
  fprintf (stderr, "air_temperature: %d\n", status.air_temperature);
  fprintf (stderr, "ccd_temperature: %d\n", status.ccd_temperature);

  return (TRUE);
}

/* block until exposure is complete */
int Exposure (double exptime) {

  int i, state;
  struct sbig_expose expose;
  struct sbig_status status;

  expose.ccd = SBIG_IMAGING_CCD;
  expose.exposure_time = (int)(100.0*exptime);
  expose.abg_state = SBIG_ABG_OFF;
  expose.shutter = SBIG_EXPOSE_SHUTTER_NORMAL;  /* shuttermode = ? */

  /* drop this ? */
  /* usleep ((int)(exptime*1000000)); */
  state = sbig_expose (&expose);
  if (state < 0) {
    fprintf (stderr, "exposure error\n");
    fprintf (stderr, "%s\n", sbig_show_error (state));
    return (FALSE);
  }
  
  for (i = 0; i < expose.exposure_time + 10; ++i) {
    state = sbig_get_status (&status);
    /* fprintf (stderr, "%d\n", state); */
    /* fprintf (stderr, "%d  %d\n", status.imaging_ccd_status, status.shutter_state); */
    /*    if (state == 0) return (TRUE); */
    if (status.imaging_ccd_status == -SBIG_NO_EXPOSURE_IN_PROGRESS) return (TRUE);
    if (status.imaging_ccd_status == -SBIG_EXPOSURE_IN_PROGRESS) {
      usleep (10000);
      continue;
    }
    fprintf (stderr, "exposure error\n");
    fprintf (stderr, "%s\n", sbig_show_error (state));
    return (FALSE);
  }
  fprintf (stderr, "exposure timeout\n");
  return (FALSE);
}

int   readout_abort;
float readout_percent;
static int readout_callback (float percent) {
  /* return 1 to continue, 0 to abort */
  if (((int)(percent) % 10) == 0) { fprintf (stderr, "."); }
  readout_percent = percent;
  if (readout_abort) return 0;
  return 1;
}

int ReadOut (int x, int y, int dx, int dy, int binning, unsigned short *buffer) {

  int state, Nbytes;
  static struct sbig_readout readout;

  readout.x = x;
  readout.y = y;
  readout.width  = dx;
  readout.height = dy;

  Nbytes = readout.width*readout.height*sizeof(short);

  /* for bin 2x2 or 3x3, need to adjust dx, dy above */
  readout.ccd = SBIG_IMAGING_CCD;
  readout.binning = SBIG_BIN_1X1;
  readout.data = buffer;
  readout.data_size_in_bytes = Nbytes;
  readout.callback = readout_callback;
    
  fprintf (stderr, "%d, %d : %d x %d\n", readout.x, readout.y, readout.width, readout.height);
  sync (); 
  readout_abort = FALSE;
  state = sbig_readout (&readout);
  fprintf (stderr, "\n");
  if (state < 0) {
    fprintf (stderr, "sbig error: %s\n", sbig_show_error (state));
    return (FALSE);
  }
  return (TRUE);
}

int OpenShutter () {

  int state;
  struct sbig_control control;

  control.shutter = SBIG_OPEN_SHUTTER;
  state = sbig_control (&control);
  if (state < 0) {
    fprintf(stderr, "sbig error: %s\n", sbig_show_error (state));
    return (FALSE);
  }
  return (TRUE);
}

int CloseShutter () {

  int state;
  struct sbig_control control;

  control.shutter = SBIG_CLOSE_SHUTTER;
  state = sbig_control (&control);
  if (state < 0) {
    fprintf(stderr, "sbig error: %s\n", sbig_show_error (state));
    return (FALSE);
  }
  return (TRUE);
}
