Weird behavior when displaying buffer
RapierXbox opened this issue · 0 comments
RapierXbox commented
I am trying to display a GIF that I converted to binary on my 128x32 OLED display. I am converting the GIF to raw bytes using this Python script. Each 512-byte sector in the file represents a frame. Here is my Python function:
frames = []
for frame in ImageSequence.Iterator(img):
frame = frame.convert('1')
original_width, original_height = frame.size
aspect_ratio = original_width / original_height
if aspect_ratio < (128 / 32):
new_width = 128
new_height = int(128 / aspect_ratio)
else:
new_height = 32
new_width = int(32 * aspect_ratio)
resized_frame = frame.resize((new_width, new_height), PIL.Image.LANCZOS)
new_image = Image.new('1', (128, 32), color=1)
paste_x = (128 - new_width) // 2
paste_y = (32 - new_height) // 2
new_image.paste(resized_frame, (paste_x, paste_y))
frames.append(new_image)
return frames
def RawToFrame(raw):
data = []
for byte in raw:
data.extend([int(bit) for bit in f'{byte:08b}'])
data = data[:128 * 32]
frame = Image.new('1', (128, 32))
frame.putdata(data)
return frame
def GifToRaw(img, out_name):
frames = GIFtoFrame(Image.open(img))
raw = []
for frame in frames:
raw.extend(frame.tobytes())
file = open(out_name + ".oled", 'wb')
file.write(bytearray(raw))
file.close()
After converting, I revert the raw file back to a GIF to verify its correctness, and it works fine. Here is my Arduino code:
for (size_t i = 0; i < oledfilepaths.size(); ++i) {
String oledfilename = oledfilepaths[i].c_str();
Serial.println("Opening: " + oledfilename);
int frames = 0;
int frame = 0;
File oledfile = SD.open("/oled/" + oledfilename);
frames = oledfile.size() / 512;
for (frame; frame < frames; frame++) {
uint8_t framedata[512];
int readfile = oledfile.read(framedata, 512);
obdDumpBuffer(&obd, framedata);
delay(100);
}
oledfile.close();
}
However, when displayed on the OLED, it comes out like this:
This is the preview generated with Python:
Maybe the buffer is stored in a strange way, or perhaps I'm just not understanding it correctly.