/*
* led_driver.c
*
*/
#include "led_driver.h"
#include "mcc_generated_files/mtouch/mtouch_surface.h"
#include "mcc_generated_files/examples/i2c1_master_example.h"
#if ENABLE_LED == 1u
/*
* =======================================================================
* LED driver globals
* =======================================================================
*/
static uint8_t i2c_write_buf[3];
/*
* =======================================================================
* LED driver Function Definitions
* =======================================================================
*/
void init_led_driver(void)
{
i2c_write_buf[0] = LED_HOR_DIR_ADDR; // reg direction
i2c_write_buf[1] = 0x00; // output
I2C1_WriteNBytes(LED_DRIVER_ADDR, i2c_write_buf, 2u);
i2c_write_buf[0] = LED_HOR_REG_ADDR; // reg address
i2c_write_buf[1] = 0x00; // all low 0X6F
I2C1_WriteNBytes(LED_DRIVER_ADDR, i2c_write_buf, 2u);
i2c_write_buf[0] = LED_VER_DIR_ADDR; // reg direction
i2c_write_buf[1] = 0x00; // output
I2C1_WriteNBytes(LED_DRIVER_ADDR, i2c_write_buf, 2u);
i2c_write_buf[0] = LED_VER_REG_ADDR; // reg address
i2c_write_buf[1] = 0x00; // all low 0X1F
I2C1_WriteNBytes(LED_DRIVER_ADDR, i2c_write_buf, 2u);
}
void led_gpio_update(uint8_t data, uint8_t ver_or_hor)
{
if (ver_or_hor == LED_HOR) {
i2c_write_buf[0] = 0x14; // reg address
i2c_write_buf[1] = data; // all low
I2C1_WriteNBytes(LED_DRIVER_ADDR, i2c_write_buf, 2u);
} else {
i2c_write_buf[0] = 0x15; // reg address
i2c_write_buf[1] = data; // all low
I2C1_WriteNBytes(LED_DRIVER_ADDR, i2c_write_buf, 2u);
}
}
void led_decode_position(void)
{
uint8_t h_pos = 0u, v_pos = 0u;
led_reset();
if (MTOUCH_Surface_Contact_Status_Get(0u)) {
h_pos = (MTOUCH_Surface_Position_Get(HORIZONTAL,0u) / 43u);
h_pos = (1u << (5u - h_pos));
led_gpio_update(h_pos, LED_HOR);
v_pos = (MTOUCH_Surface_Position_Get(VERTICAL,0u) / 52u);
v_pos = (1u << (4u - v_pos));
led_gpio_update(v_pos, LED_VER);
}
}
void led_reset(void)
{
led_gpio_update(0u, LED_HOR);
led_gpio_update(0u, LED_VER);
}
#endif