Hookup OLED I2C display to temorary display some data.

Wed, 09 Oct 2019 21:00:27 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Wed, 09 Oct 2019 21:00:27 +0200
changeset 6
06a5028dbcdf
parent 5
4b1c65e4d863
child 7
6eafc4c2bf3d

Hookup OLED I2C display to temorary display some data.

main/co2meter.c file | annotate | diff | comparison | revisions
--- a/main/co2meter.c	Tue Oct 08 21:43:18 2019 +0200
+++ b/main/co2meter.c	Wed Oct 09 21:00:27 2019 +0200
@@ -46,6 +46,7 @@
     gettimeofday(&now, NULL);
     int			sleep_time_ms = (now.tv_sec - sleep_enter_time.tv_sec) * 1000 + (now.tv_usec - sleep_enter_time.tv_usec) / 1000;
     int			New_Loop2 = MAIN_LOOP2_INIT;
+    char		buf[65];
     esp_err_t           ret;
 
     Main_Loop1 = MAIN_LOOP1_INIT;
@@ -102,42 +103,25 @@
      * Setup the OLED display.
      * See: https://github.com/nkolban/esp32-snippets/blob/master/hardware/displays/U8G2/
      */
-//    u8g2_esp32_hal_t u8g2_esp32_hal = U8G2_ESP32_HAL_DEFAULT;
-//    u8g2_esp32_hal.sda = PIN_SDA;
-//    u8g2_esp32_hal.scl = PIN_SCL;
-//    u8g2_esp32_hal_init(u8g2_esp32_hal);
-
-//    u8g2_t u8g2; // a structure which will contain all the data for one display
-////    u8g2_Setup_ssd1306_i2c_128x32_univision_f(
-//    u8g2_Setup_sh1106_i2c_128x64_noname_f(
-//		&u8g2,
-//		U8G2_R0,
-//		//u8x8_byte_sw_i2c,
-//		u8g2_esp32_i2c_byte_cb,
-//		u8g2_esp32_gpio_and_delay_cb);  // init u8g2 structure
-//    u8x8_SetI2CAddress(&u8g2.u8x8,0x78);
-
-//    ESP_LOGI(TAG, "u8g2_InitDisplay");
-//    u8g2_InitDisplay(&u8g2); // send init sequence to the display, display is in sleep mode after this,
+    u8g2_esp32_hal_t u8g2_esp32_hal = U8G2_ESP32_HAL_DEFAULT;
+    u8g2_esp32_hal.sda = PIN_SDA;
+    u8g2_esp32_hal.scl = PIN_SCL;
+    u8g2_esp32_hal_init(u8g2_esp32_hal);
 
-//    ESP_LOGI(TAG, "u8g2_SetPowerSave");
-//    u8g2_SetPowerSave(&u8g2, 0); // wake up display
-//    ESP_LOGI(TAG, "u8g2_ClearBuffer");
-//    u8g2_ClearBuffer(&u8g2);
-//    ESP_LOGI(TAG, "u8g2_DrawBox");
-//    u8g2_DrawBox(&u8g2, 0, 26, 80,6);
-//    u8g2_DrawFrame(&u8g2, 0,26,100,6);
+    u8g2_t u8g2; // a structure which will contain all the data for one display
+    u8g2_Setup_sh1106_i2c_128x64_noname_f(
+	&u8g2,
+	U8G2_R0,
+	u8g2_esp32_i2c_byte_cb,
+	u8g2_esp32_gpio_and_delay_cb);  // init u8g2 structure
+    u8x8_SetI2CAddress(&u8g2.u8x8, 0x78);
+    ESP_LOGI(TAG, "u8g2_InitDisplay");
+    u8g2_InitDisplay(&u8g2); // send init sequence to the display, display is in sleep mode after this,
 
-//    ESP_LOGI(TAG, "u8g2_SetFont");
-//    u8g2_SetFont(&u8g2, u8g2_font_ncenB14_tr);
-//    ESP_LOGI(TAG, "u8g2_DrawStr");
-//    u8g2_DrawStr(&u8g2, 2,17,"Hi nkolban!");
-//    ESP_LOGI(TAG, "u8g2_SendBuffer");
-//    u8g2_SendBuffer(&u8g2);
-
-
+    /*
+     * Setup SPIFFS filesystem
+     */
     ESP_LOGI(TAG, "Initializing SPIFFS");
-
     esp_vfs_spiffs_conf_t conf = {
         .base_path = "/spiffs",
         .partition_label = NULL,
@@ -145,8 +129,10 @@
         .format_if_mount_failed = true
     };
 
-    // Use settings defined above to initialize and mount SPIFFS filesystem.
-    // Note: esp_vfs_spiffs_register is an all-in-one convenience function.
+    /*
+     * Use settings defined above to initialize and mount SPIFFS filesystem.
+     * Note: esp_vfs_spiffs_register is an all-in-one convenience function.
+     */
     ret = esp_vfs_spiffs_register(&conf);
 
     if (ret != ESP_OK) {
@@ -157,8 +143,11 @@
         } else {
             ESP_LOGE(TAG, "Failed to initialize SPIFFS (%d)", ret);
         }
-//        _fg = TFT_RED;
-//        TFT_print((char *)"error\r\n", LASTX, LASTY);
+	u8g2_SetFont(&u8g2, u8g2_font_t0_15_tr);
+	u8g2_DrawStr(&u8g2,2,12,"SPIFFS:");
+	u8g2_DrawStr(&u8g2,2,24,"init error");
+	u8g2_SendBuffer(&u8g2);
+        u8g2_SetPowerSave(&u8g2, 0);
         return; // Stop application.
     }
 
@@ -166,15 +155,18 @@
     ret = esp_spiffs_info(NULL, &total, &used);
     if (ret != ESP_OK) {
         ESP_LOGE(TAG, "Failed to get SPIFFS partition information");
-//        _fg = TFT_RED;
-//        TFT_print((char *)"error\r\n", LASTX, LASTY);
+	u8g2_SetFont(&u8g2, u8g2_font_t0_15_tr);
+        u8g2_DrawStr(&u8g2,2,12,"SPIFFS:");
+	u8g2_DrawStr(&u8g2,2,24,"partition error");
+        u8g2_SendBuffer(&u8g2);
+        u8g2_SetPowerSave(&u8g2, 0);
         return; // Stop application.
     } else {
         ESP_LOGI(TAG, "Partition size: %d, used: %d - %d%%", total, used, (used * 100) / total);
     }
 
     // Just to debug, list the /spiffs filesystem.
-#if 1
+#if 0
     DIR *dir = opendir("/spiffs");
     struct dirent* de = readdir(dir);
     while (de) {
@@ -193,15 +185,15 @@
     /*
      * Read or create configuration
      */
-//    TFT_print((char *)"Ophalen configuratie ", LASTX, LASTY);
     read_config();
     read_units();
 
 //add_station((uint8_t *)"MBSE_WLR", (uint8_t *)"abcjkltuv");
 //remove_station((uint8_t *)"MBSE_WLP");
-//sprintf(config.lastSSID, "%s", "BREWER");
-//write_config();
 
+    /*
+     * Create FreeRTOS tasks
+     */
     xSemaphoreDS18B20 = xSemaphoreCreateMutex();
     xSemaphoreADC = xSemaphoreCreateMutex();
     xSemaphoreUnits = xSemaphoreCreateMutex();
@@ -210,7 +202,7 @@
     xTaskCreate(&task_adc,     "task_adc",      2560, NULL, 8, &xTaskADC);
     esp_log_level_set("wifi", ESP_LOG_ERROR);
     xTaskCreate(&task_wifi,    "task_wifi",     4096, NULL, 3, &xTaskWifi);
-    vTaskDelay( (TickType_t)10);
+    vTaskDelay(10 / portTICK_PERIOD_MS);
     xTaskCreate(&task_mqtt,    "task_mqtt",     4096, NULL, 5, &xTaskMQTT);
 
     // esp32-rotary-encoder requires that the GPIO ISR service is installed before calling rotary_encoder_register()
@@ -230,27 +222,6 @@
 //    ESP_ERROR_CHECK(rotary_encoder_set_queue(&info, event_queue));
 
 
-    /* Print chip information */
-//    esp_chip_info_t chip_info;
-//    esp_chip_info(&chip_info);
-//    printf("This is ESP32 chip with %d CPU cores, WiFi%s%s, ",
-//            chip_info.cores,
-//            (chip_info.features & CHIP_FEATURE_BT) ? "/BT" : "",
-//            (chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : "");
-
-//    printf("silicon revision %d, ", chip_info.revision);
-
-//    printf("%dMB %s flash\n", spi_flash_get_chip_size() / (1024 * 1024),
-//            (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");
-
-//    esp_err_t status = adc2_vref_to_gpio(GPIO_NUM_26);
-//    if (status == ESP_OK) {
-//        printf("v_ref routed to GPIO\n");
-//    } else {
-//        printf("failed to route v_ref\n");
-//    }
-//    vTaskDelay(1000 * wakeup_time_sec / portTICK_PERIOD_MS);
-
     /*
      * Main application loop.
      */
@@ -268,6 +239,14 @@
 		    requestWiFi_system(true);
 		    request_ds18b20();
 		    request_adc();
+
+                    u8g2_ClearBuffer(&u8g2);
+		    u8g2_DrawHLine(&u8g2, 0, 14, 128);
+                    u8g2_SetFont(&u8g2, u8g2_font_t0_15_tr);
+		    sprintf(buf, "CO2 meter %s", app_desc->version);
+		    u8g2_uint_t w = u8g2_GetStrWidth(&u8g2, buf);
+                    u8g2_DrawStr(&u8g2, (128 - w) / 2,12, buf);
+
 		    break;
 
 		case MAIN_LOOP1_CONNECT:
@@ -292,6 +271,12 @@
         		    xSemaphoreGive(xSemaphoreDS18B20);
     			}
 
+			u8g2_SetFont(&u8g2, u8g2_font_t0_22b_tf);
+			sprintf(buf, "%.1f °C", temp / 1000.0);
+			u8g2_uint_t w = u8g2_GetUTF8Width(&u8g2, buf);
+			u8g2_DrawUTF8(&u8g2, (128 - w) / 2,40, buf);
+			u8g2_SetFont(&u8g2, u8g2_font_t0_18b_tf);
+
 			/* Copy measured data and calculate results */
 			if (xSemaphoreTake(xSemaphoreUnits, 25) == pdTRUE) {
 			    for (int i = 0; i < 3; i++) {
@@ -307,6 +292,9 @@
 				    if (P < 0)
 				    	P = 0;
 				    units[i].pressure = P;
+				    sprintf(buf, "%.1f", P / 1000.0);
+				    w = u8g2_GetUTF8Width(&u8g2, buf);
+				    u8g2_DrawUTF8(&u8g2, ((42 - w) / 2) + i * 43,63, buf);
 printf("%d volt: %d batt: %d scale: %d  bar: %d\n", i, units[i].pressure_voltage, adc_state->Batt_voltage, 
 		units[i].pressure_voltage / (adc_state->Batt_voltage / 1000) - units[i].pressure_zero, P);
 // Moet die echt op 5 volt?
@@ -317,12 +305,12 @@
 			    }
 			    write_units();
 			    xSemaphoreGive(xSemaphoreUnits);
+			    u8g2_SendBuffer(&u8g2);
+			    u8g2_SetPowerSave(&u8g2, 0); // wake up display
 			}
 		    }
 		    break;
 
-		// calculate stap en data copy
-
 		case MAIN_LOOP1_WAITCON:
 		    if (ready_mqtt())
 			Main_Loop1 = MAIN_LOOP1_SEND;
@@ -413,7 +401,11 @@
 
 	    vTaskDelay(10 / portTICK_PERIOD_MS);
 	}
- 
+
+//	u8g2_ClearBuffer(&u8g2);
+//	u8g2_SendBuffer(&u8g2);
+//	u8g2_SetPowerSave(&u8g2, 1);
+
 //	printf("Simulate deep sleep\n");
 // 	vTaskDelay(1000 * wakeup_time_sec / portTICK_PERIOD_MS);
   

mercurial