2016-03-01 17:56:55 +00:00
|
|
|
#include "pcf8574.h"
|
|
|
|
#include <i2c/i2c.h>
|
|
|
|
|
|
|
|
uint8_t pcf8574_port_read(uint8_t addr)
|
|
|
|
{
|
2017-03-21 06:41:47 +00:00
|
|
|
uint8_t res;
|
|
|
|
if (i2c_slave_read(addr, NULL, &res, 1))
|
|
|
|
return 0;
|
2016-03-01 21:17:07 +00:00
|
|
|
return res;
|
2016-03-01 17:56:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2016-03-01 17:56:55 +00:00
|
|
|
|
2017-03-21 06:41:47 +00:00
|
|
|
if (i2c_slave_read(addr, NULL, _buf, len))
|
|
|
|
return 0;
|
2016-03-01 21:17:07 +00:00
|
|
|
return len;
|
2016-03-01 17:56:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2016-03-01 17:56:55 +00:00
|
|
|
|
2017-03-21 06:41:47 +00:00
|
|
|
if (i2c_slave_write(addr, NULL, _buf, len))
|
|
|
|
return 0;
|
2016-03-01 21:17:07 +00:00
|
|
|
return len;
|
2016-03-01 17:56:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pcf8574_port_write(uint8_t addr, uint8_t value)
|
|
|
|
{
|
2017-03-21 06:41:47 +00:00
|
|
|
i2c_slave_write(addr, NULL, &value, 1);
|
2016-03-01 17:56:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2016-03-01 17:56:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2016-03-01 17:56:55 +00:00
|
|
|
}
|