#include <avr/io.h>
#include "display.h"


void Clock(void) {
	D_PORT |= _BV(D_CLK);
	D_PORT &= ~_BV(D_CLK);
}

void Strobe(void) {
	D_PORT |= _BV(D_STR);
	D_PORT &= ~_BV(D_STR);
}

void ShowNumber(uint8_t line1, uint8_t line2, uint8_t line3, uint8_t  line4) {
	uint8_t bit;
	
	for(bit=0;bit<BITCOUNT;bit++) {
		if( (line1 & _BV(bit)) == _BV(bit) ) {
  		D_PORT |= _BV(D_D1);
		}
		else {
			D_PORT &= ~_BV(D_D1);
		}

		if( (line2 & _BV(bit)) == _BV(bit) ) {
  		D_PORT |= _BV(D_D2);
		}
		else {
			D_PORT &= ~_BV(D_D2);
		}

		if( (line3 & _BV(bit)) == _BV(bit) ) {
  		D_PORT |= _BV(D_D3);
		}
		else {
			D_PORT &= ~_BV(D_D3);
		}
		
		if( (line4 & _BV(bit)) == _BV(bit) ) {
  		D_PORT |= _BV(D_D4);
		}
		else {
			D_PORT &= ~_BV(D_D4);
		}
		
		Clock();
	}
	Strobe();
}

void convertTime(uint8_t hour, uint8_t minute, uint8_t second, uint8_t* data)
{ /*{{{*/
    /* input and output */
    uint8_t hms[] = {hour, minute, second};
    /* loop counter vars */
    uint8_t i, j;

    /* array which contains our splitted time later */
    uint8_t time[] = {0, 0, 0, 0, 0, 0};

    /* depending on how to read the display */
    uint8_t rowpow[] = {8, 4, 2, 1};
    uint8_t colpow[] = {32, 16, 8, 4, 2, 1};

	/* initialize timeline array with zero */
	for (i=0; i < 4; i++){
		data[i] = 0;
	}

    /* fill time array */
    for (i = 0; i < 3; i++) {
        uint8_t unit = hms[i];
        uint8_t ten = 0;
        while (unit >= 10) { 
            unit -= 10; 
            ten++; 
        }                           
        time[i*2] = ten; 
        time[i*2+1] = unit;
    }

    /* fill rows array */
    for (i = 0; i < 4; i++) {
        uint8_t power = rowpow[i];
        for (j = 0; j < 6; j++) {
            if ((time[j] & power) > 0) {
                data[i] += colpow[j];
            }
        }
    }
} /*}}}*/

// vim:ts=4
