/**
  ******************************************************************************
  * @file    VAPC-meter
  * @author  Vladimir N. Shilov <shilow@ukr.net>
  * @version V0.0.1
  * @date    2015.05.15
  * @brief   Main program body
  ******************************************************************************
  * @attention
  *
  * ----------------------------------------------------------------------------
  * "THE BEER-WARE LICENSE" (Revision 42):
  * <shilow@ukr.net> wrote this file.  As long as you retain this notice you
  * can do whatever you want with this stuff. If we meet some day, and you think
  * this stuff is worth it, you can buy me a beer in return.   Shilov V.N.
  * ----------------------------------------------------------------------------
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm8l15x.h"
#include "rtos.h"
#include "max7219.h"

/** @addtogroup STM8L15x_StdPeriph_Template
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define LED_RED_PORT    GPIOC
#define LED_RED_PIN     GPIO_Pin_4
#define LED_GREEN_PORT  GPIOB
#define LED_GREEN_PIN   GPIO_Pin_7

/* Private macro -------------------------------------------------------------*/
#define LED_RED_ON      LED_RED_PORT->ODR &= (uint8_t)(~LED_RED_PIN)
#define LED_RED_OFF     LED_RED_PORT->ODR |= LED_RED_PIN
#define LED_GREEN_ON    LED_GREEN_PORT->ODR &= (uint8_t)(~LED_GREEN_PIN)
#define LED_GREEN_OFF   LED_GREEN_PORT->ODR |= LED_GREEN_PIN

/* Private constant ----------------------------------------------------------*/
// перевод числа 0-7 в номер индикатора
static const max7219_reg_t dig[8] = {
	Digit0,
	Digit1,
	Digit2,
	Digit3,
	Digit4,
	Digit5,
	Digit6,
	Digit7
};

// перевод значения 0x00 - 0x0F в код индикатора
static const max7219_sym_t num[16] = {
	Sym_0,
	Sym_1,
	Sym_2,
	Sym_3,
	Sym_4,
	Sym_5,
	Sym_6,
	Sym_7,
	Sym_8,
	Sym_9,
	Sym_A,
	Sym_b,
	Sym_C,
	Sym_d,
	Sym_E,
	Sym_F
};

/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
static void GPIO_Config(void);
static void CLK_Config(void);

/* RTOS function prototypes -----------------------------------------------*/
static void OutLed07(void);
static void OutLed8f(void);
static void ToggleLed(void);

/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
void main(void)
{
  /* Clock configuration -----------------------------------------*/
  CLK_Config();

  /* GPIO Configuration  -----------------------------------------*/
  GPIO_Config();

  /* RTOS Configuration */
  RTOS_Init();

  /* MAX7219 Configuration */
  MAX7219_Init();

  /* ROTS tasks */
  RTOS_SetTask(OutLed8f,0,10000);
  RTOS_SetTask(OutLed07,5000,10000);
  RTOS_SetTask(ToggleLed,1000,1000);

  /* Infinite loop */
  while (1)
  {
      RTOS_DispatchTask();
      Delay(1);
  }
}

/* для теста */
static void OutLed07(void){
  uint8_t i;
  for (i=0;i<8;i++) {
    MAX7219_WriteData(dig[i], num[i]);
  }
}

static void OutLed8f(void){
  uint8_t i;
  for (i=15;i>7;i--) {
    MAX7219_WriteData(dig[i-8], num[i]);
  }
}

static void ToggleLed(void){
    static uint8_t state=0;

    switch(state){
    case 0:
        LED_RED_ON;
        state = 1;
        break;
    case 1:
        LED_GREEN_ON;
        state = 2;
        break;
    case 2:
        LED_RED_OFF;
        state = 3;
        break;
    case 3:
        LED_GREEN_OFF;
        state = 0;
        break;
    default:
        state = 0;
    }
}


/**
  * @brief  Configure GPIO for button available on the VAPC board
  * @param  None
  * @retval None
  */
static void GPIO_Config(void)
{
  /* Configure GPIO used to drive LEDs */
  GPIO_Init(LED_RED_PORT, LED_RED_PIN, GPIO_Mode_Out_PP_High_Fast);
  GPIO_Init(LED_GREEN_PORT, LED_GREEN_PIN, GPIO_Mode_Out_PP_High_Fast);

  /* Enable general interrupts */
  enableInterrupts();
}

/**
  * @brief  Configure system clock to run at Maximum clock speed
  * @param  None
  * @retval None
  */
static void CLK_Config(void)
{
  CLK_DeInit();

  /* Configure the clock source */
  CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSI);

  /* Configure the System clock frequency to run at 16Mhz */
  CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1);

  CLK_HSICmd(ENABLE);
}


#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *   where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif

/**
  * @}
  */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
