Beispiel #1
0
// GetIdentity queries idenitity information from a bricklet
func GetIdentity(t tinkerforge.Tinkerforge, uid uint32) (*BrickletIdentity, error) {

	p, err := tinkerforge.NewPacket(uid, 255, true)
	if err != nil {
		return nil, err
	}

	res, err := t.Send(p)
	if err != nil {
		return nil, err
	}

	i := &BrickletIdentity{}
	displayUID := make([]byte, 8)
	connectedDisplayUID := make([]byte, 8)

	if err = res.Decode(&displayUID, &connectedDisplayUID, &i.Position, &i.HardwareVersion, &i.FirmwareVersion, &i.DeviceIdentifier); err != nil {
		return nil, err
	}

	i.UID = strings.TrimSpace(string(displayUID))
	i.ConnectedUID = strings.TrimSpace(string(connectedDisplayUID))

	return i, nil
}
Beispiel #2
0
// SetRGBValues sets up to 16 color values beginning from 'index' to the values in 'colors'.
func (l *LedStrip) SetRGBValues(index uint16, colors []*Color) error {
	// The rgb data
	r, g, b := [16]byte{}, [16]byte{}, [16]byte{}

	// Trim the slice if necessary
	if len(colors) > 16 {
		colors = colors[:16]
	}

	// Copy the colors into the arrays, apply color mapping
	for i, c := range colors {
		r[i] = c[l.colorMap[0]]
		g[i] = c[l.colorMap[1]]
		b[i] = c[l.colorMap[2]]
	}

	// Create packet
	p, err := tinkerforge.NewPacket(l.uid, 1, false, index, uint8(len(colors)), r, g, b)
	if err != nil {
		return err
	}

	// Send packet
	_, err = l.t.Send(p)
	return err
}
Beispiel #3
0
// GetRGBValues retrieves the currently set RGB values of the LED strip beginning from 'index' and up to 'length' values.
func (l *LedStrip) GetRGBValues(index uint16, length uint8) ([]*Color, error) {
	// Limit the length to 16 (maximum the protocol supports)
	if length > 16 {
		length = 16
	}

	// Create a new tinkerforge packet for function #2
	p, err := tinkerforge.NewPacket(l.uid, 2, true, index, length)
	if err != nil {
		return nil, err
	}

	// Send the packet
	res, err := l.t.Send(p)
	if err != nil {
		return nil, err
	}

	// Decode the values from the answer
	r, g, b := [16]byte{}, [16]byte{}, [16]byte{}
	if err = res.Decode(&r, &g, &b); err != nil {
		return nil, err
	}

	// Transform the single color arrays into color values
	result := make([]*Color, length)
	for i := 0; i < int(length); i++ {
		result[i] = &Color{}
		result[i][l.revColorMap[0]] = r[i]
		result[i][l.revColorMap[1]] = g[i]
		result[i][l.revColorMap[2]] = b[i]
	}

	return result, nil
}
Beispiel #4
0
// SetChipType sets the type of the LEDs control chip.
func (l *LedStrip) SetChipType(chipType ChipType) error {
	// Create a new tinkerforge packet
	p, err := tinkerforge.NewPacket(l.uid, 9, false, chipType)
	if err != nil {
		return err
	}

	// Send the packet
	_, err = l.t.Send(p)
	return err

}
Beispiel #5
0
// SetClockFrequency sets the frequency of the clock in hertz.
// Allowed values range from 10000 (10kHz) to 2000000 (2MHz).
// The bricklet chooses the next possible frequency automatically.
func (l *LedStrip) SetClockFrequency(frequency uint32) error {
	// Create a new tinkerforge packet
	p, err := tinkerforge.NewPacket(l.uid, 7, false, frequency)
	if err != nil {
		return err
	}

	// Send the packet
	_, err = l.t.Send(p)
	return err

}
Beispiel #6
0
// SetFrameDuration sets the number of milliseconds between frames.
func (l *LedStrip) SetFrameDuration(ms uint16) error {
	// Create a new tinkerforge packet for function #3
	p, err := tinkerforge.NewPacket(l.uid, 3, false, ms)
	if err != nil {
		return err
	}

	// Send the packet
	_, err = l.t.Send(p)
	return err

}
Beispiel #7
0
// GetChipType returns the currently set type of the LEDs control chip.
func (l *LedStrip) GetChipType() (ChipType, error) {
	// Create a new tinkerforge packet
	p, err := tinkerforge.NewPacket(l.uid, 10, true)
	if err != nil {
		return 0, err
	}

	// Send the packet
	res, err := l.t.Send(p)
	if err != nil {
		return 0, err
	}

	// Decode the chip type
	var chipType ChipType
	if err = res.Decode(&chipType); err != nil {
		return 0, err
	}

	return chipType, nil
}
Beispiel #8
0
// GetClockFrequency returns the currently used clock frequency.
func (l *LedStrip) GetClockFrequency() (uint32, error) {
	// Create a new tinkerforge packet
	p, err := tinkerforge.NewPacket(l.uid, 8, true)
	if err != nil {
		return 0, err
	}

	// Send the packet
	res, err := l.t.Send(p)
	if err != nil {
		return 0, err
	}

	// Decode the frequency
	var frequency uint32
	if err = res.Decode(&frequency); err != nil {
		return 0, err
	}

	return frequency, nil
}
Beispiel #9
0
// GetSupplyVoltage returns the current voltage the LED strip's LEDs consume in mV.
func (l *LedStrip) GetSupplyVoltage() (uint16, error) {
	// Create a new tinkerforge packet
	p, err := tinkerforge.NewPacket(l.uid, 5, true)
	if err != nil {
		return 0, err
	}

	// Send the packet
	res, err := l.t.Send(p)
	if err != nil {
		return 0, err
	}

	// Decode the voltage
	var voltage uint16
	if err = res.Decode(&voltage); err != nil {
		return 0, err
	}

	return voltage, nil
}
Beispiel #10
0
// GetFrameDuration returns the currently set number of milliseconds between frames.
func (l *LedStrip) GetFrameDuration() (uint16, error) {
	// Create a tinkerforge packet for function #4
	p, err := tinkerforge.NewPacket(l.uid, 4, true)
	if err != nil {
		return 0, err
	}

	// Send the packet
	res, err := l.t.Send(p)
	if err != nil {
		return 0, err
	}

	// Decode the duration
	var duration uint16
	if err = res.Decode(&duration); err != nil {
		return 0, err
	}

	return duration, nil
}