-
I have been google(I'm not good at it) on how to initialize an SSD1306 OLED display over I2C and then be able to draw a Graphics2d instance onto the display. and since the thing drawn onto the Graphics2D instance isn't displayed I suspect the initialization code to be the cause. initialization code: display.write((byte) 0xAE);
display.write((byte) 0xA6);
display.write((byte) 0x20, (byte) 0x00);
display.write((byte) 0x40);
display.write((byte) 0xA0);
display.write((byte) 0xC0);
display.write((byte) 0xA8, (byte) 0x3F);
display.write((byte) 0xD3, (byte) 0x00);
display.write((byte) 0xD5, (byte) 0x80);
display.write((byte) 0xD9, (byte) 0x22);
display.write((byte) 0xDA, (byte) 0x12);
display.write((byte) 0xDB, (byte) 0x20);
display.write((byte) 0x8D, (byte) 0x14); code to send the data of the Graphics2D instance to the display: byte[][] pixels = new byte[getHeight()][getWidth()];
for(int y = 0; y < getHeight(); y++){
for(int x = 0; x < getWidth(); x++){
try {
pixels[y][x] = pixelToByte(img.getRGB(x, y));
}catch (Exception e){
int i = 0;
}
}
}
for (int row = 0; row < getHeight(); row++) {
display.write(0xB0 + row);
display.write(0x00);
display.write(0x10);
display.write(pixels[row]);
} Extra context: public int getHeight(){
return 64;
}
public int getWidth(){
return 128;
}
private static byte pixelToByte(int rgb) {
int alpha = (rgb >> 24) & 0xFF;
int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = rgb & 0xFF;
byte pixelData;
if (alpha > 0 && (red > 0 || green > 0 || blue > 0)) {
pixelData = 1;
} else {
pixelData = 0;
}
return pixelData;
} I'm not the best coder and I'm unsure of if the values are correct or not. thanks for any help provided I excuse my poor grammar. English isn't my first language and I have dyslexia |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 8 replies
-
So after posting this I did see the try {
pixels[y][x] = pixelToByte(img.getRGB(x, y));
}catch (Exception e){
int i = 0;
} and I changed that out a bit and no I do not get anything there |
Beta Was this translation helpful? Give feedback.
-
I used a HiLetgo SSD1306 The attached zip initializes the chip and places a line of pixels on the display. Hopefully you can review what I have a see what changes are required in your code. |
Beta Was this translation helpful? Give feedback.
-
I completed an example SSD1306 Vertical address mode. It is in https://github.com/Pi4J/pi4j-example-devices Hope this helps if you have remaining questions on your project. Tom |
Beta Was this translation helpful? Give feedback.
-
Thanks, I will take a look at it |
Beta Was this translation helpful? Give feedback.
-
So now when i have revisited this project I checked out the example for the SSD1306 display and copied the classes required for it to not give me compilation errors and copid some code from the examples class. I get an image displayed but its just jibrish on the display, and I suspect it to be caused by the BufferdImage to Byte array method I have (provided bellow) and if that is the case how should it be made to display the correct data stored I the BufferdImage object? private static void dataToBuffer(BufferedImage img) {
for(int x = 0; x < getWidth(); x++){
for(int y = 0; y < getHeight(); y++){
if(
(float) ((0.3*(
new Color(
img.getRGB(x,y)
).getRed()
)+0.59*(
new Color(
img.getRGB(x,y)
).getGreen()
)+0.11*(
new Color(
img.getRGB(x,y)
).getBlue()
))/255) >= 0.5
){
buffer[x + (y / 8) * getWidth()] |= (1 << (y & 7));
}else{
buffer[x + (y / 8) * getWidth()] &= ~(1 << (y & 7));
}
}
}
} all the data to the display is handled by the SSD1306 class defined in the github repo you provided |
Beta Was this translation helpful? Give feedback.
-
Ok, so I fixed it whit this method instead (provided below) and changed the address mode to horizontal instead of vertical. now the only problem is that the image is upside down but that should be easier to fix. private static void dataToBuffer(BufferedImage img) {
for(int x = 0; x < 128; x++){
for(int y = 0; y < 64; y++){
if(img.getRGB(x,y) == Color.BLACK.getRGB()){
buffer[x + (y / 8) * getWidth()] &= ~(1 << (y & 7));
}else{
buffer[x + (y / 8) * getWidth()] |= (1 << (y & 7));
}
}
}
} |
Beta Was this translation helpful? Give feedback.
-
fixed the rotation by fliping the image before displaying |
Beta Was this translation helpful? Give feedback.
-
Hi @zaze06, very interested in the result of this project! |
Beta Was this translation helpful? Give feedback.
I completed an example SSD1306 Vertical address mode. It is in https://github.com/Pi4J/pi4j-example-devices
Hope this helps if you have remaining questions on your project. Tom