clean-up songs

This commit is contained in:
2022-01-05 14:52:45 +01:00
parent d27b693db1
commit b0ed6e52ca
10 changed files with 147 additions and 68 deletions

View File

@@ -29,7 +29,7 @@
#define LED_PIN 12 #define LED_PIN 12
#define VBATTMIN 3000 //before lowbatt cutoff #define VBATTMIN 3200 //before lowbatt cutoff
#define VBATTMAX 4180 #define VBATTMAX 4180
#define VBATTREF 3300 #define VBATTREF 3300
#define R12 4.7 #define R12 4.7

View File

@@ -88,6 +88,8 @@ void handleGame(void)
log_i("activeState = startPlaying"); log_i("activeState = startPlaying");
newState = false; newState = false;
setRFIDscanState(false); setRFIDscanState(false);
PowerKeepAlive();
} }
if (hallIsIdle()) if (hallIsIdle())

View File

@@ -6,6 +6,7 @@
#include "audio.h" #include "audio.h"
#include "rfid.h" #include "rfid.h"
#include "config.h" #include "config.h"
#include "power.h"
#define TIMEOUT_IDLE 20000 #define TIMEOUT_IDLE 20000

View File

@@ -4,7 +4,7 @@ bool powerbutton_released = true;
uint32_t PowerLastKeepAlive = 0; uint32_t PowerLastKeepAlive = 0;
uint32_t PowerOtaLongPressTime = 0; uint32_t PowerOtaLongPressTime = 0;
uint64_t measure_timer = 0; uint32_t powerstate_timer = 0;
POWERSTATES powerstate = off; POWERSTATES powerstate = off;
POWERSTATES lastState = off; POWERSTATES lastState = off;
@@ -124,16 +124,22 @@ void handlePowerState(void)
if (buttonPower.pressedFor(100)) if (buttonPower.pressedFor(100))
{ {
lastState = on; lastState = on;
powerstate = poweringOff; powerstate = POWERSTATES::poweringOff;
break; break;
} }
if (getLowBatt()) if (getLowBatt())
{ {
log_w("on: Lowbat"); log_w("on: Lowbat");
powerstate = lowBatt; powerstate_timer = millis();
powerstate = POWERSTATES::lowBatt;
SetLedColor(CRGB::Red, true); SetLedColor(CRGB::Red, true);
break; break;
} }
if(millis() - PowerLastKeepAlive > TIMEOUT_POWER)
{
powerstate_timer = millis();
powerstate = POWERSTATES::timeOut;
}
} }
break; break;
case poweringOff: case poweringOff:
@@ -146,16 +152,6 @@ void handlePowerState(void)
log_w("poweringoff: 3/3 ==> powerOff"); log_w("poweringoff: 3/3 ==> powerOff");
} }
else if (buttonPower.pressedFor(500))
{
log_w("poweringoff: 2/3");
SetLedColor(CRGB::Orange, true);
}
else if (buttonPower.pressedFor(200))
{
log_w("poweringoff: 1/3");
SetLedColor(CRGB::Brown, true);
}
else else
{ {
powerstate = lastState; powerstate = lastState;
@@ -174,17 +170,16 @@ void handlePowerState(void)
break; break;
case timeOut: case timeOut:
{ {
log_w("timeout ==> off"); log_w("timeout");
powerstate = off; SetLedColor(CRGB::Yellow, true);
SetLedColor(CRGB::Red, true); if(millis() - powerstate_timer > POWEROFFOFFDELAY)
powerOff(); {
delay(5000); powerstate = off;
}
} }
break; break;
case lowBatt: case lowBatt:
{ {
// add delay
//powerstate = off;
if(!getLowBatt()) if(!getLowBatt())
{ {
powerstate = on; powerstate = on;
@@ -194,6 +189,12 @@ void handlePowerState(void)
{ {
SetLedColor(CRGB::Red, true); SetLedColor(CRGB::Red, true);
} }
// add delay
if(millis() - powerstate_timer > POWEROFFOFFDELAY)
{
powerstate = off;
}
} }
break; break;
case overTheAir: case overTheAir:

View File

@@ -9,10 +9,11 @@
#include "led.h" #include "led.h"
#define TIMEOUT_POWER (5 * 1000 * 60) //5minutes timeout #define TIMEOUT_POWER (7 * 1000 * 60) // 7 minutes timeout
#define POWERBUTTONDELAY 1000 #define POWERBUTTONDELAY 1000
#define BATTERYMEASUREDELAY 60000 //#define BATTERYMEASUREDELAY 60000
#define POWERBUTTONOTADELAY 7000 #define POWERBUTTONOTADELAY 7000
#define POWEROFFOFFDELAY 3000
typedef enum typedef enum
{ {
@@ -30,6 +31,7 @@ typedef enum
} POWERSTATES; } POWERSTATES;
POWERSTATES getPowerState( void ); POWERSTATES getPowerState( void );
void PowerKeepAlive(void);
void initBattery(void); void initBattery(void);
bool handleBattery(void); bool handleBattery(void);

View File

@@ -9,12 +9,16 @@ bool batteryLow = false;
uint16_t BatterySensor = 0; uint16_t BatterySensor = 0;
uint16_t BatteryVoltage = 0; uint16_t BatteryVoltage = 0;
uint32_t BatteryWarningFirst = 0;
uint16_t HallSensor = 0; uint16_t HallSensor = 0;
uint32_t last_hall_read = 0; uint32_t last_hall_read = 0;
uint16_t last_hall_sample = 0; uint16_t last_hall_sample = 0;
uint16_t last_hall_Delta = 0; uint16_t last_hall_Delta = 0;
uint8_t hall_idle_count = 0; uint8_t hall_idle_count = 0;
uint8_t hall_decrease_count = 0;
uint8_t hall_increase_count = 0;
bool hall_is_Idle = true; bool hall_is_Idle = true;
bool hallinitOK = false; bool hallinitOK = false;
@@ -44,7 +48,7 @@ bool hallIsIdle(void)
void initBattery(void) void initBattery(void)
{ {
battery.onDemand(MEAS_EN, LOW); battery.onDemand(MEAS_EN, LOW);
battery.begin(VBATTREF, (R12 + R13) / R13); // R1 = 220K, R2 = 100K, factor = (R1+R2)/R2 battery.begin(VBATTREF, (R12 + R13) / R13, &sigmoidal); // R1 = 220K, R2 = 100K, factor = (R1+R2)/R2
} }
void initSensor(void) void initSensor(void)
@@ -73,8 +77,24 @@ bool CheckBattery(void)
log_i("read batt, ticks=%d, raw=%4.2f, vbatt=%d, level=%d", battticks, vbattraw, BatteryVoltage, BatterySensor); log_i("read batt, ticks=%d, raw=%4.2f, vbatt=%d, level=%d", battticks, vbattraw, BatteryVoltage, BatterySensor);
if (BatteryVoltage < VBATTMIN) if (BatteryVoltage < VBATTMIN)
{ {
batteryLow = true; uint32_t timeNow = millis();
return true; if(BatteryWarningFirst == 0)
{
BatteryWarningFirst = timeNow;
}
if(timeNow - BatteryWarningFirst > LOWBATTPERIOD)
{
batteryLow = true;
return true;
}
else
{
SetLedColor(CRGB::Orange, true);
}
}
else
{
BatteryWarningFirst = 0;
} }
batteryLow = false; batteryLow = false;
return false; return false;
@@ -106,53 +126,97 @@ void handleBatterySensor(void)
} }
} }
HALLSENSORSTATES hall_sensor_state = HALLSENSORSTATES::hall_idle;
void handleHallSensor(void) void handleHallSensor(void)
{ {
uint32_t timeNow = millis(); uint32_t timeNow = millis();
if (timeNow - last_hall_read > HALLINTERVAL) if (timeNow - last_hall_read > HALLINTERVAL)
{ {
//get sample
uint16_t hall_sample = ADS.readADC(HALL_INPUT); uint16_t hall_sample = ADS.readADC(HALL_INPUT);
bool skipfirstSample = false;
if (!last_hall_Delta) switch(hall_sensor_state)
{ {
skipfirstSample = true; case hall_idle:
} {
uint16_t hall_delta = (last_hall_sample > hall_sample) ? (last_hall_sample - hall_sample) : (hall_sample - last_hall_sample); if(int(hall_sample - last_hall_sample) > 0)
hall_delta = (hall_delta + last_hall_Delta) / 2; {
last_hall_Delta = hall_delta; hall_decrease_count = 0;
if (skipfirstSample) if(hall_increase_count++ > HALLTHRESHOLD) hall_sensor_state = hall_increasing;
{ }
log_v("First sample skipped"); else if(int(hall_sample - last_hall_sample) < 0)
if (hall_idle_count) {
{ hall_increase_count = 0;
hall_idle_count--; if(hall_decrease_count++ > HALLTHRESHOLD) hall_sensor_state = hall_decreasing;
} }
return; }
} break;
if (hall_delta > HALLIDLETHRESHOLD) case hall_decreasing:
{ {
if (hall_idle_count > HALLIDLESAMPLES)
{ }
hall_is_Idle = false; break;
hall_idle_count = HALLPLAYSAMPLES; case hall_tipover:
log_i("Game: playing, delta = %d", hall_delta); {
}
else }
{ break;
hall_idle_count++; case hall_increasing:
} {
}
else }
{ break;
if (hall_idle_count == 0) default:
{ {
hall_is_Idle = true;
} }
else break;
{
hall_idle_count--;
}
} }
// 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", log_v("HallSensor: val=%d, delta=%d, count=%d, idle=%s\n",
hall_sample, hall_sample,
hall_delta, hall_delta,

View File

@@ -7,13 +7,22 @@
#define ADSINTERVAL 100 #define ADSINTERVAL 100
#define VBATTINTERVALL 15000 #define VBATTINTERVALL 15000
#define VBATTMEASPRECHARGE 10 #define VBATTMEASPRECHARGE 500
#define LOWBATTPERIOD 30000
#define HALLINTERVAL 100 #define HALLINTERVAL 100
#define HALLTHRESHOLD 5
#define HALLIDLETHRESHOLD 20 #define HALLIDLETHRESHOLD 20
#define HALLIDLESAMPLES 15 #define HALLIDLESAMPLES 15
#define HALLPLAYSAMPLES 24 #define HALLPLAYSAMPLES 24
typedef enum
{
hall_idle,
hall_increasing,
hall_tipover,
hall_decreasing,
}HALLSENSORSTATES;
void initSensor(void); void initSensor(void);
void handleBatterySensor(void); void handleBatterySensor(void);