main/task_apds9930.c

changeset 16
b3e96bbe4ce4
parent 12
bb72d448e282
child 21
df8564c9701e
equal deleted inserted replaced
15:64028e178ff1 16:b3e96bbe4ce4
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 63
64 // Calculate these for auto gain scaling.
65 ALS_maxcount = (256 - APDS9930_DEFAULT_ATIME) * 1024;
66 ALS_mincount = ALS_maxcount / 64;
67
64 /* event handler and event group for this task */ 68 /* event handler and event group for this task */
65 xEventGroupAPDS9930 = xEventGroupCreate(); 69 xEventGroupAPDS9930 = xEventGroupCreate();
66 EventBits_t uxBits; 70 EventBits_t uxBits;
67 71
68 /* 72 /*
75 79
76 if (uxBits & TASK_APDS9930_REQUEST_LIGHT) { 80 if (uxBits & TASK_APDS9930_REQUEST_LIGHT) {
77 81
78 if (! apds9930_state->fake) { 82 if (! apds9930_state->fake) {
79 /* Real sensor is present */ 83 /* Real sensor is present */
80 apds9930_enablePower(&apds9930_dev);
81 apds9930_enableLightSensor(&apds9930_dev, false);
82 apds9930_disableProximitySensor(&apds9930_dev);
83 if (xSemaphoreTake(xSemaphoreAPDS9930, 25) == pdTRUE) { 84 if (xSemaphoreTake(xSemaphoreAPDS9930, 25) == pdTRUE) {
84 l_gain = apds9930_state->gain; 85 l_gain = apds9930_state->gain;
85 l_aglbit = apds9930_state->aglbit; 86 l_aglbit = apds9930_state->aglbit;
86 xSemaphoreGive(xSemaphoreAPDS9930); 87 xSemaphoreGive(xSemaphoreAPDS9930);
87 } 88 }
88 if (l_gain > 3) 89 if (l_gain > 3)
89 l_gain = 0; 90 l_gain = 0;
90 apds9930_setAmbientLightGain(&apds9930_dev, l_gain); 91 ESP_LOGI(TAG, "1");
91 apds9930_setAmbientGainLevel(&apds9930_dev, l_aglbit); 92 // ESP_ERROR_CHECK(apds9930_enablePower(&apds9930_dev));
93 ESP_ERROR_CHECK(apds9930_enableLightSensor(&apds9930_dev, false)); // Does apds9930_enablePower() too.
94 ESP_ERROR_CHECK(apds9930_disableProximitySensor(&apds9930_dev));
95 ESP_ERROR_CHECK(apds9930_setAmbientLightGain(&apds9930_dev, l_gain));
96 ESP_ERROR_CHECK(apds9930_setAmbientGainLevel(&apds9930_dev, l_aglbit));
92 vTaskDelay(200 / portTICK_PERIOD_MS); 97 vTaskDelay(200 / portTICK_PERIOD_MS);
93 tries = 6; 98 tries = 6;
94 err = ESP_OK; 99 err = ESP_OK;
95 100
96 while (tries) { 101 while (tries) {
102 ESP_LOGI(TAG, "2 tries %d", tries);
97 err = apds9930_readAmbientLightLux(&apds9930_dev, &ambient_light); 103 err = apds9930_readAmbientLightLux(&apds9930_dev, &ambient_light);
98 if (err == ESP_OK) { 104 if (err == ESP_OK) {
99 err = apds9930_readCh0Light(&apds9930_dev, &ch0); 105 err = apds9930_readCh0Light(&apds9930_dev, &ch0);
100 if (err == ESP_OK) { 106 if (err == ESP_OK) {
101 err = apds9930_readCh1Light(&apds9930_dev, &ch1); 107 err = apds9930_readCh1Light(&apds9930_dev, &ch1);
103 } 109 }
104 if (err != ESP_OK) { 110 if (err != ESP_OK) {
105 ESP_LOGE(TAG, "read APDS-9930 values error '%s'", esp_err_to_name(err)); 111 ESP_LOGE(TAG, "read APDS-9930 values error '%s'", esp_err_to_name(err));
106 break; 112 break;
107 } 113 }
114 ESP_LOGI(TAG, "2a ALS_maxcount=%d ALS_mincount=%d", ALS_maxcount, ALS_mincount);
108 115
109 /* 116 /*
110 * Check ranges 117 * Check ranges
111 */ 118 */
112 if (((ch0 == ALS_maxcount) || (ch1 == ALS_maxcount)) && ((l_gain > 0) || (l_aglbit == 0))) { 119 if (((ch0 == ALS_maxcount) || (ch1 == ALS_maxcount)) && ((l_gain > 0) || (l_aglbit == 0))) {

mercurial