/*
    PLAY Embedded demos - Copyright (C) 2014-2016 Carmine Forino

    This file is part of PLAY Embedded demos.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

/*===========================================================================*/
/* Constant values                                                           */
/*===========================================================================*/
#define NUMB_OF_PRESET          10       /* Available preset                 */
#define NUMB_OF_SEGMENTS         7       /* Available segments               */

#define MAX_NUMB                 9       /* High counting range limit        */

/*===========================================================================*/
/* PIN-map.                                                                  */
/*===========================================================================*/
#define DISP_PIN_0              2        /* digital pin input for segment A  */
#define DISP_PIN_1              3        /* digital pin input for segment B  */
#define DISP_PIN_2              4        /* digital pin input for segment C  */
#define DISP_PIN_3              5        /* digital pin input for segment D  */
#define DISP_PIN_4              6        /* digital pin input for segment E  */
#define DISP_PIN_5              7        /* digital pin input for segment F  */
#define DISP_PIN_6              8        /* digital pin input for segment G  */
#define IR1                     9        /* digital pin input for ir sensor  */
#define IR2                     10       /* digital pin input for ir sensor  */

/*===========================================================================*/
/* Lookup table.                                                             */
/*===========================================================================*/
const byte displaySEG[NUMB_OF_PRESET][NUMB_OF_SEGMENTS] = {
        /*  A     B     C     D     E     F     G            */
        { HIGH, HIGH, HIGH, HIGH, HIGH, HIGH,  LOW }, /*  0  */
        {  LOW, HIGH, HIGH,  LOW,  LOW,  LOW,  LOW }, /*  1  */
        { HIGH, HIGH,  LOW, HIGH, HIGH,  LOW, HIGH }, /*  2  */
        { HIGH, HIGH, HIGH, HIGH,  LOW,  LOW, HIGH }, /*  3  */
        {  LOW, HIGH, HIGH,  LOW,  LOW, HIGH, HIGH }, /*  4  */
        { HIGH,  LOW, HIGH, HIGH,  LOW, HIGH, HIGH }, /*  5  */
        { HIGH,  LOW, HIGH, HIGH, HIGH, HIGH, HIGH }, /*  6  */
        { HIGH, HIGH, HIGH,  LOW,  LOW,  LOW,  LOW }, /*  7  */
        { HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH }, /*  8  */
        { HIGH, HIGH, HIGH, HIGH,  LOW, HIGH, HIGH }, /*  9  */
};

/*===========================================================================*/
/* Functions and variables                                                   */
/*===========================================================================*/
int IR1_out = HIGH; /* Avoiding initial false detections.    */
int IR2_out = HIGH; /* Avoiding initial false detections.    */
int counter = 0;
int i = 0;

/**
  *  This function writes a number from 0 to 9 on the display
  */
void setDisplayValue(int n) {
  for(i = 0; i < NUMB_OF_SEGMENTS; i++){
    digitalWrite(DISP_PIN_0 + i, displaySEG[n % 10][i]);
  }
}

/**
  *  This function writes 0 on the display.
  */
#define initDisplay()     setDisplayValue(0)

/**
  *  This function increase counter and update display.
  *  Note that if counter become greater than MAX_NUMB it wraps to 1.
  */
void increaseAndDisplay() {
  counter++;
  if(counter > MAX_NUMB) {
    counter = 1;
  }
  setDisplayValue(counter);
}

/**
  *  This function decrease counter and update display.
  *  Note that this is performed only when counter is not equal to 0.
  */
void decreaseAndDisplay() {
  if(counter) {
    counter--;
    setDisplayValue(counter);
  }
}

/*===========================================================================*/
/* Setup and loop                                                            */
/*===========================================================================*/

void setup() {
  pinMode(IR1, INPUT);
  pinMode(IR2, INPUT);
  /* Setting Display related PIN as OUTPUT */
  for(i = 0; i < NUMB_OF_SEGMENTS; i++){
    pinMode(DISP_PIN_0 + i, OUTPUT);
  } 
  initDisplay();
}

void loop() {
  IR1_out = digitalRead(IR1);
  IR2_out = digitalRead(IR2);
  if(IR1_out == LOW) {
    increaseAndDisplay();
    delay(500);
  }
  if(IR2_out == LOW) {
    decreaseAndDisplay();
    delay(500);
  } 
  delay(100);
}
