2019-03-12 01:41:16 +06:00
|
|
|
#include "action.h"
|
|
|
|
#include "left.h"
|
2024-04-03 05:44:25 +06:00
|
|
|
#include "print.h"
|
2019-03-12 01:41:16 +06:00
|
|
|
#include "wait.h"
|
|
|
|
|
|
|
|
bool i2c_initialized = false;
|
|
|
|
|
|
|
|
void left_config(void);
|
2024-01-17 19:05:38 +06:00
|
|
|
i2c_status_t left_write(uint8_t reg, uint8_t data);
|
|
|
|
i2c_status_t left_read(uint8_t reg, uint8_t *data);
|
2019-03-12 01:41:16 +06:00
|
|
|
|
|
|
|
uint8_t init_mcp23017(void)
|
|
|
|
{
|
|
|
|
i2c_initialized = false;
|
|
|
|
|
|
|
|
if (i2c_initialized == 0)
|
|
|
|
{
|
|
|
|
i2c_init(); // on pins D(1,0)
|
|
|
|
_delay_ms(1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
void left_init(void)
|
|
|
|
{
|
|
|
|
init_mcp23017();
|
|
|
|
left_scan();
|
|
|
|
#if 0
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
print("wait\n");
|
|
|
|
wait_us(1000000);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void left_scan(void)
|
|
|
|
{
|
2024-01-17 19:05:38 +06:00
|
|
|
|
|
|
|
i2c_status_t ret = i2c_ping_address(I2C_ADDR, HOTDOX_I2C_TIMEOUT);
|
2019-03-12 01:41:16 +06:00
|
|
|
|
2024-01-17 19:05:38 +06:00
|
|
|
if (ret == I2C_STATUS_SUCCESS)
|
2019-03-12 01:41:16 +06:00
|
|
|
{
|
|
|
|
if (!i2c_initialized)
|
|
|
|
{
|
|
|
|
i2c_initialized = true;
|
|
|
|
left_config();
|
|
|
|
clear_keyboard();
|
|
|
|
print("mcp23017 attached!!!\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (i2c_initialized)
|
|
|
|
{
|
|
|
|
i2c_initialized = false;
|
|
|
|
clear_keyboard();
|
|
|
|
print("mcp23017 deattached!!!\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t left_read_cols(void)
|
|
|
|
{
|
|
|
|
uint8_t data = 0;
|
|
|
|
|
|
|
|
left_read(MCP23017_B0_GPIOA, &data);
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void left_unselect_rows(void)
|
|
|
|
{
|
|
|
|
left_write(MCP23017_B0_IODIRB, 0x3F);
|
|
|
|
}
|
|
|
|
|
|
|
|
void left_select_row(uint8_t row)
|
|
|
|
{
|
|
|
|
left_write(MCP23017_B0_IODIRB, ~(1 << row));
|
|
|
|
}
|
|
|
|
|
|
|
|
void left_config(void)
|
|
|
|
{
|
|
|
|
left_write(MCP23017_B0_IODIRA, 0x7F);
|
|
|
|
left_write(MCP23017_B0_IPOLA, 0x7F);
|
|
|
|
left_write(MCP23017_B0_GPPUA, 0x7F);
|
|
|
|
|
|
|
|
left_write(MCP23017_B0_IODIRB, 0xFF);
|
|
|
|
left_write(MCP23017_B0_GPIOB, 0xC0);
|
|
|
|
}
|
|
|
|
|
2024-01-17 19:05:38 +06:00
|
|
|
i2c_status_t left_write(uint8_t reg, uint8_t data)
|
2019-03-12 01:41:16 +06:00
|
|
|
{
|
|
|
|
if (!i2c_initialized)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-01-17 19:05:38 +06:00
|
|
|
i2c_status_t ret;
|
2019-03-12 01:41:16 +06:00
|
|
|
|
2024-03-27 18:41:53 +06:00
|
|
|
ret = i2c_write_register(I2C_ADDR, reg, &data, sizeof(data), HOTDOX_I2C_TIMEOUT);
|
2019-03-12 01:41:16 +06:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-01-17 19:05:38 +06:00
|
|
|
i2c_status_t left_read(uint8_t reg, uint8_t *data)
|
2019-03-12 01:41:16 +06:00
|
|
|
{
|
|
|
|
if (!i2c_initialized)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-01-17 19:05:38 +06:00
|
|
|
i2c_status_t ret = 0;
|
2024-03-27 18:41:53 +06:00
|
|
|
ret = i2c_read_register(I2C_ADDR, reg, data, 1, HOTDOX_I2C_TIMEOUT);
|
2019-03-12 01:41:16 +06:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|