瀏覽代碼

Add SSD1306 display initialization and I2C bus configuration

dacowars 2 周之前
父節點
當前提交
89980a4041
共有 2 個文件被更改,包括 74 次插入0 次删除
  1. 1 0
      main/idf_component.yml
  2. 73 0
      main/main.c

+ 1 - 0
main/idf_component.yml

@@ -20,3 +20,4 @@ dependencies:
   GPS_parser:
     git: https://dacogogs.duckdns.org/dacowars/libreria_GPSparser.git
     version: '*'
+  chill-sam/ssd1306: ^1.1.0

+ 73 - 0
main/main.c

@@ -12,10 +12,13 @@
 #include <sys/stat.h>
 #include "esp_vfs_fat.h"
 #include "sdmmc_cmd.h"
+#include "ssd1306.h"
+#include <driver/i2c_master.h>
 
 const char *DHT_TAG = "DHT22";
 const char *GPS_TAG = "GPS_PARSER";
 const char *SD_TAG = "SD_CARD";
+const char *OLED_TAG = "SSD1306OLED";
 
 #define MOUNT_POINT "/sdcard"
 
@@ -35,6 +38,8 @@ void uart_configurator();
 
 static esp_err_t s_example_write_file(const char *path, char *data);
 
+static i2c_master_bus_handle_t i2c_bus0_init(gpio_num_t sda, gpio_num_t scl, uint32_t hz);
+
 
 void app_main(void)
 {
@@ -157,6 +162,58 @@ void app_main(void)
     spi_bus_free(host.slot);
     */
 
+
+    // Initialize display
+    /*
+    i2c_master_bus_handle_t i2c_bus =
+        i2c_bus0_init(GPIO_NUM_21, GPIO_NUM_22, 400000);
+
+    ssd1306_config_t cfg = {
+        .width  = 128,
+        .height = 64,
+        .fb     = NULL, // let driver allocate internally
+        .fb_len = 0,
+        .iface.i2c =
+            {
+                .port     = I2C_NUM_0,
+                .addr     = 0x3C,        // typical SSD1306 I2C address
+                .rst_gpio = GPIO_NUM_NC, // no reset pin
+            },
+    };
+
+    ssd1306_handle_t d = NULL;
+    ESP_ERROR_CHECK(ssd1306_new_i2c(&cfg, &d));
+
+    // ----- Clear screen -----
+    ESP_ERROR_CHECK(ssd1306_clear(d));
+
+    // ----- Draw pixels in corners of screen -----
+    ESP_ERROR_CHECK(ssd1306_draw_pixel(d, 0, 0, true));
+    ESP_ERROR_CHECK(ssd1306_draw_pixel(d, cfg.width - 1, 0, true));
+    ESP_ERROR_CHECK(ssd1306_draw_pixel(d, 0, cfg.height - 1, true));
+    ESP_ERROR_CHECK(ssd1306_draw_pixel(d, cfg.width - 1, cfg.height - 1, true));
+
+    // ----- Draw rectangles -----
+    ESP_ERROR_CHECK(ssd1306_draw_rect(d, 2, 2, 40, 20, false));
+    ESP_ERROR_CHECK(ssd1306_draw_rect(d, 2, 24, 32, 16, true));
+
+    // ----- Draw circles -----
+    ESP_ERROR_CHECK(ssd1306_draw_circle(d, 32, 52, 8, true));
+    ESP_ERROR_CHECK(ssd1306_draw_circle(d, 100, 52, 4, false));
+
+    // ----- Draw lines -----
+    ESP_ERROR_CHECK(ssd1306_draw_line(d, 2, 2, 40, 20, true));
+    ESP_ERROR_CHECK(ssd1306_draw_line(d, 32, 52, 100, 52, true));
+
+    // ----- Draw text -----
+    ESP_ERROR_CHECK(ssd1306_draw_text(d, 48, 2, "OK!", true));
+    ESP_ERROR_CHECK(
+        ssd1306_draw_text_scaled(d, 48, 10, "Hello\nWorld!", true, 2));
+
+    ESP_ERROR_CHECK(ssd1306_display(d));
+
+    ESP_LOGI(OLED_TAG, "Display updated successfully.");
+    */
 }
 
 
@@ -191,3 +248,19 @@ static esp_err_t s_example_write_file(const char *path, char *data)
     return ESP_OK;
 }
 
+// Init i2c
+static i2c_master_bus_handle_t i2c_bus0_init(gpio_num_t sda, gpio_num_t scl, uint32_t hz) {
+    i2c_master_bus_config_t bus_cfg = {
+        .i2c_port                     = I2C_NUM_0,
+        .sda_io_num                   = sda,
+        .scl_io_num                   = scl,
+        .clk_source                   = I2C_CLK_SRC_DEFAULT,
+        .glitch_ignore_cnt            = 0,
+        .flags.enable_internal_pullup = true,
+    };
+    i2c_master_bus_handle_t bus = NULL;
+    ESP_ERROR_CHECK(i2c_new_master_bus(&bus_cfg, &bus));
+    // NOTE: per-device speed is set when adding the device (in driver bind).
+    return bus;
+}
+