added session addr stuff
This commit is contained in:
parent
777a760033
commit
8740fafca2
|
@ -11,22 +11,36 @@ import (
|
||||||
type Field interface {
|
type Field interface {
|
||||||
Name() string
|
Name() string
|
||||||
|
|
||||||
}
|
Size() int // the size of the data.
|
||||||
type PacketField struct {
|
|
||||||
Name string
|
// returns something like
|
||||||
Type string
|
// AuxVoltage uint16
|
||||||
Units string
|
// used inside the packet struct
|
||||||
Conversion float32
|
Embed() string
|
||||||
|
|
||||||
|
// returns
|
||||||
|
Marshal() string
|
||||||
|
Decode() string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// this is a standard field, not a bitfield.
|
||||||
|
type DataField struct {
|
||||||
|
Name string
|
||||||
|
Type string
|
||||||
|
Units string // mostly for documentation
|
||||||
|
Conversion float32
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// a PacketDef is a full can packet.
|
||||||
type PacketDef struct {
|
type PacketDef struct {
|
||||||
Name string
|
Name string
|
||||||
Description string
|
Description string
|
||||||
Id uint32
|
Id uint32
|
||||||
BigEndian bool
|
BigEndian bool
|
||||||
data: []PacketField
|
data: []Field
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// we need to generate bitfield types.
|
// we need to generate bitfield types.
|
||||||
// packet structs per each packet
|
// packet structs per each packet
|
||||||
// constancts for packet IDs or a map.
|
// constancts for packet IDs or a map.
|
||||||
|
@ -46,8 +60,8 @@ it also needs a json marshalling.
|
||||||
|
|
||||||
func (b *BMSMeasurement)MarshalPacket() ([]byte, error) {
|
func (b *BMSMeasurement)MarshalPacket() ([]byte, error) {
|
||||||
pkt := make([]byte, b.Size())
|
pkt := make([]byte, b.Size())
|
||||||
binary.LittleEndian.PutUint16(b.BatteryVoltage * 0.01)
|
binary.LittleEndian.PutUint16(pkt[0:], b.BatteryVoltage * 0.01)
|
||||||
binary.LittleEndian.PutUint16(b.AuxVoltage * 0.001)
|
binary.LittleEndian.PutUint16(pkt[2:],b.AuxVoltage * 0.001)
|
||||||
binary.LittleEndian.PutFloat32(b.Current) // TODO: make float function
|
binary.LittleEndian.PutFloat32(b.Current) // TODO: make float function
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,7 +78,7 @@ it also needs a json marshalling.
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BMSMeasurement) String() string {
|
func (b *BMSMeasurement) String() string {
|
||||||
return 'blah blah"
|
return "blah blah"
|
||||||
}
|
}
|
||||||
|
|
||||||
we also need some kind of mechanism to lookup data type.
|
we also need some kind of mechanism to lookup data type.
|
||||||
|
@ -74,4 +88,4 @@ we also need some kind of mechanism to lookup data type.
|
||||||
// insert really massive switch case statement here.
|
// insert really massive switch case statement here.
|
||||||
}
|
}
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -9,6 +9,7 @@ package xbee
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
@ -25,6 +26,19 @@ import (
|
||||||
// TODO: implement net.Conn for Session/Conn. We are missing LocalAddr, RemoteAddr,
|
// TODO: implement net.Conn for Session/Conn. We are missing LocalAddr, RemoteAddr,
|
||||||
// and Deadline related methods.
|
// and Deadline related methods.
|
||||||
|
|
||||||
|
// XBeeAddr is an XBee device address.
|
||||||
|
type XBeeAddr uint64
|
||||||
|
|
||||||
|
func (addr XBeeAddr)String() string {
|
||||||
|
return fmt.Sprintf("%X", uint64(addr))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func (addr XBeeAddr) Network() string {
|
||||||
|
return "xbee"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Session represents a connection to a locally-attached XBee. The connection can be through
|
// Session represents a connection to a locally-attached XBee. The connection can be through
|
||||||
// serial/USB or TCP/IP depending on what is supported by the device.
|
// serial/USB or TCP/IP depending on what is supported by the device.
|
||||||
// Session implements the net.Conn interface, so it can be used anywhere a net.Conn can be used.
|
// Session implements the net.Conn interface, so it can be used anywhere a net.Conn can be used.
|
||||||
|
@ -137,6 +151,8 @@ func (sess *Session) Write(p []byte) (int, error) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// internal function used by Conn to write data to a specific address.
|
||||||
func (sess *Session) writeAddr(p []byte, dest uint64) (n int, err error) {
|
func (sess *Session) writeAddr(p []byte, dest uint64) (n int, err error) {
|
||||||
|
|
||||||
idx, ch, err := sess.ct.GetMark()
|
idx, ch, err := sess.ct.GetMark()
|
||||||
|
@ -228,6 +244,20 @@ func (sess *Session) Close() error {
|
||||||
return sess.ioDev.Close()
|
return sess.ioDev.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func (sess *Session) LocalAddr() XBeeAddr {
|
||||||
|
// TODO: should we get this once at the start? and then just store it?
|
||||||
|
sh, _ := sess.ATCommand([2]rune{'S', 'H'}, nil, false)
|
||||||
|
sl, _ := sess.ATCommand([2]rune{'S', 'L'}, nil, false)
|
||||||
|
|
||||||
|
addr := uint64(binary.BigEndian.Uint32(sh)) << 32 & uint64(binary.BigEndian.Uint32(sl))
|
||||||
|
return XBeeAddr(addr)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sess *Session) RemoteAddr() XBeeAddr {
|
||||||
|
return 0xFFFF
|
||||||
|
}
|
||||||
|
|
||||||
// Conn is a connection to a specific remote XBee. Conn allows for the user to
|
// Conn is a connection to a specific remote XBee. Conn allows for the user to
|
||||||
// contact one Xbee for point-to-point communications. This enables ACK packets
|
// contact one Xbee for point-to-point communications. This enables ACK packets
|
||||||
// for reliable transmission.
|
// for reliable transmission.
|
||||||
|
|
Loading…
Reference in a new issue