#include <stdlib.h>
#include "board.h"
#include "ioutil.h"
#include "i2c.h"
#include "sensors/isl29020.h"
#include "hif.h"
#include "xmpl.h"
isl29020_ctx_t light1;
bool do_measure;
bool process_command(char chr);
int main(void)
{
const uint32_t br = HIF_DEFAULT_BAUDRATE;
uint8_t cmd;
uint8_t rv;
uint16_t lv;
float lv_f;
int chr;
#if HIF_TYPE == HIF_AT90USB
do
{
}
while (EOF == inchar);
#endif
PRINTF(
"\n\rISL29020 Light Sensor Example : %s : %ld bit/s\n\r", BOARD_NAME, br);
rv = isl29020_init(&light1, ISL29020_ADDR);
if (rv == 0)
{
PRINTF(
"ERROR: init, addr=0x%x\n", ISL29020_ADDR);
}
else
{
PRINTF(
"OK: init, addr=0x%x\n", ISL29020_ADDR);
}
cmd = 0;
ISL29020_SET_ENABLE(cmd);
ISL29020_SET_MODE_CONT(cmd);
ISL29020_SET_LIGHT(cmd);
isl29020_set_command(&light1, cmd);
do_measure = 1;
while (1)
{
if (chr != -1)
{
do_measure = process_command(chr);
}
if (do_measure)
{
lv = isl29020_get(&light1);
lv_f = isl29020_scale(&light1, lv);
PRINTF(
"data: %-8u E[lux]: %f\n", lv, lv_f);
WAIT_MS(1000);
}
}
}
bool process_command(char chr)
{
int nb;
uint8_t cmd;
bool rv = 0;
cmd = isl29020_get_command(&light1);
if (chr == 'R')
{
PRINT(
"Enter range [0-3]:");
ISL29020_SET_RANGE(cmd, nb);
isl29020_set_command(&light1, cmd);
}
else if (chr == 'r')
{
PRINT(
"Enter resolution [0-3]:");
ISL29020_SET_RESOLUTION(cmd, nb);
isl29020_set_command(&light1, cmd);
}
else if (chr == 'l')
{
ISL29020_SET_LIGHT(cmd);
isl29020_set_command(&light1, cmd);
}
else if (chr == 'i')
{
PRINT(
"detect infrared\n");
ISL29020_SET_IR(cmd);
isl29020_set_command(&light1, cmd);
}
else if (chr == 's')
{
PRINT(
"single measurement\n");
ISL29020_SET_MODE_SINGLE(cmd);
isl29020_set_command(&light1, cmd);
}
else if (chr == 'c')
{
PRINT(
"continous measurement\n");
ISL29020_SET_MODE_CONT(cmd);
isl29020_set_command(&light1, cmd);
}
else if (chr == 'p')
{
ISL29020_SET_DISABLE(cmd);
isl29020_set_command(&light1, cmd);
}
else if (chr == 'P')
{
ISL29020_SET_ENABLE(cmd);
isl29020_set_command(&light1, cmd);
}
else if (chr == 'm' || chr == ' ')
{
ISL29020_SET_ENABLE(cmd);
isl29020_set_command(&light1, cmd);
PRINT(
"run measurement\n");
rv = 1;
}
else if (chr == 'h')
{
"R - set range 0 - 3 \n"\
"r - set resolution 0 - 3\n"\
"l - detect light\n"\
"i - detect infrared\n"\
"s - single measurement\n"\
"c - continous measurement\n"\
"p - power down\n"\
"P - power up\n"\
"m - run measurement\n"
);
}
"enable: %d, "\
"mode cont: %d, "\
"detect IR: %d, "\
"range: %d, "\
"resolution: %d\n",
cmd,
ISL29020_GET_ENABLE(cmd),
ISL29020_GET_MODE_CONT(cmd),
ISL29020_GET_IR(cmd),
ISL29020_GET_RANGE(cmd),
ISL29020_GET_RES(cmd));
return rv;
}