Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions bmxx80/bmxx80.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import (
"periph.io/x/conn/v3/spi"
)

// I2CAddr i2c default address.
Copy link
Member

Choose a reason for hiding this comment

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

Can you make a separate PR for this and the file below? They are unrelated to firmata.

Copy link
Author

Choose a reason for hiding this comment

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

Didn't mean for that to make it in, will move to another PR.

const I2CAddr uint16 = 0x77

// Oversampling affects how much time is taken to measure each of temperature,
// pressure and humidity.
//
Expand Down
43 changes: 43 additions & 0 deletions firmata/analog_mapping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package firmata
Copy link
Member

Choose a reason for hiding this comment

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

For every files: please add copyright.


import (
"bytes"
"fmt"
)

type AnalogMappingResponse struct {
Copy link
Member

Choose a reason for hiding this comment

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

For every public symbols: Please document or make private.

AnalogPinToDigital []uint8
DigitalPinToAnalog map[uint8]uint8
}

func ParseAnalogMappingResponse(data []byte) AnalogMappingResponse {
var response = AnalogMappingResponse{
Copy link
Member

Choose a reason for hiding this comment

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

For most var uses in this PR: var is unnecessary.

AnalogPinToDigital: []uint8{},
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.

DigitalPinToAnalog: map[uint8]uint8{},
}

for i := 0; i < len(data); i++ {
if data[i] != CapabilityResponsePinDelimiter {
response.DigitalPinToAnalog[uint8(i)] = uint8(len(response.AnalogPinToDigital))
response.AnalogPinToDigital = append(response.AnalogPinToDigital, uint8(i))
}
}

return response
}

func (a AnalogMappingResponse) String() string {
Copy link
Member

Choose a reason for hiding this comment

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

I recommend a pointer receiver otherwise it makes a copy.

str := bytes.Buffer{}
for analogPin, digitalPin := range a.AnalogPinToDigital {
_, _ = fmt.Fprintf(&str, "A%d: %d\n", analogPin, digitalPin)
Copy link
Member

Choose a reason for hiding this comment

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

Generally speaking String() return value doesn't contain \n. Please use Sprintf() plus normal concatenation, given the expected number of pins it'll be faster in practice.

}
return str.String()
}

type ExtendedAnalogMappingResponse struct {
Pin uint8
}

func (a ExtendedAnalogMappingResponse) String() string {
return fmt.Sprintf("%d", a.Pin)
}
75 changes: 75 additions & 0 deletions firmata/capability.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package firmata

import (
"bytes"
"fmt"

"periph.io/x/conn/v3/pin"
)

var pinModeOrder = []pin.Func{
PinFuncDigitalInput,
PinFuncDigitalOutput,
PinFuncAnalogInput,
PinFuncPWM,
PinFuncServo,
PinFuncShift,
PinFuncI2C,
PinFuncOneWire,
PinFuncStepper,
PinFuncEncoder,
PinFuncSerial,
PinFuncInputPullUp,
PinFuncSPI,
PinFuncSonar,
PinFuncTone,
PinFuncDHT,
}

const CapabilityResponsePinDelimiter = 0x7F

type CapabilityResponse struct {
PinToModeToResolution []map[pin.Func]uint8
SupportedPinModes [][]pin.Func
}

func ParseCapabilityResponse(data []byte) CapabilityResponse {
var response = CapabilityResponse{
PinToModeToResolution: []map[pin.Func]uint8{{}},
SupportedPinModes: [][]pin.Func{{}},
}

var pindex = 0
for i := 0; i < len(data); {
if data[i] == CapabilityResponsePinDelimiter {
response.PinToModeToResolution = append(response.PinToModeToResolution, map[pin.Func]uint8{})
response.SupportedPinModes = append(response.SupportedPinModes, []pin.Func{})
i += 1
pindex++
} else {
pinFunc := pinModeToFuncMap[data[i]]
response.PinToModeToResolution[pindex][pinFunc] = data[i+1]
response.SupportedPinModes[pindex] = append(response.SupportedPinModes[pindex], pinFunc)
i += 2
}
}

return response
}

func (c CapabilityResponse) String() string {
str := bytes.Buffer{}
for p, modeMap := range c.PinToModeToResolution {
_, _ = fmt.Fprintf(&str, "pin %2v: [", p)
if len(modeMap) > 0 {
for _, mode := range pinModeOrder {
if resolution, ok := modeMap[mode]; ok {
_, _ = fmt.Fprintf(&str, "%s: %d, ", mode, resolution)
}
}
str.Truncate(str.Len() - 2)
}
_, _ = fmt.Fprintf(&str, "]\n")
}
return str.String()
}
Loading