Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
Nodemcu Firmware
Commits
537cd680
Commit
537cd680
authored
Aug 23, 2015
by
aeprox
Browse files
Made device package and address configurable at runtime
parent
0f6a0c59
Changes
3
Hide whitespace changes
Inline
Side-by-side
app/modules/tsl2561.c
View file @
537cd680
...
...
@@ -15,12 +15,12 @@ static uint16_t ch0;
static
uint16_t
ch1
;
/* Initialises the device on pins sdapin and sclpin
* Lua: status = tsl2561.init(sdapin, sclpin)
* Lua: status = tsl2561.init(sdapin, sclpin
, address(optional), package(optional)
)
*/
static
int
ICACHE_FLASH_ATTR
tsl2561_init
(
lua_State
*
L
)
{
uint32_t
sda
;
uint32_t
scl
;
// check parameters
if
(
!
lua_isnumber
(
L
,
1
)
||
!
lua_isnumber
(
L
,
2
))
{
return
luaL_error
(
L
,
"wrong arg range"
);
}
...
...
@@ -31,8 +31,28 @@ static int ICACHE_FLASH_ATTR tsl2561_init(lua_State* L) {
if
(
scl
==
0
||
sda
==
0
)
{
return
luaL_error
(
L
,
"no i2c for D0"
);
}
// init I2C
uint8_t
error
=
tsl2561Init
(
sda
,
scl
);
// Parse optional parameters
if
(
lua_isnumber
(
L
,
3
))
{
uint8_t
address
=
luaL_checkinteger
(
L
,
3
);
if
(
!
((
address
==
TSL2561_ADDRESS_GND
)
||
(
address
==
TSL2561_ADDRESS_FLOAT
)
||
(
address
==
TSL2561_ADDRESS_VDD
)))
{
return
luaL_error
(
L
,
"Invalid argument: address"
);
}
else
{
tsl2561SetAddress
(
address
);
}
}
if
(
lua_isnumber
(
L
,
4
))
{
uint8_t
package
=
luaL_checkinteger
(
L
,
4
);
if
(
!
((
package
==
TSL2561_PACKAGE_T_FN_CL
)
||
(
package
==
TSL2561_PACKAGE_CS
)))
{
return
luaL_error
(
L
,
"Invalid argument: package"
);
}
else
{
tsl2561SetPackage
(
package
);
}
}
lua_pushnumber
(
L
,
error
);
return
1
;
}
...
...
@@ -46,11 +66,11 @@ static int ICACHE_FLASH_ATTR tsl2561_lua_settiming(lua_State* L) {
}
uint8_t
integration
=
luaL_checkinteger
(
L
,
1
);
if
(
!
((
integration
==
TSL2561_INTEGRATIONTIME_13MS
)
||
(
integration
==
TSL2561_INTEGRATIONTIME_101MS
)
||
(
integration
==
TSL2561_INTEGRATIONTIME_402MS
)))
{
return
luaL_error
(
L
,
"
wrong range for arg
integration"
);
return
luaL_error
(
L
,
"
Invalid argument:
integration"
);
}
uint8_t
gain
=
luaL_checkinteger
(
L
,
2
);
if
(
!
((
gain
==
TSL2561_GAIN_16X
)
||
(
gain
==
TSL2561_GAIN_1X
)))
{
return
luaL_error
(
L
,
"
wrong range for arg
gain"
);
return
luaL_error
(
L
,
"
Invalid argument:
gain"
);
}
lua_pushnumber
(
L
,
tsl2561SetTiming
(
integration
,
gain
));
...
...
@@ -103,6 +123,13 @@ const LUA_REG_TYPE tsl2561_map[] =
{
LSTRKEY
(
"GAIN_1X"
),
LNUMVAL
(
TSL2561_GAIN_1X
)},
{
LSTRKEY
(
"GAIN_16X"
),
LNUMVAL
(
TSL2561_GAIN_16X
)},
{
LSTRKEY
(
"PACKAGE_CS"
),
LNUMVAL
(
TSL2561_PACKAGE_CS
)},
{
LSTRKEY
(
"PACKAGE_T_FN_CL"
),
LNUMVAL
(
TSL2561_PACKAGE_T_FN_CL
)},
{
LSTRKEY
(
"ADDRESS_GND"
),
LNUMVAL
(
TSL2561_ADDRESS_GND
)},
{
LSTRKEY
(
"ADDRESS_FLOAT"
),
LNUMVAL
(
TSL2561_ADDRESS_FLOAT
)},
{
LSTRKEY
(
"ADDRESS_VDD"
),
LNUMVAL
(
TSL2561_ADDRESS_VDD
)},
{
LNILKEY
,
LNILVAL
}
};
...
...
app/tsl2561/tsl2561.c
View file @
537cd680
...
...
@@ -72,6 +72,8 @@ static const uint32_t tsl2561_i2c_id = 0;
static
bool
_tsl2561Initialised
=
0
;
static
tsl2561IntegrationTime_t
_tsl2561IntegrationTime
=
TSL2561_INTEGRATIONTIME_402MS
;
static
tsl2561Gain_t
_tsl2561Gain
=
TSL2561_GAIN_1X
;
static
tsl2561Address_t
tsl2561Address
=
TSL2561_ADDRESS_FLOAT
;
static
tsl2561Package_t
tsl2561Package
=
TSL2561_PACKAGE_T_FN_CL
;
/**************************************************************************/
/*!
...
...
@@ -80,7 +82,7 @@ static tsl2561Gain_t _tsl2561Gain = TSL2561_GAIN_1X;
/**************************************************************************/
tsl2561Error_t
tsl2561Write8
(
uint8_t
reg
,
uint8_t
value
)
{
platform_i2c_send_start
(
tsl2561_i2c_id
);
platform_i2c_send_address
(
tsl2561_i2c_id
,
TSL
2561
_ADDRESS
,
PLATFORM_I2C_DIRECTION_TRANSMITTER
);
platform_i2c_send_address
(
tsl2561_i2c_id
,
tsl
2561
Address
,
PLATFORM_I2C_DIRECTION_TRANSMITTER
);
platform_i2c_send_byte
(
tsl2561_i2c_id
,
reg
);
platform_i2c_send_byte
(
tsl2561_i2c_id
,
value
);
platform_i2c_send_stop
(
tsl2561_i2c_id
);
...
...
@@ -94,22 +96,22 @@ tsl2561Error_t tsl2561Write8(uint8_t reg, uint8_t value) {
/**************************************************************************/
tsl2561Error_t
tsl2561Read16
(
uint8_t
reg
,
uint16_t
*
value
)
{
platform_i2c_send_start
(
tsl2561_i2c_id
);
platform_i2c_send_address
(
tsl2561_i2c_id
,
TSL
2561
_ADDRESS
,
PLATFORM_I2C_DIRECTION_TRANSMITTER
);
platform_i2c_send_address
(
tsl2561_i2c_id
,
tsl
2561
Address
,
PLATFORM_I2C_DIRECTION_TRANSMITTER
);
platform_i2c_send_byte
(
tsl2561_i2c_id
,
reg
);
platform_i2c_send_stop
(
tsl2561_i2c_id
);
platform_i2c_send_start
(
tsl2561_i2c_id
);
platform_i2c_send_address
(
tsl2561_i2c_id
,
TSL
2561
_ADDRESS
,
PLATFORM_I2C_DIRECTION_RECEIVER
);
platform_i2c_send_address
(
tsl2561_i2c_id
,
tsl
2561
Address
,
PLATFORM_I2C_DIRECTION_RECEIVER
);
uint8_t
ch_low
=
platform_i2c_recv_byte
(
tsl2561_i2c_id
,
0
);
platform_i2c_send_stop
(
tsl2561_i2c_id
);
platform_i2c_send_start
(
tsl2561_i2c_id
);
platform_i2c_send_address
(
tsl2561_i2c_id
,
TSL
2561
_ADDRESS
,
PLATFORM_I2C_DIRECTION_TRANSMITTER
);
platform_i2c_send_address
(
tsl2561_i2c_id
,
tsl
2561
Address
,
PLATFORM_I2C_DIRECTION_TRANSMITTER
);
platform_i2c_send_byte
(
tsl2561_i2c_id
,
reg
+
1
);
platform_i2c_send_stop
(
tsl2561_i2c_id
);
platform_i2c_send_start
(
tsl2561_i2c_id
);
platform_i2c_send_address
(
tsl2561_i2c_id
,
TSL
2561
_ADDRESS
,
PLATFORM_I2C_DIRECTION_RECEIVER
);
platform_i2c_send_address
(
tsl2561_i2c_id
,
tsl
2561
Address
,
PLATFORM_I2C_DIRECTION_RECEIVER
);
uint8_t
ch_high
=
platform_i2c_recv_byte
(
tsl2561_i2c_id
,
0
);
platform_i2c_send_stop
(
tsl2561_i2c_id
);
...
...
@@ -147,6 +149,15 @@ tsl2561Error_t tsl2561Disable(void) {
TSL2561_CONTROL_POWEROFF
);
}
void
tsl2561SetAddress
(
uint8_t
address
){
tsl2561Address
=
address
;
return
;
}
void
tsl2561SetPackage
(
uint8_t
package
){
tsl2561Package
=
package
;
return
;
}
/**************************************************************************/
/*!
@brief Initialises the I2C block
...
...
@@ -289,50 +300,51 @@ uint32_t tsl2561CalculateLux(uint16_t ch0, uint16_t ch1) {
unsigned
int
b
,
m
;
#ifdef TSL2561_PACKAGE_CS
if
((
ratio
>=
0
)
&&
(
ratio
<=
TSL2561_LUX_K1C
))
{
b
=
TSL2561_LUX_B1C
;
m
=
TSL2561_LUX_M1C
;
}
else
if
(
ratio
<=
TSL2561_LUX_K2C
)
{
b
=
TSL2561_LUX_B2C
;
m
=
TSL2561_LUX_M2C
;
}
else
if
(
ratio
<=
TSL2561_LUX_K3C
)
{
b
=
TSL2561_LUX_B3C
;
m
=
TSL2561_LUX_M3C
;
}
else
if
(
ratio
<=
TSL2561_LUX_K4C
)
{
b
=
TSL2561_LUX_B4C
;
m
=
TSL2561_LUX_M4C
;
}
else
if
(
ratio
<=
TSL2561_LUX_K5C
)
{
b
=
TSL2561_LUX_B5C
;
m
=
TSL2561_LUX_M5C
;
}
else
if
(
ratio
<=
TSL2561_LUX_K6C
)
{
b
=
TSL2561_LUX_B6C
;
m
=
TSL2561_LUX_M6C
;
}
else
if
(
ratio
<=
TSL2561_LUX_K7C
)
{
b
=
TSL2561_LUX_B7C
;
m
=
TSL2561_LUX_M7C
;
}
else
if
(
ratio
>
TSL2561_LUX_K8C
)
{
b
=
TSL2561_LUX_B8C
;
m
=
TSL2561_LUX_M8C
;
if
(
tsl2561Package
==
TSL2561_PACKAGE_CS
){
if
((
ratio
>=
0
)
&&
(
ratio
<=
TSL2561_LUX_K1C
))
{
b
=
TSL2561_LUX_B1C
;
m
=
TSL2561_LUX_M1C
;
}
else
if
(
ratio
<=
TSL2561_LUX_K2C
)
{
b
=
TSL2561_LUX_B2C
;
m
=
TSL2561_LUX_M2C
;
}
else
if
(
ratio
<=
TSL2561_LUX_K3C
)
{
b
=
TSL2561_LUX_B3C
;
m
=
TSL2561_LUX_M3C
;
}
else
if
(
ratio
<=
TSL2561_LUX_K4C
)
{
b
=
TSL2561_LUX_B4C
;
m
=
TSL2561_LUX_M4C
;
}
else
if
(
ratio
<=
TSL2561_LUX_K5C
)
{
b
=
TSL2561_LUX_B5C
;
m
=
TSL2561_LUX_M5C
;
}
else
if
(
ratio
<=
TSL2561_LUX_K6C
)
{
b
=
TSL2561_LUX_B6C
;
m
=
TSL2561_LUX_M6C
;
}
else
if
(
ratio
<=
TSL2561_LUX_K7C
)
{
b
=
TSL2561_LUX_B7C
;
m
=
TSL2561_LUX_M7C
;
}
else
if
(
ratio
>
TSL2561_LUX_K8C
)
{
b
=
TSL2561_LUX_B8C
;
m
=
TSL2561_LUX_M8C
;
}
}
else
{
if
((
ratio
>=
0
)
&&
(
ratio
<=
TSL2561_LUX_K1T
))
{
b
=
TSL2561_LUX_B1T
;
m
=
TSL2561_LUX_M1T
;}
else
if
(
ratio
<=
TSL2561_LUX_K2T
)
{
b
=
TSL2561_LUX_B2T
;
m
=
TSL2561_LUX_M2T
;}
else
if
(
ratio
<=
TSL2561_LUX_K3T
)
{
b
=
TSL2561_LUX_B3T
;
m
=
TSL2561_LUX_M3T
;}
else
if
(
ratio
<=
TSL2561_LUX_K4T
)
{
b
=
TSL2561_LUX_B4T
;
m
=
TSL2561_LUX_M4T
;}
else
if
(
ratio
<=
TSL2561_LUX_K5T
)
{
b
=
TSL2561_LUX_B5T
;
m
=
TSL2561_LUX_M5T
;}
else
if
(
ratio
<=
TSL2561_LUX_K6T
)
{
b
=
TSL2561_LUX_B6T
;
m
=
TSL2561_LUX_M6T
;}
else
if
(
ratio
<=
TSL2561_LUX_K7T
)
{
b
=
TSL2561_LUX_B7T
;
m
=
TSL2561_LUX_M7T
;}
else
if
(
ratio
>
TSL2561_LUX_K8T
)
{
b
=
TSL2561_LUX_B8T
;
m
=
TSL2561_LUX_M8T
;}
}
#else
if
((
ratio
>=
0
)
&&
(
ratio
<=
TSL2561_LUX_K1T
))
{
b
=
TSL2561_LUX_B1T
;
m
=
TSL2561_LUX_M1T
;}
else
if
(
ratio
<=
TSL2561_LUX_K2T
)
{
b
=
TSL2561_LUX_B2T
;
m
=
TSL2561_LUX_M2T
;}
else
if
(
ratio
<=
TSL2561_LUX_K3T
)
{
b
=
TSL2561_LUX_B3T
;
m
=
TSL2561_LUX_M3T
;}
else
if
(
ratio
<=
TSL2561_LUX_K4T
)
{
b
=
TSL2561_LUX_B4T
;
m
=
TSL2561_LUX_M4T
;}
else
if
(
ratio
<=
TSL2561_LUX_K5T
)
{
b
=
TSL2561_LUX_B5T
;
m
=
TSL2561_LUX_M5T
;}
else
if
(
ratio
<=
TSL2561_LUX_K6T
)
{
b
=
TSL2561_LUX_B6T
;
m
=
TSL2561_LUX_M6T
;}
else
if
(
ratio
<=
TSL2561_LUX_K7T
)
{
b
=
TSL2561_LUX_B7T
;
m
=
TSL2561_LUX_M7T
;}
else
if
(
ratio
>
TSL2561_LUX_K8T
)
{
b
=
TSL2561_LUX_B8T
;
m
=
TSL2561_LUX_M8T
;}
#endif
unsigned
long
temp
;
temp
=
((
channel0
*
b
)
-
(
channel1
*
m
));
...
...
app/tsl2561/tsl2561.h
View file @
537cd680
...
...
@@ -38,10 +38,6 @@
#ifndef _TSL2561_H_
#define _TSL2561_H_
#define TSL2561_PACKAGE_CS // Lux calculations differ slightly for CS package
// #define TSL2561_PACKAGE_T_FN_CL
#define TSL2561_ADDRESS (0x39) // GND=>0x29, float=>0x39 or VDD=>0x49
#define TSL2561_READBIT (0x01)
#define TSL2561_COMMAND_BIT (0x80) // Must be 1
...
...
@@ -152,11 +148,29 @@ typedef enum
}
tsl2561Error_t
;
// GND=>0x29, float=>0x39 or VDD=>0x49
typedef
enum
{
TSL2561_ADDRESS_GND
=
0x29
,
TSL2561_ADDRESS_FLOAT
=
0x39
,
TSL2561_ADDRESS_VDD
=
0x49
,
}
tsl2561Address_t
;
// Lux calculations differ slightly for CS package
typedef
enum
{
TSL2561_PACKAGE_CS
=
0
,
TSL2561_PACKAGE_T_FN_CL
}
tsl2561Package_t
;
tsl2561Error_t
tsl2561Init
(
uint8_t
sda
,
uint8_t
scl
);
tsl2561Error_t
tsl2561SetTiming
(
tsl2561IntegrationTime_t
integration
,
tsl2561Gain_t
gain
);
tsl2561Error_t
tsl2561GetLuminosity
(
uint16_t
*
broadband
,
uint16_t
*
ir
);
uint32_t
tsl2561CalculateLux
(
uint16_t
ch0
,
uint16_t
ch1
);
void
tsl2561SetAddress
(
uint8_t
address
);
void
tsl2561SetPackage
(
uint8_t
package
);
#endif
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment