TRC Library for FRC (2011) 2011
TRC Library Code Project

C:/Users/Michael/Frc/2011/code/trclib/AnalogIn.h

Go to the documentation of this file.
00001 #if 0
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 #endif
00015 
00016 #ifndef _ANALOGIN_H
00017 #define _ANALOGIN_H
00018 
00019 #ifdef MOD_ID
00020     #undef MOD_ID
00021 #endif
00022 #define MOD_ID                  MOD_ANALOGIN
00023 #ifdef MOD_NAME
00024     #undef MOD_NAME
00025 #endif
00026 #define MOD_NAME                "AnalogIn"
00027 
00028 #define ANALOGINF_USER_MASK     0x0000ffff
00029 #define ANALOGINF_INVERSE       0x00000001
00030 #define ANALOGINF_CALIBRATING   0x80000000
00031 
00032 #define ANALOGIN_ZONE_LO        0
00033 #define ANALOGIN_ZONE_MID       1
00034 #define ANALOGIN_ZONE_HI        2
00035 
00042 class AnalogInNotify
00043 {
00044 public:
00053     virtual
00054     void
00055     NotifyAIn(
00056         __in UINT32 slot,
00057         __in UINT32 channel,
00058         __in UINT32 zone,
00059         __in INT16  value
00060         ) = 0;
00061 };  //class AnalogInNotify
00062 
00073 class AnalogIn: public AnalogChannel
00074 {
00075 private:
00076     UINT32          m_slot;
00077     UINT32          m_channel;
00078     AnalogInNotify *m_notify;
00079     INT16           m_thresholdLo;
00080     INT16           m_thresholdHi;
00081     UINT32          m_flagsAIn;
00082     INT16           m_minRaw;
00083     INT16           m_maxRaw;
00084     UINT32          m_zone;
00085 
00086 public:
00099     AnalogIn(
00100         __in UINT32          slot,
00101         __in UINT32          channel,
00102         __in INT16           thresholdLo,
00103         __in INT16           thresholdHi,
00104         __in UINT32          flags,
00105         __in AnalogInNotify *notify = NULL
00106         ): AnalogChannel(slot, channel),
00107            m_slot(slot),
00108            m_channel(channel),
00109            m_notify(notify)
00110     {
00111         TLevel(INIT);
00112         TEnterMsg(("Slot=%d,Channel=%d,ThresholdLo=%d,ThresholdHi=%d,flags=%x,"
00113                    "notify=%p",
00114                    slot, channel, thresholdLo, thresholdHi, flags, notify));
00115 
00116         m_thresholdLo = thresholdLo;
00117         m_thresholdHi = thresholdHi;
00118         m_flagsAIn = flags & ANALOGINF_USER_MASK;
00119         m_zone = ANALOGIN_ZONE_LO;
00120 
00121         TExit();
00122     }   //AnalogIn
00123 
00127     ~AnalogIn(
00128         void
00129         )
00130     {
00131         TLevel(INIT);
00132         TEnter();
00133         TExit();
00134     }   //~AnalogIn
00135 
00143     bool
00144     IsCalibrating(
00145         void
00146         )
00147     {
00148         TLevel(API);
00149         TEnter();
00150         TExitMsg(("=%d", (m_flagsAIn & ANALOGINF_CALIBRATING) != 0));
00151         return (m_flagsAIn & ANALOGINF_CALIBRATING) != 0;
00152     }   //IsCalibrating
00153 
00161     void
00162     Calibrate(
00163         __in bool fStart
00164         )
00165     {
00166         TLevel(API);
00167         TEnterMsg(("fStart=%d", fStart));
00168 
00169         if (fStart)
00170         {
00171            m_flagsAIn |= ANALOGINF_CALIBRATING;
00172            m_minRaw = 4095;     //Assuming 12-bit A/D
00173            m_maxRaw = 0;
00174         }
00175         else
00176         {
00177             m_flagsAIn &= ~ANALOGINF_CALIBRATING;
00178             INT16 zoneRange = (m_maxRaw - m_minRaw)/3;
00179             m_thresholdLo = m_minRaw + zoneRange;
00180             m_thresholdHi = m_maxRaw - zoneRange;
00181             TInfo(("ThresholdLo=%d,ThresholdHi=%d",
00182                    m_thresholdLo, m_thresholdHi));
00183         }
00184 
00185         TExit();
00186     }   //Calibrate
00187 
00194     void
00195     AnalogInTask(
00196         void
00197         )
00198     {
00199         TLevel(HIFREQ);
00200         TEnter();
00201 
00202         INT16 value = GetValue();
00203         if (IsCalibrating())
00204         {
00205             if (value < m_minRaw)
00206             {
00207                 m_minRaw = value;
00208             }
00209             else if (value > m_maxRaw)
00210             {
00211                 m_maxRaw = value;
00212             }
00213         }
00214         else
00215         {
00216             UINT32 zone;
00217             if (value <= m_thresholdLo)
00218             {
00219                 zone = (m_flagsAIn & ANALOGINF_INVERSE)?
00220                        ANALOGIN_ZONE_HI: ANALOGIN_ZONE_LO;
00221             }
00222             else if (value <= m_thresholdHi)
00223             {
00224                 zone = ANALOGIN_ZONE_MID;
00225             }
00226             else
00227             {
00228                 zone = (m_flagsAIn & ANALOGINF_INVERSE)?
00229                        ANALOGIN_ZONE_LO: ANALOGIN_ZONE_HI;
00230             }
00231 
00232             if (zone != m_zone)
00233             {
00234                 //
00235                 // We have crossed to another zone, let's notify somebody.
00236                 //
00237                 m_zone = zone;
00238                 if (m_notify != NULL)
00239                 {
00240                     m_notify->NotifyAIn(m_slot, m_channel, m_zone, value);
00241                 }
00242             }
00243         }
00244 
00245         TExit();
00246     }   //AnalogInTask
00247 };  //class AnalogIn
00248 
00249 #endif  //ifndef _ANALOGIN_H
 All Classes Files Functions Variables Defines