diff --git a/CAD/Leo_muziekdoos_ESP32/ESP32 Pins.xlsx b/CAD/Leo_muziekdoos_ESP32/ESP32 Pins.xlsx index c5775e2..bbc763d 100644 Binary files a/CAD/Leo_muziekdoos_ESP32/ESP32 Pins.xlsx and b/CAD/Leo_muziekdoos_ESP32/ESP32 Pins.xlsx differ diff --git a/FW/leo_muziekdoos_esp32/lib/PN532 b/FW/leo_muziekdoos_esp32/lib/PN532 index e88576e..47ec860 160000 --- a/FW/leo_muziekdoos_esp32/lib/PN532 +++ b/FW/leo_muziekdoos_esp32/lib/PN532 @@ -1 +1 @@ -Subproject commit e88576ed9339ef469dbc334cc7404fd6eab9e6eb +Subproject commit 47ec8602987d177e7326d5788be38be9a293af6c diff --git a/FW/leo_muziekdoos_esp32/platformio.ini b/FW/leo_muziekdoos_esp32/platformio.ini index fc5cec6..73d33e7 100644 --- a/FW/leo_muziekdoos_esp32/platformio.ini +++ b/FW/leo_muziekdoos_esp32/platformio.ini @@ -21,6 +21,5 @@ extra_scripts = ./littlefsbuilder.py build_flags = -DHARDWARE=2 -DCORE_DEBUG_LEVEL=4 - -DNDEF_DEBUG=1 upload_protocol = espota upload_port = muziekdoos.local diff --git a/FW/leo_muziekdoos_esp32/src/led.cpp b/FW/leo_muziekdoos_esp32/src/led.cpp index 23f465b..1ea10b5 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(LEDBRIGHTNESS); } void handleLed(void) diff --git a/FW/leo_muziekdoos_esp32/src/led.h b/FW/leo_muziekdoos_esp32/src/led.h index face1bb..0eb7dfd 100644 --- a/FW/leo_muziekdoos_esp32/src/led.h +++ b/FW/leo_muziekdoos_esp32/src/led.h @@ -8,6 +8,7 @@ #define NUM_LEDS 1 #define LEDTIMEOUT 100 #define LEDBLINKTIME 500 +#define LEDBRIGHTNESS 75 void initLed(void); void handleLed(void); diff --git a/FW/leo_muziekdoos_esp32/src/sensor.cpp b/FW/leo_muziekdoos_esp32/src/sensor.cpp index fbcdaa8..dc1bbad 100644 --- a/FW/leo_muziekdoos_esp32/src/sensor.cpp +++ b/FW/leo_muziekdoos_esp32/src/sensor.cpp @@ -74,15 +74,15 @@ bool CheckBattery(void) BatteryVoltage = battery.voltage(); BatterySensor = battery.level(BatteryVoltage); digitalWrite(MEAS_EN, HIGH); - log_i("read batt, ticks=%d, raw=%4.2f, vbatt=%d, level=%d", battticks, vbattraw, BatteryVoltage, BatterySensor); + log_v("read batt, ticks=%d, raw=%4.2f, vbatt=%d, level=%d", battticks, vbattraw, BatteryVoltage, BatterySensor); 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; @@ -118,106 +118,193 @@ void handleBatterySensor(void) if (timeNow - lastVbatt > VBATTINTERVALL) { 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); + log_i("vbatt voltage = %d mv", BatteryVoltage); + 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; +uint32_t hall_timeout = 0; +uint32_t hall_stateTimer = 0; +HALLSENSORSTATES previous_hall_state = hall_sensor_state; + +#define HALLSTATETIMEOUT 300 +#define HALLTIMEOUT 1000 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); + int hall_delta = hall_sample - last_hall_sample; - switch(hall_sensor_state) + switch (hall_sensor_state) { - case hall_idle: + case hall_idle: + { + if (hall_delta > HALLTHRESHOLD) { - if(int(hall_sample - last_hall_sample) > 0) + hall_decrease_count = 0; + if (hall_increase_count++ > HALLTHRESHOLD) { - 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_sensor_state = hall_increasing; + log_i("next state = increasing"); } } - break; - case hall_decreasing: + else if (hall_delta < HALLTHRESHOLD) { + hall_increase_count = 0; + if (hall_decrease_count++ > HALLTHRESHOLD) + { + hall_sensor_state = hall_decreasing; + log_i("next state = decreasing"); + } + } + else + { + /* stay idle */ + } + } + break; + case hall_decreasing: + { + // lets see if we are bottoming out or stalling + if (hall_delta < HALLIDLETHRESHOLD) + { + // reset timer + hall_timeout = timeNow; + hall_stateTimer = timeNow; + } - } - break; - case hall_tipover: + if (timeNow - hall_stateTimer > HALLSTATETIMEOUT) { + // we are stalling or tipping under + hall_sensor_state = hall_tipunder; + hall_stateTimer = 0; + } + } + break; + case hall_tipunder: + { + // check if we are stalling or moving into the increasing state + if (!(hall_delta > HALLIDLETHRESHOLD)) + { + hall_stateTimer = timeNow; + } - } - break; - case hall_increasing: + if (timeNow - hall_stateTimer > HALLSTATETIMEOUT) { - + // samples detected to the upside, move to increasing state + hall_sensor_state = hall_increasing; + hall_stateTimer = 0; } - break; - default: - { + if(timeNow - hall_timeout > HALLTIMEOUT) + { + //timeed out, move to idle + hall_sensor_state = hall_idle; + hall_stateTimer = 0; + hall_timeout = 0; } - break; + } + break; + case hall_increasing: + { + // check if we are increasing + if (hall_delta > HALLIDLETHRESHOLD) + { + hall_stateTimer = timeNow; + } + + if (timeNow - hall_stateTimer > HALLSTATETIMEOUT) + { + // samples detected to the upside, move to increasing state + hall_sensor_state = hall_tipover; + hall_stateTimer = 0; + } + } + break; + case hall_tipover: + { + // check if we are stalling or moving into the increasing state + if (!(hall_delta > HALLIDLETHRESHOLD)) + { + hall_stateTimer = timeNow; + } + + if (timeNow - hall_stateTimer > HALLSTATETIMEOUT) + { + // samples detected to the upside, move to increasing state + hall_sensor_state = hall_decreasing; + hall_stateTimer = 0; + } + + if(timeNow - hall_timeout > HALLTIMEOUT) + { + //timeed out, move to idle + hall_sensor_state = hall_idle; + hall_stateTimer = 0; + hall_timeout = 0; + } + } + break; + default: + { + } + break; } - - // bool skipfirstSample = false; - // if (!last_hall_Delta) - // { - // skipfirstSample = true; - // } - // uint16_t hall_delta = (last_hall_sample > hall_sample) ? (last_hall_sample - hall_sample) : (hall_sample - last_hall_sample); - // hall_delta = (hall_delta + last_hall_Delta) / 2; - // last_hall_Delta = hall_delta; - // if (skipfirstSample) - // { - // log_v("First sample skipped"); - // if (hall_idle_count) - // { - // hall_idle_count--; - // } - // return; - // } - // if (hall_delta > HALLIDLETHRESHOLD) - // { - // if (hall_idle_count > HALLIDLESAMPLES) - // { - // hall_is_Idle = false; - // hall_idle_count = HALLPLAYSAMPLES; - // log_i("Game: playing, delta = %d", hall_delta); - // } - // else - // { - // hall_idle_count++; - // } - // } - // else - // { - // if (hall_idle_count == 0) - // { - // hall_is_Idle = true; - // } - // else - // { - // hall_idle_count--; - // } - // } - log_v("HallSensor: val=%d, delta=%d, count=%d, idle=%s\n", + bool skipfirstSample = false; + if (!last_hall_Delta) + { + skipfirstSample = true; + } + uint16_t hall_delta = (last_hall_sample > hall_sample) ? (last_hall_sample - hall_sample) : (hall_sample - last_hall_sample); + hall_delta = (hall_delta + last_hall_Delta) / 2; + last_hall_Delta = hall_delta; + if (skipfirstSample) + { + log_v("First sample skipped"); + if (hall_idle_count) + { + hall_idle_count--; + } + return; + } + if (hall_delta > HALLIDLETHRESHOLD) + { + if (hall_idle_count > HALLIDLESAMPLES) + { + hall_is_Idle = false; + hall_idle_count = HALLPLAYSAMPLES; + log_i("Game: playing, delta = %d", hall_delta); + } + else + { + hall_idle_count++; + } + } + else + { + if (hall_idle_count == 0) + { + hall_is_Idle = true; + } + else + { + hall_idle_count--; + } + } + log_i("HallSensor: val=%d, delta=%d, count=%d, idle=%s\n", hall_sample, hall_delta, hall_idle_count, diff --git a/FW/leo_muziekdoos_esp32/src/sensor.h b/FW/leo_muziekdoos_esp32/src/sensor.h index 28b3245..deccbf0 100644 --- a/FW/leo_muziekdoos_esp32/src/sensor.h +++ b/FW/leo_muziekdoos_esp32/src/sensor.h @@ -12,7 +12,7 @@ #define HALLINTERVAL 100 #define HALLTHRESHOLD 5 -#define HALLIDLETHRESHOLD 20 +#define HALLIDLETHRESHOLD 10 #define HALLIDLESAMPLES 15 #define HALLPLAYSAMPLES 24 @@ -22,8 +22,19 @@ typedef enum hall_increasing, hall_tipover, hall_decreasing, + hall_tipunder, + HALL_LAST }HALLSENSORSTATES; +static String HALLSENESORSTATES_ENUM2STR[HALL_LAST] +{ + "Hall Idle", + "Hall Increasing", + "Hall Tipover", + "Hall decreasing", + "Hall tipUnder" +}; + void initSensor(void); void handleBatterySensor(void); void handleHallSensor(void);