Skip to content

Conversation

@Eibborest
Copy link

First draft of driver for ina260 i2c Voltage, Current, Power monitor.

Currently Read() updates a structure with the voltage, current and power.

@maruel
Copy link
Member

maruel commented Nov 5, 2023

Thanks for the contribution! I left comments to help with the code but there's nothing critical.

@@ -0,0 +1,11 @@
// Copyright 2018 The Periph Authors. All rights reserved.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update.


)

type Power struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please document exported symbols.

)

const (
INA260_CHIP_ID uint8 = 0x40
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not export these.


dev := &i2c.Dev{Bus: bus, Addr: uint16(INA260_CHIP_ID)}
power := Power{
Current: 0,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unnecessary. Memory allocation in Go is zero-initialized.


func New(bus i2c.Bus) *ina260 {

dev := &i2c.Dev{Bus: bus, Addr: uint16(INA260_CHIP_ID)}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not an ID, this is an address. I'd put 0x40 directly as is there and would remove the constant.


ina260 := &ina260{
Conn: dev,
Power: power,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unnecessary.

Power: 0,
}

ina260 := &ina260{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can return this without using a named variable.


reg := []byte{byte(INA260_CURRENT)}
currentBytes := make([]byte, 2)
if err := i.Conn.Tx(reg, currentBytes); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

)

type Power struct {
Current float64
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants