mirror of
https://github.com/openshwprojects/OpenBK7231T_App.git
synced 2026-02-04 19:25:47 +00:00
adc smoother p2
This commit is contained in:
@ -322,6 +322,7 @@
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug Win32 ScriptOnly|Win32'">true</ExcludedFromBuild>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\driver\drv_adcButton.c" />
|
||||
<ClCompile Include="src\driver\drv_adcSmoother.c" />
|
||||
<ClCompile Include="src\driver\drv_battery.c" />
|
||||
<ClCompile Include="src\driver\drv_bl0937.c">
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug Win32 ScriptOnly|Win32'">true</ExcludedFromBuild>
|
||||
|
||||
@ -888,6 +888,9 @@
|
||||
<ClCompile Include="src\selftest\selftest_chargingDriver.c">
|
||||
<Filter>SelfTest</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\driver\drv_adcSmoother.c">
|
||||
<Filter>Drv</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="src\new_cfg.h" />
|
||||
|
||||
@ -9,9 +9,20 @@
|
||||
#include "../hal/hal_adc.h"
|
||||
#include "../mqtt/new_mqtt.h"
|
||||
|
||||
// smoothinng
|
||||
static int *g_samples = NULL;
|
||||
static int g_samplesCount = 0;
|
||||
static int g_nextSample = 0;
|
||||
static int g_sampleIntervalMS = 100;
|
||||
static int g_timeAccum = 0;
|
||||
// generic
|
||||
static int g_adcPin = 0;
|
||||
static int g_margin = 0;
|
||||
static int g_smoothed = -1;
|
||||
static int g_lh = -1;
|
||||
// targets
|
||||
static int g_channel_smoothed = 0;
|
||||
static int g_channel_lh = 0;
|
||||
|
||||
void ADCSmoother_SetupWindow(int size) {
|
||||
g_samples = realloc(g_samples, sizeof(int) * size);
|
||||
@ -33,3 +44,63 @@ float ADCSmoother_Sample() {
|
||||
s /= g_samplesCount;
|
||||
return s;
|
||||
}
|
||||
|
||||
// ADCSmoother [Pindex] [TotalSamples] [SampleIntervalMS] [TargetChannelADCValue] [MarginValue] [TargetChannel0or1]
|
||||
// TargetChannelADCValue is a channel which will get smoother value, like 640, etc
|
||||
// MarginValue is a value that used to tell which smoothed adc values are considered high and which are low
|
||||
// TargetChannel0or1 will be set depending on MarginValue to either 0 or 1
|
||||
// something like:
|
||||
// ADCSmoother 27 10 50 10 2048 11
|
||||
//
|
||||
commandResult_t Cmd_SetupADCSmoother(const void* context, const char* cmd, const char* args, int cmdFlags) {
|
||||
|
||||
Tokenizer_TokenizeString(args, 0);
|
||||
// following check must be done after 'Tokenizer_TokenizeString',
|
||||
// so we know arguments count in Tokenizer. 'cmd' argument is
|
||||
// only for warning display
|
||||
if (Tokenizer_CheckArgsCountAndPrintWarning(cmd, 6))
|
||||
{
|
||||
return CMD_RES_NOT_ENOUGH_ARGUMENTS;
|
||||
}
|
||||
g_adcPin = Tokenizer_GetArgInteger(0);
|
||||
int size = Tokenizer_GetArgInteger(1);
|
||||
ADCSmoother_SetupWindow(size);
|
||||
g_sampleIntervalMS = Tokenizer_GetArgInteger(2);
|
||||
g_channel_smoothed = Tokenizer_GetArgInteger(3);
|
||||
g_margin = Tokenizer_GetArgInteger(4);
|
||||
g_channel_lh = Tokenizer_GetArgInteger(5);
|
||||
|
||||
HAL_ADC_Init(g_adcPin);
|
||||
|
||||
return CMD_RES_OK;
|
||||
}
|
||||
|
||||
void DRV_ADCSmoother_Init() {
|
||||
|
||||
CMD_RegisterCommand("ADCSmoother", Cmd_SetupADCSmoother, NULL);
|
||||
}
|
||||
void DRV_ADCSmootherDoSmooth() {
|
||||
int raw = HAL_ADC_Read(g_adcPin);
|
||||
ADCSmoother_AppendSample(raw);
|
||||
int smoothed = ADCSmoother_Sample();
|
||||
int lowHigh = smoothed > g_margin;
|
||||
addLogAdv(LOG_INFO, LOG_FEATURE_DRV, "AS: raw %i, smoothed %i, LowHigh %i\n", raw, smoothed, lowHigh);
|
||||
|
||||
if (smoothed != g_smoothed) {
|
||||
g_smoothed = smoothed;
|
||||
CHANNEL_Set(g_channel_smoothed, g_smoothed, 0);
|
||||
}
|
||||
if (lowHigh != g_lh) {
|
||||
g_lh = lowHigh;
|
||||
CHANNEL_Set(g_channel_lh, g_lh, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void DRV_ADCSmoother_RunFrame() {
|
||||
g_timeAccum += g_deltaTimeMS;
|
||||
if (g_timeAccum > g_sampleIntervalMS) {
|
||||
DRV_ADCSmootherDoSmooth();
|
||||
g_timeAccum = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -49,6 +49,9 @@ void TM1638_Init();
|
||||
|
||||
void HT16K33_Init();
|
||||
|
||||
void DRV_ADCSmoother_Init();
|
||||
void DRV_ADCSmoother_RunFrame();
|
||||
|
||||
bool DRV_IsRunning(const char* name);
|
||||
|
||||
// this is exposed here only for debug tool with automatic testing
|
||||
|
||||
@ -274,6 +274,11 @@ static driver_t g_drivers[] = {
|
||||
//drvdetail:"descr":"Driver for 7-segment LED display with DIO/CLK/STB interface. TM1638 is very similiar to GN6932 and TM1637. See [this topic](https://www.elektroda.com/rtvforum/viewtopic.php?p=20553628#20553628) for details.",
|
||||
//drvdetail:"requires":""}
|
||||
{ "TM1638", TM1638_Init, NULL, NULL, TMGN_RunQuickTick,NULL, NULL, false },
|
||||
//drvdetail:{"name":"ADCSmoother",
|
||||
//drvdetail:"title":"TODO",
|
||||
//drvdetail:"descr":"qq",
|
||||
//drvdetail:"requires":""}
|
||||
{ "ADCSmoother", DRV_ADCSmoother_Init, NULL, NULL, DRV_ADCSmoother_RunFrame, NULL, NULL, false },
|
||||
#if ENABLE_DRIVER_HT16K33
|
||||
//drvdetail:{"name":"HT16K33",
|
||||
//drvdetail:"title":"TODO",
|
||||
|
||||
Reference in New Issue
Block a user