diff --git a/README.md b/README.md index 2db727b5..c825deec 100644 --- a/README.md +++ b/README.md @@ -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)
[![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¤cy_code=EUR&bn=PP%2dDonationsBF%3abtn_donate_LG%2egif%3aNonHostedGuest) diff --git a/code/espurna/config/sensors.h b/code/espurna/config/sensors.h index 771b7c50..2de7a48c 100644 --- a/code/espurna/config/sensors.h +++ b/code/espurna/config/sensors.h @@ -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 diff --git a/code/espurna/sensors/AnalogSensor.h b/code/espurna/sensors/AnalogSensor.h index e988ab96..ede70b46 100644 --- a/code/espurna/sensors/AnalogSensor.h +++ b/code/espurna/sensors/AnalogSensor.h @@ -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; };