/**
 * Драйвер модуля LED-индикаторов
 */

#include "stm8s.h"
#include "led.h"

#define GPIO_HIGH(a,b)    a->ODR |= b
#define GPIO_LOW(a,b)     a->ODR &= ~b
#define GPIO_TOGGLE(a,b)  a->ODR ^= b

uint8_t LedDigits[8] = {1, 2, 3, 4, 5, 6, 7, 8}; // digits to dsplay
uint8_t LedPoint[8] = {0, 0, 0, 1, 0, 0, 0, 1}; // dots for digits

static const uint16_t led_num[8] = {0x10, 0x20, 0x80, 0x40, 0x01, 0x02, 0x08, 0x04};

static void led_SelectDigit (const uint8_t first);

/*
 * Output current value to next led
 */
void led_OutputValue(void) {
  static uint8_t ledn = 0;

  /* all off */
  LED_OUT_OFF;

  /* Fire on nex led */
  led_SelectDigit(ledn);

  /* out next value */
  switch (LedDigits[ledn]) {
    case 0:
      LED_OUT_0;
      break;
    case 1:
      LED_OUT_1;
      break;
    case 2:
      LED_OUT_2;
      break;
    case 3:
      LED_OUT_3;
      break;
    case 4:
      LED_OUT_4;
      break;
    case 5:
      LED_OUT_5;
      break;
    case 6:
      LED_OUT_6;
      break;
    case 7:
      LED_OUT_7;
      break;
    case 8:
      LED_OUT_8;
      break;
    case 9:
      LED_OUT_9;
      break;
    default:
      LED_OUT_MM;
  }
  /*
  if(LedPoint[ledn] != 0) {
    LED_OUT_DP;
  }
  */
  ledn ++;
  if (ledn > 7) {
    ledn = 0;
  }
}

/*
 * Select LED Digit.
 * If first != 0 - select next, by shift '1' on outputs,
 * else select first digit.
 * led digits sequence (b - bottom, t - top):
 * b1, b2, b4, b3, t1, t2, t4, t3.
 */
static void led_SelectDigit (const uint8_t first) {
  uint8_t i;
  uint8_t data = led_num[first];
  for (i=0; i<8; i++) {
    GPIO_LOW(SPI_PORT, SPI_SCK); // prepare CLK
    if (data & 0x80) { // if msb == 1
      GPIO_HIGH(SPI_PORT, SPI_DATA); // DATA = 1
    } else {
      GPIO_LOW(SPI_PORT, SPI_DATA); // DATA = 0
    }
    GPIO_HIGH(SPI_PORT, SPI_SCK); // shift bit
    data <<= 1;
  }
}
