#include <SPI.h>
#include <PC_Epd_154_low_memory.h>

 
 
void setup() {
  const char Tsep[] = "\x21\0";
  int  i;
  char C;
  if (EpdInit(4, 5, 6, 7) != 0){  // CS, DC, RST, BUSY pins
    return -1;
  }

  // Example 1 : clear displya
  EpdSessionClearDisplay();   // effacement total de l'écran et refresh
  delay(5000);

  
  // Example 2 : Black and Red
  EpdSessionStart(BLACK);     // Select BLACK bank
  EpdSessionGoToLineAbs(100,COLORED); // The top of the display in BLACK
  EpdSessionStart(RED);       // Select RED bank
  EpdSessionGoToLineAbs(100,UNCOLORED); 
  EpdSessionFill(COLORED); // The bottom of the display in RED
  EpdSessionClose();          // Refresh
  delay(5000);

  // Example 3 : White and Red
  EpdSessionStart(BLACK);     // Select BLACK bank
  EpdSessionGoToLineAbs(100,UNCOLORED); // The  top of the display in WHITE. The bottom of the display stays in RED
  EpdSessionClose();
  delay(5000);


  // Example 4 : checkerboard Black and White
  EpdSessionStart(BLACK);     // Select BLACK bank
  for (i=0; i<10; i++){
    EpdSessionWriteString(0,"\x20\x19\x20\x19\x20\x19\x20\x19\x20\x19\x20\0", 20, NOTINVERTED, NOTINVERTED);
    EpdSessionWriteString(0,"\x20\x19\x20\x19\x20\x19\x20\x19\x20\x19\x20\0", 20, INVERTED, NOTINVERTED);
  }
  EpdSessionStart(RED);
  EpdSessionFill(UNCOLORED);
  EpdSessionClose();
  delay(5000);


  // Example 4 : checkerboard. The white Square become red
  EpdSessionStart(RED);     // Select BLACK bank
  for (i=0; i<9; i++){
    EpdSessionWriteString(0,"\x19\x20\x19\x20\x19\x20\x19\x20\x19\x20\x19\0", 20, NOTINVERTED, NOTINVERTED);
    EpdSessionWriteString(0,"\x19\x20\x19\x20\x19\x20\x19\x20\x19\x20\x19\0", 20, INVERTED, NOTINVERTED);
  }
  EpdSessionClose();
  delay(5000);

 
  // Example 5
  EpdSessionStart(BLACK);      // Selection BLACK bank, current position = 0
  
  EpdSessionGoToLineRel(12,COLORED); // goto from current position to current position +12 lines and fill the background in black color
  EpdSessionWriteString(3, "NORMAL\0", 20, INVERTED, INVERTED); // write a white color string on a black background color. 3 ' ' before the text
  EpdSessionGoToLineRel(12,COLORED); // goto from current position to current position +12 lines and fill the background in black color
  EpdSessionGoToLineRel(2,COLORED);  // draw two black lines
  EpdSessionWriteString(5,Tsep, 2, NOTINVERTED, NOTINVERTED);  // write Tsep with font 2
  EpdSessionWriteString(5,Tsep, 2, NOTINVERTED, NOTINVERTED);  // write Tsep with font 2
  EpdSessionWriteString(1,"Moy |  201\0", 20, NOTINVERTED, NOTINVERTED); // write "Moy |" with font 20
  EpdSessionWriteString(5,Tsep, 2, NOTINVERTED, NOTINVERTED);  // etc.
  EpdSessionWriteString(5,Tsep, 2, NOTINVERTED, NOTINVERTED);
  EpdSessionGoToLineRel(2,COLORED); 
  EpdSessionWriteString(5,Tsep, 2, NOTINVERTED, NOTINVERTED);
  EpdSessionWriteString(5,Tsep, 2, NOTINVERTED, NOTINVERTED);
  EpdSessionWriteString(1,"Max |  300\0", 20, NOTINVERTED, NOTINVERTED);
  EpdSessionWriteString(5,Tsep, 2, NOTINVERTED, NOTINVERTED);
  EpdSessionWriteString(5,Tsep, 2, NOTINVERTED, NOTINVERTED);
  EpdSessionGoToLineRel(2,COLORED); 
  EpdSessionWriteString(5,Tsep, 2, NOTINVERTED, NOTINVERTED);
  EpdSessionWriteString(5,Tsep, 2, NOTINVERTED, NOTINVERTED);
  EpdSessionWriteString(1,"Min |  103\0", 20, NOTINVERTED, NOTINVERTED);
  EpdSessionWriteString(5,Tsep, 2, NOTINVERTED, NOTINVERTED);
  EpdSessionWriteString(5,Tsep, 2, NOTINVERTED, NOTINVERTED);
  EpdSessionGoToLineRel(2,COLORED); 
  EpdSessionGoToLineAbs(150,UNCOLORED);  // goto line 150 (absolute)
  EpdSessionWriteString(1,"\x1D\x1E\x1F\x41\x20\x1A\x1E\x1F\0", 20,NOTINVERTED, NOTINVERTED);
  EpdSessionGoToLineAbs(175,UNCOLORED);  // Goto line 175 (absolute)
  EpdSessionWriteString(4,"Chour\0", 20, NOTINVERTED, NOTINVERTED);
  EpdSessionFill(UNCOLORED); // fill the background in white color from the current position to the end of the screen 
   
  EpdSessionStart(RED);  // Select the Red banc
  EpdSessionGoToLineAbs(175,UNCOLORED);
  EpdSessionWriteString(0,"(c)\0", 20, NOTINVERTED, NOTINVERTED);
  EpdSessionFill(UNCOLORED);
  
  EpdSessionClose();
  delay(5000);

  
  // Example 6
  EpdSessionStart(BLACK);  // Select the Red banc
  EpdSessionGoToLineRel(12,COLORED); // goto from current position to current position +12 lines and fill the background in black color
  EpdSessionWriteString(3, "NORMAL\0", 20, INVERTED, INVERTED); // write a white color string on a black background color. 3 ' ' before the text
  EpdSessionGoToLineRel(12,COLORED); // goto from current position to current position +12 lines and fill the background in black color
  EpdSessionGoToLineRel(2,COLORED);  // draw two black lines
  EpdSessionWriteString(5,Tsep, 2, NOTINVERTED, NOTINVERTED);  // write Tsep with font 2
  EpdSessionWriteString(5,Tsep, 2, NOTINVERTED, NOTINVERTED);  // write Tsep with font 2
  EpdSessionWriteString(1,"Moy |  201\0", 20, NOTINVERTED, NOTINVERTED); // write "Moy |" with font 20
  EpdSessionWriteString(5,Tsep, 2, NOTINVERTED, NOTINVERTED);  // etc.
  EpdSessionWriteString(5,Tsep, 2, NOTINVERTED, NOTINVERTED);
  EpdSessionGoToLineRel(2,COLORED); 
  EpdSessionWriteString(5,Tsep, 2, NOTINVERTED, NOTINVERTED);
  EpdSessionWriteString(5,Tsep, 2, NOTINVERTED, NOTINVERTED);
  EpdSessionWriteString(1,"Max |  300\0", 20, NOTINVERTED, NOTINVERTED);
  EpdSessionWriteString(5,Tsep, 2, NOTINVERTED, NOTINVERTED);
  EpdSessionWriteString(5,Tsep, 2, NOTINVERTED, NOTINVERTED);
  EpdSessionGoToLineRel(2,COLORED); 
  EpdSessionWriteString(5,Tsep, 2, NOTINVERTED, NOTINVERTED);
  EpdSessionWriteString(5,Tsep, 2, NOTINVERTED, NOTINVERTED);
  EpdSessionWriteString(1,"Min |  103\0", 20, NOTINVERTED, NOTINVERTED);
  EpdSessionWriteString(5,Tsep, 2, NOTINVERTED, NOTINVERTED);
  EpdSessionWriteString(5,Tsep, 2, NOTINVERTED, NOTINVERTED);
  EpdSessionGoToLineRel(2,COLORED); 
  EpdSessionGoToLineAbs(150,UNCOLORED);  // goto line 150 (absolute)
  EpdSessionWriteString(1,"\x1D\x1E\x1F\0", 20,NOTINVERTED, NOTINVERTED);
  EpdSessionGoToLineAbs(175,UNCOLORED);  // Goto line 175 (absolute)
  EpdSessionWriteString(4,"Chour\0", 20, NOTINVERTED, NOTINVERTED);
  EpdSessionFill(UNCOLORED); // fill the background in white color from the current position to the end of the screen 
   
  EpdSessionStart(RED);  // Select the Red banc
  EpdSessionGoToLineAbs(150,UNCOLORED);
  EpdSessionWriteString(6,"\x1A\x1B\x1F\0", 20,NOTINVERTED, NOTINVERTED);
  EpdSessionGoToLineAbs(175,UNCOLORED);
  EpdSessionWriteString(0,"(c)\0", 20, NOTINVERTED, NOTINVERTED);
  EpdSessionFill(UNCOLORED);
  EpdSessionClose();
  
  delay(5000);

  
  // Example 7
  
  EpdSessionStart(BLACK);      // Select BLACK bank, current position = 0
  EpdSessionGoToLineRel(12+EpdSessionGetFontHeight(20)+12,UNCOLORED); // Goto line 24+Height font 20 + 2*Height font 2 => Erase the top of the screen
  EpdSessionGoToLineRel(2,COLORED);  // draw two black lines
  EpdSessionWriteString(5,Tsep, 2, NOTINVERTED, NOTINVERTED);  // write Tsep with font 2
  EpdSessionWriteString(5,Tsep, 2, NOTINVERTED, NOTINVERTED);  // write Tsep with font 2
  EpdSessionWriteString(1,"Moy |  201\0", 20, NOTINVERTED, NOTINVERTED); // write "Moy |" with font 20
  EpdSessionWriteString(5,Tsep, 2, NOTINVERTED, NOTINVERTED);  // etc.
  EpdSessionWriteString(5,Tsep, 2, NOTINVERTED, NOTINVERTED);
  EpdSessionGoToLineRel(2,COLORED); 
  EpdSessionWriteString(5,Tsep, 2, NOTINVERTED, NOTINVERTED);
  EpdSessionWriteString(5,Tsep, 2, NOTINVERTED, NOTINVERTED);
  EpdSessionWriteString(5,"|\0", 20, NOTINVERTED, NOTINVERTED);
  
  EpdSessionStart(RED);      // Select BLACK bank, current position = 0
  EpdSessionGoToLineRel(12,COLORED); // goto from current position to current position +12 lines and fill the background in black color
  EpdSessionWriteString(3,"ALARME\0", 20, INVERTED, INVERTED); // write a white color string on a black background color
  EpdSessionGoToLineRel(12,COLORED); // goto from current position to current position +12 lines and fill the background in black color
  EpdSessionGoToLineRel(4+6*EpdSessionGetFontHeight(2)+EpdSessionGetFontHeight(20),UNCOLORED); 
  EpdSessionWriteString(1,"Max |  500\0", 20, NOTINVERTED, NOTINVERTED);
  EpdSessionClose();
  EpdSleep();
}


void BlinkLedRX(int n)
{
 while (n) {
    digitalWrite(17, HIGH);  
    delay(500);
    digitalWrite(17, LOW);  
    delay(500);
    n = n-1;
  }
}
void BlinkLedTX(int n)
{
 while (n) {
    TXLED1;
    delay(500);
    TXLED0;
    delay(500);
    n = n-1;
  }
}

void loop() {
  // put your main code here, to run repeatedly:
    BlinkLedTX(2);
    BlinkLedRX(2);
}