Parcourir la source

Merge pull request #260 from mbartlett21/dithering-3-7

Add a dithered mode to the 3.7 inch display
Wilson il y a 3 ans
Parent
commit
c76950ac96

+ 27 - 3
RaspberryPi_JetsonNano/c/examples/EPD_3in7_test.c

@@ -51,10 +51,13 @@ int EPD_3in7_test(void)
 
     //Create a new image cache
     UBYTE *BlackImage;
-    /* you have to edit the startup_stm32fxxx.s file and set a big enough heap size */
-    UWORD Imagesize = ((EPD_3IN7_WIDTH % 4 == 0)? (EPD_3IN7_WIDTH / 4 ): (EPD_3IN7_WIDTH / 4 + 1)) * EPD_3IN7_HEIGHT;
+
+    // Allocate enough for for 4 bits for each pixel (16 gray)
+    // If you don't use the dithering, this will only need to be 2 bits per pixel
+    // you may have to edit the startup_stm32fxxx.s file and set a big enough heap size
+    int Imagesize = (EPD_3IN7_WIDTH + 1) / 2 * EPD_3IN7_HEIGHT;
     if((BlackImage = (UBYTE *)malloc(Imagesize)) == NULL) {
-        printf("Failed to apply for black memory...\r\n");
+        printf("Failed to apply for memory...\r\n");
         return -1;
     }
 
@@ -154,6 +157,27 @@ int EPD_3in7_test(void)
         DEV_Delay_ms(500);
     }
 
+#if 1   // show dithered bmp
+    EPD_3IN7_4Gray_Init(); // Clear
+    EPD_3IN7_4Gray_Clear();
+
+    EPD_3IN7_1Gray_Init();       //init 1 Gray mode
+    EPD_3IN7_1Gray_Clear();
+
+    Paint_NewImage(BlackImage, 280, 480, 0, WHITE);
+    Paint_SetScale(16);
+    Paint_Clear(WHITE);
+
+    printf("show dithered BMP with Sierra-Lite\r\n");
+    GUI_ReadBmp_16Gray("./pic/3in7_4bit1.bmp", 0, 0);
+    EPD_3IN7_1Gray_Display_Dithered(BlackImage, 0);
+    DEV_Delay_ms(4000);
+
+    printf("show dithered BMP with Floyd-Steinberg\r\n");
+    EPD_3IN7_1Gray_Display_Dithered(BlackImage, 1);
+    DEV_Delay_ms(4000);
+#endif
+
 #endif
     EPD_3IN7_4Gray_Init();
     printf("Clear...\r\n");

+ 80 - 0
RaspberryPi_JetsonNano/c/lib/GUI/GUI_BMPfile.c

@@ -205,6 +205,86 @@ UBYTE GUI_ReadBmp_4Gray(const char *path, UWORD Xstart, UWORD Ystart)
     return 0;
 }
 
+UBYTE GUI_ReadBmp_16Gray(const char *path, UWORD Xstart, UWORD Ystart)
+{
+    FILE *fp;                     //Define a file pointer
+    BMPFILEHEADER bmpFileHeader;  //Define a bmp file header structure
+    BMPINFOHEADER bmpInfoHeader;  //Define a bmp info header structure
+
+    // Binary file open
+    if((fp = fopen(path, "rb")) == NULL) {
+        Debug("Cann't open the file!\n");
+        exit(0);
+    }
+
+    // Set the file pointer from the beginning
+    fseek(fp, 0, SEEK_SET);
+    fread(&bmpFileHeader, sizeof(BMPFILEHEADER), 1, fp);    //sizeof(BMPFILEHEADER) must be 14
+    fread(&bmpInfoHeader, sizeof(BMPINFOHEADER), 1, fp);    //sizeof(BMPFILEHEADER) must be 50
+    printf("pixel = %d * %d\r\n", bmpInfoHeader.biWidth, bmpInfoHeader.biHeight);
+
+    // They are both the same width in bytes
+    // round up to the next byte
+    UWORD Width_Byte = (bmpInfoHeader.biWidth + 1) / 2;
+    UBYTE Image[Width_Byte * bmpInfoHeader.biHeight];
+    memset(Image, 0xFF, Width_Byte * bmpInfoHeader.biHeight);
+
+    // Determine if it is a monochrome bitmap
+    int readbyte = bmpInfoHeader.biBitCount;
+    printf("biBitCount = %d\r\n",readbyte);
+    if(readbyte != 4) {
+        Debug("Bmp image is not a 4-bit bitmap!\n");
+        exit(0);
+    }
+
+    // Determine colors based on the palette
+
+    // A map from palette entry to color
+    UBYTE colors[16];
+    UBYTE i;
+    BMPRGBQUAD rgbData;
+
+    for (i = 0; i < 16; i++){
+        fread(&rgbData, sizeof(BMPRGBQUAD), 1, fp);
+
+        // Work out the closest colour
+        // 16 colours over 0-255 => 0-8 => 0, 9-25 => 1 (17), 26-42 => 2 (34), etc
+
+        // Base it on red
+        colors[i] = (rgbData.rgbRed + 8) / 17;
+    }
+
+    // Read image data into the cache
+    UWORD x, y;
+    UBYTE Rdata;
+    fseek(fp, bmpFileHeader.bOffset, SEEK_SET);
+
+    for (y = 0; y < bmpInfoHeader.biHeight; y++) {//Total display column
+        for (x = 0; x < Width_Byte; x++) {//Show a line in the line
+            if (fread((char *) &Rdata, 1, 1, fp) != 1) {
+                perror("get bmpdata:\r\n");
+                break;
+            }
+            Image[x + (bmpInfoHeader.biHeight - y - 1) * Width_Byte] = Rdata;
+        }
+    }
+    fclose(fp);
+
+    // Refresh the image to the display buffer based on the displayed orientation
+    UBYTE coloridx;
+    printf("bmpInfoHeader.biWidth = %d\r\n", bmpInfoHeader.biWidth);
+    printf("bmpInfoHeader.biHeight = %d\r\n", bmpInfoHeader.biHeight);
+    for (y = 0; y < bmpInfoHeader.biHeight; y++) {
+        for (x = 0; x < bmpInfoHeader.biWidth; x++) {
+            if (Xstart + x > Paint.Width || Ystart + y > Paint.Height)
+                break;
+
+            coloridx = (Image[x / 2 + y * Width_Byte] >> ((x % 2) ? 0 : 4)) & 15;
+            Paint_SetPixel(Xstart + x, Ystart + y, colors[coloridx]);
+        }
+    }
+    return 0;
+}
 
 UBYTE GUI_ReadBmp_RGB_7Color(const char *path, UWORD Xstart, UWORD Ystart)
 {

+ 1 - 0
RaspberryPi_JetsonNano/c/lib/GUI/GUI_BMPfile.h

@@ -87,6 +87,7 @@ typedef struct RGB_QUAD {
 
 UBYTE GUI_ReadBmp(const char *path, UWORD Xstart, UWORD Ystart);
 UBYTE GUI_ReadBmp_4Gray(const char *path, UWORD Xstart, UWORD Ystart);
+UBYTE GUI_ReadBmp_16Gray(const char *path, UWORD Xstart, UWORD Ystart);
 UBYTE GUI_ReadBmp_RGB_4Color(const char *path, UWORD Xstart, UWORD Ystart);
 UBYTE GUI_ReadBmp_RGB_7Color(const char *path, UWORD Xstart, UWORD Ystart);
 #endif

+ 6 - 4
RaspberryPi_JetsonNano/c/lib/GUI/GUI_Paint.c

@@ -169,12 +169,14 @@ void Paint_SetScale(UBYTE scale)
     }else if(scale == 4){
         Paint.Scale = scale;
         Paint.WidthByte = (Paint.WidthMemory % 4 == 0)? (Paint.WidthMemory / 4 ): (Paint.WidthMemory / 4 + 1);
-    }else if(scale == 7){//Only applicable with 5in65 e-Paper
+    }else if(scale == 7 || scale == 16){
+        /* 7 colours are only applicable with 5in65 e-Paper */
+        /* 16 colours are used for dithering */
 		Paint.Scale = scale;
 		Paint.WidthByte = (Paint.WidthMemory % 2 == 0)? (Paint.WidthMemory / 2 ): (Paint.WidthMemory / 2 + 1);;
 	}else{
         Debug("Set Scale Input parameter error\r\n");
-        Debug("Scale Only support: 2 4 7\r\n");
+        Debug("Scale Only support: 2 4 7 16\r\n");
     }
 }
 /******************************************************************************
@@ -247,7 +249,7 @@ void Paint_SetPixel(UWORD Xpoint, UWORD Ypoint, UWORD Color)
         UBYTE Rdata = Paint.Image[Addr];
         Rdata = Rdata & (~(0xC0 >> ((X % 4)*2)));//Clear first, then set value
         Paint.Image[Addr] = Rdata | ((Color << 6) >> ((X % 4)*2));
-    }else if(Paint.Scale == 7){
+    }else if(Paint.Scale == 7 || Paint.Scale == 16){
 		UDOUBLE Addr = X / 2  + Y * Paint.WidthByte;
 		UBYTE Rdata = Paint.Image[Addr];
 		Rdata = Rdata & (~(0xF0 >> ((X % 2)*4)));//Clear first, then set value
@@ -277,7 +279,7 @@ void Paint_Clear(UWORD Color)
 				Paint.Image[Addr] = (Color<<6)|(Color<<4)|(Color<<2)|Color;
 			}
 		}		
-	}else if(Paint.Scale == 7) {
+	}else if(Paint.Scale == 7 || Paint.Scale == 16) {
 		for (UWORD Y = 0; Y < Paint.HeightByte; Y++) {
 			for (UWORD X = 0; X < Paint.WidthByte; X++ ) {
 				UDOUBLE Addr = X + Y*Paint.WidthByte;

+ 52 - 0
RaspberryPi_JetsonNano/c/lib/e-Paper/EPD_3in7.c

@@ -536,6 +536,58 @@ void EPD_3IN7_1Gray_Display(const UBYTE *Image)
     EPD_3IN7_ReadBusy_HIGH();  
 }
 
+/******************************************************************************
+function :  Sends a 16-colour image to the display and dithers it to
+            black and white
+parameter:
+    Ditherer:
+        Set to 0 to use Sierra-Lite dithering
+        Set to 1 to use Floyd-Steinberg dithering
+******************************************************************************/
+void EPD_3IN7_1Gray_Display_Dithered(const UBYTE *Image, int Ditherer)
+{
+    int i;
+    int IMAGE_COUNTER = EPD_3IN7_WIDTH * EPD_3IN7_HEIGHT / 2; /* 4-bit */
+
+    int ditherdat = 0;
+    if (Ditherer == 0) /* Sierra-Lite */
+        ditherdat = 0x80;
+    else if (Ditherer == 1) /* Floyd-Steinberg */
+        ditherdat = 0x83;
+    else {
+        Debug("Ditherer can only be 0 or 1");
+        return;
+    }
+
+    EPD_3IN7_SendCommand(0x4E);
+    EPD_3IN7_SendData(0x00);
+    EPD_3IN7_SendData(0x00);
+    EPD_3IN7_SendCommand(0x4F);
+    EPD_3IN7_SendData(0x00);
+    EPD_3IN7_SendData(0x00);
+
+    EPD_3IN7_SendCommand(0x4D); /* Dithering engine start/stop */
+    EPD_3IN7_SendData(ditherdat);
+    EPD_3IN7_SendData(0x00);
+    EPD_3IN7_SendData(0x78);
+    EPD_3IN7_SendData(0x00);
+
+    EPD_3IN7_SendCommand(0x25); /* Write dithering RAM */
+    for (i = 0; i < IMAGE_COUNTER; i++)
+        EPD_3IN7_SendData(Image[i]);
+
+    EPD_3IN7_SendCommand(0x4D); /* Dithering engine start/stop */
+    EPD_3IN7_SendData(0x00);
+    EPD_3IN7_SendData(0x00);
+    EPD_3IN7_SendData(0x78);
+    EPD_3IN7_SendData(0x00);
+
+    EPD_3IN7_Load_LUT(1); /* Do a full refresh of the display */
+
+    EPD_3IN7_SendCommand(0x20);
+    EPD_3IN7_ReadBusy_HIGH();
+}
+
 /******************************************************************************
 function :  Sends part the image buffer in RAM to e-Paper and displays
 notes:

+ 1 - 0
RaspberryPi_JetsonNano/c/lib/e-Paper/EPD_3in7.h

@@ -45,6 +45,7 @@ void EPD_3IN7_1Gray_Clear(void);
 void EPD_3IN7_1Gray_Init(void);
 void EPD_3IN7_1Gray_Display(const UBYTE *Image);
 void EPD_3IN7_1Gray_Display_Part(const UBYTE *Image, UWORD Xstart, UWORD Ystart, UWORD Xend, UWORD Yend);
+void EPD_3IN7_1Gray_Display_Dithered(const UBYTE *Image, int Ditherer);
 
 void EPD_3IN7_Sleep(void);
 

BIN
RaspberryPi_JetsonNano/c/pic/3in7_4bit1.bmp