Balboa32U4 library
QTRSensors.h
Go to the documentation of this file.
1 
3 #pragma once
4 
5 #include <stdint.h>
6 
13 enum class QTRReadMode : uint8_t {
16  Off,
17 
20  On,
21 
27  OnAndOff,
28 
34  OddEven,
35 
41 
46  Manual
47 };
48 
50 enum class QTRType : uint8_t {
51  Undefined,
52  RC,
53  Analog
54 };
55 
57 enum class QTREmitters : uint8_t {
58  All,
59  Odd,
60  Even,
61  None
62 };
63 
65 const uint8_t QTRNoEmitterPin = 255;
66 
68 const uint16_t QTRRCDefaultTimeout = 2500;
69 
71 const uint8_t QTRMaxSensors = 31;
72 
84 {
85  public:
86 
87  QTRSensors() = default;
88 
89  ~QTRSensors();
90 
94  void setTypeRC();
95 
99  void setTypeAnalog();
100 
106  QTRType getType() { return _type; }
107 
134  void setSensorPins(const uint8_t * pins, uint8_t sensorCount);
135 
154  void setTimeout(uint16_t timeout);
155 
161  uint16_t getTimeout() { return _timeout; }
162 
172  void setSamplesPerSensor(uint8_t samples);
173 
180  uint16_t getSamplesPerSensor() { return _samplesPerSensor; }
181 
201  void setEmitterPin(uint8_t emitterPin);
202 
218  void setEmitterPins(uint8_t oddEmitterPin, uint8_t evenEmitterPin);
219 
227  void releaseEmitterPins();
228 
235  uint8_t getEmitterPinCount() { return _emitterPinCount; }
236 
247  uint8_t getEmitterPin() { return _oddEmitterPin; }
248 
259  uint8_t getOddEmitterPin() { return _oddEmitterPin; }
260 
271  uint8_t getEvenEmitterPin() { return _evenEmitterPin; }
272 
280  void setDimmable() { _dimmable = true; }
281 
289  void setNonDimmable() { _dimmable = false; }
290 
297  bool getDimmable() { return _dimmable; }
298 
315  void setDimmingLevel(uint8_t dimmingLevel);
316 
322  uint8_t getDimmingLevel() { return _dimmingLevel; }
323 
338  void emittersOff(QTREmitters emitters = QTREmitters::All, bool wait = true);
339 
357  void emittersOn(QTREmitters emitters = QTREmitters::All, bool wait = true);
358 
372  void emittersSelect(QTREmitters emitters);
373 
401 
403  void resetCalibration();
404 
434  void read(uint16_t * sensorValues, QTRReadMode mode = QTRReadMode::On);
435 
456  void readCalibrated(uint16_t * sensorValues, QTRReadMode mode = QTRReadMode::On);
457 
501  uint16_t readLineBlack(uint16_t * sensorValues, QTRReadMode mode = QTRReadMode::On)
502  {
503  return readLinePrivate(sensorValues, mode, false);
504  }
505 
526  uint16_t readLineWhite(uint16_t * sensorValues, QTRReadMode mode = QTRReadMode::On)
527  {
528  return readLinePrivate(sensorValues, mode, true);
529  }
530 
531 
536  {
538  bool initialized = false;
540  uint16_t * minimum = nullptr;
542  uint16_t * maximum = nullptr;
543  };
544 
553 
556 
559 
561 
562  private:
563 
564  uint16_t emittersOnWithPin(uint8_t pin);
565 
566  // Handles the actual calibration, including (re)allocating and
567  // initializing the storage for the calibration values if necessary.
568  void calibrateOnOrOff(CalibrationData & calibration, QTRReadMode mode);
569 
570  void readPrivate(uint16_t * sensorValues, uint8_t start = 0, uint8_t step = 1);
571 
572  uint16_t readLinePrivate(uint16_t * sensorValues, QTRReadMode mode, bool invertReadings);
573 
574  QTRType _type = QTRType::Undefined;
575 
576  uint8_t * _sensorPins = nullptr;
577  uint8_t _sensorCount = 0;
578 
579  uint16_t _timeout = QTRRCDefaultTimeout; // only used for RC sensors
580  uint16_t _maxValue = QTRRCDefaultTimeout; // the maximum value returned by readPrivate()
581  uint8_t _samplesPerSensor = 4; // only used for analog sensors
582 
583  uint8_t _oddEmitterPin = QTRNoEmitterPin; // also used for single emitter pin
584  uint8_t _evenEmitterPin = QTRNoEmitterPin;
585  uint8_t _emitterPinCount = 0;
586 
587  bool _dimmable = true;
588  uint8_t _dimmingLevel = 0;
589 
590  uint16_t _lastPosition = 0;
591 };
uint16_t getSamplesPerSensor()
Returns the number of analog readings to average per analog sensor.
Definition: QTRSensors.h:180
const uint8_t QTRMaxSensors
The maximum number of sensors supported by an instance of this class.
Definition: QTRSensors.h:71
QTREmitters
Emitters selected to turn on or off.
Definition: QTRSensors.h:57
void calibrate(QTRReadMode mode=QTRReadMode::On)
Reads the sensors for calibration.
Definition: QTRSensors.cpp:304
const uint8_t QTRNoEmitterPin
Represents an undefined emitter control pin.
Definition: QTRSensors.h:65
void setSamplesPerSensor(uint8_t samples)
Sets the number of analog readings to average per analog sensor.
Definition: QTRSensors.cpp:50
Stores sensor calibration data.
Definition: QTRSensors.h:535
void setDimmable()
Specifies that the sensors are dimmable.
Definition: QTRSensors.h:280
void setEmitterPins(uint8_t oddEmitterPin, uint8_t evenEmitterPin)
Sets separate odd and even emitter control pins for the sensors.
Definition: QTRSensors.cpp:66
void setTypeRC()
Specifies that the sensors are RC.
Definition: QTRSensors.cpp:4
uint8_t getEmitterPin()
Returns the emitter control pin.
Definition: QTRSensors.h:247
void setTimeout(uint16_t timeout)
Sets the timeout for RC sensors.
Definition: QTRSensors.cpp:43
void setEmitterPin(uint8_t emitterPin)
Sets the emitter control pin for the sensors.
Definition: QTRSensors.cpp:56
void setSensorPins(const uint8_t *pins, uint8_t sensorCount)
Sets the sensor pins.
Definition: QTRSensors.cpp:16
uint8_t getDimmingLevel()
Returns the dimming level.
Definition: QTRSensors.h:322
QTRType getType()
Returns the type of the sensors.
Definition: QTRSensors.h:106
QTRReadMode
Emitter behavior when taking readings.
Definition: QTRSensors.h:13
void readCalibrated(uint16_t *sensorValues, QTRReadMode mode=QTRReadMode::On)
Reads the sensors and provides calibrated values between 0 and 1000.
Definition: QTRSensors.cpp:467
bool getDimmable()
Returns whether the sensors are dimmable.
Definition: QTRSensors.h:297
uint16_t * minimum
Lowest readings seen during calibration.
Definition: QTRSensors.h:540
void resetCalibration()
Resets all calibration that has been done.
Definition: QTRSensors.cpp:293
void read(uint16_t *sensorValues, QTRReadMode mode=QTRReadMode::On)
Reads the raw sensor values into an array.
Definition: QTRSensors.cpp:407
CalibrationData calibrationOff
Data from calibrating with emitters off.
Definition: QTRSensors.h:558
Represents a QTR sensor array.
Definition: QTRSensors.h:83
uint16_t * maximum
Highest readings seen during calibration.
Definition: QTRSensors.h:542
uint16_t readLineBlack(uint16_t *sensorValues, QTRReadMode mode=QTRReadMode::On)
Reads the sensors, provides calibrated values, and returns an estimated black line position.
Definition: QTRSensors.h:501
const uint16_t QTRRCDefaultTimeout
Default timeout for RC sensors (in microseconds).
Definition: QTRSensors.h:68
void emittersOff(QTREmitters emitters=QTREmitters::All, bool wait=true)
Turns the IR LEDs off.
Definition: QTRSensors.cpp:102
uint16_t getTimeout()
Returns the timeout for RC sensors.
Definition: QTRSensors.h:161
CalibrationData calibrationOn
Data from calibrating with emitters on.
Definition: QTRSensors.h:555
uint16_t readLineWhite(uint16_t *sensorValues, QTRReadMode mode=QTRReadMode::On)
Reads the sensors, provides calibrated values, and returns an estimated white line position.
Definition: QTRSensors.h:526
void emittersSelect(QTREmitters emitters)
Turns on the selected emitters and turns off the other emitters with optimized timing.
Definition: QTRSensors.cpp:247
uint8_t getEvenEmitterPin()
Returns the even emitter control pin.
Definition: QTRSensors.h:271
void setDimmingLevel(uint8_t dimmingLevel)
Sets the dimming level.
Definition: QTRSensors.cpp:95
void emittersOn(QTREmitters emitters=QTREmitters::All, bool wait=true)
Turns the IR LEDs on.
Definition: QTRSensors.cpp:151
void setTypeAnalog()
Specifies that the sensor type is analog.
Definition: QTRSensors.cpp:10
void setNonDimmable()
Specifies that the sensors are non-dimmable.
Definition: QTRSensors.h:289
uint8_t getOddEmitterPin()
Returns the odd emitter control pin.
Definition: QTRSensors.h:259
uint8_t getEmitterPinCount()
Returns the number of emitter control pins in use.
Definition: QTRSensors.h:235
QTRType
Sensor types.
Definition: QTRSensors.h:50
void releaseEmitterPins()
Releases emitter pin/pins that have been set.
Definition: QTRSensors.cpp:78
bool initialized
Whether array pointers have been allocated and initialized.
Definition: QTRSensors.h:538