change rel control
This commit is contained in:
@@ -1,132 +0,0 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <rtthread.h>
|
||||
|
||||
#include "chrg_i2c.h"
|
||||
#include "chrg_pcf8574.h"
|
||||
|
||||
#define LOG_TAG "chrg.pcf"
|
||||
#define DBG_LEVEL DBG_LOG
|
||||
#include <rtdbg.h>
|
||||
|
||||
|
||||
// 读取所有8口IO状态
|
||||
rt_uint8_t chrg_pcf8574_get_port (eIDX_I2C idx)
|
||||
{
|
||||
rt_uint8_t value = 0;
|
||||
if (idx >= TOTAL_I2CS) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
rt_ssize_t ret = 0;
|
||||
struct chrg_i2c_t *pI2C = &chrgi2c[idx];
|
||||
|
||||
if (!pI2C->opened) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
ret = rt_device_read(&pI2C->dev->parent, pI2C->slave, &value, 1);
|
||||
if (ret <= 0) {
|
||||
LOG_E("read failed. [%d]", ret);
|
||||
return 0;
|
||||
}
|
||||
|
||||
pI2C->port_value = value;
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
// 读取指定管脚状态值
|
||||
rt_uint8_t chrg_pcf8574_get_pin (eIDX_I2C idx, uint8_t bit)
|
||||
{
|
||||
rt_uint8_t data = chrg_pcf8574_get_port(idx);
|
||||
if (data & (1 << bit)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 设置所有8口IO状态值
|
||||
int chrg_pcf8574_set_port (eIDX_I2C idx, uint8_t value)
|
||||
{
|
||||
if (idx >= TOTAL_I2CS) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
rt_ssize_t ret = 0;
|
||||
struct chrg_i2c_t *pI2C = &chrgi2c[idx];
|
||||
|
||||
if (!pI2C->opened) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = rt_device_write(&pI2C->dev->parent, pI2C->slave, &value, 1);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
// 设置指定管脚状态值
|
||||
int chrg_pcf8574_set_pin (eIDX_I2C idx, uint8_t bit, uint8_t value)
|
||||
{
|
||||
if (idx >= TOTAL_I2CS) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ret = 0;
|
||||
struct chrg_i2c_t *pI2C = &chrgi2c[idx];
|
||||
|
||||
if (!pI2C->opened) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
rt_uint8_t data = chrg_pcf8574_get_port(idx);
|
||||
if (value == 0) {
|
||||
data &= ~(1 << bit);
|
||||
} else {
|
||||
data |= 1 << bit;
|
||||
}
|
||||
|
||||
ret = chrg_pcf8574_set_port(idx, data);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#if 1
|
||||
int pcf_test (int argc, char **argv)
|
||||
{
|
||||
if (argc < 3) {
|
||||
rt_kprintf("help : %s <get|set> <0|1|2|3> [0, 255]", argv[0]);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int idx = atoi(argv[2]);
|
||||
if ((idx > 3)||(idx < 0)) {
|
||||
rt_kprintf("help : %s <get|set> <0|1|2|3> [0, 255]", argv[0]);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (strcmp(argv[1], "set") == 0) {
|
||||
if (argc != 4) {
|
||||
rt_kprintf("help : %s <get|set> <0|1|2|3> [0, 255]", argv[0]);
|
||||
return -1;
|
||||
}
|
||||
rt_uint8_t val = (rt_uint8_t)atoi(argv[3]);
|
||||
chrg_pcf8574_set_port((eIDX_I2C)idx, val);
|
||||
} else if (strcmp(argv[1], "get") == 0) {
|
||||
rt_uint8_t data = chrg_pcf8574_get_port((eIDX_I2C)idx);
|
||||
rt_kprintf("value : 0x%02X", data);
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
MSH_CMD_EXPORT(pcf_test, test pcf8574 port/pin.);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
#ifndef __CHRG_PCF8574_H__
|
||||
#define __CHRG_PCF8574_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
#include "chrg_i2c.h"
|
||||
|
||||
// 读取所有8口IO状态
|
||||
extern rt_uint8_t chrg_pcf8574_get_port (eIDX_I2C idx);
|
||||
|
||||
// 读取指定管脚状态值
|
||||
extern rt_uint8_t chrg_pcf8574_get_pin (eIDX_I2C idx, uint8_t bit);
|
||||
|
||||
// 设置所有8口IO状态值
|
||||
extern int chrg_pcf8574_set_port (eIDX_I2C idx, uint8_t value);
|
||||
|
||||
// 设置指定管脚状态值
|
||||
extern int chrg_pcf8574_set_pin (eIDX_I2C idx, uint8_t bit, uint8_t value);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Reference in New Issue
Block a user