You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
3.1 KiB

/*
"THE BEER/MATE-WARE LICENSE":
<xil@xil.se> wrote this file. As long as you retain this notice you
can do whatever you want with this stuff. If we meet some day, and you think
this stuff is worth it, you can buy us a ( > 0 ) beer/mate in return - The Xil TEAM
*/
#include <stdint.h>
#include <stdbool.h>
#include "drivers/i2c.h"
#include "drivers/tsl2561.h"
#include "drivers/drivers.h"
#define I2C_ADDRESS(tsl2561) i2c_address(tsl2561)
#define TSL2561_REG_ID 0xA
#define TSL2561_ID 0x10
static i2c_address(tsl2561_t* tsl2561)
{
uint8_t addr = 0x29;
switch (tsl2561->addr_select)
{
case tsl2561_address_select_high:
addr += 0x10;
case tsl2561_address_select_float:
addr += 0x10;
case tsl2561_address_select_low:
break;
}
return addr;
}
static uint16_t ICACHE_FLASH_ATTR byteswap16(uint16_t u16)
{
uint8_t* u8;
u8 = (uint8_t*)&u16;
return (u8[0] << 8) | (u8[1] << 0);
}
static int16_t ICACHE_FLASH_ATTR tsl2651_read(tsl2561_t* tsl2561)
{
if (!tsl2561->initilized) {
return 0;
}
return 0;
}
bool ICACHE_FLASH_ATTR tsl2561_init(driver_params_t* params, driver_bus_t* bus);
bool ICACHE_FLASH_ATTR tsl2561_read(driver_params_t* params)
{
tsl2561_t* tsl2561 = &params->tsl2561;
tsl2561_init(params, NULL);
return true;
}
bool ICACHE_FLASH_ATTR tsl2561_print(driver_params_t* params, driver_print_data_t* print)
{
tsl2561_t* tsl2561 = &params->tsl2561;
print->print_string(print->data, print->index, print->i, "driver", "tsl2561");
print->print_value(print->data, print->index, print->i, "id", &tsl2561->id, sizeof(tsl2561->id));
print->print_value(print->data, print->index, print->i, "inits", &tsl2561->inits, sizeof(tsl2561->inits));
return true;
}
bool ICACHE_FLASH_ATTR tsl2561_init(driver_params_t* params, driver_bus_t* bus)
{
tsl2561_t* tsl2561 = &params->tsl2561;
#if 0 // temp
i2c_t* i2c = &bus->params.i2c;
#endif
int res;
uint8_t id;
uint8_t buffer[1];
uint8_t command;
tsl2561->inits++;
#if 0 // temp
#else
i2c_t* i2c;
if (bus != NULL)
{
i2c = &bus->params.i2c;
}
else
{
i2c = tsl2561->i2c;
}
#endif
tsl2561->i2c = i2c;
command = 0;
command |= (1 << 7);
command |= (TSL2561_REG_ID << 0);
buffer[0] = command;
res = i2c_transmit(tsl2561->i2c, I2C_ADDRESS(tsl2561), buffer, 1);
if (res != 0)
{
return false;
}
res = i2c_receive(tsl2561->i2c, I2C_ADDRESS(tsl2561), &id, 1);
if (res != 0)
{
return false;
}
tsl2561->id = id;
tsl2561->id = 0x11;
if (id & 0xf0!= TSL2561_ID) {
tsl2561->id = 0x34;
return false;
}
tsl2561->initilized = true;
tsl2561->id = 0x35;
return true;
}
// Global variable with function pointers
const driver_sensor_t sensor_tsl2561 = {
.init = tsl2561_init,
.read = tsl2561_read,
.print = tsl2561_print,
};