Skip to content

Commit

Permalink
Simplify OSR, Rshift assign
Browse files Browse the repository at this point in the history
  • Loading branch information
muink authored Feb 20, 2022
1 parent 81b1eea commit 7655b83
Showing 1 changed file with 5 additions and 38 deletions.
43 changes: 5 additions & 38 deletions src/Oversampling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,9 @@ Oversampling::Oversampling(int adcbytes, int samplebytes, int Averaging)

unsigned long Oversampling::read(int pin)
{
int SampleCount = _samplebytes - _adcbytes;
SampleCount = constrain(SampleCount, 1, 8);
int OversampleCount;// = pow(SampleCount, 4);
int DecimationCount;// = pow(SampleCount, 2);
if (SampleCount==1){
OversampleCount = 4;
DecimationCount = 2;
}
if (SampleCount==2){
OversampleCount = 16;
DecimationCount = 4;
}
if (SampleCount==3){
OversampleCount = 64;
DecimationCount = 8;
}
if (SampleCount==4){
OversampleCount = 256;
DecimationCount = 16;
}
if (SampleCount==5){
OversampleCount = 1024;
DecimationCount = 32;
}
if (SampleCount==6){
OversampleCount = 4096;
DecimationCount = 64;
}
if (SampleCount==7){
OversampleCount = 16384;
DecimationCount = 128;
}
if (SampleCount==8){
OversampleCount = 65536;
DecimationCount = 256;
}
int DecimationCount = _samplebytes - _adcbytes;
DecimationCount = constrain(DecimationCount, 1, 8);
int OversampleCount = 1 << DecimationCount*2;// = pow(2, DecimationCount*2);

// Can Lead To Overflow
unsigned long TotalADC = 0;
Expand All @@ -63,8 +30,8 @@ unsigned long Oversampling::read(int pin)
TotalADC = TotalADC + analogRead(pin);
}
}
TotalADC = TotalADC / DecimationCount / _Averaging;
TotalADC = (TotalADC >> DecimationCount) / _Averaging;

//_Averaging
return TotalADC;
}
}

0 comments on commit 7655b83

Please sign in to comment.