diff --git a/FW/leo_muziekdoos_esp32/src/audio.cpp b/FW/leo_muziekdoos_esp32/src/audio.cpp index 3a9417f..b95ea26 100644 --- a/FW/leo_muziekdoos_esp32/src/audio.cpp +++ b/FW/leo_muziekdoos_esp32/src/audio.cpp @@ -103,6 +103,8 @@ void initAudio() void setAudioState(bool state) { + if(state == audioState) return; + audioState = state; if (state) { diff --git a/FW/leo_muziekdoos_esp32/src/led.cpp b/FW/leo_muziekdoos_esp32/src/led.cpp index 23f465b..cb7cba4 100644 --- a/FW/leo_muziekdoos_esp32/src/led.cpp +++ b/FW/leo_muziekdoos_esp32/src/led.cpp @@ -28,7 +28,7 @@ void SetLedColor(CRGB color, bool blink) void initLed(void) { FastLED.addLeds(leds, NUM_LEDS); // GRB ordering is typical - FastLED.setBrightness(40); + FastLED.setBrightness(20); } void handleLed(void) diff --git a/FW/leo_muziekdoos_esp32/src/power.h b/FW/leo_muziekdoos_esp32/src/power.h index 1ea0b68..ade355d 100644 --- a/FW/leo_muziekdoos_esp32/src/power.h +++ b/FW/leo_muziekdoos_esp32/src/power.h @@ -10,9 +10,9 @@ #define TIMEOUT_POWER (7 * 1000 * 60) // 7 minutes timeout -#define POWERBUTTONDELAY 1000 +#define POWERBUTTONDELAY 400 //#define BATTERYMEASUREDELAY 60000 -#define POWERBUTTONOTADELAY 7000 +#define POWERBUTTONOTADELAY 4000 #define POWEROFFOFFDELAY 3000 typedef enum diff --git a/FW/leo_muziekdoos_esp32/src/sensor.cpp b/FW/leo_muziekdoos_esp32/src/sensor.cpp index fbcdaa8..007e177 100644 --- a/FW/leo_muziekdoos_esp32/src/sensor.cpp +++ b/FW/leo_muziekdoos_esp32/src/sensor.cpp @@ -12,6 +12,7 @@ uint16_t BatteryVoltage = 0; uint32_t BatteryWarningFirst = 0; uint16_t HallSensor = 0; +HALLSENSORSTATES hall_sensor_state = HALLSENSORSTATES::hall_idle; uint32_t last_hall_read = 0; uint16_t last_hall_sample = 0; @@ -23,6 +24,23 @@ bool hall_is_Idle = true; bool hallinitOK = false; +String HALLSENSORSTATES_STR[] = { + "hall_idle", + "hall_increasing", + "hall_tipover", + "hall_decreasing", + "unknown"}; + +String getHallSensorStateStr(HALLSENSORSTATES state) +{ + if (state < sizeof(HALLSENSORSTATES)) + { + return HALLSENSORSTATES_STR[state]; + } + log_d("unknown state %d", state); + return "unknown state"; +} + bool getSensorInitStatus(void) { return hallinitOK; @@ -55,6 +73,7 @@ void initSensor(void) { log_i("sensor init ADS1x15:"); bool result = ADS.begin(I2C_SDA, I2C_SCL); + ADS.setMode(0); initBattery(); if (!result) { @@ -78,11 +97,11 @@ bool CheckBattery(void) if (BatteryVoltage < VBATTMIN) { uint32_t timeNow = millis(); - if(BatteryWarningFirst == 0) + if (BatteryWarningFirst == 0) { BatteryWarningFirst = timeNow; } - if(timeNow - BatteryWarningFirst > LOWBATTPERIOD) + if (timeNow - BatteryWarningFirst > LOWBATTPERIOD) { batteryLow = true; return true; @@ -119,63 +138,109 @@ void handleBatterySensor(void) { CheckBattery(); log_i("vbatt level = %d %%", BatterySensor); - if(BatterySensor > 80) SetLedColor(CRGB::Green); - else if(BatterySensor > 50) SetLedColor(CRGB::Orange); - else if(BatterySensor > 20) SetLedColor(CRGB::Red); + if (BatterySensor > 80) + SetLedColor(CRGB::Green); + else if (BatterySensor > 50) + SetLedColor(CRGB::Orange); + else if (BatterySensor > 20) + SetLedColor(CRGB::Red); lastVbatt = timeNow; } } -HALLSENSORSTATES hall_sensor_state = HALLSENSORSTATES::hall_idle; +HALLSENSORSTATES getprogressstate(uint16_t sample) +{ + if (int(sample - last_hall_sample) > HALLTHRESHOLD) + { + return hall_increasing; + } + else if (int(sample - last_hall_sample) < HALLTHRESHOLD) + { + return hall_decreasing; + } + else + { + return hall_idle; + } +} void handleHallSensor(void) { uint32_t timeNow = millis(); if (timeNow - last_hall_read > HALLINTERVAL) { - //get sample + // get sample uint16_t hall_sample = ADS.readADC(HALL_INPUT); - switch(hall_sensor_state) + switch (hall_sensor_state) { - case hall_idle: + case hall_idle: + { + hall_is_Idle = true; + if (int(hall_sample - last_hall_sample) > HALLIDLETHRESHOLD) { - if(int(hall_sample - last_hall_sample) > 0) - { - hall_decrease_count = 0; - if(hall_increase_count++ > HALLTHRESHOLD) hall_sensor_state = hall_increasing; - } - else if(int(hall_sample - last_hall_sample) < 0) - { - hall_increase_count = 0; - if(hall_decrease_count++ > HALLTHRESHOLD) hall_sensor_state = hall_decreasing; - } + hall_decrease_count = 0; + if (hall_increase_count++ > HALLIDLETHRESHOLD) + hall_sensor_state = hall_increasing; } - break; - case hall_decreasing: + else if (int(hall_sample - last_hall_sample) < HALLTHRESHOLD) { - + hall_increase_count = 0; + if (hall_decrease_count++ > HALLIDLETHRESHOLD) + hall_sensor_state = hall_decreasing; } - break; - case hall_tipover: + } + break; + case hall_decreasing: + { + hall_is_Idle = false; + if (int(hall_sample - last_hall_sample) > HALLIDLETHRESHOLD) { - + hall_decrease_count = 0; + log_i("goto hall_increasing"); + if (hall_increase_count++ > HALLIDLETHRESHOLD) + hall_sensor_state = hall_increasing; } - break; - case hall_increasing: + else if (int(hall_sample - last_hall_sample) < HALLTHRESHOLD) { - + hall_increase_count = 0; + if(hall_decrease_count < HALLIDLETHRESHOLD) hall_decrease_count++; } - break; - default: + else if (hall_decrease_count == 0) { - + hall_sensor_state = hall_tipover; + log_i("go to tipover"); } - break; + else + { + hall_decrease_count--; + } + } + break; + case hall_tipover: + { + // check if samples increase + // check if samples decrease + // check if samples are steady + } + break; + case hall_increasing: + { + } + break; + default: + { + log_d("state: default"); + } + break; } + log_d("state: %s (sample: %d), countup(%d), countdown(%d)", + getHallSensorStateStr(hall_sensor_state).c_str(), + hall_sample, + hall_increase_count, + hall_decrease_count); - // bool skipfirstSample = false; // if (!last_hall_Delta) // { diff --git a/FW/leo_muziekdoos_esp32/src/sensor.h b/FW/leo_muziekdoos_esp32/src/sensor.h index 28b3245..22b70fc 100644 --- a/FW/leo_muziekdoos_esp32/src/sensor.h +++ b/FW/leo_muziekdoos_esp32/src/sensor.h @@ -1,17 +1,18 @@ #pragma once +#include "Arduino.h" #include "ADS1X15.h" #include "Battery.h" #include "board.h" #include "led.h" -#define ADSINTERVAL 100 -#define VBATTINTERVALL 15000 -#define VBATTMEASPRECHARGE 500 -#define LOWBATTPERIOD 30000 +#define ADSINTERVAL 100 +#define VBATTINTERVALL 15000 +#define VBATTMEASPRECHARGE 500 +#define LOWBATTPERIOD 30000 -#define HALLINTERVAL 100 -#define HALLTHRESHOLD 5 +#define HALLINTERVAL 50 +#define HALLCNTTHRESHOLD 20 #define HALLIDLETHRESHOLD 20 #define HALLIDLESAMPLES 15 #define HALLPLAYSAMPLES 24 @@ -24,6 +25,7 @@ typedef enum hall_decreasing, }HALLSENSORSTATES; + void initSensor(void); void handleBatterySensor(void); void handleHallSensor(void);