Skip to content

Commit

Permalink
Fix firmata slice bounds out of range error
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Jul 10, 2014
1 parent 8d76823 commit f0ca761
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
33 changes: 25 additions & 8 deletions platforms/firmata/firmata.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ func newBoard(sp io.ReadWriteCloser) *board {
board.MinorVersion = 0
board.Serial = sp
board.FirmwareName = ""
board.Pins = make([]pin, 100)
board.AnalogPins = make([]byte, 0)
board.Pins = []pin{}
board.AnalogPins = []byte{}
board.Connected = false
board.Events = make([]event, 0)
board.Events = []event{}
return board
}

Expand Down Expand Up @@ -209,7 +209,8 @@ func (b *board) togglePinReporting(pin byte, state byte, mode byte) {
}

func (b *board) i2cReadRequest(slaveAddress byte, numBytes uint) {
b.write([]byte{StartSysex, I2CRequest, slaveAddress, (I2CModeRead << 3), byte(numBytes & 0x7F), byte(((numBytes >> 7) & 0x7F)), EndSysex})
b.write([]byte{StartSysex, I2CRequest, slaveAddress, (I2CModeRead << 3),
byte(numBytes & 0x7F), byte(((numBytes >> 7) & 0x7F)), EndSysex})
}

func (b *board) i2cWriteRequest(slaveAddress byte, data []byte) {
Expand Down Expand Up @@ -262,7 +263,13 @@ func (b *board) process(data []byte) {
pin := (messageType & 0x0F)

b.Pins[b.AnalogPins[pin]].Value = int(value)
b.Events = append(b.Events, event{Name: fmt.Sprintf("analog_read_%v", pin), Data: []byte{byte(value >> 24), byte(value >> 16), byte(value >> 8), byte(value & 0xff)}})
b.Events = append(b.Events,
event{Name: fmt.Sprintf("analog_read_%v", pin),
Data: []byte{
byte(value >> 24), byte(value >> 16), byte(value >> 8), byte(value & 0xff),
},
},
)

case DigitalMessageRangeStart <= messageType && DigitalMessageRangeEnd >= messageType:
port := messageType & 0x0F
Expand All @@ -275,7 +282,11 @@ func (b *board) process(data []byte) {
pin := b.Pins[pinNumber]
if byte(pin.Mode) == Input {
pin.Value = int((portValue >> (byte(i) & 0x07)) & 0x01)
b.Events = append(b.Events, event{Name: fmt.Sprintf("digital_read_%v", pinNumber), Data: []byte{byte(pin.Value & 0xff)}})
b.Events = append(b.Events,
event{Name: fmt.Sprintf("digital_read_%v", pinNumber),
Data: []byte{byte(pin.Value & 0xff)},
},
)
}
}

Expand Down Expand Up @@ -346,7 +357,11 @@ func (b *board) process(data []byte) {
pin.Value = int(uint(pin.Value) | uint(currentBuffer[6])<<14)
}

b.Events = append(b.Events, event{Name: fmt.Sprintf("pin_%v_state", currentBuffer[2]), Data: []byte{byte(pin.Value & 0xff)}})
b.Events = append(b.Events,
event{Name: fmt.Sprintf("pin_%v_state", currentBuffer[2]),
Data: []byte{byte(pin.Value & 0xff)},
},
)
case I2CReply:
i2cReply := map[string][]byte{
"slave_address": []byte{byte(currentBuffer[2]) | byte(currentBuffer[3])<<7},
Expand All @@ -360,7 +375,9 @@ func (b *board) process(data []byte) {
if i+2 > len(currentBuffer) {
break
}
i2cReply["data"] = append(i2cReply["data"], byte(currentBuffer[i])|byte(currentBuffer[i+1])<<7)
i2cReply["data"] = append(i2cReply["data"],
byte(currentBuffer[i])|byte(currentBuffer[i+1])<<7,
)
}
b.Events = append(b.Events, event{Name: "i2c_reply", I2cReply: i2cReply})

Expand Down
8 changes: 6 additions & 2 deletions platforms/firmata/firmata_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package firmata

import (
"fmt"
"strconv"

"github.com/hybridgroup/gobot"
"github.com/tarm/goserial"
"strconv"
)

type FirmataAdaptor struct {
Expand Down Expand Up @@ -89,7 +90,10 @@ func (f *FirmataAdaptor) AnalogRead(pin string) int {
events := f.Board.findEvents(fmt.Sprintf("analog_read_%v", pin))
if len(events) > 0 {
event := events[len(events)-1]
return int(uint(event.Data[0])<<24 | uint(event.Data[1])<<16 | uint(event.Data[2])<<8 | uint(event.Data[3]))
return int(uint(event.Data[0])<<24 |
uint(event.Data[1])<<16 |
uint(event.Data[2])<<8 |
uint(event.Data[3]))
}
return -1
}
Expand Down

0 comments on commit f0ca761

Please sign in to comment.