49 */ |
49 */ |
50 void task_apds9930(void *pvParameter) |
50 void task_apds9930(void *pvParameter) |
51 { |
51 { |
52 int tries; |
52 int tries; |
53 esp_err_t err = ESP_OK; |
53 esp_err_t err = ESP_OK; |
54 uint8_t l_gain = 0, l_aglbit = 0; |
54 uint8_t l_gain, l_aglbit; |
55 |
55 |
56 ESP_LOGI(TAG, "Starting task APDS9930 sda=%d scl=%d", CONFIG_I2C_MASTER_SDA, CONFIG_I2C_MASTER_SCL); |
56 ESP_LOGI(TAG, "Starting task APDS9930 sda=%d scl=%d", CONFIG_I2C_MASTER_SDA, CONFIG_I2C_MASTER_SCL); |
57 apds9930_state = malloc(sizeof(APDS9930_State)); |
57 apds9930_state = malloc(sizeof(APDS9930_State)); |
58 |
58 |
59 apds9930_state->valid = false; |
59 apds9930_state->valid = false; |
60 apds9930_state->fake = (apds9930_dev.i2c_dev.addr == 0) ? true:false; |
60 apds9930_state->fake = (apds9930_dev.i2c_dev.addr == 0) ? true:false; |
61 apds9930_state->address = apds9930_dev.i2c_dev.addr; |
61 apds9930_state->address = apds9930_dev.i2c_dev.addr; |
62 apds9930_state->error = APDS9930_ERR_NONE; |
62 apds9930_state->error = APDS9930_ERR_NONE; |
|
63 |
|
64 /* Gain settings are saved in NVS for quick startup. */ |
|
65 l_gain = nvsio_read_u8((char *)"l_gain"); |
|
66 l_aglbit = nvsio_read_u8((char *)"l_aglbit"); |
|
67 ESP_LOGI(TAG, "gain %d agl: %d", l_gain, l_aglbit); |
63 |
68 |
64 // Calculate these for auto gain scaling. |
69 // Calculate these for auto gain scaling. |
65 ALS_maxcount = (256 - APDS9930_DEFAULT_ATIME) * 1024; |
70 ALS_maxcount = (256 - APDS9930_DEFAULT_ATIME) * 1024; |
66 ALS_mincount = ALS_maxcount / 64; |
71 ALS_mincount = ALS_maxcount / 64; |
67 |
72 |
79 |
84 |
80 if (uxBits & TASK_APDS9930_REQUEST_LIGHT) { |
85 if (uxBits & TASK_APDS9930_REQUEST_LIGHT) { |
81 |
86 |
82 if (! apds9930_state->fake) { |
87 if (! apds9930_state->fake) { |
83 /* Real sensor is present */ |
88 /* Real sensor is present */ |
84 if (xSemaphoreTake(xSemaphoreAPDS9930, 25) == pdTRUE) { |
|
85 l_gain = apds9930_state->gain; |
|
86 l_aglbit = apds9930_state->aglbit; |
|
87 xSemaphoreGive(xSemaphoreAPDS9930); |
|
88 } |
|
89 if (l_gain > 3) |
89 if (l_gain > 3) |
90 l_gain = 0; |
90 l_gain = 0; |
91 ESP_LOGI(TAG, "1"); |
|
92 // ESP_ERROR_CHECK(apds9930_enablePower(&apds9930_dev)); |
|
93 ESP_ERROR_CHECK(apds9930_enableLightSensor(&apds9930_dev, false)); // Does apds9930_enablePower() too. |
91 ESP_ERROR_CHECK(apds9930_enableLightSensor(&apds9930_dev, false)); // Does apds9930_enablePower() too. |
94 ESP_ERROR_CHECK(apds9930_disableProximitySensor(&apds9930_dev)); |
92 ESP_ERROR_CHECK(apds9930_disableProximitySensor(&apds9930_dev)); |
95 ESP_ERROR_CHECK(apds9930_setAmbientLightGain(&apds9930_dev, l_gain)); |
93 ESP_ERROR_CHECK(apds9930_setAmbientLightGain(&apds9930_dev, l_gain)); |
96 ESP_ERROR_CHECK(apds9930_setAmbientGainLevel(&apds9930_dev, l_aglbit)); |
94 ESP_ERROR_CHECK(apds9930_setAmbientGainLevel(&apds9930_dev, l_aglbit)); |
97 vTaskDelay(200 / portTICK_PERIOD_MS); |
95 vTaskDelay(200 / portTICK_PERIOD_MS); |
98 tries = 6; |
96 tries = 6; |
99 err = ESP_OK; |
97 err = ESP_OK; |
100 |
98 |
101 while (tries) { |
99 while (tries) { |
102 ESP_LOGI(TAG, "2 tries %d", tries); |
|
103 err = apds9930_readAmbientLightLux(&apds9930_dev, &ambient_light); |
|
104 if (err == ESP_OK) { |
100 if (err == ESP_OK) { |
105 err = apds9930_readCh0Light(&apds9930_dev, &ch0); |
101 err = apds9930_readCh0Light(&apds9930_dev, &ch0); |
106 if (err == ESP_OK) { |
102 if (err == ESP_OK) { |
107 err = apds9930_readCh1Light(&apds9930_dev, &ch1); |
103 err = apds9930_readCh1Light(&apds9930_dev, &ch1); |
108 } |
104 } |
109 } |
105 } |
110 if (err != ESP_OK) { |
106 if (err != ESP_OK) { |
111 ESP_LOGE(TAG, "read APDS-9930 values error '%s'", esp_err_to_name(err)); |
107 ESP_LOGE(TAG, "read APDS-9930 values error '%s'", esp_err_to_name(err)); |
112 break; |
108 break; |
113 } |
109 } |
114 ESP_LOGI(TAG, "2a ALS_maxcount=%d ALS_mincount=%d", ALS_maxcount, ALS_mincount); |
110 ambient_light = apds9930_floatAmbientToLux(&apds9930_dev, ch0, ch1); |
115 |
111 |
116 /* |
112 /* |
117 * Check ranges |
113 * Check ranges |
118 */ |
114 */ |
119 if (((ch0 == ALS_maxcount) || (ch1 == ALS_maxcount)) && ((l_gain > 0) || (l_aglbit == 0))) { |
115 if (((ch0 == ALS_maxcount) || (ch1 == ALS_maxcount)) && ((l_gain > 0) || (l_aglbit == 0))) { |
142 if ((ch0 == ALS_maxcount) || (ch1 == ALS_maxcount)) { |
138 if ((ch0 == ALS_maxcount) || (ch1 == ALS_maxcount)) { |
143 ambient_light = -1; |
139 ambient_light = -1; |
144 } else if (l_aglbit) { |
140 } else if (l_aglbit) { |
145 ambient_light = ambient_light / 0.1666; |
141 ambient_light = ambient_light / 0.1666; |
146 } |
142 } |
147 ESP_LOGI(TAG, "Ambient: %.3f Ch0: %d Ch1: %d Gain: %d AGL: %d", ambient_light, ch0, ch1, l_gain, l_aglbit); |
143 nvsio_write_u8((char *)"l_gain", l_gain); |
|
144 nvsio_write_u8((char *)"l_aglbit", l_aglbit); |
|
145 ESP_LOGI(TAG, "Ambient: %.2f Ch0: %d Ch1: %d Gain: %d AGL: %d", ambient_light, ch0, ch1, l_gain, l_aglbit); |
148 break; |
146 break; |
149 } |
147 } |
150 tries--; |
148 tries--; |
151 } |
149 } |
152 if (tries == 0) { |
150 if (tries == 0) { |
183 } |
181 } |
184 } |
182 } |
185 |
183 |
186 xEventGroupClearBits(xEventGroupAPDS9930, TASK_APDS9930_REQUEST_LIGHT); |
184 xEventGroupClearBits(xEventGroupAPDS9930, TASK_APDS9930_REQUEST_LIGHT); |
187 xEventGroupSetBits(xEventGroupAPDS9930, TASK_APDS9930_REQUEST_DONE); |
185 xEventGroupSetBits(xEventGroupAPDS9930, TASK_APDS9930_REQUEST_DONE); |
188 #if 1 |
186 #if 0 |
189 ESP_LOGI(TAG, " Light: %.2f, gain: %d, error: %d", apds9930_state->ambient_light, apds9930_state->gain, apds9930_state->error); |
187 ESP_LOGI(TAG, " Light: %.2f, gain: %d, error: %d", apds9930_state->ambient_light, apds9930_state->gain, apds9930_state->error); |
190 #endif |
188 #endif |
191 } |
189 } |
192 } |
190 } |
193 } |
191 } |