Browse Source

Option to take several samples with AnalogSensor

rfm69
Xose Pérez 6 years ago
parent
commit
b4adf5d001
3 changed files with 44 additions and 4 deletions
  1. +3
    -3
      README.md
  2. +8
    -0
      code/espurna/config/sensors.h
  3. +33
    -1
      code/espurna/sensors/AnalogSensor.h

+ 3
- 3
README.md View File

@ -4,9 +4,9 @@ ESPurna ("spark" in Catalan) is a custom firmware for ESP8285/ESP8266 based smar
It uses the Arduino Core for ESP8266 framework and a number of 3rd party libraries.
[![version](https://img.shields.io/badge/version-1.13.1a-brightgreen.svg)](CHANGELOG.md)
[![branch](https://img.shields.io/badge/branch-dev-orange.svg)](https://github.com/xoseperez/espurna/tree/dev/)
[![travis](https://travis-ci.org/xoseperez/espurna.svg?branch=dev)](https://travis-ci.org/xoseperez/espurna)
[![codacy](https://img.shields.io/codacy/grade/c9496e25cf07434cba786b462cb15f49/dev.svg)](https://www.codacy.com/app/xoseperez/espurna/dashboard)
[![branch](https://img.shields.io/badge/branch-sensors-orange.svg)](https://github.com/xoseperez/espurna/tree/sensors/)
[![travis](https://travis-ci.org/xoseperez/espurna.svg?branch=sensors)](https://travis-ci.org/xoseperez/espurna)
[![codacy](https://img.shields.io/codacy/grade/c9496e25cf07434cba786b462cb15f49/sensors.svg)](https://www.codacy.com/app/xoseperez/espurna/dashboard)
[![license](https://img.shields.io/github/license/xoseperez/espurna.svg)](LICENSE)
<br />
[![donate](https://img.shields.io/badge/donate-PayPal-blue.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=xose%2eperez%40gmail%2ecom&lc=US&no_note=0&currency_code=EUR&bn=PP%2dDonationsBF%3abtn_donate_LG%2egif%3aNonHostedGuest)


+ 8
- 0
code/espurna/config/sensors.h View File

@ -79,6 +79,14 @@
#define ANALOG_SUPPORT 0
#endif
#ifndef ANALOG_SAMPLES
#define ANALOG_SAMPLES 10 // Number of samples
#endif
#ifndef ANALOG_DELAY
#define ANALOG_DELAY 0 // Delay between samples in micros
#endif
//------------------------------------------------------------------------------
// BH1750
// Enable support by passing BH1750_SUPPORT=1 build flag


+ 33
- 1
code/espurna/sensors/AnalogSensor.h View File

@ -27,6 +27,24 @@ class AnalogSensor : public BaseSensor {
_sensor_id = SENSOR_ANALOG_ID;
}
void setSamples(unsigned int samples) {
if (_samples > 0) _samples = samples;
}
void setDelay(unsigned long micros) {
_micros = micros;
}
// ---------------------------------------------------------------------
unsigned int getSamples() {
return _samples;
}
unsigned long getDelay() {
return _micros;
}
// ---------------------------------------------------------------------
// Sensor API
// ---------------------------------------------------------------------
@ -60,10 +78,24 @@ class AnalogSensor : public BaseSensor {
// Current value for slot # index
double value(unsigned char index) {
if (index == 0) return analogRead(0);
if (index == 0) return _read();
return 0;
}
protected:
unsigned int _read() {
if (1 == _samples) return analogRead(0);
unsigned long sum = 0;
for (unsigned int i=0; i<_samples; i++) {
if (i>0) delayMicroseconds(_micros);
sum += analogRead(0);
}
return sum / _samples;
}
unsigned int _samples = 1;
unsigned long _micros = 0;
};


Loading…
Cancel
Save