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
54class Sensor {
55public:
67 enum Port {
68 S1 = 0,
71 Battery
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
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
177protected:
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
Battery-sensor interface (RCX Internal).
Definition Battery.H:38
Basic sensor interface.
Definition Sensor.H:54
void active() const
Set the sensor to active (provide voltage to connector)
Definition Sensor.H:133
volatile unsigned int & sensor
The address of our sensor value.
Definition Sensor.H:181
unsigned int get() const
Get the current sensor reading.
Definition Sensor.H:109
Port
List of sensor identifiers (IDs).
Definition Sensor.H:67
@ S3
RCX sensor port 3.
Definition Sensor.H:70
@ S1
RCX sensor port 1.
Definition Sensor.H:68
@ S2
RCX sensor port 2.
Definition Sensor.H:69
void mode(bool makeActive) const
Set the sensor (mode) to active or passive.
Definition Sensor.H:118
void strobe() const
Quickly turn sensor off then back on.
Definition Sensor.H:151
void strobe(const int ms) const
Turn sensor off, wait for {ms} milliSeconds, then then turn it back on.
Definition Sensor.H:158
Sensor(const Port port, bool makeActive=false)
Construct a new sensor instance.
Definition Sensor.H:84
void off() const
Turn the sensor off (turn off voltage to connector)
Definition Sensor.H:145
void passive() const
Set the sensor to passive (turn off voltage to connector)
Definition Sensor.H:127
void on() const
Turn the sensor on (provide voltage to connector)
Definition Sensor.H:139
~Sensor()
Destroy this sensor instance.
Definition Sensor.H:101
unsigned int sample(unsigned int size=10, int wait=2) const
Get the average of {size} samples, waiting {wait} mSec between each sample.
Definition Sensor.H:167
Interface: console input / output.
void delay(unsigned ms)
delay approximately ms mSec
Interface: direct reading of sensors.
#define SENSOR_2
Sensor on input pad 2.
Definition dsensor.h:62
void ds_passive(volatile unsigned *sensor)
set sensor mode to passive (light sensor detects ambient light)
Definition dsensor.h:180
void ds_active(volatile unsigned *sensor)
set sensor mode to active (light sensor emits light, rotation works)
Definition dsensor.h:167
#define BATTERY
Battery sensor.
Definition dsensor.h:64
#define SENSOR_1
< the raw sensors
Definition dsensor.h:61
#define SENSOR_3
Sensor on input pad 3.
Definition dsensor.h:63

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

Generated for brickOS C++ by doxygen 1.9.8