#pragma once

#include <NewPing.h>
#include <Screen.h>

// define pins
#define TRIGGER_PIN 12
#define ECHO_PIN 11

// define max distance so it doesnt go infinite
#define MAX_DISTANCE 400
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

namespace DistanceSensor{
    unsigned long previousMillis = 0;
    unsigned long timerInterval = 500;

    float stdDistance = 180;

    void handle(SimpleSerialProtocol *endpoint);
}

void DistanceSensor::handle(SimpleSerialProtocol *endpoint){
    if ((millis() - previousMillis) > timerInterval){
        previousMillis = millis();

        // Send ping, get distance in cm
        float distance = sonar.ping_cm();

        // Send results to Serial Monitor
        if(distance < 30 - 20){
            endpoint->sendPacket("access", "update_state", "bool;motion;true");
        }else{
            endpoint->sendPacket("access", "update_state", "bool;motion;false");
        }
        
    }
}