I am trying to get to read individual bits of a byte array. I am basically iterating through the byte array and want to tell whether each individual bit is 0 or 1.
My problem is, I am unable to differentiate between a 0 and 1 bit. The code is always reading each bit as a 1.
This is my code:
const unsigned char bitmap[] = {
0x00,0xFF,0xFF,0x00,0x00,0x00,
0x00,0x00,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF
};
void drawBitmap(Framebuffer *fb) {
uint8_t x = 1;
for (int i = 0; i < sizeof(bitmap); ++i) {
for (int p = 0; p < 8; ++p) {
if ((bitmap[i] >> p) & 1) { // If bit
fb->drawPixel(x, 1); // **RIGHT HERE** --> I AM ALWAYS GETTING THIS AS TRUE
}
x++;
}
}
}
Note that there are some bytes that should be all zeroes (0x00). I am assuming by default these are bytes (8 bits), right? Any ideas why am I unable to differentiate between a 1 and a 0?
Note: Here's the whole code... I am trying to use this library: https://github.com/tibounise/SSD1306-AVR with an atmega328P... This just doesn't make any sensse. Whenever I use "fb->drawPixel(x, 1);" on it's own it works fine, but on that particular function I always get a "1" (a pixel).
#define F_CPU 14745600
#include <stdint.h>
#include <stdio.h>
#include <math.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <inttypes.h>
#include <util/delay.h>
//#include "SSD1306.h"
#include "Framebuffer.h"
const unsigned char bitmap[] = {
0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF
};
void drawBitmap(Framebuffer *fb) {
uint8_t x = 1;
int z = 0;
for (int i = 0; i < sizeof(bitmap); ++i) {
for (int p = 0; p < 8; ++p) {
if ((bitmap[i] >> p) & 1) { // If bit
fb->drawPixel(x,1);
}
x++;
}
}
}
int main(void) {
//const uint8_t *bitmap;
//bitmap = &bitmap1;
Framebuffer fb;
Framebuffer *FB;
//Pointer to the class
FB = &fb;
//fb.drawPixel(5, 5);
drawBitmap(FB);
fb.show();
//delay_ms(1000);
return 0;
}
Any ideas?
Thanks in advance.