Files
muziekdoos/FW/leo_muziekdoos_esp32/src/game.cpp
2021-10-11 12:22:29 +02:00

63 lines
1.3 KiB
C++

#include "game.h"
#include "math.h"
uint32_t last_hall_read;
uint16_t last_hall_sample;
uint8_t hall_idle_count;
bool hall_is_Idle = true;
void initGame(void)
{
pinMode(HALL_INPUT, ANALOG);
//analogReadResolution(10);
analogSetAttenuation(ADC_11db);
}
void handleGame(void)
{
uint32_t timeNow = millis();
if(timeNow - last_hall_read > HALLINTERVAL)
{
uint16_t hall_sample = analogRead(HALL_INPUT);
uint16_t hall_delta = (last_hall_sample > hall_sample)? (last_hall_sample - hall_sample) : (hall_sample - last_hall_sample);
if(hall_delta > HALLIDLETHRESHOLD)
{
if(hall_idle_count > HALLIDLESAMPLES)
{
hall_is_Idle = false;
hall_idle_count = 8;
}
else
{
hall_idle_count++;
}
}
else
{
if(hall_idle_count == 0)
{
hall_is_Idle = true;
}
else
{
hall_idle_count--;
}
}
Serial.printf("HallSensor: val=%d, delta=%d, count=%d, idle=%s\n",
hall_sample,
hall_delta,
hall_idle_count,
(hall_is_Idle? "yes":"no"));
last_hall_sample = hall_sample;
last_hall_read = timeNow;
}
}
bool hallIsIdle(void)
{
return hall_is_Idle;
}