/*
 * File:   main.c
 * Author: boos
 *
 * Created on June 1, 2019, 12:26 PM
 */

#include <xc.h>

// BEGIN CONFIG
#pragma config FOSC = INTOSCIO
#pragma config WDTE = OFF
#pragma config PWRTE = OFF
#pragma config MCLRE = ON
#pragma config BOREN = OFF
#pragma config LVP = OFF
#pragma config CPD = OFF
#pragma config CP = OFF
//END CONFIG

// define ports for LEDs
#define LED1 RB0
#define LED2 RB1
#define LED3 RB2
#define LED4 RB3
#define LED5 RB4
#define LED6 RB5
#define LED7 RB6
#define LED8 RA0
#define LED9 RA1

// define port for switch
// (note the "!" which inverts the status, see below)
#define BUTTON !RB7

void main(void) {

  // enable weak pull-up resistors on PORTB
  nRBPU = 0;
  
  // all LEDs are outputs
  TRISB0 = 0;
  TRISB1 = 0;
  TRISB2 = 0;
  TRISB3 = 0;
  TRISB4 = 0;
  TRISB5 = 0;
  TRISB6 = 0;
  TRISA0 = 0;
  TRISA1 = 0;
  
  // the switch is an input
  TRISB7 = 1;
  
  // switch off comparator at port A
  CMCON = 0b111;
  
  // "counter" is the variable that stores the current
  // number that is displayed on the dice
  static unsigned char counter = 0;
  
  // this variable is used for the "slowing down" effect
  static int buffer = 0;
  
  // main loop
  while(1) {
    
    // when the button is pressed, keep advancing the dice number
    // (this happens really fast and makes the number "random")
    if (BUTTON) {
      counter++;
      if (counter > 11) {  
          counter = 0;
      }
      buffer = 1500;
      
    // when the button is released, reduce the buffer variable
    // one by one. Whenever it passes 100 steps, advance the
    // dice number by one.
    } else {
        if (buffer > 0) {
            buffer--;
            if (buffer % 100 == 0) {
                counter++;
                if (counter > 11) {  
                    counter = 0;
                }
            }
        }
    }
    
    // The following lines show the LED patterns that correspond
    // to a dice number. There are 12 variations because we also
    // include dice outcomes that are rotated by 90 degrees so that
    // there is a larger variety in outcomes.
    
    // 1
    if (counter == 0) {  
        LED1 = 0;
        LED2 = 0;
        LED3 = 0;
        LED4 = 0;
        LED5 = 1;
        LED6 = 0;
        LED7 = 0;
        LED8 = 0;
        LED9 = 0;

    // 2
    } else if (counter == 1) {    
        LED1 = 1;
        LED2 = 0;
        LED3 = 0;
        LED4 = 0;
        LED5 = 0;
        LED6 = 0;
        LED7 = 0;
        LED8 = 0;
        LED9 = 1;

    // 3
    } else if (counter == 2) {    
        LED1 = 1;
        LED2 = 0;
        LED3 = 0;
        LED4 = 0;
        LED5 = 1;
        LED6 = 0;
        LED7 = 0;
        LED8 = 0;
        LED9 = 1;

    // 4
    } else if (counter == 3) {    
        LED1 = 1;
        LED2 = 0;
        LED3 = 1;
        LED4 = 0;
        LED5 = 0;
        LED6 = 0;
        LED7 = 1;
        LED8 = 0;
        LED9 = 1;

    // 5
    } else if (counter == 4) {    
        LED1 = 1;
        LED2 = 0;
        LED3 = 1;
        LED4 = 0;
        LED5 = 1;
        LED6 = 0;
        LED7 = 1;
        LED8 = 0;
        LED9 = 1;

    // 6
    } else if (counter == 5) {    
        LED1 = 1;
        LED2 = 0;
        LED3 = 1;
        LED4 = 1;
        LED5 = 0;
        LED6 = 1;
        LED7 = 1;
        LED8 = 0;
        LED9 = 1;
 
    // 1
    } else if (counter == 6) {  
        LED1 = 0;
        LED2 = 0;
        LED3 = 0;
        LED4 = 0;
        LED5 = 1;
        LED6 = 0;
        LED7 = 0;
        LED8 = 0;
        LED9 = 0;

    // 2
    } else if (counter == 7) {    
        LED1 = 0;
        LED2 = 0;
        LED3 = 1;
        LED4 = 0;
        LED5 = 0;
        LED6 = 0;
        LED7 = 1;
        LED8 = 0;
        LED9 = 0;

    // 3
    } else if (counter == 8) {    
        LED1 = 0;
        LED2 = 0;
        LED3 = 1;
        LED4 = 0;
        LED5 = 1;
        LED6 = 0;
        LED7 = 1;
        LED8 = 0;
        LED9 = 0;

    // 4
    } else if (counter == 9) {    
        LED1 = 1;
        LED2 = 0;
        LED3 = 1;
        LED4 = 0;
        LED5 = 0;
        LED6 = 0;
        LED7 = 1;
        LED8 = 0;
        LED9 = 1;

    // 5
    } else if (counter == 10) {    
        LED1 = 1;
        LED2 = 0;
        LED3 = 1;
        LED4 = 0;
        LED5 = 1;
        LED6 = 0;
        LED7 = 1;
        LED8 = 0;
        LED9 = 1;

    // 6
    } else if (counter == 11) {    
        LED1 = 1;
        LED2 = 1;
        LED3 = 1;
        LED4 = 0;
        LED5 = 0;
        LED6 = 0;
        LED7 = 1;
        LED8 = 1;
        LED9 = 1;
    }
  
  }    
  
  return;
  
}