
/***********************************************************************************
* functions for drawing 7 segemnts digits 
*/

/***********************************************************************************
* INCLUDES
*/
#include "ssd1306.h"
#include <hal_types.h>

//-------------------------------------------------
// prototypes

//typedef void (*digfun_ptr_t)(uint8,uint8);
//                  

//static digfun_ptr_t digfun[]={digit_0,digit_1,digit_2,digit_3,digit_4,digit_5,digit_6,digit_7,digit_8,digit_9};
//                        0     1    2    3    4    5    6     7    8    9  
static uint8 digMask[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d ,0x27,0x7f,0x67 };
/**   
  bits - segments relations
   1
  +--+
 6|7 |2
  +--+
 5|  |3
  +--+
    4
**/
//                                                                                 index 0 1 2 3  
//static uint8 segStat[4]; // current segments status for each digit bitwize. Digits order h h m m

//-------------------------
void left_seg(uint8 x,uint8 y,uint8 on){
  uint8 h=y+VER_SEG_HIGHT;
  uint8 i;
  
  for(i=0;i<SEG_THICK;i++){
      oled_draw_line(x,y,x,h,on);
      x++;
      y++;
      h--;
      //oled_display();
      
  }
  
}
//-------------------------
void top_seg(uint8 x,uint8 y,uint8 on){
  uint8 w=x+HOR_SEG_WIDTH;
  uint8 i;
 
  for(i=0;i<SEG_THICK;i++){
      oled_draw_line(x,y,w,y,on);
      x++;
      y++;
      w--;
      //oled_display();
      
  }

}
//-------------------------
void bot_seg(uint8 x,uint8 y,uint8 on){
  
  uint8 w=x+HOR_SEG_WIDTH;
  uint8 i;
 
  for(i=0;i<SEG_THICK;i++){
      oled_draw_line(x,y,w,y,on);
      x++;
      y--;
      w--;
      //oled_display();
      
  }

}
//-------------------------

void right_seg(uint8 x,uint8 y,uint8 on){
  uint8 h=y+VER_SEG_HIGHT;
  uint8 i;
  x+=SEG_THICK;
  
  for(i=0;i<SEG_THICK;i++){
      oled_draw_line(x,y,x,h,on);
    
      x--;
      y++;
      h--;
      
      
  }
}
//-------------------------
void mid_seg(uint8 x,uint8 y,uint8 on){
  
  bot_seg(x+1,y,on);
  top_seg(x+1,y,on);
  
 
    
  
  
}
//-------------------------
static void draw_digit(uint8 x, uint8 y,uint8 segs){
  
  segs & 1?top_seg(x+2,y,1):top_seg(x+2,y,0);
  segs & 2?right_seg(x+HOR_SEG_WIDTH,y,1):right_seg(x+HOR_SEG_WIDTH,y,0);
  segs & 4?right_seg(x+HOR_SEG_WIDTH,y+VER_SEG_HIGHT+1,1):right_seg(x+HOR_SEG_WIDTH,y+VER_SEG_HIGHT+1,0);
  segs & 8?bot_seg(x+1,y+VER_SEG_HIGHT+VER_SEG_HIGHT+1,1):bot_seg(x+1,y+VER_SEG_HIGHT+VER_SEG_HIGHT+1,0);
  segs & 16?left_seg(x,y+VER_SEG_HIGHT+1,1):left_seg(x,y+VER_SEG_HIGHT+1,0);
  segs & 32?left_seg(x,y,1):left_seg(x,y,0);
  segs & 64?mid_seg(x+1,y+VER_SEG_HIGHT,1):mid_seg(x+1,y+VER_SEG_HIGHT,0);
  
}
//-------------------------
void colon_seg(uint8 x,uint8 y,uint8 onOff){
      
    oled_draw_line(x,y,x+4,y,onOff);
    oled_draw_line(x,y+1,x+4,y+1,onOff);
    
    oled_draw_line(x,y+16,x+4,y+16,onOff);
    oled_draw_line(x,y+17,x+4,y+17,onOff);
}
//-------------------------------------------
// DIGITS
//-------------------------------------------

//-------------------------------------------

//-------------------------------------------
void disp_minute(uint8 min){
  static uint8 lastHiDigit=100;
  static uint8 lastLoDigit=100;
  
  uint8 newDig=min%10;
  if(newDig!=lastLoDigit){
    draw_digit(MINUTE_LOW_DIG_X,ALL_DIGS_Y,digMask[newDig]);
    oled_dig_disp_update(MINUTE_LOW_DIG_X,ALL_DIGS_Y);
    lastLoDigit=newDig;
    //return;
  }
  newDig=(min/10)%10;
  if(newDig!=lastHiDigit){
    draw_digit(MINUTE_HIGH_DIG_X,ALL_DIGS_Y,digMask[newDig]);
    oled_dig_disp_update(MINUTE_HIGH_DIG_X,ALL_DIGS_Y);
    lastHiDigit=newDig;
    return;
  }
  
  
}
//-------------------------------------------
void disp_hour(uint8 hour){
  static uint8 lastHiDigit=100;
  static uint8 lastLoDigit=100;
  uint8 newDig;
  newDig=hour%10;
  if(newDig!=lastLoDigit){
    draw_digit(HOUR_LOW_DIG_X,ALL_DIGS_Y,digMask[newDig]);
    oled_dig_disp_update(HOUR_LOW_DIG_X,ALL_DIGS_Y);
    lastLoDigit=newDig;
    return;
  }
  newDig=(hour/10)%10;
  if(newDig!=lastHiDigit){
      draw_digit(HOUR_HIGH_DIG_X,ALL_DIGS_Y,digMask[newDig]);  
      oled_dig_disp_update(HOUR_HIGH_DIG_X,ALL_DIGS_Y);
      lastHiDigit=newDig;
      return;
  }
  
  
  
  
}

//-------------------------------------------
void disp_colon(uint8 onOff){
  colon_seg(COLON_X,COLON_Y,onOff);
  
}
//-------------------------------------------
//-------------------------------------------
//-------------------------------------------



//-------------------------------------------