/*
 * File:   3digit_7segment.c
 * Author: Vojta
 *
 * Created on 8. srpna 2022, 21:30
 */


#include "hlava.h"


void seg3_print(unsigned int num, unsigned char tecka)
{ 
    if(seg_count1 < 1) 
    {
       digit = 1;
    }
        
    if(digit ==1)
    {
        if((num/100)%10 == 0) //****zabraneni rosvziceni nuly (na miste stovek je 0 takze vypni))
        nul = 1;
        else nul = 0;
        
         if(!dig1)
         seg3_send(num_sel((num/100)%10,tecka)); //**** rozlození císla na stovky
         dig1 = 1;
 
        if(seg_count1 >= 2)
        {
           dig1 = 0;
           digit++;
        }
    }
    
    if(digit ==2)
    {
        if(!dig2)
        seg3_send(num_sel((num/10)%10,tecka)); //**** rozlození císla na desitky
        dig2 = 1;
      
        if(seg_count1 >= 4)
        {
           dig2 = 0;
           digit++;
        }
    }
    
    if(digit ==3)
    {
        if(!dig3)
        seg3_send(num_sel(num%10,tecka));   //**** rozlození císla na jednotky
        dig3 = 1;         

        if(seg_count1 >= 6)
        {
           dig3 = 0;
           digit = 1;
           seg_count1 = 0;
        }
    }
      
      
}


void seg3_send(unsigned char segnum) //*** zapis do 8-bit shift registru
{ 
    for(i=0;i<8;i++)
      {
          if((segnum>>i & 0x01))
          SER = 1;
          else SER = 0;

          SRCLK = 1;
          SRCLK = 0;      
      }RCLK = 1; RCLK = 0;    
}

unsigned char num_sel(unsigned char num, unsigned char tecka)
{
    unsigned char num_seg;
    switch(num)
    {
        case 0:
            if(nul == 0 || digit == 3 || tecka )  //****zabraneni rosvziceni nuly na desitkach a stovkach ale na jednotkach vzdy nechat (proto: || digit == 3)
            num_seg = 0b11111100; //0
            else num_seg = 0b00000000;//nic 
            break;
        case 1: num_seg = 0b01100000; break;//1
        case 2: num_seg = 0b11011010; break;//2
        case 3: num_seg = 0b11110010; break;//3     
        case 4: num_seg = 0b01100110; break;//4
        case 5: num_seg = 0b10110110; break;//5
        case 6: num_seg = 0b10111110; break;//6
        case 7: num_seg = 0b11100000; break;//7
        case 8: num_seg = 0b11111110; break;//8     
        case 9: num_seg = 0b11110110; break;//9    
                      
        default: break;
    }
    if(tecka && digit == 1) num_seg |= 0x01; //zobrazeni tecky (0bxxxxxxx1)
        
    return num_seg;
}

void seg3_zhav(void)
{
    seg3_send(1); // tecka
    dig1 = 1;
    dig2 = 1;
    dig3 = 1;
    
}

void seg3_tri_pomlcky(void)
{
    seg3_send(0b00000010); // pomlcky
    dig1 = 1;
    dig2 = 1;
    dig3 = 1;    
}