// Display on SSD connected at PORTA

// Table of values, or look up table (LUT) with the BCD codes
// for every digit from 0 to 9. Every bit corresponds to a LED,
// 1 means the LED is lit and 0 means it is not giving light.
const unsigned char SSD_LUT[] = {
  0b00111111, // 0
  0b00000110, // 1
  0b01011011, // 2
  0b01001111, // 3
  0b01100110, // 4
  0b01101101, // 5
  0b01111101, // 6
  0b00000111, // 7
  0b01111111, // 8
  0b01101111, // 9
  // DECIMAL FORMAT, SO NOT NECESSARY!
  // 0b01110111, // A
  // 0b01111100, // b
  // 0b00111001, // C
  // 0b01011110, // d
  // 0b01111001, // E
  // 0b01110001  // F
};

// As per the lab documentation for the PmodSSD
const int DELAY_IN_MILLISECONDS = 8;

// The number to display
// "nr" in the lab documentation
const int NUMBER_TO_DISPLAY = 27;

// Tens digit + remainder
// "cz" in the lab documentation
const int DIGIT_TENS = NUMBER_TO_DISPLAY / 10;
// "cu" in the lab documentation
const int DIGIT_UNITS = NUMBER_TO_DISPLAY % 10;

int cpos = 0; // current position
int cdigit = 0; // first digit from the two

void setup() {
  // setting port A as output
  DDRA = 0b11111111;

  pinMode(37, OUTPUT);
  pinMode(39, OUTPUT);
}

void loop() {
  PORTA = SSD_LUT[DIGIT_UNITS];
  digitalWrite(37, LOW);
  digitalWrite(39, HIGH);
  delay(DELAY_IN_MILLISECONDS);

  // TURN OFF BOTH DIGITS
  digitalWrite(37, HIGH);
  digitalWrite(39, HIGH);

  // Only display first digit if greater than zero
  if (DIGIT_TENS > 0) {
    PORTA = SSD_LUT[DIGIT_TENS];
    digitalWrite(37, HIGH);
    digitalWrite(39, LOW);
    delay(DELAY_IN_MILLISECONDS);

    // TURN OFF BOTH DIGITS
    digitalWrite(37, HIGH);
    digitalWrite(39, HIGH);
  }
}