void selectDigit(int d) {
  if (d <= 0 || d > 5)
    return;
  
  PORTK = (1 << (d - 1));
}

void selectGraphic(int g) {
  if      (g ==    0) PORTF = ~0xFC;//0
  else if (g ==    1) PORTF = ~0x60;//1
  else if (g ==    2) PORTF = ~0xDA;//2
  else if (g ==    3) PORTF = ~0xF2;//3
  else if (g ==    4) PORTF = ~0x66;//4
  else if (g ==    5) PORTF = ~0xB6;//5
  else if (g ==    6) PORTF = ~0xBE;//6
  else if (g ==    7) PORTF = ~0xE0;//7
  else if (g ==    8) PORTF = ~0xFE;//8
  else if (g ==    9) PORTF = ~0xF6;//9
  else if (g ==   21) PORTF = ~0xEE;//A
  else if (g ==   23) PORTF = ~0x9C;//C
  else if (g ==   25) PORTF = ~0x9E;//E
  else if (g ==   26) PORTF = ~0x8E;//F
  else if (g ==   28) PORTF = ~0x6E;//H
  else if (g ==   30) PORTF = ~0x70;//J
  else if (g ==   32) PORTF = ~0x1C;//L
  else if (g ==   36) PORTF = ~0xCE;//P
  else if (g ==   41) PORTF = ~0x7C;//U
  else if (g ==   61) PORTF = ~0xFA;//a
  else if (g ==   62) PORTF = ~0x3E;//b
  else if (g ==   63) PORTF = ~0x1A;//c
  else if (g ==   64) PORTF = ~0x7A;//d
  else if (g ==   68) PORTF = ~0x2E;//h
  else if (g ==   74) PORTF = ~0x2A;//n
  else if (g ==   75) PORTF = ~0x3A;//o
  else if (g ==   78) PORTF = ~0x0A;//r
  else if (g ==   81) PORTF = ~0x38;//u
  else if (g ==   85) PORTF = ~0x76;//y

  else if (g ==  -10) PORTF = ~0xFD;//0.
  else if (g ==   -1) PORTF = ~0x61;//1.
  else if (g ==   -2) PORTF = ~0xDB;//2.
  else if (g ==   -3) PORTF = ~0xF3;//3.
  else if (g ==   -4) PORTF = ~0x67;//4.
  else if (g ==   -5) PORTF = ~0xB7;//5.
  else if (g ==   -6) PORTF = ~0xBF;//6.
  else if (g ==   -7) PORTF = ~0xE1;//7.
  else if (g ==   -8) PORTF = ~0xFF;//8.
  else if (g ==   -9) PORTF = ~0xF7;//9.
  else if (g ==  -21) PORTF = ~0xEF;//A.
  else if (g ==  -23) PORTF = ~0x9D;//C.
  else if (g ==  -25) PORTF = ~0x9F;//E.
  else if (g ==  -26) PORTF = ~0x8F;//F.
  else if (g ==  -28) PORTF = ~0x6F;//H.
  else if (g ==  -30) PORTF = ~0x71;//J.
  else if (g ==  -32) PORTF = ~0x1D;//L.
  else if (g ==  -36) PORTF = ~0xCF;//P.
  else if (g ==  -41) PORTF = ~0x7D;//U.
  else if (g ==  -61) PORTF = ~0xFB;//a.
  else if (g ==  -62) PORTF = ~0x3F;//b.
  else if (g ==  -63) PORTF = ~0x1B;//c.
  else if (g ==  -64) PORTF = ~0x7B;//d.
  else if (g ==  -68) PORTF = ~0x2F;//h.
  else if (g ==  -74) PORTF = ~0x2B;//n.
  else if (g ==  -75) PORTF = ~0x3B;//o.
  else if (g ==  -78) PORTF = ~0x0B;//r.
  else if (g ==  -81) PORTF = ~0x39;//u.
  else if (g ==  -85) PORTF = ~0x77;//y.

  else                PORTF = ~0x00;
}

struct screen_t {
  int *digit1 = nullptr;
  int *digit2 = nullptr;
  int *digit3 = nullptr;
  int *digit4 = nullptr;
  int *digit5 = nullptr;
  
  screen_t() {
	  digit1 = &m_digits[0];
	  digit2 = &m_digits[1];
	  digit3 = &m_digits[2];
	  digit4 = &m_digits[3];
	  digit5 = &m_digits[4];
  }

  void begin() {
    selectDigit(1);
    selectGraphic(100);

    for (int i = 0; i < 5; i++)
      m_digits[i] = 100;
  }

  void loop() {
    if (micros() - m_lastTimer < 200)
      return;
    
    m_lastTimer = micros();
	  m_lastDigit++;
	  if (m_lastDigit >= 5)
		  m_lastDigit = 0;

    Serial.print(m_lastDigit);
    Serial.print("=");
    Serial.println(m_digits[m_lastDigit]);
  
    selectGraphic(100);
	  selectDigit(m_lastDigit);
	  selectGraphic(m_digits[m_lastDigit]);
  }

  private:
    int m_lastTimer = 0;
    int m_lastDigit = 0;
    int m_digits[5] = {100};
};

screen_t screen;
volatile byte state = LOW;

void setup() {
  // put your setup code here, to run once:
 DDRF = 0xFF;
 DDRK = 0x1F;

 screen.begin();
 *screen.digit1 = 0;
 *screen.digit2 = 1;
 *screen.digit3 = 2;
 *screen.digit4 = 3;
 *screen.digit5 = 4;

 attachInterrupt(digitalPinToInterrupt(2), blink, CHANGE);

 Serial.begin(115200);
}

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

void blink() {
  state = !state;
}
virtual-encoderBreakout