#define OP_NOOP   0
#define OP_DIGIT0 1
#define OP_DIGIT1 2
#define OP_DIGIT2 3
#define OP_DIGIT3 4
#define OP_DIGIT4 5
#define OP_DIGIT5 6
#define OP_DIGIT6 7
#define OP_DIGIT7 8
#define OP_DECODEMODE  9
#define OP_INTENSITY   10
#define OP_SCANLIMIT   11
#define OP_SHUTDOWN    12
#define OP_DISPLAYTEST 15


int CLK = 5; // 5 = PD5
int LOAD = 6; // 6 = PD6
int DIN = 7; // 7 = PD7

byte znak[2][8] = {
  {0xFE, 0x62, 0x68, 0x78, 0x68, 0x62, 0xFE, 0x00},
  {0x1C, 0x3C, 0x6C, 0xCC, 0xFE, 0x0C, 0x1E, 0x00}
};


void sendData( byte address, byte data)
{
  byte packet[2];
  byte bitmask;
  byte i;
  packet[0] = address;
  packet[1] = data;

  for (i = 0; i < 2; i++)
    for ( bitmask = 0x80 ; bitmask != 0 ; bitmask = bitmask >> 1 )
    {
      if (bitmask & packet[i]) digitalWrite(DIN, 1);
      else digitalWrite(DIN, 0);

      digitalWrite(CLK, HIGH);
      digitalWrite(CLK, LOW);
    }
  digitalWrite(LOAD, HIGH);
  digitalWrite(LOAD, LOW);
}

void sendData2( byte address1, byte data1, byte address2, byte data2)
{
  byte packet1[2];
  byte packet2[2];
  byte bitmask;
  byte i;
  packet1[0] = address1;
  packet1[1] = data1;
  packet2[0] = address2;
  packet2[1] = data2;


  for (i = 0; i < 2; i++)
    for ( bitmask = 0x80 ; bitmask != 0 ; bitmask = bitmask >> 1 )
    {
      if (bitmask & packet1[i]) digitalWrite(DIN, 1);
      else digitalWrite(DIN, 0);

      digitalWrite(CLK, HIGH);
      digitalWrite(CLK, LOW);
    }

  for (i = 0; i < 2; i++)
    for ( bitmask = 0x80 ; bitmask != 0 ; bitmask = bitmask >> 1 )
    {
      if (bitmask & packet2[i]) digitalWrite(DIN, 1);
      else digitalWrite(DIN, 0);

      digitalWrite(CLK, HIGH);
      digitalWrite(CLK, LOW);
    }
  digitalWrite(LOAD, HIGH);
  digitalWrite(LOAD, LOW);
}

void setup() {
  // put your setup code here, to run once:
  pinMode(DIN, OUTPUT);
  pinMode(CLK, OUTPUT);
  pinMode(LOAD, OUTPUT);
  digitalWrite(LOAD, LOW);


  sendData(OP_DISPLAYTEST, 0);
  sendData(OP_INTENSITY, 0);
  sendData(OP_SCANLIMIT, 7);

  sendData(OP_DECODEMODE, 0);
  sendData(OP_SHUTDOWN, 1);
  for (int i = 0; i < 8; i++) // ovo treba ponoviti 2 puta
  {
    sendData(i + 1, 0b00000000);
    digitalWrite(LOAD, HIGH);
    digitalWrite(LOAD, LOW);
  }
}

void loop() {
  // put your main code here, to run repeatedly:

  for (int i = 0; i < 8; i++)   sendData2(i + 1, znak[0][i], i + 1, znak[1][i] );
  delay(300);
}