 /**********************************  espdwarf.h  *******************************
 * $Source: /home/cvs/ESP/gen2/software/msp430/dwarves/espdwarf.h,v $
 *  Copyright (C) 2003 MBARI
 *
 *  MBARI Proprietary Information. All rights reserved.
 * $Id: espdwarf.h,v 1.19 2005/11/24 00:20:55 brent Exp $
 *
 * shared definitions for espdwarf components
 *
 *****************************************************************************/

#ifndef espdwarf_h
#define espdwarf_h

#include "types.h"
#include "timers.h"       //interval timers
#include "trapservo.h"
#include "parser.h"
#include "I2Cnode.h"      //parse messages from host

//declare and initialize a fixed size reply message buffer
// must be invoked as the last declaration in a block
// returns a byte pointer to the first byte in the reply body 
//   (ie. its command code)
#define beginReply(msgType, tag, cursor) beginVarReply(msgType, tag, cursor, 1)

//declare and initialize a variable sized reply message buffer
// must be invoked as the last declaration in a block
// returns a byte pointer to the first byte in the reply body 
//   (ie. its command code)
#define beginVarReply(msgType, tag, cursor, extraBytes) \
  byte msgType[1 + sizeof (struct msgType##Reply) + extraBytes]; \
  byte *cursor=bufAddByteW (bufAddByte(msgType, current.ownAdr|0x80), tag|0x80);


#define NSERVO   2   //# of servo channels
#define NROTARY  4   //# of rotary valves

// Software timers (each decriments TIMERHZ counts/second)
enum timerIndex {   //allocate and name interval timers
  servoTimer,       //PID servo control loop
  fastTimer,        //misc polling and control
  portTimer,        //serial port forwarding
  gripperTimer,     //gripper control
  dcoTimer,         //adjust DCO frequency to compensate for temperature change
  NTIMERS   //number of interval timers
};

extern struct timer itimer[NTIMERS];

enum debugMask {  //debugging code enable mask
  debugCmds     = 0x01,    //parse ASCII test commands input on debug port
  debugI2C      = 0x02,    //verbose debugging output for I2C operations
  debugEnc      = 0x04,    //" for motor/encoder operations
  debugValve    = 0x08,    //" for rotary valve operations
  debugTemp     = 0x10,    //" for temperature sensor operations
  debugHeatPWM  = 0x20,    //" for heater pwm operations
  debugPressure = 0x40,
  debugMSB      = 0x80     //debugMask of zero suppresses ALL debugging I/O
};                         //so setting the MSB bit outputs at least the signon

extern uint8 debugFlag;

// Operation complete mask for communication between background and servos

extern uint16 opComplete;

extern I2CnodeInstance node;
extern struct I2Cnode current;

extern parserInstance debugPort;

//bit field offsets within opComplete (from LSB)
#define rotationOp   0                 //four rotation operation complete flags
#define servoOp      NROTARY           //two servo operation complete flags
#define thermalOp    (servoOp+NSERVO)  //one thermal operation complete flag
#define gripOp       (thermalOp+1)     //two gripper operation complete flag
//the MSB must be zero

#define rvalve0 1
#define servo0 (rvalve0<<NROTARY)
#define thermal (servo0 << NSERVO)
#define grip (thermal<<1)

#define opCompleted(offset, channel)  set16(opComplete, 1<<((channel)+offset))
#define opServiced(offset, channel) clear16(opComplete, 1<<((channel)+offset))


#define baseI2Cadr  0x20    //added to addrsws to yield node's I2C address
                            //note that baseI2Cadr is the address of the gateway
#define I2CadrSWmask (DCORSWS & 0xf)   //address selection switches
                            //address is baseI2Cadr + switches
                                    
#define debugSilent  0x00
#define debugParse   0x10   //parse one-letter cmds input on the debug port
#define debugVerbose 0x20   //enable verbose reporting on debug port
#define debugAll     0x30
#define debugMask    (DCORSWS & debugAll)  //debugging modes mask


void putDebugNewLine (void);

void putDebugString (const char *string);

void putDebugLine (const char *line);

void reportErr (const char *errTxt, ...);
void checkServoStatus(MotorChannel *mcChan);

//write a message to dstAdr & report errs to the debugPort
I2CnodeErr writeNode (unsigned dst, byte *body, unsigned len);

#endif
