/*
 * $Id: lightlevel.c 458 2019-06-17 01:16:35Z true $
 * begin 20190505 true
 *
 * continuing the tradition of using LEDs as photodetection diodes
 */

#include "sensor/lightlevel.h"


uint8_t idata lsens_timeout;


void lsens_pulse(uint8_t cnt)
{
	// is the button being pushed?
	// configure A as push/pull to facilitate, otherwise we don't ever rise enough
	// note: yes, this means we have the potential to briefly short, but
	//       at least it is through a 200ohm resistor, we should be OK.
	LSENS_K = 1;
	LSENS_A = 1;
	LSENS_A_DOUT |= LSENS_A_PIN;
	LSENS_A_DOUT &= ~LSENS_A_PIN;
	if (!LSENS_A) {
		// yup. set as input again.
		LSENS_A_DOUT &= ~LSENS_A_PIN;
		return;
	}

	// A as push-pull again, configure K as low digital, this will turn on LED
	LSENS_A_DOUT |= LSENS_A_PIN;
	LSENS_K_DIN |= LSENS_K_PIN;
	LSENS_K = 0;

	// this is how long we'll be on for
	// note, we don't re-check for a short, so set to low values
	lsens_timeout = cnt;
}

void lsens_read()
{

}

void lsens_tick()
{
	if (lsens_timeout) {
		lsens_timeout--;
		if (!lsens_timeout) {
			// turn off LED
			LSENS_K = 1;

			// set A input, K analog
			LSENS_A_DOUT &= ~LSENS_A_PIN;
			LSENS_K_DIN &= ~LSENS_K_PIN;
		}
	}
}

