#ifndef _RVALVE
#define _RVALVE

#include "dwarf.h"
#include "interrupts.h"

enum {
  SSMoff,           // motor off, encoder off, position known
  SSMread,          // read position
  SSMreading,       //  ...busy reading position...
  SSMbraking,       // braking to a stop
  SSMstart,         // start a move to specified goal angle
  SSMcheck,         // check valve position before rotating
  SSMrotatingUp,    // valve angle "increasing" (must be first)
  SSMrotatingDown   // valve angle "decreasing" (follows rotating up)
};


typedef struct
{
   byte State;
   int8 direction;  //0 --> nearest, >0 --> increasing, <0 -->decreasing
   byte Valve;	//rotary valve shaft
   byte source;
   byte tag;
   byte maxCurrent;   //hundreths of amps
   byte brakeTics;    //# of tics to delay for motor stop
   byte brakeTimer;   //down counter
   uint16 position;   //current position
   uint16 lashGoal;   //goal position compensated for backlash
   uint16 goal;       //goal position
   uint16 lash;       //+/- backlash counts from goal
   uint16 maxError;   //max acceptable error
   uint16 revCounts;  //# of counts in a shaft revolution
}RotaryValve;

/* Rotary Valve Functions*/
void configRotaryValves(RotaryValve* rv);
void valveISR(RotaryValve* rv, char debugFlag);
void idleShaft (RotaryValve *rv);  //1st ensure timers interrupts are masked


#endif