added session addr stuff
This commit is contained in:
parent
777a760033
commit
8740fafca2
|
@ -11,22 +11,36 @@ import (
|
|||
type Field interface {
|
||||
Name() string
|
||||
|
||||
}
|
||||
type PacketField struct {
|
||||
Name string
|
||||
Type string
|
||||
Units string
|
||||
Conversion float32
|
||||
Size() int // the size of the data.
|
||||
|
||||
// returns something like
|
||||
// AuxVoltage uint16
|
||||
// used inside the packet struct
|
||||
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 {
|
||||
Name string
|
||||
Description string
|
||||
Id uint32
|
||||
BigEndian bool
|
||||
data: []PacketField
|
||||
|
||||
data: []Field
|
||||
}
|
||||
|
||||
// we need to generate bitfield types.
|
||||
// packet structs per each packet
|
||||
// constancts for packet IDs or a map.
|
||||
|
@ -46,8 +60,8 @@ it also needs a json marshalling.
|
|||
|
||||
func (b *BMSMeasurement)MarshalPacket() ([]byte, error) {
|
||||
pkt := make([]byte, b.Size())
|
||||
binary.LittleEndian.PutUint16(b.BatteryVoltage * 0.01)
|
||||
binary.LittleEndian.PutUint16(b.AuxVoltage * 0.001)
|
||||
binary.LittleEndian.PutUint16(pkt[0:], b.BatteryVoltage * 0.01)
|
||||
binary.LittleEndian.PutUint16(pkt[2:],b.AuxVoltage * 0.001)
|
||||
binary.LittleEndian.PutFloat32(b.Current) // TODO: make float function
|
||||
}
|
||||
|
||||
|
@ -64,7 +78,7 @@ it also needs a json marshalling.
|
|||
}
|
||||
|
||||
func (b *BMSMeasurement) String() string {
|
||||
return 'blah blah"
|
||||
return "blah blah"
|
||||
}
|
||||
|
||||
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.
|
||||
}
|
||||
|
||||
*/
|
||||
*/
|
||||
|
|
|
@ -9,6 +9,7 @@ package xbee
|
|||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
|
@ -25,6 +26,19 @@ import (
|
|||
// TODO: implement net.Conn for Session/Conn. We are missing LocalAddr, RemoteAddr,
|
||||
// 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
|
||||
// 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.
|
||||
|
@ -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) {
|
||||
|
||||
idx, ch, err := sess.ct.GetMark()
|
||||
|
@ -228,6 +244,20 @@ func (sess *Session) Close() error {
|
|||
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
|
||||
// contact one Xbee for point-to-point communications. This enables ACK packets
|
||||
// for reliable transmission.
|
||||
|
|
Loading…
Reference in a new issue