This commit is contained in:
2022-01-04 15:50:20 +01:00
parent ec47ddc402
commit 14b25a9459
10 changed files with 125 additions and 134 deletions

View File

@@ -95,11 +95,10 @@ void initAudio()
out->SetPinout(I2S_BCLK, I2S_WCLK, I2S_DATA); // bclk, wclk, data out->SetPinout(I2S_BCLK, I2S_WCLK, I2S_DATA); // bclk, wclk, data
out->SetGain(AUDIOGAIN); out->SetGain(AUDIOGAIN);
pinMode(DAC_SDMODE, OUTPUT); pinMode(DAC_SDMODE, OUTPUT);
digitalWrite(DAC_SDMODE, HIGH); setAudioState(false);
mp3 = new AudioGeneratorMP3(); mp3 = new AudioGeneratorMP3();
audioInitOk = true; audioInitOk = true;
log_i("init Audio Done"); log_i("init Audio Done");
playSong("/Billy-Jean.mp3");
} }
void setAudioState(bool state) void setAudioState(bool state)

View File

@@ -26,9 +26,10 @@
#define HALL_INPUT 0 // ADC1_CH0 #define HALL_INPUT 0 // ADC1_CH0
#define LED_PIN 12 #define LED_PIN 12
#define VBATTMIN 3200 #define VBATTMIN 3600
#define VBATTMAX 4180 #define VBATTMAX 4180
#define VBATTREF 3300 #define VBATTREF 3300
#define R12 4.7 #define R12 4.7

View File

@@ -38,7 +38,6 @@ void handleGame(void)
break; break;
case stateIdle: case stateIdle:
{ {
uint32_t timeNow = millis();
if (newState) if (newState)
{ {
log_i("activeState = Idle"); log_i("activeState = Idle");

View File

@@ -6,6 +6,7 @@ bool ledstate = false;
bool blinkState = false; bool blinkState = false;
CRGB ledcolor = CRGB::Black; CRGB ledcolor = CRGB::Black;
uint32_t lastLedTime = 0; uint32_t lastLedTime = 0;
uint32_t lastBlinkTime = 0;
void setLedBlink(bool blink) void setLedBlink(bool blink)
{ {
@@ -36,7 +37,7 @@ void handleLed(void)
uint32_t timeNow = millis(); uint32_t timeNow = millis();
if (timeNow - lastLedTime > LEDTIMEOUT) if (timeNow - lastLedTime > LEDTIMEOUT)
{ {
if (blinkState) if (blinkState && (timeNow - lastBlinkTime > LEDBLINKTIME))
{ {
if (!ledstate) if (!ledstate)
{ {
@@ -47,46 +48,12 @@ void handleLed(void)
leds[0] = ledcolor; leds[0] = ledcolor;
} }
ledstate = !ledstate; ledstate = !ledstate;
lastBlinkTime = timeNow;
} }
else else
{ {
leds[0] = ledcolor; leds[0] = ledcolor;
} }
// if (ledstate)
// {
// if (getPowerState() == POWERSTATES::on)
// {
// if(getAudioState())
// {
// leds[0] = CRGB::Purple;
// }
// else if( getRFIDlastUID() != "")
// {
// leds[0] = CRGB::Yellow;
// }
// else
// {
// leds[0] = CRGB::Green;
// }
// }
// else if (getPowerState() == POWERSTATES::poweringOn2)
// {
// leds[0] = CRGB::Blue;
// }
// else if (getPowerState() == POWERSTATES::poweringOff2)
// {
// leds[0] = CRGB::Orange;
// }
// else
// {
// leds[0] = CRGB::Red;
// }
// }
// else
// {
// leds[0] = CRGB::Black;
// }
FastLED.show(); FastLED.show();
lastLedTime = timeNow; lastLedTime = timeNow;
} }

View File

@@ -6,7 +6,8 @@
#include "rfid.h" #include "rfid.h"
#define NUM_LEDS 1 #define NUM_LEDS 1
#define LEDTIMEOUT 500 #define LEDTIMEOUT 100
#define LEDBLINKTIME 500
void initLed(void); void initLed(void);
void handleLed(void); void handleLed(void);

View File

@@ -66,7 +66,7 @@ bool OtaProcess_class::initialize(void)
{ {
log_i("Otastate = initialize(connect)"); log_i("Otastate = initialize(connect)");
uint32_t timeTemp = millis(); uint32_t timeTemp = millis();
if (m_lastconnectTime - timeTemp > WIFICONNECTINTERVAL) if (timeTemp - m_lastconnectTime > WIFICONNECTINTERVAL)
{ {
if (WiFi.status() != WL_CONNECTED) if (WiFi.status() != WL_CONNECTED)
{ {

View File

@@ -12,8 +12,6 @@ POWERSTATES lastState = off;
Button buttonPower(PWR_BTN, 250UL, 1U, 0); Button buttonPower(PWR_BTN, 250UL, 1U, 0);
extern OtaProcess_class ota; extern OtaProcess_class ota;
Battery battery(VBATTMIN, VBATTMAX, MEAS_ADC, &getvbatt);
void initPowerOn(void) void initPowerOn(void)
{ {
pinMode(PWR_HOLD, OUTPUT); pinMode(PWR_HOLD, OUTPUT);
@@ -21,12 +19,6 @@ void initPowerOn(void)
buttonPower.begin(); buttonPower.begin();
} }
void initBattery(void)
{
battery.onDemand(MEAS_EN, LOW);
battery.begin(VBATTREF, (R12 + R13) / R13); // R1 = 220K, R2 = 100K, factor = (R1+R2)/R2
}
POWERSTATES getPowerState(void) POWERSTATES getPowerState(void)
{ {
return powerstate; return powerstate;
@@ -40,6 +32,7 @@ void PowerKeepAlive(void)
void powerOn(void) void powerOn(void)
{ {
digitalWrite(PWR_HOLD, HIGH); digitalWrite(PWR_HOLD, HIGH);
delay(200);
} }
void powerOff(void) void powerOff(void)
@@ -51,27 +44,6 @@ void powerOff(void)
// ESP.restart(); // ESP.restart();
} }
bool measureBattery(void)
{
uint16_t vbatt = battery.voltage();
if (vbatt < VBATTMIN)
{
return true;
}
return false;
}
bool handleBattery(void)
{
uint64_t currentmillis = millis();
if (currentmillis - measure_timer > BATTERYMEASUREDELAY)
{
return measureBattery();
measure_timer = currentmillis;
}
return false;
}
// handle power asynchronious // handle power asynchronious
void handlePowerState(void) void handlePowerState(void)
{ {
@@ -94,7 +66,7 @@ void handlePowerState(void)
if (buttonPower.pressedFor(POWERBUTTONDELAY)) if (buttonPower.pressedFor(POWERBUTTONDELAY))
{ {
powerstate = poweringOn2; powerstate = poweringOn2;
SetLedColor(CRGB::White); SetLedColor(CRGB::Green, true);
log_i("poweron 3/3 => Go"); log_i("poweron 3/3 => Go");
} }
@@ -109,7 +81,7 @@ void handlePowerState(void)
SetLedColor(CRGB::GhostWhite); SetLedColor(CRGB::GhostWhite);
} }
if (!buttonread) if (buttonPower.releasedFor(200))
{ {
powerstate = off; powerstate = off;
} }
@@ -117,15 +89,17 @@ void handlePowerState(void)
break; break;
case poweringOn2: case poweringOn2:
{ {
powerOn();
if (buttonPower.releasedFor(200)) if (buttonPower.releasedFor(200))
{ {
powerstate = powerinit; powerstate = powerinit;
powerOn(); if (CheckBattery())
// if (measureBattery()) {
// { log_w("poweringOn: Lowbat");
// log_w("poweringOn: Lowbat"); SetLedColor(CRGB::Red, true);
// //powerstate = lowBatt;
// } // powerstate = lowBatt;
}
} }
else else
{ {
@@ -153,12 +127,13 @@ void handlePowerState(void)
powerstate = poweringOff; powerstate = poweringOff;
break; break;
} }
// if (handleBattery()) if (getLowBatt())
// { {
// log_w("on: Lowbat"); log_w("on: Lowbat");
// //powerstate = lowBatt; // powerstate = lowBatt;
// break; SetLedColor(CRGB::Red, true);
// } break;
}
} }
break; break;
case poweringOff: case poweringOff:
@@ -184,12 +159,13 @@ void handlePowerState(void)
else else
{ {
powerstate = lastState; powerstate = lastState;
SetLedColor(CRGB::Green);
} }
} }
break; break;
case poweringOff2: case poweringOff2:
{ {
if (!buttonPower.releasedFor(200)) if (buttonPower.releasedFor(200))
{ {
powerstate = off; powerstate = off;
SetLedColor(CRGB::Red, true); SetLedColor(CRGB::Red, true);
@@ -200,50 +176,58 @@ void handlePowerState(void)
{ {
log_w("timeout ==> off"); log_w("timeout ==> off");
powerstate = off; powerstate = off;
SetLedColor(CRGB::Red); SetLedColor(CRGB::Red, true);
powerOff(); powerOff();
delay(5000); delay(5000);
} }
break; break;
case lowBatt: case lowBatt:
{ {
// add delay
powerstate = off; powerstate = off;
for (int i = 0; i < 6; i++) for (int i = 0; i < 6; i++)
{ {
log_w("lowbatt"); log_w("lowbatt");
SetLedColor(CRGB::Red, true);
} }
} }
break; break;
case overTheAir: case overTheAir:
{ {
powerOn();
if (buttonPower.releasedFor(200)) if (buttonPower.releasedFor(200))
{ {
powerstate = overTheAir2; powerstate = overTheAir2;
otaEnable(); otaEnable();
SetLedColor(CRGB::Blue, true); SetLedColor(CRGB::Blue);
powerOn();
} }
else else
{ {
SetLedColor(CRGB::Blue, false); SetLedColor(CRGB::Blue, true);
log_i("ota state active, release powerbutton"); log_i("ota state active, release powerbutton");
} }
} }
break; break;
case overTheAir2: case overTheAir2:
{ {
if(buttonPower.pressedFor(200)) if (buttonPower.pressedFor(200))
{ {
log_i("ota poweroff"); log_i("ota poweroff");
lastState = overTheAir2; lastState = overTheAir2;
powerstate = poweringOff; powerstate = poweringOff;
} }
if (getOtaState() != otaInitDone)
{
SetLedColor(CRGB::Orange, true);
}
else
{
SetLedColor(CRGB::Orange);
}
if (getOtaState() == OTASTATES::otaBusy) if (getOtaState() == OTASTATES::otaBusy)
{ {
SetLedColor(CRGB::Blue, true); SetLedColor(CRGB::Purple, true);
log_i("ota state active, ota busy ==> On"); log_i("ota state active, ota busy ==> On");
} }
if (getOtaState() == OTASTATES::otaDone) if (getOtaState() == OTASTATES::otaDone)
{ {

View File

@@ -3,7 +3,6 @@
#include "board.h" #include "board.h"
#include "Arduino.h" #include "Arduino.h"
#include "JC_Button.h" #include "JC_Button.h"
#include "Battery.h"
#include "sensor.h" #include "sensor.h"
#include "audio.h" #include "audio.h"
#include "ota.h" #include "ota.h"

View File

@@ -1,39 +1,87 @@
#include "sensor.h" #include "sensor.h"
ADS1115 ADS(0x48); ADS1115 ADS(0x48);
Battery battery(VBATTMIN, VBATTMAX, MEAS_ADC, &getvbatt);
uint32_t lastADS = 0; uint32_t lastADS = 0;
uint32_t lastVbatt = 0; uint32_t lastVbatt = 0;
bool batteryLow = false;
uint16_t BatterySensor = 0; uint16_t BatterySensor = 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;
bool hall_is_Idle = true; bool hall_is_Idle = true;
bool hallinitOK = false; bool hallinitOK = false;
bool getSensorInitStatus(void)
{
return hallinitOK;
}
uint16_t getHall(void)
{
return HallSensor;
}
uint16_t getvbatt(uint8_t dummy = 0)
{
int16_t readbatt = ADS.readADC(MEAS_ADC);
log_i("readvbat=%d", readbatt);
return readbatt;
}
bool hallIsIdle(void)
{
return hall_is_Idle;
}
void initBattery(void)
{
battery.onDemand(MEAS_EN, LOW);
battery.begin(VBATTREF, (R12 + R13) / R13); // R1 = 220K, R2 = 100K, factor = (R1+R2)/R2
}
void initSensor(void) void initSensor(void)
{ {
log_i("sensor init ADS1x15:"); log_i("sensor init ADS1x15:");
bool result = ADS.begin(I2C_SDA, I2C_SCL); bool result = ADS.begin(I2C_SDA, I2C_SCL);
pinMode(MEAS_EN, OUTPUT); initBattery();
if (!result) if (!result)
{ {
log_e("sensor init: FAIL"); log_e("sensor init: FAIL");
} }
else else
{ {
log_i("sensor init: OK"); log_i("sensor init: OK");
hallinitOK = true; hallinitOK = true;
} }
} }
bool getSensorInitStatus(void) bool CheckBattery(void)
{ {
return hallinitOK; int16_t battticks = ADS.readADC(MEAS_ADC);
float vbattraw = ADS.toVoltage(battticks);
uint16_t vbatt = battery.voltage(vbattraw * 1000);
BatterySensor = battery.level(vbatt);
digitalWrite(MEAS_EN, HIGH);
log_i("read batt, ticks=%d, raw=%4.2f, vbatt=%d, level=%d", battticks, vbattraw, vbatt, BatterySensor);
if (vbatt < VBATTMIN)
{
return true;
batteryLow = true;
}
batteryLow = false;
return false;
}
bool getLowBatt(void)
{
return batteryLow;
} }
void handleSensor(void) void handleSensor(void)
@@ -48,31 +96,17 @@ void handleSensor(void)
if (timeNow - lastVbatt > VBATTINTERVALL) if (timeNow - lastVbatt > VBATTINTERVALL)
{ {
BatterySensor = ADS.readADC(MEAS_ADC); CheckBattery();
digitalWrite(MEAS_EN, HIGH); log_i("vbatt level = %d %%", BatterySensor);
log_i("read vbatt %4.2f",ADS.toVoltage(BatterySensor)); if(BatterySensor > 80) SetLedColor(CRGB::Green);
else if(BatterySensor > 50) SetLedColor(CRGB::Orange);
else if(BatterySensor > 20) SetLedColor(CRGB::Red);
lastVbatt = timeNow; lastVbatt = timeNow;
} }
log_v("Read sensor: Hall=%d, vbatt=%d", HallSensor, BatterySensor); log_v("Read sensor: Hall=%d, vbatt=%d", HallSensor, BatterySensor);
} }
uint16_t getHall(void)
{
return HallSensor;
}
uint16_t getvbatt(uint8_t dummy = 0)
{
return BatterySensor;
}
bool hallIsIdle(void)
{
return hall_is_Idle;
}
void handleHallSensor(void) void handleHallSensor(void)
{ {
uint32_t timeNow = millis(); uint32_t timeNow = millis();
@@ -80,19 +114,19 @@ void handleHallSensor(void)
{ {
uint16_t hall_sample = ADS.readADC(HALL_INPUT); uint16_t hall_sample = ADS.readADC(HALL_INPUT);
bool skipfirstSample = false; bool skipfirstSample = false;
if(!last_hall_Delta) if (!last_hall_Delta)
{ {
skipfirstSample = true; skipfirstSample = true;
} }
uint16_t hall_delta = (last_hall_sample > hall_sample) ? (last_hall_sample - hall_sample) : (hall_sample - last_hall_sample); 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; hall_delta = (hall_delta + last_hall_Delta) / 2;
last_hall_Delta = hall_delta; last_hall_Delta = hall_delta;
if(skipfirstSample) if (skipfirstSample)
{ {
log_v("First sample skipped"); log_v("First sample skipped");
if(hall_idle_count) if (hall_idle_count)
{ {
hall_idle_count --; hall_idle_count--;
} }
return; return;
} }
@@ -102,7 +136,7 @@ void handleHallSensor(void)
{ {
hall_is_Idle = false; hall_is_Idle = false;
hall_idle_count = HALLPLAYSAMPLES; hall_idle_count = HALLPLAYSAMPLES;
log_i("Game: playing, delta = %d",hall_delta); log_i("Game: playing, delta = %d", hall_delta);
} }
else else
{ {

View File

@@ -1,16 +1,18 @@
#pragma once #pragma once
#include "ADS1X15.h" #include "ADS1X15.h"
#include "Battery.h"
#include "board.h" #include "board.h"
#include "led.h"
#define ADSINTERVAL 100 #define ADSINTERVAL 100
#define VBATTINTERVALL 6000 #define VBATTINTERVALL 15000
#define VBATTMEASPRECHARGE 240 #define VBATTMEASPRECHARGE 10
#define HALLINTERVAL 100 #define HALLINTERVAL 100
#define HALLIDLETHRESHOLD 20 #define HALLIDLETHRESHOLD 20
#define HALLIDLESAMPLES 8 #define HALLIDLESAMPLES 15
#define HALLPLAYSAMPLES 16 #define HALLPLAYSAMPLES 24
void initSensor(void); void initSensor(void);
@@ -22,3 +24,8 @@ uint16_t getHall( void );
bool hallIsIdle(void); bool hallIsIdle(void);
uint16_t getvbatt(uint8_t dummy); uint16_t getvbatt(uint8_t dummy);
bool getSensorInitStatus(void); bool getSensorInitStatus(void);
bool CheckBattery(void);
bool getLowBatt(void);