/************************************************************************/
/* Copyright 1992 MBARI							*/
/************************************************************************/
/* $Header: ibcData.c,v 2.2 96/05/15 11:15:36 bobh Exp $		*/
/* Summary  : Data File For IBC Module of ROV User Interface		*/
/* Filename : ibcData.c						*/
/* Author   : Bob Herlien (rah)						*/
/* Project  : New ROV							*/
/* $Revision: 2.2 $							*/
/* Created  : 11/13/92							*/
/************************************************************************/
/* Modification History:						*/
/* $Log:	ibcData.c,v $
 * Revision 2.2  96/05/15  11:15:36  11:15:36  bobh (Bob Herlien)
 * Still supporting proto during final vehicle build
 * 
 * Revision 2.0  95/09/20  14:23:04  14:23:04  hebo (Bob Herlien)
 * Support for final vehicle integration.  AC, PDU micros.
 * 
 * Revision 1.6  93/12/17  15:05:02  15:05:02  hebo (Bob Herlien)
 * December Wet Test
 * 
 * Revision 1.5  93/07/08  18:10:58  18:10:58  hebo (Bob Herlien)
 * Prior to Prototype Phase I testing
 * 
 * Revision 1.4  93/05/27  15:48:04  15:48:04  hebo (Bob Herlien)
 * Data Manager Interface
 * 
 * Revision 1.2  93/01/21  10:34:20  10:34:20  hebo (Bob Herlien)
 * Changes after Level 0
 * 
 * Revision 1.0  92/11/20  12:03:28  12:03:28  hebo (Bob Herlien)
 * Initial revision
 * 
*/
/************************************************************************/

#include <mbari/types.h>
#include <mbari/const.h>
#include <Xm/Xm.h>
#include <X11/Intrinsic.h>
#include "rdmif.h"
#include "rov.h"
#include "alarm.h"
#include "ibc.h"
#include <thrusterDM.h>
#include <powerDM.h>
#include <microDm.h>
#include <vmeIbc.h>
#include <ibcDm.h>
#include <powerIbc.h>
#include <cameraDcon.h>
#include <sensorDcon.h>
#include <sonarDcon.h>
#include <lightingDcon.h>
#include <ersDcon.h>
#include <microcmd.h>
#include <gf_5vDm.h>
#include <sio32Micro.h>
#include <sio32Diag.h>
#include <sio32Server.h>
#include <acMicro.h>
#include <pduMicro.h>
#include <telemDiag.h>
#include <panTilt.h>
#include <camera.h>


/********************************/
/*	External Functions	*/
/********************************/

Extern Void	resetButtonCreate( Widget parent, ibcItem *itm );
Extern Void	pushButtonCreate( Widget parent, ibcItem *itm );
Extern Void	toggleButtonCreate( Widget parent, ibcItem *itm );
Extern Void	labelCreate( Widget parent, ibcItem *itm,
			     Int32 lpos, Int32 rpos );
Extern Void	labelGroupCreate( Widget parent, ibcItem *itm,
				  Int32 lpos, Int32 rpos );
Extern Void	labelVarCreate( Widget parent, ibcItem *itm, 
			        Int32 lpos, Int32 rpos );
Extern Void	xmLabelCreate( Widget parent, ibcItem *itm,
			       Int32 lpos, Int32 rpos );
Extern Void	staticLabelCreate( Widget parent, ibcItem *itm,
				   Int32 lpos, Int32 rpos );
Extern Void	textFieldCreate( Widget parent, ibcItem *itm,
				 Int32 lpos, Int32 rpos );
Extern Void	multHdrCreate( Widget parent, ibcItem *itm );
Extern Void	multFieldCreate( Widget parent, ibcItem *itm,
				 Int32 lpos, Int32 rpos );
Extern Void	optionMenuCreate( Widget parent, ibcItem *itm,
				  Int32 lpos, Int32 rpos );
Extern Void	updateAsciiLabel( ibcItem *itm );
Extern Void	updateIntLabel( ibcItem *itm );
Extern Void	updateGfLabel( ibcItem *itm );
Extern Void	updateEnumLabel( ibcItem *itm );
Extern Void	updateFixPtLabel( ibcItem *itm );
Extern Void	updateFltLabel( ibcItem *itm );
Extern Void	updateAsciiTextField( ibcItem *itm );
Extern Void	updateNumTextField( ibcItem *itm );
Extern Void	updateMultIntTextField( ibcItem *itm );
Extern Void	updateFixPtTextField( ibcItem *itm );
Extern Void	updateMotorToggleButton( ibcItem *itm );
Extern Void	updateMBoolToggleButton( ibcItem *itm );
Extern Void	updateOptionMenu( ibcItem *itm );
Extern Void	updateIbcBoards( ibcItem *itm );
Extern Void	modifyTextField( Widget w, ibcItem *itm, 
			XmTextVerifyCallbackStruct *tvp );
Extern Void	modifyIntTextField( Widget w, ibcItem *itm, 
				    XmTextVerifyCallbackStruct *tvp );
Extern Void	acceptTextField( Widget w, ibcItem *itm, 
				XmAnyCallbackStruct *cbp );
Extern Void	acceptIntTextField( Widget w, ibcItem *itm, 
				    XmAnyCallbackStruct *cbp );
Extern Void	acceptFlt32TextField( Widget w, ibcItem *itm, 
				      XmAnyCallbackStruct *cbp );
Extern Void	acceptMultInt16TextField( Widget w, ibcItem *itm, 
					  XmAnyCallbackStruct *cbp );
Extern Void	acceptFixPtTextField( Widget w, ibcItem *itm, 
				      XmAnyCallbackStruct *cbp );
Extern Void	ibcWriteEvent( Widget w, ibcItem *itm, 
			       XmAnyCallbackStruct *cbp );
Extern Void	activateButton( Widget w, ibcItem *itm, 
			       XmAnyCallbackStruct *cbp );
Extern Void	activateMotorToggleButton( Widget w, ibcItem *itm, 
					   XmAnyCallbackStruct *cbp );
Extern Void	activateMBoolToggleButton( Widget w, ibcItem *itm, 
					   XmAnyCallbackStruct *cbp );
Extern Void	activateOptionMenu( Widget w, ibcItem *itm, 
				    XmAnyCallbackStruct *cbp );
Extern Void	ibcSelect( Select *sp );
Extern Void	ibcUnselect( Select *sp );


/********************************/
/*	External Data		*/
/********************************/

Extern AppData		appdata;	/* Application-wide resources	*/
Extern char		*ibcAlarmStrings[];
Extern char		*gfAlarmStrings[];
Extern char		*gfTestStrings[];
Extern char		*gf5vTestStrings[];
Extern char		*motorStatus[];
Extern Widget		thrustButtonForm, ibcButtonForm, micro3x5ButtonForm;


/********************************/
/*	Global Data		*/
/********************************/

Global SelectStatus	ibcSts;


/********************************/
/*	Module Local Data	*/
/********************************/

MLocal char		*enableDisable[] = { "Disabled", "Enabled" };
MLocal char		*passFail[] = { "Pass", "Fail" };
MLocal char		*onOff[] = { "Off", "On" };
MLocal char		*resetStrings[] = 
{ "Reset\nMicro", "This will reset the\nmicrocontroller.\nAre you sure?" };

MLocal char		*nvRamStrings[] = 
{ "Write to\nNVRAM", "Really write to\nmicro's NVRAM?"};

MLocal char		*rpmCalString = "Calibrate\nVelocity";

MLocal char		*gfSelftestString = "Start\nSelfTest";

MLocal char		*panTiltInitStrings[] = 
{ "Initialize\nController", "Really reinitialize Pan/Tilt Controller?" };

MLocal char		*panTiltCalibrateString = "Calibrate";

MLocal char		*panTiltResetStrings[] = 
{ "Reset\nPan/Tilt\nCtrlr",
 "This will reset the\nPan/Tilt controller.\nAre you sure?" };

MLocal char		separatorString[] = 
"============================================================";

MLocal ibcMenuItem	controlModes[] = 
{ {"Current", 0, NULLW}, {"Torque", 1, NULLW},
  {"Velocity", 2, NULLW}, {"Linearized", 3, NULLW} };

MLocal ibcMenuItem	vmeGfEnable[] = 
{ {"Disabled", 0, NULLW}, {"Enabled", -1, NULLW} };

MLocal ibcMenuItem	pwrAGfEnable[] = 
{ {"Disabled", 0, NULLW}, {"Enabled", -1, NULLW} };

MLocal ibcMenuItem	pwrBGfEnable[] = 
{ {"Disabled", 0, NULLW}, {"Enabled", -1, NULLW} };

MLocal ibcMenuItem	miscGfEnable[] = 
{ {"Disabled", 0, NULLW}, {"Enabled", -1, NULLW} };

MLocal Widget	gfCurrentLabels[] = {NULLW, NULLW, NULLW, NULLW};


/****************************************/
/*	Global IBC Item Structs		*/
/****************************************/

MLocal ibcItem	allMicroItems[] =
{ {"Micro ID String", MICRO_IDENT_DM, DM_CHAR, MICRO_ID_LEN, labelVarCreate,
    updateAsciiLabel, NULLF, NULLF, UP_CONSUME, 0, 0, NULL, 0,
    NULLW, NULLW, NULLW, 0, -1},
  {"Software Revision String", SOFTWARE_REV_DM, DM_CHAR, SOFTWARE_REV_LEN,
    labelVarCreate, updateAsciiLabel, NULLF, NULLF, UP_CONSUME, 0, 0,
    NULL, 0, NULLW, NULLW, NULLW, 0, -1},
  {"RAM Test", RAM_TEST_STATUS_DM, DM_MBOOL, 1, labelCreate,
    updateEnumLabel, NULLF, NULLF, UP_CONSUME, XtNumber(passFail),
    0, passFail, 0, NULLW, NULLW, NULLW, 0, -1},
  {"Micro Serial Number", MICRO_SERIAL_NO_DM, DM_CHAR, MICRO_SERNO_LEN,
    textFieldCreate, updateAsciiTextField, acceptTextField,
    modifyTextField, UP_CONSUME, MICRO_SERNO_LEN, 0, NULL, 0, NULLW,
    NULLW, NULLW, 0, -1}
};

MLocal ibcItem	thrustItems[] =
{ {NULL, MICRO_RESET_CMD_DM, DM_EMPTY, 1, resetButtonCreate, NULLF,
    activateButton, NULLF, UP_NONE, 10, 25, resetStrings, 0, NULLW,
    NULLW, NULLW, 0, -1},
  {NULL, MICRO_WRITE_NVRAM_DM, DM_EMPTY, 1, pushButtonCreate, NULLF,
    activateButton, NULLF, UP_NONE, 30, 45, nvRamStrings, 0, 
    NULLW, NULLW, NULLW, 0, -1},
  {NULL, MOTOR_ENABLE_STATUS_DM, DM_ENUM, 1, toggleButtonCreate,
    updateMotorToggleButton, activateMotorToggleButton, NULLF, UP_CONSUME,
    50, 70, "Enable", 0, NULLW, NULLW, NULLW, 0, -1},
  {NULL, MOTOR_RPM_CAL_DM, DM_EMPTY, 1, pushButtonCreate, NULLF,
    ibcWriteEvent, NULLF, UP_NONE, 75, 90, &rpmCalString, 0, NULLW,
    NULLW, NULLW, 0, -1},
  {"Motor Velocity (RPM)", MOTOR_VELOCITY_DM, DM_INT16, 1, labelCreate,
    updateIntLabel, NULLF, NULLF, UP_CONSUME, 0, 0, NULL, 0,
    NULLW, NULLW, NULLW, 0, -1},
  {"Commanded Thrust", MOTOR_CMD_THRUST_DM, DM_INT16, 1, textFieldCreate,
    updateNumTextField, acceptIntTextField, modifyIntTextField, UP_CONSUME,
    6, 0, NULL, 0, NULLW, NULLW, NULLW, 0, -1},
  {"Commanded Power", MOTOR_CMD_POWER_DM, DM_INT16, 1, textFieldCreate,
    updateNumTextField, acceptIntTextField, modifyIntTextField, UP_CONSUME,
    6, 0, NULL, 0, NULLW, NULLW, NULLW, 0, -1},
  {"Motor Temperature", TEMPERATURE_DM, DM_INT16, 1, labelCreate,
    updateIntLabel, NULLF, NULLF, UP_CONSUME, 0, 0, NULL, 0,
    NULLW, NULLW, NULLW, 0, -1},
  {"NV RAM Date/Time", MICRO_NVRAM_DATE_DM, DM_CHAR, 22, labelCreate, 
    updateAsciiLabel, NULLF, NULLF, UP_CONSUME, 0, 0, NULL, 0, 
    NULLW, NULLW, NULLW, 0, -1},
  {"NV RAM Write Cycles", MICRO_NVRAM_CYCLES_DM, DM_NAT16, 1, labelCreate,
    updateIntLabel, NULLF, NULLF, UP_CONSUME, 0, 0, NULL, 0,
    NULLW, NULLW, NULLW, 0, -1},
  {"Regen Limit", MOTOR_REGEN_LIMIT_MODE_DM, DM_MBOOL, 1, labelCreate,
    updateEnumLabel, NULLF, NULLF, UP_CONSUME,  XtNumber(enableDisable), 
    0, enableDisable, 0, NULLW, NULLW, NULLW, 0, -1},
  {"Motor Error Status", MOTOR_STATUS_DM, DM_ENUM, 1, labelVarCreate,
    updateEnumLabel, NULLF, NULLF, UP_CONSUME, NUM_MOTOR_STATUS,
    0, motorStatus, 0, NULLW, NULLW, NULLW, 0, -1},
  {"Water Alarm", WATER_ALARM_DM, DM_MBOOL, 1, labelCreate, updateEnumLabel,
    NULLF, NULLF, UP_CONSUME, 2, 0, onOff, 0, NULLW, NULLW, 
    NULLW, 0, -1},
  {"Temperature Alarm", TEMPERATURE_ALARM_DM, DM_ENUM, 1, labelCreate, 
    updateEnumLabel, NULLF, NULLF, UP_CONSUME, NUM_IBC_ALARMS, 0,
    ibcAlarmStrings, 0, NULLW, NULLW, NULLW, 0, -1},
  {"Alarm Temperature", TEMP_ALARM_THRESH_DM, DM_INT16, 1, textFieldCreate,
    updateNumTextField, acceptIntTextField, modifyIntTextField, 
    UP_CONSUME, 6, 0, NULL, 0, NULLW, NULLW, NULLW, 0, -1},
  {"Motor Serial Number", MOTOR_SERIAL_NO_DM, DM_CHAR, 10, textFieldCreate,
    updateAsciiTextField, acceptTextField, modifyTextField, UP_CONSUME,
    10, 0, NULL, 0, NULLW, NULLW, NULLW, 0, -1},
  {"Motor Control Mode", MOTOR_CTRL_MODE_DM, DM_ENUM, 1, optionMenuCreate,
    updateOptionMenu, activateOptionMenu, NULLF, UP_CONSUME,
    XtNumber(controlModes), 0, controlModes, 0, NULLW, NULLW, NULLW, 0, -1},
  {"Current Mode Gain", MOTOR_CURRENT_GAIN_DM, DM_INT16, 1, textFieldCreate, 
    updateNumTextField, acceptIntTextField, modifyIntTextField,
    UP_CONSUME, 6, 0, NULL, 0, NULLW, NULLW, NULLW, 0, -1},
  {"Torque Mode Gain", MOTOR_TORQUE_GAIN_DM, DM_INT16, 1, textFieldCreate,
    updateNumTextField, acceptIntTextField, modifyIntTextField,
    UP_CONSUME, 6, 0, NULL, 0, NULLW, NULLW, NULLW, 0, -1},
  {"Torque Offset", MOTOR_TORQUE_OFFSET_DM, DM_INT16, 1, textFieldCreate,
    updateNumTextField, acceptIntTextField, modifyIntTextField,
    UP_CONSUME, 6, 0, NULL, 0, NULLW, NULLW, NULLW, 0, -1},
  {"Velocity Mode Gains", MOTOR_VELOCITY_GAINS_DM, DM_INT16, 3, multHdrCreate,
    updateMultIntTextField, NULLF, NULLF, UP_CONSUME, 0, 0, NULL, 0,
    NULLW, NULLW, NULLW, 0, -1},
  {"Proportional", NULL, DM_INT16, 0, multFieldCreate, NULLF,
    acceptMultInt16TextField, modifyIntTextField, UP_NONE, 6, 0, NULL, 0,
    NULLW, NULLW, NULLW, 0, -1},
  {"Derivative", NULL, DM_INT16, 0, multFieldCreate, NULLF,
    acceptMultInt16TextField, modifyIntTextField, UP_NONE, 6, 1, NULL, 0,
    NULLW, NULLW, NULLW, 0, -1},
  {"Power", NULL, DM_INT16, 0, multFieldCreate, NULLF,
    acceptMultInt16TextField, modifyIntTextField, UP_NONE, 6, 2, NULL, 0,
    NULLW, NULLW, NULLW, 0, -1},
  {"Linear Mode Gains", MOTOR_LINEARIZED_GAINS_DM, DM_INT16, 2, multHdrCreate,
    updateMultIntTextField, NULLF, NULLF, UP_CONSUME, 0, 0, NULL, 0,
    NULLW, NULLW, NULLW, 0, -1},
  {"Thrust", NULL, DM_INT16, 2, multFieldCreate, NULLF,
    acceptMultInt16TextField, modifyIntTextField, UP_NONE, 6, 0, NULL, 0,
    NULLW, NULLW, NULLW, 0, -1},
  {"Velocity", NULL, DM_INT16, 2, multFieldCreate, NULLF,
    acceptMultInt16TextField, modifyIntTextField, UP_NONE, 6, 1, NULL, 0,
    NULLW, NULLW, NULLW, 0, -1},
  {"Motor Model Parm K4", MOTOR_MODEL_PARAM_K4_DM, DM_INT16, 1, 
    textFieldCreate, updateNumTextField, acceptIntTextField,
    modifyIntTextField, UP_CONSUME, 6, 0, NULL, 0, NULLW, NULLW, 
    NULLW, 0, -1},
  {"Regen Torque Limit", MOTOR_REGEN_LIMIT_DM, DM_NAT16, 1, textFieldCreate, 
    updateNumTextField, acceptIntTextField, modifyIntTextField,
    UP_CONSUME, 6, 0, NULL, 0, NULLW, NULLW, NULLW, 0, -1}
};

MLocal ibcItemSet  allMicroItemSet = 
{ allMicroItems, XtNumber(allMicroItems), NULLW };

MLocal ibcItemSet  thrustItemSet = 
{ thrustItems, XtNumber(thrustItems), NULLW };

Global ibcItemSet *ibcSets[] =
{ &allMicroItemSet, &thrustItemSet, NULLIS };

MLocal ibcItemDesc  thrustItemDesc = 
{ {&allMicroItemSet, &thrustItemSet, NULLIS, NULLIS},
  &appdata.ibc_update, 0, 0, 0 };

Global ibcItemDesc *ibcDescs[] =
{ &thrustItemDesc, NULLID };


/****************************************/
/*	Global IBC Select Structs	*/
/****************************************/

/* Thruster Selects		*/

Global Select testMotorSel = { ibcSelect, ibcUnselect, NULLWP, &ibcSts,
			       NULLSS, "WESTERN_FLYER.MOTOR_TEST.",
			       &thrustItemDesc };

Global Select *ibcSelects[] = { &testMotorSel, (Select *)NULL };
