Sensor.H
Go to the documentation of this file.
1 
7 // The contents of this file are subject to the Mozilla Public License
8 // Version 1.0 (the "License"); you may not use this file except in
9 // compliance with the License. You may obtain a copy of the License
10 // at http://www.mozilla.org/MPL/
11 //
12 // Software distributed under the License is distributed on an "AS IS"
13 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
14 // the License for the specific language governing rights and
15 // limitations under the License.
16 //
17 // This software was developed as part of the legOS project.
18 //
19 // Contributor: Pat Welch (legOS@mousebrains.com)
20 
21 #ifndef _Sensor_H_
22 #define _Sensor_H_
23 
24 #include <config.h>
25 
26 #if defined(CONF_DSENSOR)
27 
28 #include <dsensor.h>
29 #include <conio.h>
30 
54 class Sensor {
55 public:
67  enum Port {
68  S1 = 0,
69  S2,
70  S3,
72  };
73 
84  Sensor(const Port port, bool makeActive = false)
85  : sensor((port == S1) ? SENSOR_1 :
86  (port == S2) ? SENSOR_2 :
87  (port == S3) ? SENSOR_3 :
88  BATTERY)
89  {
90  if (makeActive) active(); // Don't use mode here, since this saves memory
91  else passive();
92  }
93 
101  ~Sensor() {off();}
102 
109  unsigned int get() const {return sensor;}
110 
118  void mode(bool makeActive) const {
119  if (makeActive) active();
120  else passive();
121  }
122 
127  void passive() const {ds_passive(&sensor);}
128 
133  void active() const {ds_active(&sensor);}
134 
139  void on () const {active();}
140 
145  void off() const {passive();}
146 
151  void strobe() const {off(); on();}
152 
158  void strobe(const int ms) const {off(); delay(ms); on();}
159 
167  unsigned int sample(unsigned int size = 10, int wait = 2) const {
168  if (size == 0) size = 1;
169  unsigned int sum(get());
170  for (unsigned int i = 1; i < size; ++i) {
171  sum += get();
172  delay(wait);
173  }
174  return sum / size;
175  }
176 
177 protected:
181  volatile unsigned int& sensor;
182 };
183 
184 #else // CONF_DSENSOR
185 #warning Enable CONF_DSENSOR to use Sensor.H
186 #endif // CONF_DSENSOR
187 #endif // _Sensor_H_
188 

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

Generated on Sat Mar 15 2014 11:28:21 for brickOS Kernel Developer by doxygen 1.8.1.2