Skip to content

Commit

Permalink
Send LDR and BATT raw values through standard median and mean filters…
Browse files Browse the repository at this point in the history
… to get rid of spikes.

Remove obsolete (and even duplicate) declarations
  • Loading branch information
luebbe committed Feb 4, 2024
1 parent 615ee24 commit bb7a907
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 28 deletions.
6 changes: 6 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ lib_deps =
marcmerlin/FastLED NeoMatrix@^1.2
knolleary/PubSubClient@^2.8
densaugeo/base64@^1.4.0
luisllamasbinaburo/MedianFilterLib@^1.0.0
luisllamasbinaburo/MeanFilterLib@^1.0.0

[env:awtrix2_upgrade]
platform = espressif32
Expand Down Expand Up @@ -60,6 +62,8 @@ lib_deps =
marcmerlin/FastLED NeoMatrix@^1.2
knolleary/PubSubClient@^2.8
densaugeo/base64@^1.4.0
luisllamasbinaburo/MedianFilterLib@^1.0.0
luisllamasbinaburo/MeanFilterLib@^1.0.0

[env:ESP32_S3]
platform = espressif32
Expand All @@ -78,3 +82,5 @@ lib_deps =
marcmerlin/FastLED NeoMatrix@^1.2
knolleary/PubSubClient@^2.8
densaugeo/base64@^1.4.0
luisllamasbinaburo/MedianFilterLib@^1.0.0
luisllamasbinaburo/MeanFilterLib@^1.0.0
37 changes: 19 additions & 18 deletions src/PeripheryManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@
#include <LightDependentResistor.h>
#include <MenuManager.h>
#include <ServerManager.h>
#include <MedianFilterLib.h>
#include <MeanFilterLib.h>
const int buzzerPin = 2; // Buzzer an GPIO2
const int baudRate = 50; // Nachrichtenübertragungsrate
const char *message = "HELLO"; // Die Nachricht, die gesendet werden soll
#define LEDC_CHANNEL 0
#define LEDC_RESOLUTION 8 // 8 bit resolution
#define LEDC_TIMER LEDC_TIMER_0
#define LEDC_MODE LEDC_LOW_SPEED_MODE
#define MEDIAN_WND 7 // A median filter window size of seven should be enough to filter out most spikes
#define MEAN_WND 7 // After filtering the spikes we don't need many samples anymore for the average

#ifdef awtrix2_upgrade
// Pinouts für das WEMOS_D1_MINI32-Environment
Expand Down Expand Up @@ -94,12 +98,13 @@ const unsigned long interval_LDR = 100;
int total = 0;
unsigned long startTime;

const int LDRReadings = 1000;
int TotalLDRReadings[LDRReadings];
float sampleSum = 0.0;
float sampleAverage = 0.0;

MedianFilter<uint16_t> medianFilterBatt(MEDIAN_WND);
MedianFilter<uint16_t> medianFilterLDR(MEDIAN_WND);
MeanFilter<uint16_t> meanFilterBatt(MEAN_WND);
MeanFilter<uint16_t> meanFilterLDR(MEAN_WND);

float brightnessPercent = 0.0;
int lastBrightness = 0;

// The getter for the instantiated singleton instance
PeripheryManager_ &PeripheryManager_::getInstance()
Expand Down Expand Up @@ -441,11 +446,12 @@ void PeripheryManager_::tick()
#ifndef awtrix2_upgrade
uint16_t ADCVALUE = analogRead(BATTERY_PIN);
// Discard values that are totally out of range, especially the first value read after a reboot.
// Meaningful values for a Ulanzi are in the range 400..700
// Meaningful values for an Ulanzi clock are in the range 400..700
if ((ADCVALUE > 100) && (ADCVALUE < 1000))
{
BATTERY_PERCENT = max(min((int)map(ADCVALUE, MIN_BATTERY, MAX_BATTERY, 0, 100), 100), 0);
BATTERY_RAW = ADCVALUE;
// Send ADC values through median filter to get rid of the remaining spikes and then calculate the average
BATTERY_RAW = meanFilterBatt.AddValue(medianFilterBatt.AddValue(ADCVALUE));
BATTERY_PERCENT = max(min((int)map(BATTERY_RAW, MIN_BATTERY, MAX_BATTERY, 0, 100), 100), 0);
SENSORS_STABLE = true;
}
#else
Expand Down Expand Up @@ -491,20 +497,15 @@ void PeripheryManager_::tick()
if (currentMillis_LDR - previousMillis_LDR >= interval_LDR)
{
previousMillis_LDR = currentMillis_LDR;
TotalLDRReadings[sampleIndex] = analogRead(LDR_PIN);

sampleIndex = (sampleIndex + 1) % LDRReadings;
sampleSum = 0.0;
for (int i = 0; i < LDRReadings; i++)
{
sampleSum += TotalLDRReadings[i];
}
sampleAverage = sampleSum / (float)LDRReadings;
LDR_RAW = sampleAverage;
uint16_t LDRVALUE = analogRead(LDR_PIN);

// Send LDR values through median filter to get rid of the remaining spikes and then calculate the average
LDR_RAW = medianFilterLDR.AddValue(medianFilterLDR.AddValue(LDRVALUE));
CURRENT_LUX = (roundf(photocell.getSmoothedLux() * 1000) / 1000);
if (AUTO_BRIGHTNESS && !MATRIX_OFF)
{
brightnessPercent = (sampleAverage * LDR_FACTOR) / 1023.0 * 100.0;
brightnessPercent = (LDR_RAW * LDR_FACTOR) / 1023.0 * 100.0;
brightnessPercent = pow(brightnessPercent, LDR_GAMMA) / pow(100.0, LDR_GAMMA - 1);
BRIGHTNESS = map(brightnessPercent, 0, 100, MIN_BRIGHTNESS, MAX_BRIGHTNESS);
DisplayManager.setBrightness(BRIGHTNESS);
Expand Down
10 changes: 0 additions & 10 deletions src/PeripheryManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@ class PeripheryManager_
{
private:
PeripheryManager_() = default;
#ifdef ULANZI
const int BatReadings = 10;
uint16_t TotalBatReadings[10];
#endif
int readIndex = 0;
uint16_t total = 0;
uint16_t average = 0;
const int LDRReadings = 30;
uint16_t TotalLDRReadings[30];
int sampleIndex = 0;
unsigned long previousMillis = 0;
const unsigned long interval = 1000;

Expand Down

0 comments on commit bb7a907

Please sign in to comment.