1.hx711模组命名修正。

This commit is contained in:
MacRsh
2023-11-19 15:24:13 +08:00
parent 7f18983f4c
commit 40263fb932
3 changed files with 48 additions and 42 deletions

View File

@@ -7,7 +7,7 @@ HX711常与应力传感器或其他模拟传感器组合,采集传感器的模
## 注册HX711
```c
int hx711_register(struct hx711 *hx711, const char *name, int sck_pin, int dout_pin);
int mr_hx711_register(struct mr_hx711 *hx711, const char *name, int sck_pin, int dout_pin);
```
| 参数 | 描述 |
@@ -24,7 +24,7 @@ int hx711_register(struct hx711 *hx711, const char *name, int sck_pin, int dout_
```c
#define FILTER_BITS 4
mr_dev_ioctl(desc, HX711_CTRL_SET_FILTER_BITS, mr_make_local(int, FILTER_BITS));
mr_dev_ioctl(desc, MR_CTRL_HX711_SET_FILTER_BITS, mr_make_local(int, FILTER_BITS));
```
## 自校准
@@ -32,7 +32,7 @@ mr_dev_ioctl(desc, HX711_CTRL_SET_FILTER_BITS, mr_make_local(int, FILTER_BITS));
自校准零点,使用前请确保输入值稳定。
```c
mr_dev_ioctl(desc, HX711_CTRL_SET_SELF_CALIBRATION, MR_NULL);
mr_dev_ioctl(desc, MR_CTRL_HX711_SET_SELF_CAL, MR_NULL);
```
## 读取数据
@@ -51,12 +51,12 @@ mr_dev_read(desc, &data, sizeof(data));
#define HX711_FILTER_BITS 4
struct struct hx711 hx711;
struct struct mr_hx711 hx711;
int main(void)
{
/* 注册hx711 */
hx711_register(&hx711, "hx711", HX711_SCK_PIN, HX711_DOUT_PIN);
mr_hx711_register(&hx711, "hx711", HX711_SCK_PIN, HX711_DOUT_PIN);
/* 打开hx711 */
int desc = mr_dev_open("hx711", MR_OFLAG_RDONLY);
@@ -67,10 +67,10 @@ int main(void)
}
/* 设置滤波位数 */
mr_dev_ioctl(desc, HX711_CTRL_SET_FILTER_BITS, mr_make_local(int, HX711_FILTER_BITS));
mr_dev_ioctl(desc, MR_CTRL_HX711_SET_FILTER_BITS, mr_make_local(int, HX711_FILTER_BITS));
/* 自校准 */
mr_dev_ioctl(desc, HX711_CTRL_SET_SELF_CALIBRATION, MR_NULL);
mr_dev_ioctl(desc, MR_CTRL_HX711_SET_SELF_CAL, MR_NULL);
/* 读取数据 */
uint32_t data = 0;

View File

@@ -16,13 +16,13 @@
#include "include/device/gpio.h"
static void hx711_set_sck(struct hx711 *hx711, uint8_t value)
static void hx711_set_sck(struct mr_hx711 *hx711, uint8_t value)
{
mr_dev_ioctl(hx711->desc, MR_CTRL_SET_OFFSET, &hx711->sck_pin);
mr_dev_write(hx711->desc, &value, sizeof(value));
}
static uint8_t hx711_get_dout(struct hx711 *hx711)
static uint8_t hx711_get_dout(struct mr_hx711 *hx711)
{
uint8_t value = 0;
@@ -31,7 +31,7 @@ static uint8_t hx711_get_dout(struct hx711 *hx711)
return value;
}
static uint32_t hx711_get_value(struct hx711 *hx711)
static uint32_t hx711_get_value(struct mr_hx711 *hx711)
{
uint32_t value = 0;
int i = 0;
@@ -71,9 +71,9 @@ static uint32_t hx711_get_value(struct hx711 *hx711)
return value;
}
static int hx711_open(struct mr_dev *dev)
static int mr_hx711_open(struct mr_dev *dev)
{
struct hx711 *hx711 = (struct hx711 *)dev;
struct mr_hx711 *hx711 = (struct mr_hx711 *)dev;
hx711->desc = mr_dev_open("gpio", MR_OFLAG_RDWR);
if (hx711->desc < 0)
@@ -91,9 +91,9 @@ static int hx711_open(struct mr_dev *dev)
return MR_EOK;
}
static int hx711_close(struct mr_dev *dev)
static int mr_hx711_close(struct mr_dev *dev)
{
struct hx711 *hx711 = (struct hx711 *)dev;
struct mr_hx711 *hx711 = (struct mr_hx711 *)dev;
/* Reset the sck pin mode */
mr_dev_ioctl(hx711->desc, MR_CTRL_SET_OFFSET, &hx711->sck_pin);
@@ -108,9 +108,9 @@ static int hx711_close(struct mr_dev *dev)
return MR_EOK;
}
static ssize_t hx711_read(struct mr_dev *dev, int off, void *buf, size_t size, int sync_or_async)
static ssize_t mr_hx711_read(struct mr_dev *dev, int off, void *buf, size_t size, int sync_or_async)
{
struct hx711 *hx711 = (struct hx711 *)dev;
struct mr_hx711 *hx711 = (struct mr_hx711 *)dev;
uint32_t *rd_buf = (uint32_t *)buf;
ssize_t rd_size = 0;
@@ -118,19 +118,19 @@ static ssize_t hx711_read(struct mr_dev *dev, int off, void *buf, size_t size, i
for (rd_size = 0; rd_size < size; rd_size += sizeof(*rd_buf))
{
uint32_t value = hx711_get_value(hx711);
*rd_buf = (value > hx711->self_calibration) ? (value - hx711->self_calibration) : 0;
*rd_buf = (value > hx711->self_cal) ? (value - hx711->self_cal) : 0;
rd_buf++;
}
return rd_size;
}
static int hx711_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
static int mr_hx711_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
{
struct hx711 *hx711 = (struct hx711 *)dev;
struct mr_hx711 *hx711 = (struct mr_hx711 *)dev;
switch (cmd)
{
case HX711_CTRL_SET_FILTER_BITS:
case MR_CTRL_HX711_SET_FILTER_BITS:
{
if (args != MR_NULL)
{
@@ -140,13 +140,13 @@ static int hx711_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
}
return MR_EINVAL;
}
case HX711_CTRL_SET_SELF_CALIBRATION:
case MR_CTRL_HX711_SET_SELF_CAL:
{
hx711->self_calibration = hx711_get_value(hx711);
hx711->self_cal = hx711_get_value(hx711);
return MR_EOK;
}
case HX711_CTRL_GET_FILTER_BITS:
case MR_CTRL_HX711_GET_FILTER_BITS:
{
if (args != MR_NULL)
{
@@ -155,11 +155,11 @@ static int hx711_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
}
return MR_EINVAL;
}
case HX711_CTRL_GET_SELF_CALIBRATION:
case MR_CTRL_HX711_GET_SELF_CAL:
{
if (args != MR_NULL)
{
*(uint32_t *)args = hx711->self_calibration;
*(uint32_t *)args = hx711->self_cal;
return MR_EOK;
}
return MR_EINVAL;
@@ -182,15 +182,15 @@ static int hx711_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
*
* @return MR_EOK on success, otherwise an error code.
*/
int hx711_register(struct hx711 *hx711, const char *name, int sck_pin, int dout_pin)
int mr_hx711_register(struct mr_hx711 *hx711, const char *name, int sck_pin, int dout_pin)
{
static struct mr_dev_ops ops =
{
hx711_open,
hx711_close,
hx711_read,
mr_hx711_open,
mr_hx711_close,
mr_hx711_read,
MR_NULL,
hx711_ioctl,
mr_hx711_ioctl,
MR_NULL
};
@@ -201,9 +201,10 @@ int hx711_register(struct hx711 *hx711, const char *name, int sck_pin, int dout_
/* Initialize the fields */
hx711->filter_bits = 0;
hx711->self_calibration = 0;
hx711->self_cal = 0;
hx711->sck_pin = sck_pin;
hx711->dout_pin = dout_pin;
hx711->desc = -1;
/* Register the hx711 */
return mr_dev_register(&hx711->dev, name, Mr_Dev_Type_Adc, MR_SFLAG_RDONLY | MR_SFLAG_NONDRV, &ops, MR_NULL);

View File

@@ -6,10 +6,10 @@
* @date 2023-11-17 MacRsh First version
*/
#ifndef _HX711_H
#define _HX711_H
#ifndef _HX711_H_
#define _HX711_H_
#include "mr_lib.h"
#include "mr_api.h"
#ifdef __cplusplus
extern "C" {
@@ -20,20 +20,25 @@ extern "C" {
/**
* @brief HX711 command.
*/
#define HX711_CTRL_SET_FILTER_BITS ((0x01|0x80) << 16)
#define HX711_CTRL_SET_SELF_CALIBRATION ((0x02|0x80) << 16)
#define HX711_CTRL_GET_FILTER_BITS ((0x01|0x00) << 16)
#define HX711_CTRL_GET_SELF_CALIBRATION ((0x02|0x00) << 16)
#define MR_CTRL_HX711_SET_FILTER_BITS ((0x01|0x80) << 16) /**< Set filter bits */
#define MR_CTRL_HX711_SET_SELF_CAL ((0x02|0x80) << 16) /**< Set self calibration */
#define MR_CTRL_HX711_GET_FILTER_BITS ((0x01|0x00) << 16) /**< Get filter bits */
#define MR_CTRL_HX711_GET_SELF_CAL ((0x02|0x00) << 16) /**< Get self calibration */
/**
* @brief HX711 data type.
*/
typedef uint32_t mr_hx711_data_t; /**< HX711 read data type */
/**
* @brief Hx711 structure.
*/
struct hx711
struct mr_hx711
{
struct mr_dev dev; /**< Device */
int filter_bits; /**< Filter bits */
uint32_t self_calibration; /**< Self calibration */
uint32_t self_cal; /**< Self calibration */
int sck_pin; /**< SCK pin */
int dout_pin; /**< DOUT pin */
int desc; /**< Descriptor */
@@ -43,7 +48,7 @@ struct hx711
* @addtogroup HX711.
* @{
*/
int hx711_register(struct hx711 *hx711, const char *name, int sck_pin, int dout_pin);
int mr_hx711_register(struct mr_hx711 *hx711, const char *name, int sck_pin, int dout_pin);
/** @} */
#endif /* MR_USING_HX711 */
@@ -51,4 +56,4 @@ int hx711_register(struct hx711 *hx711, const char *name, int sck_pin, int dout_
}
#endif /* __cplusplus */
#endif /* _HX711_H */
#endif /* _HX711_H_ */