brickOS C++ v0.9.0
LightSensor.H
Go to the documentation of this file.
1
7//
8// The contents of this file are subject to the Mozilla Public License
9// Version 1.0 (the "License"); you may not use this file except in
10// compliance with the License. You may obtain a copy of the License
11// at http://www.mozilla.org/MPL/
12//
13// Software distributed under the License is distributed on an "AS IS"
14// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
15// the License for the specific language governing rights and
16// limitations under the License.
17//
18// This software was developed as part of the legOS project.
19//
20// Contributor: Pat Welch (legOS@mousebrains.com)
21
22#ifndef _LightSensor_H_
23#define _LightSensor_H_
24
25#include <config.h>
26#include <c++/Sensor.H>
27
28#if defined(CONF_DSENSOR)
46class LightSensor : public Sensor {
47public:
53 LightSensor(const Sensor::Port port) : Sensor(port, true) {}
61 unsigned int get() const {return LIGHT(sensor);}
69 unsigned int sample(unsigned int size = 10, int wait = 2) const {
70 if (size == 0) size = 1;
71 unsigned int sum(get());
72 for (unsigned int i = 1; i < size; ++i) {
73 sum += get();
74 delay(wait);
75 }
76 return sum / size;
77 }
78};
79
80#else // CONF_DSENSOR
81#warning Enable CONF_DSENSOR to use LightSensor.H
82#endif // CONF_DSENSOR
83#endif // _LightSensor_H_
C++ Sensor Class Interface.
Light-sensor interface (active/passive mode).
Definition LightSensor.H:46
LightSensor(const Sensor::Port port)
Instance an active light sensor at {port}.
Definition LightSensor.H:53
unsigned int sample(unsigned int size=10, int wait=2) const
Get the average of {size} samples, waiting {wait} mSec between each sample.
Definition LightSensor.H:69
unsigned int get() const
Get the current lightSensor value.
Definition LightSensor.H:61
Basic sensor interface.
Definition Sensor.H:54
volatile unsigned int & sensor
The address of our sensor value.
Definition Sensor.H:181
Port
List of sensor identifiers (IDs).
Definition Sensor.H:67
void delay(unsigned ms)
delay approximately ms mSec
#define LIGHT(a)
map light sensor to 0..LIGHT_MAX
Definition dsensor.h:77