/****************************************************************************/
/* Copyright (c) 2015 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  : Command interface to the CANBus power buoy controllers.       */
/* Filename : setloadfactors.c                                              */
/* Author   : Henthorn                                                      */
/* Project  : Power Buoy                                                    */
/* Version  : 1.0                                                           */
/* Created  : 05/06/2015                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
/*                                                                          */
/****************************************************************************/

#include <unistd.h>
#include "pbcom.h"

// Set load factors associated scale and retract factors. Returns
// 0 when successful. Get command-specific arguments here.
// 
int setloadfactors(pb_stuff *pbs, int argc, char** argv)
{
   if (pbs->verbose) printf("setloadfactors...\n");

   if (pbs->help)
   {
      printf("setloadfactors: Help here...\n");
      // report command-specific usage here
      //
      return OK;
   }

   float scale = -1., retract = -1;
   if (pbs->interactive)
   {
      scale   = f_prompt("Enter scale factor (over 0.5 and up to 1.2): ");
      retract = f_prompt("Enter retract factor (over 0.5 and up to 1.0): ");
   }
   else
   {
      float scale = atof(argv[argc-2]);
      float retract = atof(argv[argc-1]);
   }
   // Perform Range checking on the scale value which
   // is the next to last argument
   //
   if (scale <= 0.5 || scale > 1.2)
   {
      // report command-specific usage here
      //
      printf("setloadfactors: Scale (%.3f) factor must be specified > 0.5 and <= 1.2\n",
              scale);
      return BAD_OPTS;
   }

   if (retract <= 0.5 || retract > 1.0)
   {
      // report command-specific usage here
      //
      printf("setloadfactors: Retract factor (%.3f) must be specified > 0.5 and <= 1.0\n",
              retract);
      return BAD_OPTS;
   }
   if (pbs->verbose) printf("setloadfactors: Using scale=%.3f, retract=%.3f\n", scale, retract);

   // Format the command in the CANBus frame
   //
   struct can_frame frame;                //        id  cmd    unused
   frame.can_id = 0x9400;                 // binary 10 01010 000000000
   frame.can_dlc = 4;                     // 2 bytes
   frame.data[0] = (unsigned)(scale * (255./1.2));
   frame.data[2] = (unsigned)(retract * 255.);

   // Send the command (fire-and-forget)
   //
   if (pbs->verbose) printf("setloadfactors: Sending command payload %d and %d...\n",
                             frame.data[0], frame.data[1]);
   if (OK) // != send_frame(pbs->can_fd, &frame))
   {
      perror("setloadfactors: Comms failure");
      return BAD_SOCK;
   }

   return OK;
}
