浏览代码

Added tsl2561 modules2

Thomas Chef 7 月之前
父节点
当前提交
bdec962547
共有 9 个文件被更改,包括 130 次插入107 次删除
  1. 6 0
      .gitmodules
  2. 1 0
      components/esp32-smbus
  3. 1 0
      components/esp32-tsl2561
  4. 1 0
      main/CMakeLists.txt
  5. 2 1
      main/config.h
  6. 115 0
      main/lux.c
  7. 1 1
      main/tsl2561.h
  8. 3 3
      main/main.c
  9. 0 102
      main/tsl2561.c

+ 6 - 0
.gitmodules

@@ -4,3 +4,9 @@
 [submodule "components/esp32-owb"]
 	path = components/esp32-owb
 	url = https://github.com/DavidAntliff/esp32-owb.git
+[submodule "components/esp32-smbus"]
+	path = components/esp32-smbus
+	url = https://github.com/DavidAntliff/esp32-smbus.git
+[submodule "components/esp32-tsl2561"]
+	path = components/esp32-tsl2561
+	url = https://github.com/DavidAntliff/esp32-tsl2561.git

+ 1 - 0
components/esp32-smbus

@@ -0,0 +1 @@
+Subproject commit 1bca026c4fe8d28f9cb76da30da05e8a70a8231f

+ 1 - 0
components/esp32-tsl2561

@@ -0,0 +1 @@
+Subproject commit 55185696cfbd9eb91da9cb262b277d4c29bd08ee

+ 1 - 0
main/CMakeLists.txt

@@ -6,4 +6,5 @@ set(COMPONENT_ADD_INCLUDEDIRS "." "./Sensors")
 
 
 
+
 register_component()

+ 2 - 1
main/config.h

@@ -19,10 +19,11 @@
 #define ONE_WIRE_BUS_IO           GPIO_NUM_26   // Temp sensors
 
 
-//#define ENABLE_TSL2561
+#define ENABLE_LUX_SENSOR
 #define I2C_MASTER_SCL  4   // GPIO number for I2C Master clock line.
 #define I2C_MASTER_SDA  5   // GPIO number for I2C Master data line.
 #define I2C_MASTER_FREQUENCY 400000  // I2C master clock frequency
+#define CONFIG_TSL2561_I2C_ADDRESS 0x39
 
 
 

+ 115 - 0
main/lux.c

@@ -0,0 +1,115 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2017 David Antliff
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+/**
+ * @file app_main.c
+ * @brief Example application for the TSL2561 Light-to-Digital Converter.
+ */
+
+
+#include <stdio.h>
+#include "freertos/FreeRTOS.h"
+#include "freertos/task.h"
+#include "esp_system.h"
+#include "driver/gpio.h"
+#include "driver/i2c.h"
+#include "esp_log.h"
+#include "sdkconfig.h"
+
+#include "smbus.h"
+#include "tsl2561.h"
+#include "config.h"
+
+#ifdef ENABLE_LUX_SENSOR
+
+#define TAG "app"
+
+#define I2C_MASTER_NUM           I2C_NUM_0
+#define I2C_MASTER_TX_BUF_LEN    0                     // disabled
+#define I2C_MASTER_RX_BUF_LEN    0                     // disabled
+#define I2C_MASTER_FREQ_HZ       100000
+#define I2C_MASTER_SDA_IO        I2C_MASTER_SDA
+#define I2C_MASTER_SCL_IO        I2C_MASTER_SCL
+
+static void i2c_master_init(void)
+{
+    int i2c_master_port = I2C_MASTER_NUM;
+    i2c_config_t conf;
+    conf.mode = I2C_MODE_MASTER;
+    conf.sda_io_num = I2C_MASTER_SDA_IO;
+    conf.sda_pullup_en = GPIO_PULLUP_DISABLE;  // GY-2561 provides 10kΩ pullups
+    conf.scl_io_num = I2C_MASTER_SCL_IO;
+    conf.scl_pullup_en = GPIO_PULLUP_DISABLE;  // GY-2561 provides 10kΩ pullups
+    conf.master.clk_speed = I2C_MASTER_FREQ_HZ;
+    i2c_param_config(i2c_master_port, &conf);
+    i2c_driver_install(i2c_master_port, conf.mode,
+                       I2C_MASTER_RX_BUF_LEN,
+                       I2C_MASTER_TX_BUF_LEN, 0);
+}
+
+static void tsl2561_task(void * pvParameter)
+{
+    // Set up I2C
+    i2c_master_init();
+    i2c_port_t i2c_num = I2C_MASTER_NUM;
+    uint8_t address = CONFIG_TSL2561_I2C_ADDRESS;
+
+    // Set up the SMBus
+    smbus_info_t * smbus_info = smbus_malloc();
+    smbus_init(smbus_info, i2c_num, address);
+    smbus_set_timeout(smbus_info, 1000 / portTICK_RATE_MS);
+
+    // Set up the TSL2561 device
+    tsl2561_info_t * tsl2561_info = tsl2561_malloc();
+    tsl2561_init(tsl2561_info, smbus_info);
+
+    // Set sensor integration time and gain
+    tsl2561_set_integration_time_and_gain(tsl2561_info, TSL2561_INTEGRATION_TIME_402MS, TSL2561_GAIN_1X);
+    //tsl2561_set_integration_time_and_gain(tsl2561_info, TSL2561_INTEGRATION_TIME_402MS, TSL2561_GAIN_16X);
+
+    while (1)
+    {
+        tsl2561_visible_t visible = 0;
+        tsl2561_infrared_t infrared = 0;
+        tsl2561_read(tsl2561_info, &visible, &infrared);
+
+        printf("\nFull spectrum: %d\n", visible + infrared);
+        printf("Infrared:      %d\n", infrared);
+        printf("Visible:       %d\n", visible);
+        printf("Lux:           %d\n", tsl2561_compute_lux(tsl2561_info, visible, infrared));
+
+        vTaskDelay(2000 / portTICK_RATE_MS);
+    }
+}
+
+void init_tsl2561()
+{
+    xTaskCreate(&tsl2561_task, "tsl2561_task", 2048, NULL, 5, NULL);
+
+    // I2C/SMBus Test application
+    //extern void test_smbus_task(void * pvParameter);
+    //xTaskCreate(&test_smbus_task, "test_smbus_task", 2048, NULL, 5, NULL);
+}
+
+#endif

+ 1 - 1
main/tsl2561.h

@@ -5,7 +5,7 @@
 #include "freertos/queue.h"
 #include <stdint.h>
 
-void tsl2561Init();
+void init_tsl2561();
 
 
 

+ 3 - 3
main/main.c

@@ -8,7 +8,7 @@
 #include "rxTimer.h"
 #include "transceiver.h"
 #include "receiver.h"
-#include "tsl2561.h"
+#include "lux.h"
 
 
 // Chip info:
@@ -56,8 +56,8 @@ void app_main(void)
     mqtt_init();
 #endif
 
-#ifdef ENABLE_TSL2561
-    tsl2561Init();
+#ifdef ENABLE_LUX_SENSOR
+    init_tsl2561();
 #endif
 
     TickType_t vLastWakeTime = xTaskGetTickCount();

+ 0 - 102
main/tsl2561.c

@@ -1,102 +0,0 @@
-/* i2c - Simple example
-
-   Simple I2C example that shows how to initialize I2C
-   as well as reading and writing from and to registers for a sensor connected over I2C.
-
-   The sensor used in this example is a MPU9250 inertial measurement unit.
-
-   For other examples please check:
-   https://github.com/espressif/esp-idf/tree/master/examples
-
-   See README.md file to get detailed usage of this example.
-
-   This example code is in the Public Domain (or CC0 licensed, at your option.)
-
-   Unless required by applicable law or agreed to in writing, this
-   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
-   CONDITIONS OF ANY KIND, either express or implied.
-*/
-#include <stdio.h>
-#include "esp_log.h"
-#include "driver/i2c.h"
-#include "config.h"
-
-#ifdef ENABLE_TSL2561
-
-static const char *TAG = "i2c-simple-example";
-
-#define I2C_MASTER_SCL_IO           I2C_MASTER_SCL             /*!< GPIO number used for I2C master clock */
-#define I2C_MASTER_SDA_IO           I2C_MASTER_SDA             /*!< GPIO number used for I2C master data  */
-#define I2C_MASTER_NUM              0                          /*!< I2C master i2c port number, the number of i2c peripheral interfaces available will depend on the chip */
-#define I2C_MASTER_FREQ_HZ          400000                     /*!< I2C master clock frequency */
-#define I2C_MASTER_TX_BUF_DISABLE   0                          /*!< I2C master doesn't need buffer */
-#define I2C_MASTER_RX_BUF_DISABLE   0                          /*!< I2C master doesn't need buffer */
-#define I2C_MASTER_TIMEOUT_MS       1000
-
-#define MPU9250_SENSOR_ADDR                 0x68        /*!< Slave address of the MPU9250 sensor */
-#define MPU9250_WHO_AM_I_REG_ADDR           0x75        /*!< Register addresses of the "who am I" register */
-
-#define MPU9250_PWR_MGMT_1_REG_ADDR         0x6B        /*!< Register addresses of the power managment register */
-#define MPU9250_RESET_BIT                   7
-
-/**
- * @brief Read a sequence of bytes from a MPU9250 sensor registers
- */
-static esp_err_t mpu9250_register_read(uint8_t reg_addr, uint8_t *data, size_t len)
-{
-    return i2c_master_write_read_device(I2C_MASTER_NUM, MPU9250_SENSOR_ADDR, &reg_addr, 1, data, len, I2C_MASTER_TIMEOUT_MS / portTICK_RATE_MS);
-}
-
-/**
- * @brief Write a byte to a MPU9250 sensor register
- */
-static esp_err_t mpu9250_register_write_byte(uint8_t reg_addr, uint8_t data)
-{
-    int ret;
-    uint8_t write_buf[2] = {reg_addr, data};
-
-    ret = i2c_master_write_to_device(I2C_MASTER_NUM, MPU9250_SENSOR_ADDR, write_buf, sizeof(write_buf), I2C_MASTER_TIMEOUT_MS / portTICK_RATE_MS);
-
-    return ret;
-}
-
-/**
- * @brief i2c master initialization
- */
-static esp_err_t i2c_master_init(void)
-{
-    int i2c_master_port = I2C_MASTER_NUM;
-
-    i2c_config_t conf = {
-        .mode = I2C_MODE_MASTER,
-        .sda_io_num = I2C_MASTER_SDA_IO,
-        .scl_io_num = I2C_MASTER_SCL_IO,
-        .sda_pullup_en = GPIO_PULLUP_ENABLE,
-        .scl_pullup_en = GPIO_PULLUP_ENABLE,
-        .master.clk_speed = I2C_MASTER_FREQ_HZ,
-    };
-
-    i2c_param_config(i2c_master_port, &conf);
-
-    return i2c_driver_install(i2c_master_port, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
-}
-
-
-void tsl2561Init(void)
-{
-    uint8_t data[2];
-    ESP_ERROR_CHECK(i2c_master_init());
-    ESP_LOGI(TAG, "I2C initialized successfully");
-
-    /* Read the MPU9250 WHO_AM_I register, on power up the register should have the value 0x71 */
-    ESP_ERROR_CHECK(mpu9250_register_read(MPU9250_WHO_AM_I_REG_ADDR, data, 1));
-    ESP_LOGI(TAG, "WHO_AM_I = %X", data[0]);
-
-    /* Demonstrate writing by reseting the MPU9250 */
-    ESP_ERROR_CHECK(mpu9250_register_write_byte(MPU9250_PWR_MGMT_1_REG_ADDR, 1 << MPU9250_RESET_BIT));
-
-    ESP_ERROR_CHECK(i2c_driver_delete(I2C_MASTER_NUM));
-    ESP_LOGI(TAG, "I2C unitialized successfully");
-}
-
-#endif