#include <SmingCore.h>
#include "configuration.h"
#include "tm1650.h"
#include "Wire.h"

/* Private Defines */
#define TM1650_ADDR_SYS  (uint8_t)(0x48 >> 1)
#define TM1650_ADDR_BTN  (uint8_t)(0x4F >> 1)
#define TM1650_ADDR_DIG1 (uint8_t)(0x68 >> 1)
#define TM1650_ADDR_DIG2 (uint8_t)(0x6A >> 1)
#define TM1650_ADDR_DIG3 (uint8_t)(0x6C >> 1)
#define TM1650_ADDR_DIG4 (uint8_t)(0x6E >> 1)

/* Private Variables */
static const uint8_t Digits[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
};
static const uint8_t Addr[LED_NUM] = {
  TM1650_ADDR_DIG1, TM1650_ADDR_DIG2, TM1650_ADDR_DIG3, TM1650_ADDR_DIG4
};
static bool isPresent;
static uint8 Buffer[LED_NUM] = {0};

/* Private Fuctions */ 
static bool I2CWriteTo(uint8_t addr, uint8_t data);

/**
 * @brief Initialization
 */
void TM1650_Init(void) {
  if (I2CWriteTo(TM1650_ADDR_SYS, 0x71)) {
    Serial.println("TM1650 not respond!");
    isPresent = false;
    return;
  }
  isPresent = true;
  TM1650_Bright(1);
}

/**
 * @brief Output digit to all indicators
 * 
 * @param i1 Value in range 0..F
 * @param i2 Value in range 0..F
 * @param i3 Value in range 0..F
 * @param i4 Value in range 0..F
 */
void TM1650_Out(uint8_t i1, uint8_t i2, uint8_t i3, uint8_t i4) {
  if (!isPresent) { return; }

  Buffer[0] = Digits[i1];
  Buffer[1] = Digits[i2];
  Buffer[2] = Digits[i3];
  Buffer[3] = Digits[i4];

  I2CWriteTo(TM1650_ADDR_DIG1, Buffer[0]);
  I2CWriteTo(TM1650_ADDR_DIG2, Buffer[1]);
  I2CWriteTo(TM1650_ADDR_DIG3, Buffer[2]);
  I2CWriteTo(TM1650_ADDR_DIG4, Buffer[3]);
}

/**
 * @brief Output symbol to indicator 1.
 * 
 * @param value Segment code.
 */
void TM1650_Out1(tm1650_sym_t value) {
  if (!isPresent) { return; }
  Buffer[0] = value;
  I2CWriteTo(TM1650_ADDR_DIG1, value);
}

/**
 * @brief Output symbol to indicator 2.
 * 
 * @param value Segment code.
 */
void TM1650_Out2(tm1650_sym_t value) {
  if (!isPresent) { return; }
  Buffer[1] = value;
  I2CWriteTo(TM1650_ADDR_DIG2, value);
}

/**
 * @brief Output symbol to indicator 3.
 * 
 * @param value Segment code.
 */
void TM1650_Out3(tm1650_sym_t value) {
  if (!isPresent) { return; }
  Buffer[2] = value;
  I2CWriteTo(TM1650_ADDR_DIG3, value);
}

/**
 * @brief Output symbol to indicator 4.
 * 
 * @param value Segment code.
 */
void TM1650_Out4(tm1650_sym_t value) {
  if (!isPresent) { return; }
  Buffer[3] = value;
  I2CWriteTo(TM1650_ADDR_DIG4, value);
}

/**
 * @brief Set dot for selected led
 * 
 * @param pos Led 0..3
 */
void TM1650_DotSet(tm1650_pos_t pos) {
  if (!isPresent) { return; }
  if (pos > LED_NUM-1) {
    return;
  }
  I2CWriteTo(Addr[pos], (Buffer[pos] | Sym_Dot));
}

/**
 * @brief Reset dot for selected led
 * 
 * @param pos Led 0..3
 */
void TM1650_DotRes(tm1650_pos_t pos) {
  if (!isPresent) { return; }
  if (pos > LED_NUM-1) {
    return;
  }
  I2CWriteTo(Addr[pos], (Buffer[pos] & ~(Sym_Dot)));
}

/**
 * @brief Set LED bright
 * 
 * @param bright Brgiht level 0..7
 */
void TM1650_Bright(uint8_t bright) {
  if (!isPresent) { return; }
  if (bright > LedBrightMax) {
    bright = LedBrightMax;
  }
  bright <<= 4;
  bright |= 1; // Display On
  I2CWriteTo(TM1650_ADDR_SYS, bright);
}

static bool I2CWriteTo(uint8_t addr, uint8_t data) {
	Wire.beginTransmission(addr);
	Wire.write(data);
	return Wire.endTransmission();
}
