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

48 lines
1 KiB
C
Raw Normal View History

#include "pcf8574.h"
#include <i2c/i2c.h>
uint8_t pcf8574_port_read(uint8_t addr)
{
uint8_t res;
if (i2c_slave_read(addr, NULL, &res, 1))
return 0;
2016-03-01 21:17:07 +00:00
return res;
}
size_t pcf8574_port_read_buf(uint8_t addr, void *buf, size_t len)
{
2016-03-01 21:17:07 +00:00
if (!len || !buf) return 0;
uint8_t *_buf = (uint8_t *)buf;
if (i2c_slave_read(addr, NULL, _buf, len))
return 0;
2016-03-01 21:17:07 +00:00
return len;
}
size_t pcf8574_port_write_buf(uint8_t addr, void *buf, size_t len)
{
2016-03-01 21:17:07 +00:00
if (!len || !buf) return 0;
uint8_t *_buf = (uint8_t *)buf;
if (i2c_slave_write(addr, NULL, _buf, len))
return 0;
2016-03-01 21:17:07 +00:00
return len;
}
void pcf8574_port_write(uint8_t addr, uint8_t value)
{
i2c_slave_write(addr, NULL, &value, 1);
}
bool pcf8574_gpio_read(uint8_t addr, uint8_t num)
{
2016-03-01 21:17:07 +00:00
return (bool)((pcf8574_port_read(addr) >> num) & 1);
}
void pcf8574_gpio_write(uint8_t addr, 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);
pcf8574_port_write (addr, (pcf8574_port_read(addr) & mask) | bit);
}