diff options
| author | Loic Guegan <manzerbredes@mailbox.org> | 2023-08-11 09:48:06 +0200 |
|---|---|---|
| committer | Loic Guegan <manzerbredes@mailbox.org> | 2023-08-11 09:48:06 +0200 |
| commit | d43c92e2a1abc64e86c59529ec02d3eb394ccf4d (patch) | |
| tree | 06a73ed52dd00c44fdec9de7ef9d0b3e7450e997 | |
| parent | 451a80d636e184be9902d0dd466a36747f8291a6 (diff) | |
Change sysfs architecture and add power,current and voltage metrics
| -rw-r--r-- | ina260.c | 33 |
1 files changed, 28 insertions, 5 deletions
@@ -16,7 +16,6 @@ #define INA260_REG_MANUFACTURER 0xFE #define INA260_REG_DIE 0xFF -#define INA260_LSB_POWER 10 // 10mW #define INA260_IS_ATTR(_name) (strcmp(attr->attr.name, #_name) == 0) // ina260 average modes list @@ -156,12 +155,26 @@ static ssize_t attr_metric_show(struct kobject *_kobj, char *buf) { int rvalue; + int op1, op2; struct client_data *cdata=container_of(_kobj,struct client_data,kobj); if(INA260_IS_ATTR(power)){ if(ina260_read_register(cdata, INA260_REG_POWER,&rvalue)) - return -1; - return sprintf(buf, "%d.%d\n", INA260_LSB_POWER*rvalue/1000,(INA260_LSB_POWER*rvalue) % 1000); + return -1; + op1=10*rvalue; + return sprintf(buf, "%d.%d\n", op1/1000,(op1) % 1000); + } else if(INA260_IS_ATTR(voltage)){ + if(ina260_read_register(cdata, INA260_REG_VOLTAGE,&rvalue)) + return -1; + op1=rvalue*25/100 + rvalue; + op2=rvalue*25%100; + return sprintf(buf, "%d.%03d%d\n",(op1)/1000,(op1)%1000,op2); + } else if(INA260_IS_ATTR(current)){ + if(ina260_read_register(cdata, INA260_REG_CURRENT,&rvalue)) + return -1; + op1=rvalue*25/100 + rvalue; + op2=rvalue*25%100; + return sprintf(buf, "%d.%03d%d\n",(op1)/1000,(op1)%1000,op2); } return -1; @@ -359,13 +372,22 @@ static const struct attribute_group registers_group = { // ----- Metrics ----- static struct kobj_attribute metric_power_attribute = __ATTR(power, 0664, attr_metric_show, NULL); +static struct kobj_attribute metric_voltage_attribute = + __ATTR(voltage, 0664, attr_metric_show, NULL); +static struct kobj_attribute metric_current_attribute = { + .attr = {.name = "current", // current is a defined macro so need to do it manually + .mode = VERIFY_OCTAL_PERMISSIONS(0444) }, + .show = attr_metric_show, + .store = NULL +}; static struct attribute *metrics_attrs[] = { &metric_power_attribute.attr, + &metric_voltage_attribute.attr, + &metric_current_attribute.attr, NULL, }; static const struct attribute_group metrics_group = { - .attrs = metrics_attrs, - .name = "metrics" + .attrs = metrics_attrs }; // ----- Fields ----- static struct kobj_attribute reset_field_attribute = @@ -427,6 +449,7 @@ static struct attribute *fields_attrs[] = { }; static const struct attribute_group fields_group = { .attrs = fields_attrs, + .name = "fields" }; |
