/*
lednumeric.cpp - Library for writing on a 8 led 
LED display using a shiftRegister 74HC595 and an atmega328p
Created by Yuchen Zhang 
http://yuchenzhang.com
base on fito_segrera's Code, Feb-2015.
http://fii.to
*/

#include "Arduino.h"
#include "lednumeric.h"

int numbers[] = {
	1, //   00000001 	--> Display #0
	2, //   00000010 	--> Display #1
	4, //   00000100 	--> Display #2
	8, //   00001000 	--> Display #3
	16, //  00010000 	--> Display #4
	32, //  00100000 	--> Display #5
	64, //  01000000 	--> Display #6
	128, // 10000000 	--> Display #7
};

int l, c, d;

void Lednumeric::setupPins(int latchpin, int clockpin, int datapin){
	pinMode(latchpin, OUTPUT); // Shiftregister's latch pin
	pinMode(clockpin, OUTPUT); // Shiftregister's clock pin
	pinMode(datapin, OUTPUT); // Shiftregister's data pin

	l = latchpin;
	c = clockpin;
	d = datapin;
        
}

void Lednumeric::alphaWrite (int number){
	int b;
	int n = number;
	//Get the decimal equivalent to the binarry corresponding
	//to the nuber tha will be written on the display
	for(int i = 0; i < sizeof(numbers); i++){
		if(i == n){
			b = numbers[i]; 
		}
	}
	//first set latch pin low so we can shift in data without disrupting the outputs
    digitalWrite(l, LOW);
    // shift out bits of data
    shiftOut(d, c, LSBFIRST, b);  
    //set latch pin high to send data to output pins
    digitalWrite(l, HIGH);
    delay(5);
}

// turn off the output when the press is released
void Lednumeric::betaWrite (int number){
	int b;
	int n = number;
	//Get the decimal equivalent to the binarry corresponding
	//to the nuber tha will be written on the display
	for(int i = 0; i < sizeof(numbers); i++){
		if(i == n){
			b = numbers[i]; 
		}
	}
	//first set latch pin low so we can shift in data without disrupting the outputs
    digitalWrite(l, LOW);
    // shift out bits of data
    shiftOut(d, c, LSBFIRST, 0);  
    //set latch pin high to send data to output pins
    digitalWrite(l, HIGH);
    delay(5);
}