ESP32 problem - updating SSD1306 on second core

What I’m trying to do:


I’m turning a PS/2 keyboard from 1998 into a 8-bit synthesizer piano thing. It has an audio jack connected to one of the DAC pins, a 27 RGB led strip for cool light effects (using FastLED library) and an SSD1306 128x64 OLED display. I wrote the synthesizer part - it works. The led strip works and OLED also works. Hooray!

MH-ET LIVE ESP32 Devkit

The problem, however, is that I can’t generate clean sound with RGB led strip or OLED update processes running within the same core. Luckily, this ESP32 devkit has 2 cores. I’ve successfuly made the 27 RGB diodes run on the second core, but when I try to update the OLED I get tons of weird artifacts like this:


YouTube video of OLED artifacts

I really want to get the OLED working on the second core so I could do live updates while generating synth sounds.

Repro code:

#include "SSD1306Wire.h"

SSD1306Wire  display(0x3c, 21, 22);

// Second core test
TaskHandle_t Task1;

unsigned long last_update;

void CoreTest(void * parameter) {
  while (true) {
    if (millis() - last_update >= 20) {
      last_update = millis();
      SSD1306_DrawSineWave(30, 64, 200);
      delay(20);
    }
  }
}

int sinewave[256] = {
   0,    3,    6,    9,   13,   16,   19,   22, 
  25,   28,   31,   34,   37,   40,   43,   46, 
  49,   52,   55,   58,   61,   63,   66,   69, 
  71,   74,   77,   79,   81,   84,   86,   89, 
  91,   93,   95,   97,   99,  101,  103,  105, 
 107,  108,  110,  112,  113,  115,  116,  117, 
 118,  120,  121,  122,  123,  124,  124,  125, 
 126,  126,  127,  127,  127,  128,  128,  128, 
 128,  128,  128,  128,  127,  127,  126,  126, 
 125,  125,  124,  123,  122,  121,  120,  119, 
 118,  117,  115,  114,  112,  111,  109,  108, 
 106,  104,  102,  100,   98,   96,   94,   92, 
  90,   87,   85,   83,   80,   78,   75,   73, 
  70,   67,   65,   62,   59,   56,   53,   51, 
  48,   45,   42,   39,   36,   33,   30,   27, 
  24,   20,   17,   14,   11,    8,    5,    2, 
  -2,   -5,   -8,  -11,  -14,  -17,  -20,  -24, 
 -27,  -30,  -33,  -36,  -39,  -42,  -45,  -48, 
 -51,  -53,  -56,  -59,  -62,  -65,  -67,  -70, 
 -73,  -75,  -78,  -80,  -83,  -85,  -87,  -90, 
 -92,  -94,  -96,  -98, -100, -102, -104, -106, 
-108, -109, -111, -112, -114, -115, -117, -118, 
-119, -120, -121, -122, -123, -124, -125, -125, 
-126, -126, -127, -127, -128, -128, -128, -128, 
-128, -128, -128, -127, -127, -127, -126, -126, 
-125, -124, -124, -123, -122, -121, -120, -118, 
-117, -116, -115, -113, -112, -110, -108, -107, 
-105, -103, -101,  -99,  -97,  -95,  -93,  -91, 
 -89,  -86,  -84,  -81,  -79,  -77,  -74,  -71, 
 -69,  -66,  -63,  -61,  -58,  -55,  -52,  -49, 
 -46,  -43,  -40,  -37,  -34,  -31,  -28,  -25, 
 -22,  -19,  -16,  -13,   -9,   -6,   -3,    0,
};

void SSD1306_DrawSineWave(byte amplitude, int wavelength, int move_speed) {
  /*
    amplitude = 0 - 30; (height of sinde wave)
    wavelength = >0; lenght in pixels of one pulse
    move_speed = >=0; movement of wave, pixels per second
   */
   display.clear();
   unsigned long displacement = (millis() * move_speed / 1000);
   for (byte x = 0; x <= 127; x++) {
    int pos0 = sinewave[((x + displacement) % wavelength) * 255 / wavelength] * amplitude / 128;
    int pos1 = sinewave[((x + displacement + 1) % wavelength) * 255 / wavelength] * amplitude / 128;
    display.drawLine(x, pos0 + 32,  x, pos1 + 32);
   }
   display.display();
}

void setup() {
  Serial.begin(115200);
  
  display.init();
  display.flipScreenVertically();

  xTaskCreatePinnedToCore(
    CoreTest,
    "CoreTester",
    100000,
    NULL,
    1,
    &Task1,
    0
  );

}

void loop() {

}

what could possibly be the culprit of this behavior?

Update 1:
Here’s some insight. When updating the OLED with the same picture, the artifacts remain static. The horizontal dot artifacts seem to appear around every 16 pixels.

I've been asking around for 10 hours straight with no progress, heh.

loleris:
I’ve been asking around for 10 hours straight with no progress, heh.

I understand the sentiment, free advice is not what it used to be.

loleris:
The problem, however, is that I can't generate clean sound with RGB led strip or OLED update processes running within the same core.

well the ledstrip library turns of interrupts for sending the signal so that does make sense.

loleris:
I really want to get the OLED working on the second core so I could do live updates while generating synth sounds.

fair enough !

loleris:
what could possibly be the culprit of this behavior?

Dunno !! hence no response, but maybe the ledstrip is the cause ...

Deva_Rishi:
Dunno !! hence no response, but maybe the ledstrip is the cause ...

The repro code doesn't have the led strip or any synth logic and it produces identical artifacts on the OLED :confused: