|
2 | 2 |
|
3 | 3 | #include <chrono> |
4 | 4 |
|
5 | | -#include "freertos/FreeRTOS.h" |
6 | | -#include "freertos/task.h" |
| 5 | +#include <freertos/FreeRTOS.h> |
| 6 | +#include <freertos/task.h> |
7 | 7 |
|
8 | | -#include "driver/i2c.h" |
9 | | -#include "esp_heap_caps.h" |
10 | | -#include "mdns.h" |
11 | | -#include "nvs_flash.h" |
| 8 | +#include <esp_heap_caps.h> |
| 9 | +#include <mdns.h> |
| 10 | +#include <nvs_flash.h> |
12 | 11 |
|
13 | 12 | #include "format.hpp" |
14 | 13 | #include "gaussian.hpp" |
| 14 | +#include "i2c.hpp" |
15 | 15 | #include "led.hpp" |
16 | 16 | #include "oneshot_adc.hpp" |
17 | 17 | #include "task.hpp" |
@@ -154,44 +154,19 @@ extern "C" void app_main(void) { |
154 | 154 |
|
155 | 155 | // initialize the i2c bus (for RTC) |
156 | 156 | logger.info("Initializing I2C"); |
157 | | - i2c_config_t i2c_cfg; |
158 | | - memset(&i2c_cfg, 0, sizeof(i2c_cfg)); |
159 | | - i2c_cfg.sda_io_num = GPIO_NUM_12; |
160 | | - i2c_cfg.scl_io_num = GPIO_NUM_14; |
161 | | - i2c_cfg.mode = I2C_MODE_MASTER; |
162 | | - i2c_cfg.sda_pullup_en = GPIO_PULLUP_ENABLE; |
163 | | - i2c_cfg.scl_pullup_en = GPIO_PULLUP_ENABLE; |
164 | | - i2c_cfg.master.clk_speed = (400*1000); |
165 | | - err = i2c_param_config(I2C_NUM_0, &i2c_cfg); |
166 | | - if (err != ESP_OK) |
167 | | - logger.error("config i2c failed {} '{}'", err, esp_err_to_name(err)); |
168 | | - err = i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0); |
169 | | - if (err != ESP_OK) |
170 | | - logger.error("install i2c driver failed {} '{}'", err, esp_err_to_name(err)); |
171 | | - static const int I2C_TIMEOUT_MS = 10; |
172 | | - auto bm8563_write = [](uint8_t *write_data, size_t write_len) { |
173 | | - i2c_master_write_to_device(I2C_NUM_0, |
174 | | - Bm8563::ADDRESS, |
175 | | - write_data, |
176 | | - write_len, |
177 | | - I2C_TIMEOUT_MS / portTICK_PERIOD_MS); |
178 | | - }; |
179 | | - auto bm8563_read = [](uint8_t reg_addr, uint8_t *read_data, size_t read_len) { |
180 | | - i2c_master_write_read_device(I2C_NUM_0, |
181 | | - Bm8563::ADDRESS, |
182 | | - ®_addr, |
183 | | - 1, // size of addr |
184 | | - read_data, |
185 | | - read_len, |
186 | | - I2C_TIMEOUT_MS / portTICK_PERIOD_MS); |
187 | | - |
188 | | - }; |
| 157 | + espp::I2c i2c({ |
| 158 | + .port = I2C_NUM_0, |
| 159 | + .sda_io_num = GPIO_NUM_12, |
| 160 | + .scl_io_num = GPIO_NUM_14, |
| 161 | + .sda_pullup_en = GPIO_PULLUP_ENABLE, |
| 162 | + .scl_pullup_en = GPIO_PULLUP_ENABLE, |
| 163 | + }); |
189 | 164 |
|
190 | 165 | // initialize RTC |
191 | 166 | logger.info("Initializing RTC"); |
192 | | - Bm8563 bm8563(Bm8563::Config{ |
193 | | - .write = bm8563_write, |
194 | | - .read = bm8563_read, |
| 167 | + espp::Bm8563 bm8563({ |
| 168 | + .write = std::bind(&espp::I2c::write, &i2c, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3), |
| 169 | + .read = std::bind(&espp::I2c::read_at_register, &i2c, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4), |
195 | 170 | .log_level = espp::Logger::Verbosity::WARN |
196 | 171 | }); |
197 | 172 |
|
|
0 commit comments