//When a key is pressed that key should be displayed on a seven segment display.

#include "register.h"
#include "functions.h"

#define ROWS 4
#define COLS 4

// Define keypad array globally
char keypad[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

// Function to read the keypad
char read_keypad()
{
  uint8_t row, column;
  for (row = 0; row < ROWS; row++)
  {
    *outf = (1 << row); // Activate one row at a time
    for (column = 0; column < COLS; column++)
    {
      if ((*ink & (1 << column))) // Check each column in the active row
      {
        return keypad[row][column];
      }
    }
  }
}
void setup()
{
  *dirf = 0x0F; // Set first four pins of PORT F as o/p. KEYPAD row pins are connected to port f.
  *dirk = 0x00; // Set PORT K as i/p port. KEYPAD column pins are connected to port k.
  *dira = 0x0F; // Set first 4 pins of PORT A as o/p.
  *dirc = 0x0F; // Set first 4 pins of PORT C as o/p.

  char key;
  int index;
  while (1)
  {
    key = read_keypad();
    if (key >= '0' && key <= '9') // If a numeric key is pressed
    {
      index = key - '0';// Convert the key to an index
      *outa = display(index);
    }
    else if (key >= 'A' && key <= 'D') // If a letter key is pressed
    {
      index = key - 'A' + 10;// Convert the key to an index
      *outa = display(index);
    }
    else //if anything other than 0-9 or A-D is pressed display nothing
    {
      *outa = 0x00;
    }
    /* switch (key)
      {
       case '1':
        // *outa = 0x06;
         break;
       case '2':
          //*outa = 0x5B;
         break;
       case '3':
          //*outa = 0x4F;
         break;
       case 'A':
          //*outa = 0x77;
         break;
       case '4':
          //*outa = 0x66;
         break;
       case '5':
          //*outa = 0x6D;
         break;
       case '6':
          //*outa = 0x7D;
         break;
       case 'B':
          //*outa = 0x7F;
         break;
       case '7':
          //*outa = 0x07;
         break;
       case '8':
          //*outa = 0x7F;
         break;
       case '9':
          //*outa = 0x67;
         break;
       case 'C':
          //*outa = 0x39;
         break;
       case '*':
          //*outa = 0x00;
         break;
       case '0':
          //*outa = 0x3F;
         break;
       case '#':
          //*outa = 0x00;
         break;
       case 'D':
          //*outa = 0x3F;
         break;
       default:
          //*outa = 0x00;
         break;
      }*/
  }
}