Repo for ESP32 Weather Station Development
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
2.9KB

  1. //#include "freertos/FreeRTOS.h"
  2. //#include "esp_wifi.h"
  3. //#include "esp_system.h"
  4. //#include "esp_event.h"
  5. //#include "esp_event_loop.h"
  6. //#include "nvs_flash.h"
  7. //#include "driver/gpio.h"
  8. #include "bme280.h"
  9. //#include "ssd1366.h"
  10. #include "driver/i2c.h"
  11. #include <u8g2.h>
  12. #include "u8g2_esp32_hal.h"
  13. #include <esp_log.h>
  14. void i2c_setup()
  15. {
  16. printf("Setting up I²C driver... ");
  17. //i2c_driver_install(0, I2C_MODE_MASTER, 0, 0, 0);
  18. i2c_config_t config;
  19. config.mode = I2C_MODE_MASTER;
  20. config.sda_io_num = 18;
  21. config.sda_pullup_en = GPIO_PULLUP_ENABLE;
  22. config.scl_io_num = 19;
  23. config.scl_pullup_en = GPIO_PULLUP_ENABLE;
  24. config.master.clk_speed = 100000;
  25. i2c_param_config(I2C_NUM_0, &config);
  26. printf("Set driver parameters... ");
  27. esp_err_t err = i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0);
  28. if (err == ESP_OK)
  29. printf("Driver installed!\n");
  30. else if (err == ESP_ERR_INVALID_ARG)
  31. printf("Driver install failed, invalid arguments!\n");
  32. else
  33. printf("Driver install failed!\n");
  34. }
  35. void i2c_detect()
  36. {
  37. printf("Scanning I²C bus:\n");
  38. uint8_t address;
  39. printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\r\n");
  40. for (int i = 0; i < 128; i += 16) {
  41. printf("%02x: ", i);
  42. for (int j = 0; j < 16; j++) {
  43. fflush(stdout);
  44. address = i + j;
  45. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  46. i2c_master_start(cmd);
  47. i2c_master_write_byte(cmd, (address << 1) | I2C_MASTER_WRITE, 0x1);
  48. i2c_master_stop(cmd);
  49. esp_err_t ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 50 / portTICK_RATE_MS);
  50. i2c_cmd_link_delete(cmd);
  51. if (ret == ESP_OK) {
  52. printf("%02x ", address);
  53. } else if (ret == ESP_ERR_TIMEOUT) {
  54. printf("UU ");
  55. } else {
  56. printf("-- ");
  57. }
  58. }
  59. printf("\r\n");
  60. }
  61. }
  62. void i2c_shutdown()
  63. {
  64. printf("Shutting down I²C bus... ");
  65. esp_err_t err = i2c_driver_delete(I2C_NUM_0);
  66. if (err == ESP_ERR_INVALID_ARG)
  67. printf("Failed, invalid arguments!\n");
  68. else
  69. printf("Success!\n");
  70. }
  71. void app_main(void)
  72. {
  73. i2c_setup();
  74. i2c_detect();
  75. i2c_shutdown();
  76. u8g2_esp32_hal_t u8g2_esp32_hal = U8G2_ESP32_HAL_DEFAULT;
  77. u8g2_esp32_hal.sda = 18;
  78. u8g2_esp32_hal.scl = 19;
  79. u8g2_esp32_hal_init(u8g2_esp32_hal);
  80. u8g2_t u8g2;
  81. u8g2_Setup_ssd1306_i2c_128x64_vcomh0_f(&u8g2, U8G2_R0, u8g2_esp32_i2c_byte_cb,
  82. u8g2_esp32_gpio_and_delay_cb);
  83. //u8x8_SetI2CAddress(&u8g2.u8x8,0x78);
  84. u8x8_SetI2CAddress(&u8g2.u8x8,0x3C << 1);
  85. u8g2_InitDisplay(&u8g2); // send init sequence to the display, display is in sleep mode after this,
  86. u8g2_SetPowerSave(&u8g2, 0); // wake up display
  87. u8g2_SetContrast(&u8g2, 50);
  88. u8g2_uint_t r = 10;
  89. u8g2_uint_t x = r+1;
  90. u8g2_uint_t y = r+1;
  91. int8_t dx = 2;
  92. int8_t dy = 5;
  93. while (1) {
  94. u8g2_ClearBuffer(&u8g2);
  95. u8g2_DrawDisc(&u8g2, x, y, r, U8G2_DRAW_ALL);
  96. u8g2_DrawFrame(&u8g2, 0, 0, 128, 64);
  97. u8g2_SendBuffer(&u8g2);
  98. x = x + dx;
  99. y = y + dy;
  100. if (x <= 0+r+1 || x >= 127-r-1) {
  101. dx = dx * -1;
  102. }
  103. if (y <= 0+r+1 || y >= 63-r-1) {
  104. dy = dy * -1;
  105. vTaskDelay(10 / portTICK_PERIOD_MS);
  106. }
  107. }
  108. }