#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include <WiFi.h>
/* ESP32 S3 WROOM, and SSD1306 I2C OLED Display */
/**************************************************************************
Domain: IoT, RTOS, Embedded
Author : Ravi K
sta_code: AB1202
Description: This is a brief demo of using Wokwi to simulate ESP32 S3 driving I2C based OLED,
using mixed style of FreeRTOS + basic Arduino Framework type of OLED access
Issues: No issues
**************************************************************************/
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 myOled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
String oledline[9]; //One line of text per OLED row. For textsize of 1 there are 8 OLED rows/lines
char sta_code[11] = "AB1201";
char boardWifiMAC[31];
char tmpBuffer[64];
TickType_t currMillis, prevMillis;
int cnt = 0;
static TaskHandle_t count_display_task_handle = NULL;
void displayTextOLED(String oledline[]) {
int jj;
myOled.clearDisplay();
myOled.setTextSize(1);
myOled.setTextColor(SSD1306_WHITE);
myOled.setCursor(0, 0);
for (jj=1; jj<=8; jj++) {
myOled.println(oledline[jj]);
}
myOled.display();
}
void getMacWifiShield(char *macWifiShield)
{
byte mac[6];
WiFi.macAddress(mac);
sprintf(macWifiShield, "%02X%02X%02X%02X%02X%02X", mac[5], mac[4], mac[3], mac[2], mac[1], mac[0]);
};
/* Just counts each time and displays incremented counter on the OLED */
void do_count_display_task(void *pvParameters){
while(1)
{
currMillis = xTaskGetTickCount(); // Get current tick count
if (currMillis - prevMillis > pdMS_TO_TICKS(2000)) {
cnt++;
oledline[3] = String("OLED display cnt: ") + cnt;
displayTextOLED(oledline);
prevMillis = currMillis;
}//millis diff > 1000
// Small delay to avoid excessive CPU usage
vTaskDelay(pdMS_TO_TICKS(50));
} //while(1)
} //do_count_display_task();
/* Init function:
Initialize Serial Port,
initialize OLED,
Get Wifi interface MAC on the ESP32,
and
Spawn the display counter task
*/
void init_system() {
pinMode(RGB_BUILTIN, OUTPUT);
Serial.begin(921600);
Wire.begin(18, 17); //SDA, SCK
// Make sure to use the correct I2C address. Address 0x3C for 128x64
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!myOled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("SSD1306 allocation failed");
for(;;); // Don't proceed, loop forever
}
getMacWifiShield(boardWifiMAC);
oledline[1] = String("Station ID:") + sta_code;
oledline[2] = String(boardWifiMAC);
int jj; for (jj=2; jj<=8; jj++){ oledline[jj]=""; }
displayTextOLED(oledline);
printf("init_system() is done !\n");
vTaskDelay(pdMS_TO_TICKS(3 * 1000));
xTaskCreate(do_count_display_task, "Count display Task", 4096, NULL, 2, &count_display_task_handle);
}
void app_main()
{
init_system();
}
/* ********* Wokwi, dont cry \!******* */
void setup()
{
// Nothing to do here
//I just call the ESP IDF's app_main()
app_main();
}
void loop() {
// Nothing to do here
}