#!/usr/bin/tclsh
#---------------------------------------------------------------
# Create the skeleton of a drivable object with the correct 
# interface functions and some more. 
#
# Mark Koennecke, September 2005
#---------------------------------------------------------------

if { [llength $argv] < 2} {
	puts stdout "usage:\n\tmakedriveskel prefix datastuctname"
	exit 1
}

set prefix [lindex $argv 0]
set structname [lindex $argv 1]

#------------- GetInterface function
puts stdout "/*---------------------------------------------------------------*/"
puts stdout "static void *${prefix}GetInterface(void *data, int iD){"
puts stdout "    $structname self = NULL; "
puts stdout " "
puts stdout "    self = ($structname)data;"
puts stdout "    if(self != NULL && iD == DRIVEID){"
puts stdout "         return self->pDriv;"
puts stdout "     } else  {"
puts stdout "         return NULL;"
puts stdout "     }"
puts stdout "     return NULL;"
puts stdout "}"

#------------------- Halt 
puts stdout "/*----------------------------------------------------------------"
puts stdout " This routine can return either OKOK or HWFault when thing"
puts stdout " go wrong. However, the return value of Halt is usually ignored!"
puts stdout "------------------------------------------------------------------*/"
puts stdout "static int ${prefix}Halt(void *data) {"
puts stdout "    $structname self = NULL; "
puts stdout " "
puts stdout "    self = ($structname)data;"
puts stdout ""
puts stdout "    return OKOK; "
puts stdout "}"

#------------------ CheckLimits
puts stdout "/*----------------------------------------------------------------"
puts stdout " This routine can return either 1 or 0. 1 means the position can "
puts stdout " be reached, 0 NOT"
puts stdout " If 0, error shall contain up to errlen characters of information"
puts stdout " about which limit was violated"
puts stdout "------------------------------------------------------------------*/"
puts stdout "static int ${prefix}CheckLimits(void *data, float val,"
puts stdout "                         char *error, int errlen){"
puts stdout "    $structname self = NULL; "
puts stdout " "
puts stdout "    self = ($structname)data;"
puts stdout ""
puts stdout "    return 1; "
puts stdout "}"

#----------------- SetValue
puts stdout "/*----------------------------------------------------------------"
puts stdout " This routine can return 0 when a limit problem occurred "
puts stdout " OKOK when the motor was successfully started "
puts stdout " HWFault when a problem occured starting the device"
puts stdout " Possible errors shall be printed to pCon"
puts stdout " For real motors, this is supposed to try at least three times"
puts stdout " to start the motor in question"
puts stdout " val is the value to drive the motor too"
puts stdout "------------------------------------------------------------------*/"
puts stdout "static long ${prefix}SetValue(void *data, SConnection *pCon, float val){"
puts stdout "    $structname self = NULL; "
puts stdout " "
puts stdout "    self = ($structname)data;"
puts stdout ""
puts stdout "    return 1; "
puts stdout "}"

#-------------- CheckStatus
puts stdout "/*----------------------------------------------------------------"
puts stdout " Checks the status of a running motor. Possible return values"
puts stdout "   HWBusy The motor is still running"
puts stdout "   OKOK or HWIdle when the motor finished driving"
puts stdout "   HWFault when a hardware problem ocurred"
puts stdout "   HWPosFault when the hardware cannot reach a position"
puts stdout " Errors are duly to be printed to pCon"
puts stdout " For real motors CheckStatus again shall try hard to fix any "
puts stdout " issues with the motor "
puts stdout "------------------------------------------------------------------*/"
puts stdout "static int ${prefix}CheckStatus(void *data, SConnection *pCon){"
puts stdout "    $structname self = NULL; "
puts stdout " "
puts stdout "    self = ($structname)data;"
puts stdout ""
puts stdout "    return 1; "
puts stdout "}"

#----------------- GetValue
puts stdout "/*----------------------------------------------------------------"
puts stdout " GetValue is supposed to read a motor position"
puts stdout " On errors, -99999999.99 is returned and messages printed to pCon"
puts stdout "------------------------------------------------------------------*/"
puts stdout "static float ${prefix}GetValue(void *data, SConnection *pCon){"
puts stdout "    $structname self = NULL; "
puts stdout "    float val = -99999999.99;"
puts stdout " "
puts stdout "    self = ($structname)data;"
puts stdout ""
puts stdout "    return val; "
puts stdout "}"

#---------------MakeObject
puts stdout "/*----------------------------------------------------------------"
puts stdout " returns NULL on failure, a new datastructure else"
puts stdout "------------------------------------------------------------------*/"
puts stdout "static $structname ${prefix}MakeObject(){"
puts stdout "    $structname self = NULL; "
puts stdout ""
puts stdout "    self->pDes->GetInterface = ${prefix}GetInterface;"
puts stdout "    self->pDriv->Halt = ${prefix}Halt;"
puts stdout "    self->pDriv->CheckLimits = ${prefix}CheckLimits;"
puts stdout "    self->pDriv->SetValue = ${prefix}SetValue;"
puts stdout "    self->pDriv->CheckStatus = ${prefix}CheckStatus;"
puts stdout "    self->pDriv->GetValue = ${prefix}GetValue;"
puts stdout ""
puts stdout "    return self;"
puts stdout "}"
puts stdout ""

