1
0

lux.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2017 David Antliff
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. /**
  25. * @file app_main.c
  26. * @brief Example application for the TSL2561 Light-to-Digital Converter.
  27. */
  28. #include <stdio.h>
  29. #include "freertos/FreeRTOS.h"
  30. #include "freertos/task.h"
  31. #include "esp_system.h"
  32. #include "driver/gpio.h"
  33. #include "driver/i2c.h"
  34. #include "esp_log.h"
  35. #include "sdkconfig.h"
  36. #include "smbus.h"
  37. #include "tsl2561.h"
  38. #include "config.h"
  39. #ifdef ENABLE_LUX_SENSOR
  40. #define TAG "app"
  41. #define I2C_MASTER_NUM I2C_NUM_0
  42. #define I2C_MASTER_TX_BUF_LEN 0 // disabled
  43. #define I2C_MASTER_RX_BUF_LEN 0 // disabled
  44. #define I2C_MASTER_FREQ_HZ 100000
  45. #define I2C_MASTER_SDA_IO I2C_MASTER_SDA
  46. #define I2C_MASTER_SCL_IO I2C_MASTER_SCL
  47. static void i2c_master_init(void)
  48. {
  49. int i2c_master_port = I2C_MASTER_NUM;
  50. i2c_config_t conf;
  51. conf.mode = I2C_MODE_MASTER;
  52. conf.sda_io_num = I2C_MASTER_SDA_IO;
  53. conf.sda_pullup_en = GPIO_PULLUP_DISABLE; // GY-2561 provides 10kΩ pullups
  54. conf.scl_io_num = I2C_MASTER_SCL_IO;
  55. conf.scl_pullup_en = GPIO_PULLUP_DISABLE; // GY-2561 provides 10kΩ pullups
  56. conf.master.clk_speed = I2C_MASTER_FREQ_HZ;
  57. i2c_param_config(i2c_master_port, &conf);
  58. i2c_driver_install(i2c_master_port, conf.mode,
  59. I2C_MASTER_RX_BUF_LEN,
  60. I2C_MASTER_TX_BUF_LEN, 0);
  61. }
  62. static void tsl2561_task(void * pvParameter)
  63. {
  64. // Set up I2C
  65. i2c_master_init();
  66. i2c_port_t i2c_num = I2C_MASTER_NUM;
  67. uint8_t address = CONFIG_TSL2561_I2C_ADDRESS;
  68. // Set up the SMBus
  69. smbus_info_t * smbus_info = smbus_malloc();
  70. smbus_init(smbus_info, i2c_num, address);
  71. smbus_set_timeout(smbus_info, 1000 / portTICK_RATE_MS);
  72. // Set up the TSL2561 device
  73. tsl2561_info_t * tsl2561_info = tsl2561_malloc();
  74. tsl2561_init(tsl2561_info, smbus_info);
  75. // Set sensor integration time and gain
  76. tsl2561_set_integration_time_and_gain(tsl2561_info, TSL2561_INTEGRATION_TIME_402MS, TSL2561_GAIN_1X);
  77. //tsl2561_set_integration_time_and_gain(tsl2561_info, TSL2561_INTEGRATION_TIME_402MS, TSL2561_GAIN_16X);
  78. while (1)
  79. {
  80. tsl2561_visible_t visible = 0;
  81. tsl2561_infrared_t infrared = 0;
  82. tsl2561_read(tsl2561_info, &visible, &infrared);
  83. printf("\nFull spectrum: %d\n", visible + infrared);
  84. printf("Infrared: %d\n", infrared);
  85. printf("Visible: %d\n", visible);
  86. printf("Lux: %d\n", tsl2561_compute_lux(tsl2561_info, visible, infrared));
  87. vTaskDelay(2000 / portTICK_RATE_MS);
  88. }
  89. }
  90. void init_tsl2561()
  91. {
  92. xTaskCreate(&tsl2561_task, "tsl2561_task", 2048, NULL, 5, NULL);
  93. // I2C/SMBus Test application
  94. //extern void test_smbus_task(void * pvParameter);
  95. //xTaskCreate(&test_smbus_task, "test_smbus_task", 2048, NULL, 5, NULL);
  96. }
  97. #endif