]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/xd84/pca9555.c
Remove more commented out MCUs
[qmk_firmware.git] / keyboards / xd84 / pca9555.c
1 #include "i2c_master.h"
2 #include "pca9555.h"
3
4 #include "debug.h"
5
6 #define SLAVE_TO_ADDR(n)   (n<<1)
7 #define TIMEOUT 100
8
9 enum {
10   CMD_INPUT_0 = 0,
11   CMD_INPUT_1,
12   CMD_OUTPUT_0,
13   CMD_OUTPUT_1,
14   CMD_INVERSION_0,
15   CMD_INVERSION_1,
16   CMD_CONFIG_0,
17   CMD_CONFIG_1
18 };
19
20 void pca9555_init(uint8_t slave_addr) {
21   static uint8_t s_init = 0;
22   if (!s_init) {
23     i2c_init();
24
25     s_init=1;
26   }
27
28   // TODO: could check device connected
29   //i2c_start(SLAVE_TO_ADDR(slave) | I2C_WRITE);
30   //i2c_stop();
31 }
32
33 void pca9555_set_config(uint8_t slave_addr, uint8_t port, uint8_t conf) {
34   uint8_t addr = SLAVE_TO_ADDR(slave_addr);
35   uint8_t cmd = port ? CMD_OUTPUT_1 : CMD_OUTPUT_0;
36
37   i2c_status_t ret = i2c_writeReg(addr, cmd, &conf, sizeof(conf), TIMEOUT);
38   if (ret != I2C_STATUS_SUCCESS) {
39     print("pca9555_set_config::FAILED\n");
40   }
41 }
42
43 void pca9555_set_output(uint8_t slave_addr, uint8_t port, uint8_t conf) {
44   uint8_t addr = SLAVE_TO_ADDR(slave_addr);
45   uint8_t cmd = port ? CMD_CONFIG_1 : CMD_CONFIG_0;
46
47   i2c_status_t ret = i2c_writeReg(addr, cmd, &conf, sizeof(conf), TIMEOUT);
48   if (ret != I2C_STATUS_SUCCESS) {
49     print("pca9555_set_output::FAILED\n");
50   }
51 }
52
53 uint8_t pca9555_readPins(uint8_t slave_addr, uint8_t port) {
54   uint8_t addr = SLAVE_TO_ADDR(slave_addr);
55   uint8_t cmd = port ? CMD_INPUT_1 : CMD_INPUT_0;
56
57   uint8_t data = 0;
58   i2c_status_t ret = i2c_readReg(addr, cmd, &data, sizeof(data), TIMEOUT);
59   if (ret != I2C_STATUS_SUCCESS) {
60     print("pca9555_readPins::FAILED\n");
61   }
62   return data;
63 }