xxxxxxxxxx
53
let img;
let imgWidth = 24; // Width of your OLED image
let imgHeight = 9; // Height of your OLED image
let imgNo = "dec"; // Image number identifier for the bitmap array
function preload() {
img = loadImage('month_0011_dec.bmp'); // Replace with your actual image file
}
function setup() {
createCanvas(imgWidth, imgHeight);
img.resize(imgWidth, imgHeight); // Resize image to match specified width and height
img.loadPixels();
let byteArray = [];
for (let y = 0; y < imgHeight; y++) {
let byte = 0;
let bitCount = 0; // Keeps track of 8 bits to pack into one byte
for (let x = 0; x < imgWidth; x++) {
let index = (y * imgWidth + x) * 4; // Pixel index in img.pixels (RGBA)
let brightness = (img.pixels[index] + img.pixels[index + 1] + img.pixels[index + 2]) / 3; // Grayscale value
let bit = brightness < 128 ? 1 : 0; // If brightness < 128, set pixel as "on" (1)
byte = (byte << 1) | bit; // Shift the byte and add the new bit (bit-packing)
bitCount++;
// Push byte to array every 8 bits or at the end of the row
if (bitCount === 8 || x === imgWidth - 1) {
byte = byte << (8 - bitCount); // Align remaining bits if row ends before reaching 8 bits
byteArray.push(byte);
byte = 0;
bitCount = 0;
}
}
}
// Convert the byteArray into a C-style bitmap array
let cArray = `const uint8_t bitmap_${imgNo}[] PROGMEM = {\n`;
for (let i = 0; i < byteArray.length; i++) {
cArray += ` 0x${byteArray[i].toString(16).padStart(2, '0').toUpperCase()},`;
if (i % 16 === 15) cArray += "\n"; // Add line breaks for readability every 16 bytes
}
cArray += "\n};";
console.log(cArray); // Outputs the bitmap array to the console
}
function draw() {
image(img, 0, 0); // Display the image for verification
}