#include <FastLED.h>
#include <Keypad.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>
#define OLED_RESET - 1
#define NUM_LEDS_PER_DIGIT 28
#define NUM_LEDS 84
#define LED_PIN 12
#define DISPLAY_MMSS
Adafruit_SH1106 display(OLED_RESET);
const byte ROWS = 4;
const byte COLS = 4;
const uint32_t digits[10] = {
 0b00001111111111111111111111110000, // 0
 0b00001111000000000000000011110000, //1
 0b00000000111111110000111111111111, //2
 0b00001111111100000000111111111111, //3
 0b00001111000000001111000011111111, //4
 0b00001111111100001111111100001111, //5
 0b00001111111111111111111100001111, //6
 0b00001111000000000000111111110000, //7
 0b00001111111111111111111111111111, //8
 0b00001111000000001111111111111111, //9
};
char hexaKeys [ROWS] [COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {4,5,3,2};
byte colPins[COLS] = {9,8,7,6};
Keypad kpd = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
CRGB leds[NUM_LEDS];
long Num1,Num2,Number;
char key,action;
boolean result = false;
void setDigit(int display, int val, CHSV colour){
    for(int i = 0; i<NUM_LEDS_PER_DIGIT; i++){
      colour.v = bitRead(digits[val], i)*255;
      leds[display*NUM_LEDS_PER_DIGIT + i] = colour;
    }
}
void setup() {
  // put your setup code here, to run once:
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds,NUM_LEDS);
  FastLED.setBrightness(50);
  Serial.begin(9600);
  display.begin(SH1106_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
}
void loop() {
key = kpd.getKey(); //storing pressed key value in a char
if (key!=NO_KEY)
DetectButtons();
DisplayResult(); 
}
void DetectButtons()
{ 
    display.clearDisplay();
    if (key=='*') //If cancel Button is pressed
    {Serial.println ("Button Cancel"); Number=Num1=Num2=0; result=false;}
     if (key == '1') //If Button 1 is pressed
    {Serial.println ("Button 1"); 
    if (Number==0)
    Number=1;
    else
    Number = (Number*10) + 1; //Pressed twice
    }
     if (key == '4') //If Button 4 is pressed
    {Serial.println ("Button 4"); 
    if (Number==0)
    Number=4;
    else
    Number = (Number*10) + 4; //Pressed twice
    }
     if (key == '7') //If Button 7 is pressed
    {Serial.println ("Button 7");
    if (Number==0)
    Number=7;
    else
    Number = (Number*10) + 7; //Pressed twice
    } 
    if (key == '0')
    {Serial.println ("Button 0"); //Button 0 is Pressed
    if (Number==0)
    Number=0;
    else
    Number = (Number*10) + 0; //Pressed twice
    }
     if (key == '2') //Button 2 is Pressed
    {Serial.println ("Button 2"); 
     if (Number==0)
    Number=2;
    else
    Number = (Number*10) + 2; //Pressed twice
    }
     if (key == '5')
    {Serial.println ("Button 5"); 
     if (Number==0)
    Number=5;
    else
    Number = (Number*10) + 5; //Pressed twice
    }
     if (key == '8')
    {Serial.println ("Button 8"); 
     if (Number==0)
    Number=8;
    else
    Number = (Number*10) + 8; //Pressed twice
    }   
    if (key == '#')
    {Serial.println ("Button Equal"); 
    Num2=Number;
    result = true;
    }
     if (key == '3')
    {Serial.println ("Button 3"); 
     if (Number==0)
    Number=3;
    else
    Number = (Number*10) + 3; //Pressed twice
    }
     if (key == '6')
    {Serial.println ("Button 6"); 
    if (Number==0)
    Number=6;
    else
    Number = (Number*10) + 6; //Pressed twice
    }
     if (key == '9')
    {Serial.println ("Button 9");
    if (Number==0)
    Number=9;
    else
    Number = (Number*10) + 9; //Pressed twice
    }  
}
void DisplayResult()
{
  display.setTextSize(2);
  display.setCursor(0, 10);
  display.print("Takda:");
  display.setCursor(0,35);
  display.print(Number); 
  display.display();
  for(int i = 0; i<NUM_LEDS_PER_DIGIT; i++){
      setDigit(i, number, CHSV(0,255,255);
  }
  FastLED.show(); 
delay(300);
}