esp-open-rtos/extras/pcf8574/pcf8574.c
Zaltora 813477aa8a I2c optimization and features (#321)
* custom delay
* Update comment
* add bus control status, add some missing include & fixed display output on sh1104 (#319)
* add some missing include
* Fixed display on SH1106
* Fix comment, add force sytem, rework flag, 16 bits data transfert
* Update all library with new I2C API
* custom delay
* Update comment, add bus control status
* fix i2c read + fix ds3231 temp + fix ssd1306 send
2017-03-21 11:41:47 +05:00

47 lines
1 KiB
C

#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;
return res;
}
size_t pcf8574_port_read_buf(uint8_t addr, void *buf, size_t len)
{
if (!len || !buf) return 0;
uint8_t *_buf = (uint8_t *)buf;
if (i2c_slave_read(addr, NULL, _buf, len))
return 0;
return len;
}
size_t pcf8574_port_write_buf(uint8_t addr, void *buf, size_t len)
{
if (!len || !buf) return 0;
uint8_t *_buf = (uint8_t *)buf;
if (i2c_slave_write(addr, NULL, _buf, len))
return 0;
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)
{
return (bool)((pcf8574_port_read(addr) >> num) & 1);
}
void pcf8574_gpio_write(uint8_t addr, uint8_t num, bool value)
{
uint8_t bit = (uint8_t)value << num;
uint8_t mask = ~(1 << num);
pcf8574_port_write (addr, (pcf8574_port_read(addr) & mask) | bit);
}