-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
86 lines (73 loc) · 2.44 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "driver/gpio.h"
#include "driver/i2c.h"
#include "esp_err.h"
#include "esp_log.h"
#include "freertos/task.h"
#include "sdkconfig.h" // generated by "make menuconfig"
#define SDA_PIN GPIO_NUM_15
#define SCL_PIN GPIO_NUM_2
#define tag "MAX44009"
#define MAX44009_ADDRESS1 0x4A
#define MAX44009_ADDRESS2 0x4B
#define I2C_MASTER_ACK 0
#define I2C_MASTER_NACK 1
void i2c_master_init()
{
i2c_config_t i2c_config = {
.mode = I2C_MODE_MASTER,
.sda_io_num = SDA_PIN,
.scl_io_num = SCL_PIN,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = 400000
};
i2c_param_config(I2C_NUM_0, &i2c_config);
i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0);
}
void task_max44009_read_ambient_light(void *ignore)
{
uint8_t lux_h;
uint8_t lux_l;
esp_err_t espErr;
i2c_cmd_handle_t cmd;
while (true) {
vTaskDelay(800/portTICK_PERIOD_MS);
cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (MAX44009_ADDRESS1 << 1) | I2C_MASTER_WRITE, true);
i2c_master_write_byte(cmd, 0x03, true);
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (MAX44009_ADDRESS1 << 1) | I2C_MASTER_READ, true);
i2c_master_read_byte(cmd, &lux_h, I2C_MASTER_NACK);
i2c_master_stop(cmd);
espErr = i2c_master_cmd_begin(I2C_NUM_0, cmd, 10/portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);
// According to datasheet (p17), we can read two registers in one transmission
// by repeated start signal. But unfortunately it timeouts.
// So we re-create or I2C link for to get lux low-byte.
cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (MAX44009_ADDRESS1 << 1) | I2C_MASTER_WRITE, true);
i2c_master_write_byte(cmd, 0x04, true);
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (MAX44009_ADDRESS1 << 1) | I2C_MASTER_READ, true);
i2c_master_read_byte(cmd, &lux_l, I2C_MASTER_NACK);
i2c_master_stop(cmd);
espErr = i2c_master_cmd_begin(I2C_NUM_0, cmd, 10/portTICK_PERIOD_MS);
if (espErr == ESP_OK) {
int exponent = (lux_h & 0xf0) >> 4;
int mant = (lux_h & 0x0f) << 4 | lux_l;
float lux = (float)(((0x00000001 << exponent) * (float)mant) * 0.045);
ESP_LOGI(tag, "%.3f lux", lux);
} else {
ESP_LOGE(tag, "fail to read from sensor. code: %.2X", espErr);
}
i2c_cmd_link_delete(cmd);
}
vTaskDelete(NULL);
}
void app_main(void)
{
i2c_master_init();
xTaskCreate(&task_max44009_read_ambient_light, "max44009_read_ambient_light", 2048, NULL, 6, NULL);
}