/*------------------------------------------------------- 
 * 7セグメント LED 表示 (ダイナミック点灯)
 *-------------------------------------------------------*/
#include <xc.h>
#include <stdint.h>
#include <ctype.h>
#include "mcr.h"
#include "io.h"
#include "led7seg.h"

/* セグメントビット */
#define SEG_NONE  0x00
#define SEG_A     0x01
#define SEG_B     0x02
#define SEG_C     0x04
#define SEG_D     0x08
#define SEG_E     0x10
#define SEG_F     0x20
#define SEG_G     0x40
#define SEG_DP    0x80

/* 選択点灯桁(ダイナミック点灯) */
volatile static uint8_t digit_sel = 0;
/* 左桁 セグメント点灯/消灯 */
volatile static int8_t seg_d1_a, seg_d1_b, seg_d1_c;
volatile static int8_t seg_d1_d, seg_d1_e, seg_d1_f;
volatile static int8_t seg_d1_g, seg_d1_dp;
/* 中桁 セグメント点灯/消灯 */
volatile static int8_t seg_d2_a, seg_d2_b, seg_d2_c;
volatile static int8_t seg_d2_d, seg_d2_e, seg_d2_f;
volatile static int8_t seg_d2_g, seg_d2_dp;
/* 右桁 セグメント点灯/消灯 */
volatile static int8_t seg_d3_a, seg_d3_b, seg_d3_c;
volatile static int8_t seg_d3_d, seg_d3_e, seg_d3_f;
volatile static int8_t seg_d3_g, seg_d3_dp;

volatile static uint8_t ndigits = 0;

void TMR1_Init(void);
void TMR1_Start(void);
void TMR1_isr(void);

/* 7セグメント LED 表示の初期化 */
void led7seg_init(void)
{
  ndigits = 0;
  digit_sel = 0;
#if defined(LED7SEG_SEL_D1_PORT)
  LED7SEG_SEL_D1_PORT = 0; ndigits++;
#endif
#if defined(LED7SEG_SEL_D2_PORT)
  LED7SEG_SEL_D2_PORT = 0; ndigits++;
#endif
#if defined(LED7SEG_SEL_D3_PORT)
  LED7SEG_SEL_D3_PORT = 0; ndigits++;
#endif
  led7seg_display_const("   ");
  TMR1_Init();
  TMR1_Start();
}

/* 周期起動ルーチン： LED ダイナミック点灯 */
void led7seg_itimer_isr(void)
{
#if defined(LED7SEG_SEL_D1_PORT)
  LED7SEG_SEL_D1_PORT = 0;
#endif
#if defined(LED7SEG_SEL_D2_PORT)
  LED7SEG_SEL_D2_PORT = 0;
#endif
#if defined(LED7SEG_SEL_D3_PORT)
  LED7SEG_SEL_D3_PORT = 0;
#endif
  if (digit_sel == ndigits) {
    digit_sel = 0;
  }
  switch (digit_sel) {
  default:
  case 0: /* 左桁 D1 */
    LED7SEG_A_PORT  = seg_d1_a;
    LED7SEG_B_PORT  = seg_d1_b;
    LED7SEG_C_PORT  = seg_d1_c;
    LED7SEG_D_PORT  = seg_d1_d;
    LED7SEG_E_PORT  = seg_d1_e;
    LED7SEG_F_PORT  = seg_d1_f;
    LED7SEG_G_PORT  = seg_d1_g;
    LED7SEG_DP_PORT = seg_d1_dp;
#if defined(LED7SEG_SEL_D1_PORT)
    LED7SEG_SEL_D1_PORT = 1;
#endif
    digit_sel = 1;
    break;
  case 1: /* 中桁 D2 */
    LED7SEG_A_PORT  = seg_d2_a;
    LED7SEG_B_PORT  = seg_d2_b;
    LED7SEG_C_PORT  = seg_d2_c;
    LED7SEG_D_PORT  = seg_d2_d;
    LED7SEG_E_PORT  = seg_d2_e;
    LED7SEG_F_PORT  = seg_d2_f;
    LED7SEG_G_PORT  = seg_d2_g;
    LED7SEG_DP_PORT = seg_d2_dp;
#if defined(LED7SEG_SEL_D2_PORT)
    LED7SEG_SEL_D2_PORT = 1;
#endif
    digit_sel = 2;
    break;
  case 2: /* 右桁 D3 */
    LED7SEG_A_PORT  = seg_d3_a;
    LED7SEG_B_PORT  = seg_d3_b;
    LED7SEG_C_PORT  = seg_d3_c;
    LED7SEG_D_PORT  = seg_d3_d;
    LED7SEG_E_PORT  = seg_d3_e;
    LED7SEG_F_PORT  = seg_d3_f;
    LED7SEG_G_PORT  = seg_d3_g;
    LED7SEG_DP_PORT = seg_d3_dp;
#if defined(LED7SEG_SEL_D3_PORT)
    LED7SEG_SEL_D3_PORT = 1;
#endif
    digit_sel = 3;
    break;
  }
}

/* フォント: 文字コード -> セグメントビット */
static uint8_t led7seg_ch_to_sgb(char ch) 
{
  uint8_t sgb = SEG_NONE;
  switch (toupper(ch)) {
  case '0':
    sgb = (SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F); 
    break;
  case '1':
    sgb = (SEG_B | SEG_C); 
    break;
  case '2':
    sgb = (SEG_A | SEG_B | SEG_D | SEG_E | SEG_G); 
    break;
  case '3':
    sgb = (SEG_A | SEG_B | SEG_C | SEG_D | SEG_G); 
    break;
  case '4':
    sgb = (SEG_B | SEG_C | SEG_F | SEG_G); 
    break;
  case '5':
    sgb = (SEG_A | SEG_C | SEG_D | SEG_F | SEG_G); 
    break;
  case '6':
    sgb = (SEG_A | SEG_C | SEG_D | SEG_E | SEG_F | SEG_G); 
    break;
  case '7':
    sgb = (SEG_A | SEG_B | SEG_C); 
    break;
  case '8':
    sgb = (SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F | SEG_G); 
    break;
  case '9':
    sgb = (SEG_A | SEG_B | SEG_C | SEG_F | SEG_G); 
    break;
  case ' ':
    sgb = (SEG_NONE); 
    break;
  case '-':
    sgb = (SEG_G); 
    break;
  case 'A':
    sgb = (SEG_A | SEG_B | SEG_C | SEG_E | SEG_F | SEG_G); 
    break;
  case 'B':
    sgb = (SEG_C | SEG_D | SEG_E | SEG_F | SEG_G); 
    break;
  case 'C':
    sgb = (SEG_D | SEG_E | SEG_G); 
    break;
  case 'D':
    sgb = (SEG_B | SEG_C | SEG_D | SEG_E | SEG_G); 
    break;
  case 'E':
    sgb = (SEG_A | SEG_D | SEG_E | SEG_F | SEG_G); 
    break;
  case 'F':
    sgb = (SEG_A | SEG_E | SEG_F | SEG_G); 
    break;
  case 'L':
    sgb = (SEG_D | SEG_E | SEG_F); 
    break;
  case 'N': 
    sgb = (SEG_C | SEG_E | SEG_G); 
    break;
  case 'S': 
    sgb = (SEG_A | SEG_C | SEG_D | SEG_F | SEG_G); 
    break;
  }
  return sgb;
}

/* 各セグメント点灯/消灯の値 */
static void led7seg_digit(uint8_t x, uint8_t sgb)
{
  switch (x) {
  case 0: /* 左桁 D1 */
    seg_d1_a  = ((sgb & SEG_A)  == 0) ? 0 : 1;
    seg_d1_b  = ((sgb & SEG_B)  == 0) ? 0 : 1;
    seg_d1_c  = ((sgb & SEG_C)  == 0) ? 0 : 1;
    seg_d1_d  = ((sgb & SEG_D)  == 0) ? 0 : 1;
    seg_d1_e  = ((sgb & SEG_E)  == 0) ? 0 : 1;
    seg_d1_f  = ((sgb & SEG_F)  == 0) ? 0 : 1;
    seg_d1_g  = ((sgb & SEG_G)  == 0) ? 0 : 1;
    seg_d1_dp = ((sgb & SEG_DP) == 0) ? 0 : 1;
    break;
  case 1: /* 中桁 D2 */
    seg_d2_a  = ((sgb & SEG_A)  == 0) ? 0 : 1;
    seg_d2_b  = ((sgb & SEG_B)  == 0) ? 0 : 1;
    seg_d2_c  = ((sgb & SEG_C)  == 0) ? 0 : 1;
    seg_d2_d  = ((sgb & SEG_D)  == 0) ? 0 : 1;
    seg_d2_e  = ((sgb & SEG_E)  == 0) ? 0 : 1;
    seg_d2_f  = ((sgb & SEG_F)  == 0) ? 0 : 1;
    seg_d2_g  = ((sgb & SEG_G)  == 0) ? 0 : 1;
    seg_d2_dp = ((sgb & SEG_DP) == 0) ? 0 : 1;
    break;
  case 2: /* 右桁 D3 */
    seg_d3_a  = ((sgb & SEG_A)  == 0) ? 0 : 1;
    seg_d3_b  = ((sgb & SEG_B)  == 0) ? 0 : 1;
    seg_d3_c  = ((sgb & SEG_C)  == 0) ? 0 : 1;
    seg_d3_d  = ((sgb & SEG_D)  == 0) ? 0 : 1;
    seg_d3_e  = ((sgb & SEG_E)  == 0) ? 0 : 1;
    seg_d3_f  = ((sgb & SEG_F)  == 0) ? 0 : 1;
    seg_d3_g  = ((sgb & SEG_G)  == 0) ? 0 : 1;
    seg_d3_dp = ((sgb & SEG_DP) == 0) ? 0 : 1;
    break;
  }
}

/* 文字列表示(3文字): 文字列がプログラムコード領域の場合 */
void led7seg_display_const(const char *s)
{
  char r[LED7SEG_STR_SIZ_MAX];
  uint8_t i;
  for (i = 0; i < LED7SEG_STR_SIZ_MAX; i++) {
    r[i] = s[i];
    if (s[i] == '\0') {
      break;
    }
  }
  r[LED7SEG_STR_SIZ_MAX-1] = '\0';
  led7seg_display(r);
}

/* 文字列表示(3文字) */
void led7seg_display(char *s)
{
  char ch_prev = '\0';
  uint8_t x = 0;
  uint8_t i = 0;
  uint8_t sgb = 0;
  x = 0; 
  i = 0;
  ch_prev = '.';
  do {
    if (ch_prev == '.') {
      if (s[i] == '.') {
        sgb = (unsigned)led7seg_ch_to_sgb(' ') | SEG_DP;
        led7seg_digit(x, sgb);
        x++;
      } else {
        ch_prev = s[i];
      }
    } else {
      if (s[i] == '.') {
        sgb = (unsigned)led7seg_ch_to_sgb(ch_prev) | SEG_DP;
      } else {
        sgb = (unsigned)led7seg_ch_to_sgb(ch_prev);
      }
      led7seg_digit(x, sgb);
      x++;
      ch_prev = s[i];
    }
    i++;
  } while ((x < 3) && (s[i-1] != '\0'));
  while (x < 3) {
    led7seg_digit(x, SEG_NONE);
    x++;
  }
}

/*--------------------------------------------*/
/* TMR1 */
/* 7セグメントLED ダイナミック点灯用タイマとして使用 */
/* Src: FOSC/4, Prescale 1:1, タイマー周期: 4.096mSec */

#if (_XTAL_FREQ == 32000000)
#define TMR1_RELOAD_VALUE_H  0x80
#define TMR1_RELOAD_VALUE_L  0x00
#else
#error "CLOCK FREQ UNKNOWN"
#endif

void TMR1_Init(void)
{
  T1CONbits.TMR1ON = 0; /* Disabled */ 
  T1CONbits.CKPS   = 0; /* Prescale: 1:1 */
  T1CLKbits.CS     = 1; /* FOSC/4 */
  T1GCON = 0x00;
  TMR1H = TMR1_RELOAD_VALUE_H;
  TMR1L = TMR1_RELOAD_VALUE_L;
  PIR4bits.TMR1IF = 0;
  PIE4bits.TMR1IE = 1;
}

void TMR1_Start(void)
{
  T1CONbits.TMR1ON = 1;
}

void TMR1_isr(void)
{
  T1CONbits.TMR1ON = 0;
  TMR1H = TMR1_RELOAD_VALUE_H;
  TMR1L = TMR1_RELOAD_VALUE_L;
  T1CONbits.TMR1ON = 1;
  led7seg_itimer_isr();
}
