-
Notifications
You must be signed in to change notification settings - Fork 50
firmata: addition of support for firmata protocol #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package firmata | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For every files: please add copyright. |
||
|
|
||
| import ( | ||
| "bytes" | ||
| "fmt" | ||
| ) | ||
|
|
||
| type AnalogMappingResponse struct { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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{ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For most var uses in this PR: var is unnecessary. |
||
| AnalogPinToDigital: []uint8{}, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
| } | ||
| 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() | ||
| } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.