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->SetGain(AUDIOGAIN);
pinMode(DAC_SDMODE, OUTPUT);
digitalWrite(DAC_SDMODE, HIGH);
setAudioState(false);
mp3 = new AudioGeneratorMP3();
audioInitOk = true;
log_i("init Audio Done");
playSong("/Billy-Jean.mp3");
}
void setAudioState(bool state)

View File

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

View File

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

View File

@@ -6,6 +6,7 @@ bool ledstate = false;
bool blinkState = false;
CRGB ledcolor = CRGB::Black;
uint32_t lastLedTime = 0;
uint32_t lastBlinkTime = 0;
void setLedBlink(bool blink)
{
@@ -36,7 +37,7 @@ void handleLed(void)
uint32_t timeNow = millis();
if (timeNow - lastLedTime > LEDTIMEOUT)
{
if (blinkState)
if (blinkState && (timeNow - lastBlinkTime > LEDBLINKTIME))
{
if (!ledstate)
{
@@ -47,46 +48,12 @@ void handleLed(void)
leds[0] = ledcolor;
}
ledstate = !ledstate;
lastBlinkTime = timeNow;
}
else
{
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();
lastLedTime = timeNow;
}

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,39 +1,87 @@
#include "sensor.h"
ADS1115 ADS(0x48);
Battery battery(VBATTMIN, VBATTMAX, MEAS_ADC, &getvbatt);
uint32_t lastADS = 0;
uint32_t lastVbatt = 0;
bool batteryLow = false;
uint16_t BatterySensor = 0;
uint16_t HallSensor = 0;
uint32_t last_hall_read=0;
uint16_t last_hall_sample=0;
uint16_t last_hall_Delta=0;
uint8_t hall_idle_count=0;
uint32_t last_hall_read = 0;
uint16_t last_hall_sample = 0;
uint16_t last_hall_Delta = 0;
uint8_t hall_idle_count = 0;
bool hall_is_Idle = true;
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)
{
log_i("sensor init ADS1x15:");
bool result = ADS.begin(I2C_SDA, I2C_SCL);
pinMode(MEAS_EN, OUTPUT);
initBattery();
if (!result)
{
log_e("sensor init: FAIL");
}
else
{
log_i("sensor init: OK");
hallinitOK = true;
log_i("sensor init: OK");
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)
@@ -48,51 +96,37 @@ void handleSensor(void)
if (timeNow - lastVbatt > VBATTINTERVALL)
{
BatterySensor = ADS.readADC(MEAS_ADC);
digitalWrite(MEAS_EN, HIGH);
log_i("read vbatt %4.2f",ADS.toVoltage(BatterySensor));
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);
lastVbatt = timeNow;
}
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)
{
uint32_t timeNow = millis();
if (timeNow - last_hall_read > HALLINTERVAL)
{
uint16_t hall_sample = ADS.readADC(HALL_INPUT);
bool skipfirstSample = false;
if(!last_hall_Delta)
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;
hall_delta = (hall_delta + last_hall_Delta) / 2;
last_hall_Delta = hall_delta;
if(skipfirstSample)
if (skipfirstSample)
{
log_v("First sample skipped");
if(hall_idle_count)
if (hall_idle_count)
{
hall_idle_count --;
hall_idle_count--;
}
return;
}
@@ -102,7 +136,7 @@ void handleHallSensor(void)
{
hall_is_Idle = false;
hall_idle_count = HALLPLAYSAMPLES;
log_i("Game: playing, delta = %d",hall_delta);
log_i("Game: playing, delta = %d", hall_delta);
}
else
{

View File

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