1
0
Weather_ESP/main/main.c

120 lines
2.9 KiB
C
Raw Normal View History

//#include "freertos/FreeRTOS.h"
//#include "esp_wifi.h"
//#include "esp_system.h"
//#include "esp_event.h"
//#include "esp_event_loop.h"
//#include "nvs_flash.h"
//#include "driver/gpio.h"
#include "bme280.h"
//#include "ssd1366.h"
#include "driver/i2c.h"
#include <u8g2.h>
#include "u8g2_esp32_hal.h"
#include <esp_log.h>
2020-02-03 09:02:25 +01:00
void i2c_setup()
{
printf("Setting up I<>C driver... ");
//i2c_driver_install(0, I2C_MODE_MASTER, 0, 0, 0);
i2c_config_t config;
config.mode = I2C_MODE_MASTER;
config.sda_io_num = 18;
config.sda_pullup_en = GPIO_PULLUP_ENABLE;
config.scl_io_num = 19;
config.scl_pullup_en = GPIO_PULLUP_ENABLE;
config.master.clk_speed = 100000;
i2c_param_config(I2C_NUM_0, &config);
printf("Set driver parameters... ");
esp_err_t err = i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0);
if (err == ESP_OK)
printf("Driver installed!\n");
else if (err == ESP_ERR_INVALID_ARG)
printf("Driver install failed, invalid arguments!\n");
else
printf("Driver install failed!\n");
}
void i2c_detect()
{
printf("Scanning I<>C bus:\n");
uint8_t address;
printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\r\n");
for (int i = 0; i < 128; i += 16) {
printf("%02x: ", i);
for (int j = 0; j < 16; j++) {
fflush(stdout);
address = i + j;
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (address << 1) | I2C_MASTER_WRITE, 0x1);
i2c_master_stop(cmd);
esp_err_t ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 50 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
if (ret == ESP_OK) {
printf("%02x ", address);
} else if (ret == ESP_ERR_TIMEOUT) {
printf("UU ");
} else {
printf("-- ");
}
}
printf("\r\n");
}
}
void i2c_shutdown()
{
printf("Shutting down I<>C bus... ");
esp_err_t err = i2c_driver_delete(I2C_NUM_0);
if (err == ESP_ERR_INVALID_ARG)
printf("Failed, invalid arguments!\n");
else
printf("Success!\n");
}
void app_main(void)
{
i2c_setup();
i2c_detect();
i2c_shutdown();
u8g2_esp32_hal_t u8g2_esp32_hal = U8G2_ESP32_HAL_DEFAULT;
u8g2_esp32_hal.sda = 18;
u8g2_esp32_hal.scl = 19;
u8g2_esp32_hal_init(u8g2_esp32_hal);
u8g2_t u8g2;
u8g2_Setup_ssd1306_i2c_128x64_vcomh0_f(&u8g2, U8G2_R0, u8g2_esp32_i2c_byte_cb,
u8g2_esp32_gpio_and_delay_cb);
//u8x8_SetI2CAddress(&u8g2.u8x8,0x78);
u8x8_SetI2CAddress(&u8g2.u8x8,0x3C << 1);
u8g2_InitDisplay(&u8g2); // send init sequence to the display, display is in sleep mode after this,
u8g2_SetPowerSave(&u8g2, 0); // wake up display
u8g2_SetContrast(&u8g2, 50);
u8g2_uint_t r = 10;
u8g2_uint_t x = r+1;
u8g2_uint_t y = r+1;
int8_t dx = 2;
int8_t dy = 5;
while (1) {
u8g2_ClearBuffer(&u8g2);
u8g2_DrawDisc(&u8g2, x, y, r, U8G2_DRAW_ALL);
u8g2_DrawFrame(&u8g2, 0, 0, 128, 64);
u8g2_SendBuffer(&u8g2);
x = x + dx;
y = y + dy;
if (x <= 0+r+1 || x >= 127-r-1) {
dx = dx * -1;
}
if (y <= 0+r+1 || y >= 63-r-1) {
dy = dy * -1;
vTaskDelay(10 / portTICK_PERIOD_MS);
}
}
2020-02-03 09:02:25 +01:00
}