#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define BITMAP_WIDTH 22
#define BITMAP_HEIGHT 12
const unsigned char myBitmap[] PROGMEM = { // SWAP BITS IN BYTE (check) // 22px W x 12px H
0xf8, 0xe1, 0x07, 0xfe, 0xff, 0x1f, 0x06, 0x1e, 0x18, 0x63, 0x0c, 0x30, 0x63, 0x0c, 0x30, 0xfb,
0xed, 0x37, 0xfb, 0xed, 0x37, 0x63, 0x0c, 0x30, 0x63, 0x0c, 0x30, 0x06, 0x1e, 0x18, 0xfe, 0xff,
0x1f, 0xf8, 0xe1, 0x07
}; // https://en.wikipedia.org/wiki/X_BitMap
#include <U8g2lib.h>
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE); // full buffer, rotation 0, reset = none
// https://github.com/olikraus/u8g2/wiki
// https://github.com/olikraus/u8g2/wiki/u8g2setupcpp
// https://github.com/olikraus/u8g2/wiki/u8logreference
int x = random(SCREEN_WIDTH);
int y = random(SCREEN_HEIGHT); // starting location
int xMov, yMov; // H, V movement
// int xMov = 2, yMov = 2; // H, V movement
int MAXX = SCREEN_WIDTH - BITMAP_WIDTH; // size of usable screen width
int MAXY = SCREEN_HEIGHT - BITMAP_HEIGHT; // size of usable screen height
void setup() {
randomSeed(analogRead(A0));
u8g2.begin(); // initialize object
u8g2.clear(); // clear the buffer
u8g2.sendBuffer(); // send buffer to screen
xMov = random(2, 5);
yMov = random(2, 5);
}
void loop() {
bounce(); // call the local function
}
void bounce() {
if (x <= 1 || x >= MAXX) // x boundries
xMov = -xMov;
if (y <= 1 || y >= MAXY) // y boundries
yMov = -yMov;
x += xMov; // next x location
y += yMov; // next y location
u8g2.clearBuffer();
u8g2.drawXBMP(x, y, BITMAP_WIDTH, BITMAP_HEIGHT, myBitmap);
u8g2.sendBuffer();
}Loading
ssd1306
ssd1306