esp-open-rtos/extras/pcf8574/pcf8574.c

47 lines
1.1 KiB
C
Raw Permalink Normal View History

#include "pcf8574.h"
2017-10-18 19:25:48 +00:00
uint8_t pcf8574_port_read(i2c_dev_t *dev)
{
uint8_t res;
2017-09-01 09:29:32 +00:00
if (i2c_slave_read(dev->bus, dev->addr, NULL, &res, 1))
2017-10-18 19:25:48 +00:00
return 0;
2016-03-01 21:17:07 +00:00
return res;
}
2017-10-18 19:25:48 +00:00
size_t pcf8574_port_read_buf(i2c_dev_t *dev, void *buf, size_t len)
{
2016-03-01 21:17:07 +00:00
if (!len || !buf) return 0;
uint8_t *_buf = (uint8_t *)buf;
2017-09-01 09:29:32 +00:00
if (i2c_slave_read(dev->bus, dev->addr, NULL, _buf, len))
return 0;
2016-03-01 21:17:07 +00:00
return len;
}
2017-10-18 19:25:48 +00:00
size_t pcf8574_port_write_buf(const i2c_dev_t *dev, void *buf, size_t len)
{
2016-03-01 21:17:07 +00:00
if (!len || !buf) return 0;
uint8_t *_buf = (uint8_t *)buf;
2017-09-01 09:29:32 +00:00
if (i2c_slave_write(dev->bus, dev->addr, NULL, _buf, len))
return 0;
2016-03-01 21:17:07 +00:00
return len;
}
2017-10-18 19:25:48 +00:00
void pcf8574_port_write(const i2c_dev_t *dev, uint8_t value)
{
2017-09-01 09:29:32 +00:00
i2c_slave_write(dev->bus, dev->addr, NULL, &value, 1);
}
2017-10-18 19:25:48 +00:00
bool pcf8574_gpio_read(i2c_dev_t *dev, uint8_t num)
{
2017-09-01 09:29:32 +00:00
return (bool)((pcf8574_port_read(dev) >> num) & 1);
}
2017-10-18 19:25:48 +00:00
void pcf8574_gpio_write(i2c_dev_t *dev, uint8_t num, bool value)
{
2016-03-01 21:17:07 +00:00
uint8_t bit = (uint8_t)value << num;
uint8_t mask = ~(1 << num);
2017-10-18 19:25:48 +00:00
pcf8574_port_write(dev, (pcf8574_port_read(dev) & mask) | bit);
}