/*
 AurelALS300data
 Copyright (C) 2010 Martin Štrympl
 
 This library 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.0 of the License, or (at your option) any later version.
 
 This library 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 library.
 
 Martin Štrympl
 xstrym00@stud.feec.vutbr.cz
 */

/*
 *  data.c
 *  AurelALS300data
 *
 *  Created by Martin Štrympl on 11/10/10.
 *  Copyright 2010 School. All rights reserved.
 *
 */

#include "data.h"

#include "Module.h"

struct sData_Element
{
  /*
   'start x
   startx As String * 13
   'start y
   starty As String * 13
   'stop x
   stopx As String * 13
   'stop y
   stopy As String * 13
   'valore ohmico
   ohmvalue As String * 11
   */
   int32_t i4_startx, i4_starty, i4_stopx, i4_stopy;
   /*
   'velocita' taglio
   speed As String * 7
   'corrente
   current As String * 7
   'freq. laser
   freq As String * 7
   'scanner h
   scanh As String * 5
   'scanner l
   scanl As String * 5
   */
   /*
   'scanner g
   scang As String * 5
   'slope
   slope As String * 3
   'tolleranza accettazione pretest
   tolaccept As String * 5
   'tolleranza taglio finale
   tolcut As String * 5
   'tipo di taglio
   cutType As String * 4
   */
   uint8_t u1_cutType;
   /*
   'direzione inizio taglio
   cutDir As String * 3
   'percentuale primo stop
   cut1St As String * 5
   'percentuale prestop
   cutPre As String * 5
   'valore in volt scatto funzionale
   voltvalue As String * 11
   'mark condition
   mark As String * 3
   */
   /*
   'taglio funzionale
   func As String * 3
   'step taglio serp
   serpstep As String * 6
   'identificatore di res.
   reslabel As String * 12
   'se 1 usa visione
   vision As String * 3
   'i seguenti parametri vengono utilizzati per il taglio multiplo
   'start2 X
   start2X As String * 13
   */
   // reslabel
   /*
   'start2Y
   start2Y As String * 13
   'stop2X
   stop2X As String * 13
   'stop2Y
   stop2Y As String * 13
   'N. Cut Multipli
   strNCutMultiple As String * 13
   'sviluppi futuri
   sparedata As String * 1816
   */
   /*
   'Posizione X Probe A
   recProbeAX As String * 13
   'Posizione Y Probe A
   recProbeAY As String * 13
   'Posizione X Probe B
   recProbeBX As String * 13
   'Posizione Y Probe B
   recProbeBY As String * 13
   'Stato Filtro 1
   recFilter1 As String * 3
   */
   // can be zero
   //int32_t i4_recProbeAX, i4_recProbeAY,i4_recProbeBX, i4_recProbeBY;
   /*
   'Stato Filtro 2
   recFilter2 As String * 3
   'Stato Filtro 3
   recFilter3 As String * 3
   'Stato Filtro 4
   recFilter4 As String * 3
   'Stato Filtro PSI
   recFilterPSI As String * 3
   'Abilitazione AutoFuoco
   AutoFEnable As String * 3
   */
};

bool Data_Add_Element(struct sData *ps_data,
                      long double ld_startx, long double ld_starty, long double ld_stopx, long double ld_stopy,
                      uint8_t u1_cutType)
{
  struct sData_Element *as_elements;
  
  struct sData_Element s_element;
  s_element.i4_startx=floor((ld_startx*800.0L)+0.5L)+0.1L;
  s_element.i4_starty=floor((ld_starty*800.0L)+0.5L)+0.1L;
  s_element.i4_stopx=floor((ld_stopx*800.0L)+0.5L)+0.1L;
  s_element.i4_stopy=floor((ld_stopy*800.0L)+0.5L)+0.1L;
  
  if ((s_element.i4_startx==s_element.i4_stopx)&&(s_element.i4_starty==s_element.i4_stopy)) return true;
  
  as_elements=(struct sData_Element *)realloc(ps_data->as_elements, sizeof(struct sData_Element)*(ps_data->u4_elements+1));
  
  if (as_elements)
  {
    ps_data->as_elements=as_elements;
    
    //ps_data->as_elements[ps_data->u4_elements].i4_startx=floor((ld_startx*800.0L)+0.5L)+0.1L;
    //ps_data->as_elements[ps_data->u4_elements].i4_starty=floor((ld_starty*800.0L)+0.5L)+0.1L;
    //ps_data->as_elements[ps_data->u4_elements].i4_stopx=floor((ld_stopx*800.0L)+0.5L)+0.1L;
    //ps_data->as_elements[ps_data->u4_elements].i4_stopy=floor((ld_stopy*800.0L)+0.5L)+0.1L;
    ps_data->as_elements[ps_data->u4_elements]=s_element;
    
    ps_data->as_elements[ps_data->u4_elements].u1_cutType=u1_cutType;
    
    ps_data->u4_elements++;
    
    return true;
  }
  
  return false;
}

bool Data_Transformate(struct sData *ps_data)
{
  int32_t i4_x_min, i4_x_max, i4_y_min, i4_y_max;
  
  long double ld_x_ratio, ld_y_ratio;
  
  uint32_t u4_n;
  
  if (ps_data->u4_elements==0) return true;
  
  if (ps_data->as_elements->i4_startx<ps_data->as_elements->i4_stopx)
  {
    i4_x_min=ps_data->as_elements->i4_startx;
    i4_x_max=ps_data->as_elements->i4_stopx;
  }
  else
  {
    i4_x_min=ps_data->as_elements->i4_stopx;
    i4_x_max=ps_data->as_elements->i4_startx;
  }
  
  if (ps_data->as_elements->i4_starty<ps_data->as_elements->i4_stopy)
  {
    i4_y_min=ps_data->as_elements->i4_starty;
    i4_y_max=ps_data->as_elements->i4_stopy;
  }
  else
  {
    i4_y_min=ps_data->as_elements->i4_stopy;
    i4_y_max=ps_data->as_elements->i4_starty;
  }
  
  for (u4_n=1;u4_n<ps_data->u4_elements;u4_n++)
  {
    if (i4_x_min>ps_data->as_elements[u4_n].i4_startx) i4_x_min=ps_data->as_elements[u4_n].i4_startx;
    if (i4_x_min>ps_data->as_elements[u4_n].i4_stopx) i4_x_min=ps_data->as_elements[u4_n].i4_stopx;
    if (i4_x_max<ps_data->as_elements[u4_n].i4_startx) i4_x_max=ps_data->as_elements[u4_n].i4_startx;
    if (i4_x_max<ps_data->as_elements[u4_n].i4_stopx) i4_x_max=ps_data->as_elements[u4_n].i4_stopx;
    
    if (i4_y_min>ps_data->as_elements[u4_n].i4_starty) i4_y_min=ps_data->as_elements[u4_n].i4_starty;
    if (i4_y_min>ps_data->as_elements[u4_n].i4_stopy) i4_y_min=ps_data->as_elements[u4_n].i4_stopy;
    if (i4_y_max<ps_data->as_elements[u4_n].i4_starty) i4_y_max=ps_data->as_elements[u4_n].i4_starty;
    if (i4_y_max<ps_data->as_elements[u4_n].i4_stopy) i4_y_max=ps_data->as_elements[u4_n].i4_stopy;
  }
  
  // transormate to zero ?
  if (!ps_data->s_header.b_transToZero)
  {
    i4_x_min=0;
    i4_y_min=0;
  }
  
  // numering values for transformate  
  if (ps_data->s_header.b_transToArea)
  {
    ld_x_ratio = (1.0L*ps_data->i4_width)/(i4_x_max - i4_x_min);
    ld_y_ratio = (1.0L*ps_data->i4_height)/(i4_y_max - i4_y_min);
  
    // transformate
  
    for (u4_n=0;u4_n<ps_data->u4_elements;u4_n++)
    {
      ps_data->as_elements[u4_n].i4_startx = floor((ps_data->as_elements[u4_n].i4_startx-i4_x_min)*ld_x_ratio+0.5L)+0.1L;
      ps_data->as_elements[u4_n].i4_stopx = floor((ps_data->as_elements[u4_n].i4_stopx-i4_x_min)*ld_x_ratio+0.5L)+0.1L;
    
      ps_data->as_elements[u4_n].i4_starty = floor((ps_data->as_elements[u4_n].i4_starty-i4_y_min)*ld_y_ratio+0.5L)+0.1L;
      ps_data->as_elements[u4_n].i4_stopy = floor((ps_data->as_elements[u4_n].i4_stopy-i4_y_min)*ld_y_ratio+0.5L)+0.1L;
    }
  }
  else
  {
    for (u4_n=0;u4_n<ps_data->u4_elements;u4_n++)
    {
      ps_data->as_elements[u4_n].i4_startx = ps_data->as_elements[u4_n].i4_startx-i4_x_min;
      ps_data->as_elements[u4_n].i4_stopx = ps_data->as_elements[u4_n].i4_stopx-i4_x_min;
      
      ps_data->as_elements[u4_n].i4_starty = ps_data->as_elements[u4_n].i4_starty-i4_y_min;
      ps_data->as_elements[u4_n].i4_stopy = ps_data->as_elements[u4_n].i4_stopy-i4_y_min;
    }
  }
  
  //offset
  for (u4_n=0;u4_n<ps_data->u4_elements;u4_n++)
  {
    ps_data->as_elements[u4_n].i4_startx = ps_data->as_elements[u4_n].i4_startx+ps_data->s_header.u4_transOffsetX;
    ps_data->as_elements[u4_n].i4_stopx = ps_data->as_elements[u4_n].i4_stopx+ps_data->s_header.u4_transOffsetX;
    
    ps_data->as_elements[u4_n].i4_starty = ps_data->as_elements[u4_n].i4_starty+ps_data->s_header.u4_transOffsetY;
    ps_data->as_elements[u4_n].i4_stopy = ps_data->as_elements[u4_n].i4_stopy+ps_data->s_header.u4_transOffsetY;
  }
  
  return true;
}

bool Data_Save_Header(struct sData *ps_data, FILE *fi_file);
bool Data_Save_Elements(struct sData *ps_data, FILE *fi_file);
bool Data_Save_Commands(struct sData *ps_data, FILE *fi_file);
bool Data_Save(struct sData *ps_data)
{
  FILE *fi_file;
  
  if (ps_data->s_header.b_transformate)
  {
    if (!Data_Transformate(ps_data))
    {
      printf("Module AurelALS300data: Transformating failed.\n");
      return false;
    }
    printf("Module AurelALS300data: Transformating complected.\n");
  }
  
  if (ps_data->t_data)
  {    
    fi_file = fopen(ps_data->t_data,"wb");
    
    if (fi_file)
    {
      bool b_how = true;
      
      b_how&=Data_Save_Header(ps_data, fi_file);
      b_how&=Data_Save_Elements(ps_data, fi_file);
      
      fclose(fi_file);
      
      if (!b_how)
      {
        printf("Module AurelALS300data: Creating laser data file failed.\n");
        return false;
      }
    }
    else
    {
      printf("Module AurelALS300data: Creating laser data file failed.\n");
      return false;
    }
  }
  
  if (ps_data->t_command)
  {    
    fi_file = fopen(ps_data->t_command,"wb");
    
    if (fi_file)
    {
      bool b_how = true;
      
      b_how&=Data_Save_Commands(ps_data, fi_file);
      
      fclose(fi_file);
      
      if (!b_how)
      {
        printf("Module AurelALS300data: Creating laser commands file failed.\n");
        return false;
      }
    }
    else 
    {
      printf("Module AurelALS300data: Creating laser commands file failed.\n");
      return false;
    }
  }
  
  return true;
}
bool Data_Save_Header(struct sData *ps_data, FILE *fi_file)
{
  uint32_t u4_n;
  // write header data to file
  
  /*
   Put #un%, , LaserVersion$ + tm$
   Put #un%, , FileDescription$ + tm$
   Put #un%, , LasReferenceX$ + tm$
   Put #un%, , LasReferenceY$ + tm$
   Put #un%, , LasReferenceX1$ + tm$
   */
  fprintf(fi_file, "\r\n");
  fprintf(fi_file, "\r\n");
  fprintf(fi_file, "%u\r\n", ps_data->s_header.u4_lasReferenceX); // extern set
  fprintf(fi_file, "%u\r\n", ps_data->s_header.u4_lasReferenceY); // extern set
  fprintf(fi_file, "\r\n");
  /*
   Put #un%, , LasReferenceY1$ + tm$
   Put #un%, , TabReferenceZ$ + tm$
   Put #un%, , TabReferenceW$ + tm$
   Put #un%, , TabStepZ$ + tm$
   Put #un%, , TabStepW$ + tm$
   */
  fprintf(fi_file, "\r\n");
  fprintf(fi_file, " 0\r\n");
  fprintf(fi_file, " 0\r\n");
  fprintf(fi_file, " 0\r\n");
  fprintf(fi_file, " 0\r\n");
  /*
   Put #un%, , NumStepZ$ + tm$
   Put #un%, , NumStepW$ + tm$
   Put #un%, , TrimExe$ + tm$
   Put #un%, , usepsi$ + tm$
   Put #un%, , RRef1FocusX + tm$
   */
  fprintf(fi_file, " 0\r\n");
  fprintf(fi_file, " 0\r\n");
  if (ps_data->u4_elements>0) fprintf(fi_file, " 1");
  for (u4_n=2;u4_n<=ps_data->u4_elements;u4_n++)
  {
    fprintf(fi_file, ",%u", u4_n); // generate text for all draw elements
  }
  fprintf(fi_file, "\r\n");
  fprintf(fi_file, "1\r\n");
  fprintf(fi_file, " 0\r\n");
  /*
   Put #un%, , RRef1FocusY + tm$
   Put #un%, , RRef1Ang + tm$
   Put #un%, , RRef2FocusX + tm$
   Put #un%, , RRef2FocusY + tm$
   Put #un%, , RRef2Ang + tm$
   */
  fprintf(fi_file, " 0\r\n");
  fprintf(fi_file, " 0\r\n");
  fprintf(fi_file, " 0\r\n");
  fprintf(fi_file, " 0\r\n");
  fprintf(fi_file, " 0\r\n");
  /*
   Put #un%, , RRef3FocusX + tm$
   Put #un%, , RRef3FocusY + tm$
   Put #un%, , RRef3Ang + tm$
   Put #un%, , RCalFocusPos + tm$
   Put #un%, , RCalFocusANG + tm$
   */
  fprintf(fi_file, " 0\r\n");
  fprintf(fi_file, " 0\r\n");
  fprintf(fi_file, " 0\r\n");
  fprintf(fi_file, " 9731\r\n"); // constant value ?
  fprintf(fi_file, "-3.853\r\n"); // constant value ?
  /*
   TmpSaveString = Str(MasterRefXProbeA)
   Put #un%, , TmpSaveString + tm$
   TmpSaveString = Str(MasterRefYProbeA)
   Put #un%, , TmpSaveString + tm$
   TmpSaveString = Str(MasterRefXProbeB)
   Put #un%, , TmpSaveString + tm$
   TmpSaveString = Str(MasterRefYProbeB)
   Put #un%, , TmpSaveString + tm$
   TmpSaveString = Str(CoordXFiducial1)
   Put #un%, , TmpSaveString + tm$
   */
  if (ps_data->s_header.i4_masterRefXProbeA>=0) fprintf(fi_file, " %u\r\n", ps_data->s_header.i4_masterRefXProbeA); // extern set
  else fprintf(fi_file, "%i\r\n", ps_data->s_header.i4_masterRefXProbeA);
  if (ps_data->s_header.i4_masterRefYProbeA>=0) fprintf(fi_file, " %u\r\n", ps_data->s_header.i4_masterRefXProbeA); // extern set
  else fprintf(fi_file, "%i\r\n", ps_data->s_header.i4_masterRefYProbeA);
  if (ps_data->s_header.i4_masterRefXProbeB>=0) fprintf(fi_file, " %u\r\n", ps_data->s_header.i4_masterRefXProbeA); // extern set
  else fprintf(fi_file, "%i\r\n", ps_data->s_header.i4_masterRefXProbeB);
  if (ps_data->s_header.i4_masterRefYProbeB>=0) fprintf(fi_file, " %u\r\n", ps_data->s_header.i4_masterRefXProbeA); // extern set
  else fprintf(fi_file, "%i\r\n", ps_data->s_header.i4_masterRefYProbeB);
  fprintf(fi_file, " 0\r\n");
  /*
   TmpSaveString = Str(CoordYFiducial1)
   Put #un%, , TmpSaveString + tm$
   TmpSaveString = Str(CoordXFiducial2)
   Put #un%, , TmpSaveString + tm$
   TmpSaveString = Str(CoordYFiducial2)
   Put #un%, , TmpSaveString + tm$
   Put #un%, , strAsseStep + tm$
   Put #un%, , strAsseOffset2 + tm$
   */
  fprintf(fi_file, " 0\r\n");
  fprintf(fi_file, " 4\r\n"); // constant value ?
  fprintf(fi_file, " 5\r\n"); // constant value <
  fprintf(fi_file, "\r\n");
  fprintf(fi_file, "\r\n");
  /*
   Put #un%, , strNCut + tm$
   Put #un%, , VisionRefPt + tm$
   */
  fprintf(fi_file, " 0\r\n");
  fprintf(fi_file, "0\r\n");
  
  return true;
}
bool Data_Save_Elements(struct sData *ps_data, FILE *fi_file)
{
  uint32_t u4_n;
  // write element data to file
  
  for (u4_n=0;u4_n<ps_data->u4_elements;u4_n++)
  {
    /*
     'start x
     startx As String * 13
     'start y
     starty As String * 13
     'stop x
     stopx As String * 13
     'stop y
     stopy As String * 13
     'valore ohmico
     ohmvalue As String * 11
     */
    fprintf(fi_file, "%11i\r\n", ps_data->as_elements[u4_n].i4_startx);
    fprintf(fi_file, "%11i\r\n", ps_data->as_elements[u4_n].i4_starty);
    fprintf(fi_file, "%11i\r\n", ps_data->as_elements[u4_n].i4_stopx);
    fprintf(fi_file, "%11i\r\n", ps_data->as_elements[u4_n].i4_stopy);
    fprintf(fi_file, "      PSI\r\n");
    /*
     'velocita' taglio
     speed As String * 7
     'corrente
     current As String * 7
     'freq. laser
     freq As String * 7
     'scanner h
     scanh As String * 5
     'scanner l
     scanl As String * 5
     */
     fprintf(fi_file, "%5.1f\r\n", ps_data->s_header.do_speed);
     fprintf(fi_file, "%5.1f\r\n", ps_data->s_header.do_current);
     fprintf(fi_file, "%5.1f\r\n", ps_data->s_header.do_freq);
     fprintf(fi_file, "   \r\n");
     fprintf(fi_file, "   \r\n");
     /*
     'scanner g
     scang As String * 5
     'slope
     slope As String * 3
     'tolleranza accettazione pretest
     tolaccept As String * 5
     'tolleranza taglio finale
     tolcut As String * 5
     'tipo di taglio
     cutType As String * 4
     */
     fprintf(fi_file, "   \r\n");
     fprintf(fi_file, "1\r\n");
     fprintf(fi_file, "  0\r\n");
     fprintf(fi_file, "  0\r\n");
     fprintf(fi_file, "%2u\r\n", ps_data->as_elements[u4_n].u1_cutType);
     /*
     'direzione inizio taglio
     cutDir As String * 3
     'percentuale primo stop
     cut1St As String * 5
     'percentuale prestop
     cutPre As String * 5
     'valore in volt scatto funzionale
     voltvalue As String * 11
     'mark condition
     mark As String * 3
     */
     fprintf(fi_file, "1\r\n");
     fprintf(fi_file, " 40\r\n");
     fprintf(fi_file, "  0\r\n");
     fprintf(fi_file, "         \r\n");
     fprintf(fi_file, "1\r\n");
     /*
     'taglio funzionale
     func As String * 3
     'step taglio serp
     serpstep As String * 6
     'identificatore di res.
     reslabel As String * 12
     'se 1 usa visione
     vision As String * 3
     'i seguenti parametri vengono utilizzati per il taglio multiplo
     'start2 X
     start2X As String * 13
     */
    fprintf(fi_file, "0\r\n");
    fprintf(fi_file, "    \r\n");
    fprintf(fi_file, "cut %4.4u  \r\n",(u4_n+1)%10000); // label generate
    fprintf(fi_file, "0\r\n");
    fprintf(fi_file, "           \r\n");
     /*
     'start2Y
     start2Y As String * 13
     'stop2X
     stop2X As String * 13
     'stop2Y
     stop2Y As String * 13
     'N. Cut Multipli
     strNCutMultiple As String * 13
     'sviluppi futuri
     sparedata As String * 1816
     'Posizione X Probe A
     */
    fprintf(fi_file, "           \r\n");
    fprintf(fi_file, "           \r\n");
    fprintf(fi_file, "           \r\n");
    fprintf(fi_file, "           \r\n");
    fprintf(fi_file, "                                                                               "
    "                                                                                                "
    "                                                                                                "
    "                                                                                                "
    "                                                                                                "
    "                                                                                                "
    "                                                                                                "
    "                                                                                                "
    "                                                                                                "
    "                                                                                                "
    "                                                                                                "
    "                                                                                                "
    "                                                                                                "
    "                                                                                                "
    "                                                                                                "
    "                                                                                                "
    "                                                                                                "
    "                                                                                                "
    "                                                                                                "
    "       \r\n");
     /*
     recProbeAX As String * 13
     'Posizione Y Probe A
     recProbeAY As String * 13
     'Posizione X Probe B
     recProbeBX As String * 13
     'Posizione Y Probe B
     recProbeBY As String * 13
     'Stato Filtro 1
     recFilter1 As String * 3
     */
    fprintf(fi_file, "           \r\n"); // or 0 ? or constant ?
    fprintf(fi_file, "           \r\n");
    fprintf(fi_file, "           \r\n");
    fprintf(fi_file, "           \r\n");
    fprintf(fi_file, "0\r\n");
     /*
     'Stato Filtro 2
     recFilter2 As String * 3
     'Stato Filtro 3
     recFilter3 As String * 3
     'Stato Filtro 4
     recFilter4 As String * 3
     'Stato Filtro PSI
     recFilterPSI As String * 3
     'Abilitazione AutoFuoco
     AutoFEnable As String * 3
     */
    fprintf(fi_file, "0\r\n");
    fprintf(fi_file, "0\r\n");
    fprintf(fi_file, "0\r\n");
    fprintf(fi_file, "0\r\n");
    fprintf(fi_file, "0\r\n");
  }
  return true;
}
bool Data_Save_Commands(struct sData *ps_data, FILE *fi_file)
{
  uint32_t u4_n, u4_cut;
  // first code section
  /*
  EQU C1 0
  EQU C2 10
  */
  fprintf(fi_file, "'Commands file generated by module AurelALS300data.\r\n");
  fprintf(fi_file, "'Module created by Martin Strympl, mail: xstrym00@stud.feec.vutbr.cz\r\n");
  fprintf(fi_file, "'Than you from using this module.\r\n");
  fprintf(fi_file, "\r\n");
  fprintf(fi_file, "CLS\r\n");
  fprintf(fi_file, "\r\n");
  fprintf(fi_file, "EQU C1 0\r\n");
  fprintf(fi_file, "EQU C2 %u\r\n", ps_data->s_header.u4_cuts); // extern sets draw cycles
  fprintf(fi_file, "\r\n");
  // element code section
  
  u4_cut=1;
  for (u4_n=1;u4_n<=ps_data->u4_elements;u4_n++,u4_cut++)
  {
    /*
      PRINT "Rez 2"
      EQU C1 0
     #20
      D 2
      SUM C2 C2 1
      JLESS C2 2 20
     */
    if (u4_n<ps_data->u4_elements)
    {
      fprintf(fi_file, "\r\n");
      fprintf(fi_file, "PRINT \"Cut %u\"\r\n", u4_cut);
      fprintf(fi_file, "EQU C1 0\r\n");
      fprintf(fi_file, "#%u\r\n", u4_cut+100);
      fprintf(fi_file, "D %u", u4_n);
      
      while ((ps_data->as_elements[u4_n-1].u1_cutType==7)&&(ps_data->as_elements[u4_n].u1_cutType==7)
             &&(ps_data->as_elements[u4_n-1].i4_stopx==ps_data->as_elements[u4_n].i4_startx)
             &&(ps_data->as_elements[u4_n-1].i4_stopy==ps_data->as_elements[u4_n].i4_starty))
      {
        u4_n++;
        fprintf(fi_file, " %u", u4_n);
        if (u4_n==ps_data->u4_elements) break;
      }
      
      fprintf(fi_file, "\r\nSUM C1 C1 1\r\n");
      fprintf(fi_file, "JLESS C1 C2 %u\r\n", u4_cut+100);
      fprintf(fi_file, "\r\n");
    }
    else
    {
      fprintf(fi_file, "\r\n");
      fprintf(fi_file, "PRINT \"Cut %u\"\r\n", u4_cut);
      fprintf(fi_file, "EQU C1 0\r\n");
      fprintf(fi_file, "#%u\r\n", u4_cut+100);
      fprintf(fi_file, "D %u\r\n", u4_n);
      fprintf(fi_file, "SUM C1 C1 1\r\n");
      fprintf(fi_file, "JLESS C1 C2 %u\r\n", u4_cut+100);
      fprintf(fi_file, "\r\n");
    }
    
  }
  
  // end code section
  
  /*
   WAIT 1000
   
   GOTO 900
   
   #900
   END
   */
  fprintf(fi_file, "WAIT 1000\r\n"); // sets ?
  fprintf(fi_file, "\r\n");
  fprintf(fi_file, "PRINT \"Complete\"\r\n");
  fprintf(fi_file, "\r\n");
  fprintf(fi_file, "GOTO 50\r\n");
  fprintf(fi_file, "\r\n");
  fprintf(fi_file, "#50\r\n");
  fprintf(fi_file, "END\r\n");
  
  return true;
}
