Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

include/c++/Sensor.H

Go to the documentation of this file.
00001 
00007 // The contents of this file are subject to the Mozilla Public License
00008 // Version 1.0 (the "License"); you may not use this file except in
00009 // compliance with the License. You may obtain a copy of the License
00010 // at http://www.mozilla.org/MPL/
00011 // 
00012 // Software distributed under the License is distributed on an "AS IS"
00013 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
00014 // the License for the specific language governing rights and
00015 // limitations under the License.
00016 //
00017 // This software was developed as part of the legOS project.
00018 //
00019 // Contributor: Pat Welch (legOS@mousebrains.com)
00020 
00021 #ifndef _Sensor_H_
00022 #define _Sensor_H_
00023 
00024 #include <config.h>
00025 
00026 #if defined(CONF_DSENSOR)
00027 
00028 #include <dsensor.h>
00029 #include <conio.h>
00030 
00054 class Sensor {
00055 public:
00067   enum Port {
00068     S1 = 0,     
00069     S2,         
00070     S3,         
00071     Battery     
00072   };
00073 
00084   Sensor(const Port port, bool makeActive = false)
00085     : sensor((port == S1) ? SENSOR_1 : 
00086              (port == S2) ? SENSOR_2 :
00087              (port == S3) ? SENSOR_3 :
00088              BATTERY) 
00089   {
00090     if (makeActive) active();    // Don't use mode here, since this saves memory
00091     else            passive();
00092   }
00093 
00101   ~Sensor() {off();}
00102 
00109   unsigned int get() const {return sensor;}
00110 
00118   void mode(bool makeActive) const {
00119     if (makeActive) active();
00120     else            passive();
00121   }
00122 
00127   void passive() const {ds_passive(&sensor);}
00128 
00133   void active() const {ds_active(&sensor);}
00134 
00139   void on () const {active();}
00140 
00145   void off() const {passive();}
00146 
00151   void strobe() const {off(); on();}
00152 
00158   void strobe(const int ms) const {off(); delay(ms); on();}
00159 
00167   unsigned int sample(unsigned int size = 10, int wait = 2) const {
00168     if (size == 0) size = 1;
00169     unsigned int sum(get());
00170     for (unsigned int i = 1; i < size; ++i) {
00171       sum += get();
00172       delay(wait);
00173     }
00174     return sum / size;
00175   }
00176 
00177 protected:
00181   volatile unsigned int& sensor;
00182 };
00183 
00184 #else // CONF_DSENSOR
00185 #warning Enable CONF_DSENSOR to use Sensor.H
00186 #endif // CONF_DSENSOR
00187 #endif // _Sensor_H_
00188 

brickOS is released under the Mozilla Public License.
Original code copyright 1998-2002 by the authors.

Generated on Mon Feb 16 21:02:10 2004 for brickOS Kernel Developer by doxygen 1.3.5