Skip to content

Instantly share code, notes, and snippets.

@sekigon-gonnoc
Last active June 16, 2023 08:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sekigon-gonnoc/817fc00e9c54e4aaf92002cca65187c7 to your computer and use it in GitHub Desktop.
Save sekigon-gonnoc/817fc00e9c54e4aaf92002cca65187c7 to your computer and use it in GitHub Desktop.
Bad Apple!! on OLED
import cv2
import numpy as np
import math
WIDTH=32
HEIGHT=24
def frame2array(frame):
img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
img = cv2.resize(img, (WIDTH,HEIGHT))
array = []
for y in range(0,math.ceil(HEIGHT/8)):
r=[]
for x in range(0,WIDTH):
line = img[y*8:y*8+8, x]
d=0
for b in range(0,8):
c = 1 if line[b] > 127 else 0
d = d + (c << b)
r.append(d)
array.append(r)
return np.array(array, dtype=np.uint8)
video = cv2.VideoCapture('bad_apple.mp4')
with open ('badapple.bin', 'wb') as f:
while(True):
ret, frame = video.read()
if not ret:
break
array = frame2array(frame)
f.write(array)
#include "oled/oled_driver.h"
// http://elm-chan.org/junk/32bit/binclude_j.html
/* Import a binary file */
#define IMPORT_BIN(sect, file, sym) \
asm(".section " #sect "\n" /* Change section */ \
".balign 4\n" /* Word alignment */ \
".global " #sym "\n" /* Export the object address */ \
#sym ":\n" /* Define the object label */ \
".incbin \"" file "\"\n" /* Import the file */ \
".global _sizeof_" #sym "\n" /* Export the object size */ \
".set _sizeof_" #sym ", . - " #sym "\n" /* Define the object size */ \
".balign 4\n" /* Word alignment */ \
".section \".text\"\n") /* Restore section */
IMPORT_BIN(".rodata", "badapple.bin", badapple);
extern const char badapple[][3][32], _sizeof_badapple[];
void oled_task_user(void) {
static int cnt = 0;
static uint32_t t;
if (timer_elapsed32(t) > 33){
t = timer_read32();
oled_set_cursor(0, 0);
oled_write_raw(&badapple[cnt++][0][0], 96);
if (cnt >= (int32_t)_sizeof_badapple / 96) {
cnt = 0;
}
}
}
oled_rotation_t oled_init_user(oled_rotation_t rotation) {
return OLED_ROTATION_270;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment