#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) { printlnI("Game: init"); pinMode(HALL_INPUT, ANALOG); //analogReadResolution(10); analogSetAttenuation(ADC_11db); printlnI("Game: init: done"); } 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 = HALLPLAYSAMPLES; } else { hall_idle_count++; } } else { if (hall_idle_count == 0) { hall_is_Idle = true; printlnI("Game: Idle"); } 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; }