LPC1768 IR breakbeam sensor

Dependencies:   mbed

infrared.cpp

Committer:
faif
Date:
2016-11-13
Revision:
1:88b1d9bcf3a3
Parent:
0:ca4cef100279

File content as of revision 1:88b1d9bcf3a3:

#include "infrared.h"

static const char cls[] = "\x1B[2J";
static const char home[] = "\x1B[H";
static const float DEBOUNCING_DELAY = 0.1;
static const float TEXT_DELAY = 0.5;
enum led_state { OFF = 0, ON = 1 };

int main()
{
    pc.printf(home);
    pc.printf(cls);
    
    while (true) 
    {
        if (!infrared) 
        {
            set_led(my_led, ON, DEBOUNCING_DELAY);
            std::string alert("Intrusion dectected: " + current_date_time());
            show_alert(alert, TEXT_DELAY);
        }
        else 
        {
            set_led(my_led, OFF, DEBOUNCING_DELAY);
        }
    }
}

void set_led(DigitalOut led, led_state state, const float delay)
{
    led = state;
    wait(delay);
}

const std::string current_date_time() 
{
    time_t rawtime;
    time(&rawtime);
    return ctime(&rawtime);
}

void show_alert(const std::string& alert, const float delay) 
{
    pc.printf(home);
    pc.printf(alert.c_str());
    pc.printf("\r");
    wait(delay);
}