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

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

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

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

   float scale = -1., retract = -1;
   if (pbs->interactive)
   {
      scale   = f_prompt("Enter bridge duty cycle (over 0.1 and up to 1.0): ");
      retract = f_prompt("Enter load duty cycle (0.0 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.1 || scale > 1.0)
   {
      // report command-specific usage here
      //
      printf("setdutycycles: Bridge duty cycle (%.3f) must be specified > 0.1 and <= 1.0\n",
              scale);
      return BAD_OPTS;
   }

   if (retract <= 0.0 || retract > 1.0)
   {
      // report command-specific usage here
      //
      printf("setdutycycles: Load duty cycle (%.3f) must be specified > 0.0 and <= 1.0\n",
              retract);
      return BAD_OPTS;
   }
   if (pbs->verbose) printf("setdutycycles: Using bridge-dc=%.3f, load-dc=%.3f\n",
                             scale, retract);

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

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

   return OK;
}
