Rabu, 14 Juli 2021

Sketch ESP8266 - Program Untuk Menguji Panel P10

 Kode Program:

/*
 * 
 * 

Pin on  DMD P10     GPIO      NODEMCU               Pin on  DS3231      NODEMCU                   Pin on  Buzzer       NODEMCU
        2  A        GPIO16    D0                            SCL         D1 (GPIO 5)                       +            RX (GPIO 3)
        4  B        GPIO12    D6                            SDA         D2 (GPIO 4)                       -            GND
        8  CLK      GPIO14    D5                            VCC         3V
        10 SCK      GPIO0     D3                            GND         GND
        12 R        GPIO13    D7
        1  NOE      GPIO15    D8
        3  GND      GND       GND

Catatan : 
o Perlu Power Eksternal 5V ke LED P10.


*/

  //Tambahan AB 1
#include <Wire.h>
#include <RtcDS3231.h>
RtcDS3231<TwoWire> Rtc(Wire);

#include <Timer.h>
#include <HJS589.h>

#include <fonts/ABLED_ArialRMTB10b_Hrfx7.h>
#include <fonts/ABLed_numTwCenMTCond25_5x16.h>
#include <fonts/ABLed_numTWMTCondes11b_5x7.h>

//----------------------------
// SETUP DMD HJS589
#define DISPLAYS_WIDE 3 //2 
#define DISPLAYS_HIGH 1
HJS589 Disp(DISPLAYS_WIDE, DISPLAYS_HIGH);  // Jumlah Panel P10 yang digunakan (KOLOM,BARIS)

//----------------------------------------------------------------------
// HJS589 P10 FUNGSI TAMBAHAN UNTUK NODEMCU ESP8266
void ICACHE_RAM_ATTR refresh() { 
  Disp.refresh();
  timer0_write(ESP.getCycleCount() + 80000);  
}

void Disp_init() {
  Disp.setDoubleBuffer(true);
  Disp.start();
  timer0_attachInterrupt(refresh);
  Disp.clear();
}

//SETUP RTC
//year, month, date, hour, min, sec and week-day(Senin 0 sampai Ahad 6)
//DateTime dt(2018, 12, 20, 16, 30, 0, 3);
RtcDateTime now;
//char weekDay[][7] = {"AHAD", "SENIN", "SELASA", "RABU", "KAMIS", "JUM'AT", "SABTU", "AHAD"}; // array hari, dihitung mulai dari senin, hari senin angka nya =0,
char weekDay[][7] = {"Ahad", "Senin", "Selasa", "Rabu", "Kamis", "Jum'at", "Sabtu", "Ahad"}; // array hari, dihitung mulai dari senin, hari senin angka nya =0,
//char monthYear[][4] = { "DES", "JAN", "FEB", "MAR", "APR", "MEI", "JUN", "JUL", "AGU", "SEP", "OKT", "NOV", "DES" };
char monthYear[][4] = { "12", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" };


//TIMER
Timer tBlink;
//----------------------------------------------------------------------
// SETUP

void setup() 
{
  Serial.begin(115200);

  //Buzzer
  pinMode(LED_BUILTIN, OUTPUT);
  delay(50);

  //RTC D3231

  int rtn = I2C_ClearBus(); // clear the I2C bus first before calling Wire.begin()
  if (rtn != 0) {
    Serial.println(F("I2C bus error. Could not clear"));
    if (rtn == 1) {
      Serial.println(F("SCL clock line held low"));
    } else if (rtn == 2) {
      Serial.println(F("SCL clock line held low by slave clock stretch"));
    } else if (rtn == 3) {
      Serial.println(F("SDA data line held low"));
    }
  } else { // bus clear, re-enable Wire, now can start Wire Arduino master
    Wire.begin();
  }
  
  Rtc.Begin();

  if (!Rtc.GetIsRunning()) 
  {
    Rtc.SetIsRunning(true);
  }
  
  Rtc.Enable32kHzPin(false);
  Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeNone); 
  //Rtc.SetDateTime(RtcDateTime(2021, 1, 7, 17, 34, 00));
  Serial.println("Setup RTC selesai");
  
  //DMD
  Disp_init();
  Disp.setBrightness(80); //(20);

  //TIMER
  tBlink.oscillate(LED_BUILTIN, 500, HIGH);
}

//----------------------------------------------------------------------
// LOOP
void loop() {
  
  //tMain.update();
  tBlink.update();      

  Serial.println("tesSlash4()");
  
  lightAllPixel(1000);
  clearDisplay(1000);
  lightAllPixel(1000);
  clearDisplay(1000);
  lightAllPixel(4000);
  clearDisplay(1000);
  Disp.clear();
  tesSlash(40);
  tesBackSlash(40);
  clearDisplay(1000);

  tesSlash2(40);
  tesBackSlash2(40);
  tesHorLine(70);
  tesVertLine(70);

  //verticalLine(4000);
  
  //tesAllPixel();
  //delay(2000);
  //Disp.clear();
}

//----------------------------------------------------------------------
// ALARM SHOLAT BERJALAN SAAT MASUK WAKTU SHOLAT

void clearDisplay(int t)
{
  Disp.drawFilledRect(0,0,15,96,0);
  Disp.swapBuffers();
  delay(t);
}

void tesHorLine(int t)
{
  for (int y=0; y<18; y++)
  {
    Disp.drawLine(0,y,96,y,1);
    Disp.drawLine(0,y-1,96,y-1,0);
    Disp.drawLine(0,y-2,96,y-2,0);
    Disp.swapBuffers();
    delay(t);
  }
}

void tesVertLine(int t)
{
   for (int x=0; x<98; x++)
   {
      Disp.drawLine(x,0,x,15,1);
      Disp.drawLine(x-1,0,x-1,15,0);
      Disp.drawLine(x-2,0,x-2,15,0);
      Disp.swapBuffers();
      delay(t);
   }
}

void tesSlash(int t)
{
    int x;
    int y;
    
    for(int i=0; i<160; i=i+1)
    {
      y = 0;
      for (x=i; x>i-16; x--)
      {
        for (int c=0; c<16; c++)
        {
          if (c==0 || c==1 || c==4 || c==5 || c==8 || c==9 || c==12 || c==13)
          {
            Disp.setPixel(x-c,y,1);
          }
          else
          {
            Disp.setPixel(x-c,y,0);
          }
        }
        
        y++;
      }
      
      Disp.swapBuffers();
      delay(t);
    }    
}

void tesSlash2(int t)
{
    for(int x=0; x<113; x++)
    {
      Disp.drawLine(x,0,x-15,15,1);
      Disp.drawLine(x-1,0,x-15-1,15,0);
      Disp.drawLine(x-2,0,x-15-2,15,0);
  
      Disp.swapBuffers();
      delay(t);
    }
}

void tesBackSlash2(int t)
{
    for(int x=96; x>-64; x--)
    {
      Disp.drawLine(x,0,x+15,15,1);
      Disp.drawLine(x+1,0,x+15+1,15,0);
      Disp.drawLine(x+2,0,x+15+2,15,0);
  
      Disp.swapBuffers();
      delay(t);
    }
}

void tesBackSlash(int t)
{
    int x;
    int y;
    
    for(int i=96; i>-64; i=i-1)
    {
      y = 0;
      for (x=i; x<i+16; x++)
      {
        for (int c=0; c<16; c++)
        {
          if (c==0 || c==1 || c==4 || c==5 || c==8 || c==9 || c==12 || c==13)
          {
            Disp.setPixel(x+c,y,1);
          }
          else
          {
            Disp.setPixel(x+c,y,0);
          }
        }
        
        y++;
      }
      
      Disp.swapBuffers();
      delay(t);
    }
}


void lightAllPixel(int t)
{
  Disp.drawFilledRect(0,0,95,15,1);
  Disp.swapBuffers();
  delay(t);
}

//----------------------------------------------------------------------
// I2C_ClearBus menghindari gagal baca RTC (nilai 00 atau 165)

int I2C_ClearBus() {
  
#if defined(TWCR) && defined(TWEN)
  TWCR &= ~(_BV(TWEN)); //Disable the Atmel 2-Wire interface so we can control the SDA and SCL pins directly
#endif

  pinMode(SDA, INPUT_PULLUP); // Make SDA (data) and SCL (clock) pins Inputs with pullup.
  pinMode(SCL, INPUT_PULLUP);

  delay(2500);  // Wait 2.5 secs. This is strictly only necessary on the first power
  // up of the DS3231 module to allow it to initialize properly,
  // but is also assists in reliable programming of FioV3 boards as it gives the
  // IDE a chance to start uploaded the program
  // before existing sketch confuses the IDE by sending Serial data.

  boolean SCL_LOW = (digitalRead(SCL) == LOW); // Check is SCL is Low.
  if (SCL_LOW) { //If it is held low Arduno cannot become the I2C master. 
    return 1; //I2C bus error. Could not clear SCL clock line held low
  }

  boolean SDA_LOW = (digitalRead(SDA) == LOW);  // vi. Check SDA input.
  int clockCount = 20; // > 2x9 clock

  while (SDA_LOW && (clockCount > 0)) { //  vii. If SDA is Low,
    clockCount--;
  // Note: I2C bus is open collector so do NOT drive SCL or SDA high.
    pinMode(SCL, INPUT); // release SCL pullup so that when made output it will be LOW
    pinMode(SCL, OUTPUT); // then clock SCL Low
    delayMicroseconds(10); //  for >5uS
    pinMode(SCL, INPUT); // release SCL LOW
    pinMode(SCL, INPUT_PULLUP); // turn on pullup resistors again
    // do not force high as slave may be holding it low for clock stretching.
    delayMicroseconds(10); //  for >5uS
    // The >5uS is so that even the slowest I2C devices are handled.
    SCL_LOW = (digitalRead(SCL) == LOW); // Check if SCL is Low.
    int counter = 20;
    while (SCL_LOW && (counter > 0)) {  //  loop waiting for SCL to become High only wait 2sec.
      counter--;
      delay(100);
      SCL_LOW = (digitalRead(SCL) == LOW);
    }
    if (SCL_LOW) { // still low after 2 sec error
      return 2; // I2C bus error. Could not clear. SCL clock line held low by slave clock stretch for >2sec
    }
    SDA_LOW = (digitalRead(SDA) == LOW); //   and check SDA input again and loop
  }
  if (SDA_LOW) { // still low
    return 3; // I2C bus error. Could not clear. SDA data line held low
  }

  // else pull SDA line low for Start or Repeated Start
  pinMode(SDA, INPUT); // remove pullup.
  pinMode(SDA, OUTPUT);  // and then make it LOW i.e. send an I2C Start or Repeated start control.
  // When there is only one I2C master a Start or Repeat Start has the same function as a Stop and clears the bus.
  /// A Repeat Start is a Start occurring after a Start with no intervening Stop.
  delayMicroseconds(10); // wait >5uS
  pinMode(SDA, INPUT); // remove output low
  pinMode(SDA, INPUT_PULLUP); // and make SDA high i.e. send I2C STOP control.
  delayMicroseconds(10); // x. wait >5uS
  pinMode(SDA, INPUT); // and reset pins as tri-state inputs which is the default state on reset
  pinMode(SCL, INPUT);
  return 0; // all ok
}

Tidak ada komentar:

Posting Komentar