12 SemaphoreHandle_t xSemaphoreAPDS9930 = NULL; ///< Semaphore APDS9930 task |
12 SemaphoreHandle_t xSemaphoreAPDS9930 = NULL; ///< Semaphore APDS9930 task |
13 EventGroupHandle_t xEventGroupAPDS9930; ///< Events APDS9930 task |
13 EventGroupHandle_t xEventGroupAPDS9930; ///< Events APDS9930 task |
14 APDS9930_State *apds9930_state; ///< Public state for other tasks |
14 APDS9930_State *apds9930_state; ///< Public state for other tasks |
15 |
15 |
16 float ambient_light; |
16 float ambient_light; |
17 uint16_t ch0 = 0; |
|
18 uint16_t ch1 = 1; |
|
19 uint16_t ALS_maxcount; ///< Maximum ADC value |
17 uint16_t ALS_maxcount; ///< Maximum ADC value |
20 uint16_t ALS_mincount; ///< Threshold to increase gain |
18 uint16_t ALS_mincount; ///< Threshold to increase gain |
21 |
19 |
22 |
20 |
23 extern apds9930_t apds9930_dev; |
21 extern apds9930_t apds9930_dev; |
58 |
56 |
59 apds9930_state->valid = false; |
57 apds9930_state->valid = false; |
60 apds9930_state->fake = (apds9930_dev.i2c_dev.addr == 0) ? true:false; |
58 apds9930_state->fake = (apds9930_dev.i2c_dev.addr == 0) ? true:false; |
61 apds9930_state->address = apds9930_dev.i2c_dev.addr; |
59 apds9930_state->address = apds9930_dev.i2c_dev.addr; |
62 apds9930_state->error = APDS9930_ERR_NONE; |
60 apds9930_state->error = APDS9930_ERR_NONE; |
|
61 uint16_t ch0 = 0; |
|
62 uint16_t ch1 = 1; |
63 |
63 |
64 /* Gain settings are saved in NVS for quick startup. */ |
64 /* Gain settings are saved in NVS for quick startup. */ |
65 l_gain = nvsio_read_u8((char *)"l_gain"); |
65 l_gain = nvsio_read_u8((char *)"l_gain"); |
66 l_aglbit = nvsio_read_u8((char *)"l_aglbit"); |
66 l_aglbit = nvsio_read_u8((char *)"l_aglbit"); |
67 ESP_LOGI(TAG, "gain %d agl: %d", l_gain, l_aglbit); |
67 ESP_LOGI(TAG, "gain %d agl: %d", l_gain, l_aglbit); |
95 vTaskDelay(200 / portTICK_PERIOD_MS); |
95 vTaskDelay(200 / portTICK_PERIOD_MS); |
96 tries = 6; |
96 tries = 6; |
97 err = ESP_OK; |
97 err = ESP_OK; |
98 |
98 |
99 while (tries) { |
99 while (tries) { |
100 if (err == ESP_OK) { |
100 /* Read channels never returns any error */ |
101 err = apds9930_readCh0Light(&apds9930_dev, &ch0); |
101 apds9930_readCh0Light(&apds9930_dev, &ch0); |
102 if (err == ESP_OK) { |
102 apds9930_readCh1Light(&apds9930_dev, &ch1); |
103 err = apds9930_readCh1Light(&apds9930_dev, &ch1); |
|
104 } |
|
105 } |
|
106 if (err != ESP_OK) { |
|
107 ESP_LOGE(TAG, "read APDS-9930 values error '%s'", esp_err_to_name(err)); |
|
108 break; |
|
109 } |
|
110 ambient_light = apds9930_floatAmbientToLux(&apds9930_dev, ch0, ch1); |
103 ambient_light = apds9930_floatAmbientToLux(&apds9930_dev, ch0, ch1); |
111 |
104 |
112 /* |
105 /* |
113 * Check ranges |
106 * Check ranges |
114 */ |
107 */ |
133 apds9930_setAmbientLightGain(&apds9930_dev, l_gain); |
126 apds9930_setAmbientLightGain(&apds9930_dev, l_gain); |
134 apds9930_setAmbientGainLevel(&apds9930_dev, l_aglbit); |
127 apds9930_setAmbientGainLevel(&apds9930_dev, l_aglbit); |
135 vTaskDelay(200 / portTICK_PERIOD_MS); |
128 vTaskDelay(200 / portTICK_PERIOD_MS); |
136 ESP_LOGI(TAG, "Gain increased to %d AGL: %d", l_gain, l_aglbit); |
129 ESP_LOGI(TAG, "Gain increased to %d AGL: %d", l_gain, l_aglbit); |
137 } else { |
130 } else { |
138 if ((ch0 == ALS_maxcount) || (ch1 == ALS_maxcount)) { |
131 if (l_aglbit) { |
139 ambient_light = -1; |
|
140 } else if (l_aglbit) { |
|
141 ambient_light = ambient_light / 0.1666; |
132 ambient_light = ambient_light / 0.1666; |
142 } |
133 } |
143 nvsio_write_u8((char *)"l_gain", l_gain); |
134 nvsio_write_u8((char *)"l_gain", l_gain); |
144 nvsio_write_u8((char *)"l_aglbit", l_aglbit); |
135 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); |
136 ESP_LOGI(TAG, "Ambient: %.2f Ch0: %d Ch1: %d Gain: %d AGL: %d", ambient_light, ch0, ch1, l_gain, l_aglbit); |
158 apds9930_state->error = APDS9930_ERR_NONE; |
149 apds9930_state->error = APDS9930_ERR_NONE; |
159 apds9930_state->valid = true; |
150 apds9930_state->valid = true; |
160 apds9930_state->ambient_light = ambient_light; |
151 apds9930_state->ambient_light = ambient_light; |
161 apds9930_state->gain = l_gain; |
152 apds9930_state->gain = l_gain; |
162 apds9930_state->aglbit = l_aglbit; |
153 apds9930_state->aglbit = l_aglbit; |
|
154 apds9930_state->ch0 = ch0; |
|
155 apds9930_state->ch1 = ch1; |
163 } else { |
156 } else { |
164 apds9930_state->error = APDS9930_ERR_READ; |
157 apds9930_state->error = APDS9930_ERR_READ; |
165 apds9930_state->valid = false; |
158 apds9930_state->valid = false; |
166 apds9930_state->ambient_light = 12.34; |
159 apds9930_state->ambient_light = 12.34; |
167 apds9930_state->gain = 2; |
160 apds9930_state->gain = 2; |
169 } |
162 } |
170 xSemaphoreGive(xSemaphoreAPDS9930); |
163 xSemaphoreGive(xSemaphoreAPDS9930); |
171 } |
164 } |
172 } else { |
165 } else { |
173 /* Use fake values */ |
166 /* Use fake values */ |
|
167 ch0 = 12; |
|
168 ch1 = 34; |
|
169 ambient_light = apds9930_floatAmbientToLux(&apds9930_dev, ch0, ch1); |
174 if (xSemaphoreTake(xSemaphoreAPDS9930, 25) == pdTRUE) { |
170 if (xSemaphoreTake(xSemaphoreAPDS9930, 25) == pdTRUE) { |
175 apds9930_state->error = APDS9930_ERR_NONE; |
171 apds9930_state->error = APDS9930_ERR_NONE; |
176 apds9930_state->valid = true; |
172 apds9930_state->valid = true; |
177 apds9930_state->ambient_light = 12.34; |
173 apds9930_state->ambient_light = ambient_light; |
178 apds9930_state->gain = 2; |
174 apds9930_state->gain = 2; |
179 apds9930_state->aglbit = 0; |
175 apds9930_state->aglbit = 0; |
|
176 apds9930_state->ch0 = ch0; |
|
177 apds9930_state->ch1 = ch1; |
180 xSemaphoreGive(xSemaphoreAPDS9930); |
178 xSemaphoreGive(xSemaphoreAPDS9930); |
181 } |
179 } |
182 } |
180 } |
183 |
181 |
184 xEventGroupClearBits(xEventGroupAPDS9930, TASK_APDS9930_REQUEST_LIGHT); |
182 xEventGroupClearBits(xEventGroupAPDS9930, TASK_APDS9930_REQUEST_LIGHT); |