early testing of epaper and rotary encoder

Dependents:   e-ink-test_menu

Revision:
1:75b1993d74b1
Parent:
0:ac97d71fe296
--- a/epd1in54.cpp	Fri Feb 16 21:30:08 2018 +0000
+++ b/epd1in54.cpp	Mon Mar 04 21:43:23 2019 +0000
@@ -134,7 +134,8 @@
 /**
  *  @brief: set the look-up table register
  */
-void Epd::SetLut(const unsigned char* lut) {
+void Epd::SetLut(const unsigned char* lut) 
+{
     SendCommand(WRITE_LUT_REGISTER);
     /* the length of look-up table is 30 bytes */
     for (int i = 0; i < 30; i++) {
@@ -167,13 +168,15 @@
     /* x point must be the multiple of 8 or the last 3 bits will be ignored */
     x &= 0xF8;
     image_width &= 0xF8;
-    if (x + image_width >= this->width) {
-        x_end = this->width - 1;
+    //if (x + image_width >= this->width) {
+    if (x + image_width >= EPD_WIDTH) {
+        x_end = EPD_WIDTH - 1;
     } else {
         x_end = x + image_width - 1;
     }
-    if (y + image_height >= this->height) {
-        y_end = this->height - 1;
+    //if (y + image_height >= this->height) {
+    if (y + image_height >= EPD_HEIGHT) {
+        y_end = EPD_HEIGHT - 1;
     } else {
         y_end = y + image_height - 1;
     }
@@ -181,8 +184,10 @@
     SetMemoryPointer(x, y);
     SendCommand(WRITE_RAM);
     /* send the image data */
-    for (int j = 0; j < y_end - y + 1; j++) {
-        for (int i = 0; i < (x_end - x + 1) / 8; i++) {
+    for (int j = 0; j < y_end - y + 1; j++) 
+    {
+        for (int i = 0; i < (x_end - x + 1) / 8; i++) 
+        {
             SendData(image_buffer[i + j * (image_width / 8)]);
         }
     }
@@ -193,11 +198,12 @@
  *          this won't update the display.
  */
 void Epd::ClearFrameMemory(unsigned char color) {
-    SetMemoryArea(0, 0, this->width - 1, this->height - 1);
+    //SetMemoryArea(0, 0, this->width - 1, this->height - 1);
+    SetMemoryArea(0, 0, EPD_WIDTH - 1, EPD_HEIGHT - 1);
     SetMemoryPointer(0, 0);
     SendCommand(WRITE_RAM);
     /* send the color data */
-    for (int i = 0; i < this->width / 8 * this->height; i++) {
+    for (int i = 0; i < EPD_WIDTH / 8 * EPD_WIDTH; i++) {
         SendData(color);
     }
 }
@@ -257,7 +263,7 @@
     WaitUntilIdle();
 }
 
-
+/******************************
 void Epd::SetRotate(int rotate){
     if (rotate == ROTATE_0){
         rotate = ROTATE_0;
@@ -280,39 +286,140 @@
         height = EPD_WIDTH;
     }
 }
-
+**************************************/
 
-void Epd::SetPixel(unsigned char* frame_buffer, int x, int y, int colored){
-    if (x < 0 || x >= width || y < 0 || y >= height){
+/**
+ *  @brief: clear the image
+ */
+void Epd::Clear(unsigned char* frame_buffer, int colored) {
+    for (int x = 0; x < this->width; x++) {
+        for (int y = 0; y < this->height; y++) {
+            DrawAbsolutePixel(frame_buffer, x, y, colored);
+        }
+    }
+}
+/**
+ *  @brief: this draws a pixel by absolute coordinates.
+ *          this function won't be affected by the rotate parameter.
+ */
+void Epd::DrawAbsolutePixel(unsigned char* frame_buffer, int x, int y, int colored) {
+    if (x < 0 || x >= EPD_WIDTH || y < 0 || y >= EPD_HEIGHT) {
         return;
     }
-    if (rotate == ROTATE_0){
-        SetAbsolutePixel(frame_buffer, x, y, colored);
-    }
-    else if (rotate == ROTATE_90){
-        int point_temp = x;
-        x = EPD_WIDTH - y;
-        y = point_temp;
-        SetAbsolutePixel(frame_buffer, x, y, colored);
-    }
-    else if (rotate == ROTATE_180){
-        x = EPD_WIDTH - x;
-        y = EPD_HEIGHT- y;
-        SetAbsolutePixel(frame_buffer, x, y, colored);
-    }
-    else if (rotate == ROTATE_270){
-        int point_temp = x;
-        x = y;
-        y = EPD_HEIGHT - point_temp;
-        SetAbsolutePixel(frame_buffer, x, y, colored);
+    if (IF_INVERT_COLOR) {
+        if (colored) {
+            frame_buffer[(x + y * this->width) / 8] |= 0x80 >> (x % 8);
+        } else {
+            frame_buffer[(x + y * this->width) / 8] &= ~(0x80 >> (x % 8));
+        }
+    } else {
+        if (colored) {
+            frame_buffer[(x + y * this->width) / 8] &= ~(0x80 >> (x % 8));
+        } else {
+            frame_buffer[(x + y * this->width) / 8] |= 0x80 >> (x % 8);
+        }
     }
 }
 
-void Epd::SetAbsolutePixel(unsigned char *frame_buffer, int x, int y, int colored){
+/**
+ *  @brief: Getters and Setters
+ */
+int Epd::GetWidth(void) 
+{
+    return this->width;
+}
+
+void Epd::SetWidth(int width) 
+{
+    this->width = width % 8 ? width + 8 - (width % 8) : width;
+}
+
+int Epd::GetHeight(void) 
+{
+    return this->height;
+}
+
+void Epd::SetHeight(int height) 
+{
+    this->height = height;
+}
+
+int Epd::GetRotate(void) 
+{
+    return this->rotate;
+}
+
+void Epd::SetRotate(int rotate)
+{
+    this->rotate = rotate;
+}
+
+
+// RJK - adjusted for partial updates
+void Epd::SetPixel(unsigned char* frame_buffer, int x, int y, int colored)
+{
+    if (x < 0 || x >= EPD_WIDTH || y < 0 || y >= EPD_HEIGHT){
+        return;
+    }
+    if (this->rotate == ROTATE_0)
+    {
+        if(x < 0 || x >= this->width || y < 0 || y >= this->height)
+        {
+            return;
+        }
+        //SetAbsolutePixel(frame_buffer, x, y, colored);
+        DrawAbsolutePixel(frame_buffer, x, y, colored);
+    }
+    else if (this->rotate == ROTATE_90)
+    {
+        if(x < 0 || x >= this->height || y < 0 || y >= this->width) 
+        {
+          return;
+        }        
+        int point_temp = x;
+        //x = EPD_WIDTH - y;
+        x = this->width - y;
+        y = point_temp;
+        //SetAbsolutePixel(frame_buffer, x, y, colored);
+        DrawAbsolutePixel(frame_buffer, x, y, colored);
+    }
+    else if (this->rotate == ROTATE_180)
+    {
+        if(x < 0 || x >= this->width || y < 0 || y >= this->height) 
+        {
+          return;
+        }        
+        //x = EPD_WIDTH - x;
+        //y = EPD_HEIGHT- y;
+        x = this->width - x;
+        y = this->height - y;
+        //SetAbsolutePixel(frame_buffer, x, y, colored);
+        DrawAbsolutePixel(frame_buffer, x, y, colored);
+    }
+    else if (this->rotate == ROTATE_270)
+    {
+        if(x < 0 || x >= this->height || y < 0 || y >= this->width) 
+        {
+          return;
+        }
+        int point_temp = x;
+        x = y;
+        //y = EPD_HEIGHT - point_temp;
+        y = this->height - point_temp;
+        //SetAbsolutePixel(frame_buffer, x, y, colored);
+        DrawAbsolutePixel(frame_buffer, x, y, colored);
+    }
+}
+
+/************************************************************************************
+void Epd::xSetAbsolutePixel(unsigned char *frame_buffer, int x, int y, int colored)
+{
     // To avoid display orientation effects
     // use EPD_WIDTH instead of self.width
     // use EPD_HEIGHT instead of self.height
-    if (x < 0 || x >= EPD_WIDTH || y < 0 || y >= EPD_HEIGHT){
+    //if (x < 0 || x >= EPD_WIDTH || y < 0 || y >= EPD_HEIGHT){
+    // RJK - try with self width and height for partial refresh
+    if (x < 0 || x >= this->width || y < 0 || y >= this->height) {
         return;
     }
     if (colored){
@@ -322,7 +429,7 @@
         frame_buffer[(x + y * EPD_WIDTH) / 8] |= 0x80 >> (x % 8);
     }
 }
-
+***************************************************************************************/
 void Epd::DrawLine(unsigned char*frame_buffer, int x0, int y0, int x1, int y1, int colored){
     // Bresenham algorithm
     int dx = x1 - x0 >= 0 ? x1 - x0 : x0 - x1;