#include "main.hpp"
#define I2C_MASTER_SCL_IO 9 /*!< gpio number for I2C master clock */
#define I2C_MASTER_SDA_IO 8 /*!< gpio number for I2C master data */
#define I2C_MASTER_NUM I2C_NUM_0 /*!< I2C port number for master dev */
#define I2C_MASTER_FREQ_HZ 1000000 /*!< I2C master clock frequency */
RTClibWrapper rtc;
static const char TAG[] = "DHT";
/// oled
#define SDA_PIN GPIO_NUM_8
#define SCL_PIN GPIO_NUM_9
#define tag "SSD1306"
void i2c_master_init()
{
i2c_config_t i2c_config = {
.mode = I2C_MODE_MASTER,
.sda_io_num = SDA_PIN,
.scl_io_num = SCL_PIN,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master = {
.clk_speed = 1000000
}
};
i2c_param_config(I2C_NUM_0, &i2c_config);
i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0);
}
void ssd1306_init()
{
esp_err_t espRc;
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (OLED_I2C_ADDRESS << 1) | I2C_MASTER_WRITE, true);
i2c_master_write_byte(cmd, OLED_CONTROL_BYTE_CMD_STREAM, true);
i2c_master_write_byte(cmd, OLED_CMD_SET_CHARGE_PUMP, true);
i2c_master_write_byte(cmd, 0x14, true);
i2c_master_write_byte(cmd, OLED_CMD_SET_SEGMENT_REMAP, true); // reverse left-right mapping
i2c_master_write_byte(cmd, OLED_CMD_SET_COM_SCAN_MODE, true); // reverse up-bottom mapping
i2c_master_write_byte(cmd, OLED_CMD_DISPLAY_ON, true);
i2c_master_stop(cmd);
espRc = i2c_master_cmd_begin(I2C_NUM_0, cmd, 10 / portTICK_PERIOD_MS);
if (espRc == ESP_OK)
{
ESP_LOGI(tag, "OLED configured successfully");
}
else
{
ESP_LOGE(tag, "OLED configuration failed. code: 0x%.2X", espRc);
}
i2c_cmd_link_delete(cmd);
}
void task_ssd1306_display_text(void *pvParameters)
{
char *text = "Hello world!\nMulitine is OK!\nAnother line";
uint8_t text_len = strlen(text);
i2c_cmd_handle_t cmd;
uint8_t cur_page = 0;
cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (OLED_I2C_ADDRESS << 1) | I2C_MASTER_WRITE, true);
i2c_master_write_byte(cmd, OLED_CONTROL_BYTE_CMD_STREAM, true);
i2c_master_write_byte(cmd, 0x00, true); // reset column
i2c_master_write_byte(cmd, 0x10, true);
i2c_master_write_byte(cmd, 0xB0 | cur_page, true); // reset page
i2c_master_stop(cmd);
i2c_master_cmd_begin(I2C_NUM_0, cmd, 10 / portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);
for (uint8_t i = 0; i < text_len; i++)
{
if (text[i] == '\n')
{
cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (OLED_I2C_ADDRESS << 1) | I2C_MASTER_WRITE, true);
i2c_master_write_byte(cmd, OLED_CONTROL_BYTE_CMD_STREAM, true);
i2c_master_write_byte(cmd, 0x00, true); // reset column
i2c_master_write_byte(cmd, 0x10, true);
i2c_master_write_byte(cmd, 0xB0 | ++cur_page, true); // increment page
i2c_master_stop(cmd);
i2c_master_cmd_begin(I2C_NUM_0, cmd, 10 / portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);
}
else
{
cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (OLED_I2C_ADDRESS << 1) | I2C_MASTER_WRITE, true);
i2c_master_write_byte(cmd, OLED_CONTROL_BYTE_DATA_STREAM, true);
i2c_master_write(cmd, font8x8_basic_tr[(uint8_t)text[i]], 8, true);
i2c_master_stop(cmd);
i2c_master_cmd_begin(I2C_NUM_0, cmd, 10 / portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);
}
}
vTaskDelete(NULL);
}
/// oled
// i2c-address Endereço I2C da tela "0x3c"
//extern "C"
//{
// void app_main(void);
//}
void dht_test(void *pvParameters)
{
float temperature, humidity;
#ifdef CONFIG_EXAMPLE_INTERNAL_PULLUP
gpio_set_pull_mode(dht_gpio, GPIO_PULLUP_ONLY);
#endif
while (1)
{
if (dht_read_float_data(SENSOR_TYPE, CONFIG_EXAMPLE_DATA_GPIO, &humidity, &temperature) == ESP_OK)
printf("Humidity: %.1f%% Temp: %.1fC\n", humidity, temperature);
else
printf("Could not read data from sensor\n");
// If you read the sensor data too often, it will heat up
// http://www.kandrsmith.org/RJS/Misc/Hygrometers/dht_sht_how_fast.html
vTaskDelay(pdMS_TO_TICKS(2000));
}
}
extern "C"
{
void app_main(void);
}
void app_main(void)
{
/// oled
i2c_master_init();
ssd1306_init();
xTaskCreate(&task_ssd1306_display_text, "task_ssd1306_display_text", 4096, NULL, 5, NULL);
/// oled
///
/* i2c_config_t conf;
conf.mode = I2C_MODE_MASTER;
conf.sda_io_num = (gpio_num_t)I2C_MASTER_SDA_IO;
conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
conf.scl_io_num = (gpio_num_t)I2C_MASTER_SCL_IO;
conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
conf.master.clk_speed = I2C_MASTER_FREQ_HZ;
conf.clk_flags = 0; //I2C_SCLK_SRC_FLAG_FOR_NOMAL; */
// i2c_param_config(I2C_MASTER_NUM, &conf);
// i2c_driver_install(I2C_MASTER_NUM, conf.mode, 0, 0, 0);
xTaskCreate(dht_test, "dht_test", configMINIMAL_STACK_SIZE * 3, NULL, 5, NULL);
rtc.begin();
Led led1(led1gpio);
Led led2(led2gpio);
LedTask task1(led1, 1000);
LedTask task2(led2, 200);
task1.start();
task2.start();
uint16_t year;
uint8_t month, day;
uint8_t hours, minutes, seconds;
// Configurar data
rtc.setDate(2023, 5, 19);
rtc.setTime(23, 59, 54);
// Espera indefinidamente
while (true)
{
rtc.getDate(year, month, day);
rtc.getTime(hours, minutes, seconds);
printf("Data: %02d/%02d/%02d\n", day, month, year);
printf("Hora: %02d:%02d:%02d\n", hours, minutes, seconds);
vTaskDelay(3000 / portTICK_PERIOD_MS);
//vTaskDelay(portMAX_DELAY);
}
}Loading
esp32-c3-devkitm-1
esp32-c3-devkitm-1