2024-10-28 20:10:55 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-11-12 13:57:14 +01:00
|
|
|
"encoding/binary"
|
2024-10-28 20:10:55 +01:00
|
|
|
"fmt"
|
|
|
|
"machine"
|
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"tinygo.org/x/bluetooth"
|
|
|
|
"tinygo.org/x/drivers/lsm6ds3tr"
|
|
|
|
)
|
|
|
|
|
2024-11-12 13:57:14 +01:00
|
|
|
// TinyGo Drivers Docs:
|
|
|
|
// https://github.com/tinygo-org/drivers
|
|
|
|
|
|
|
|
// TinyGo Bluetooth Docs:
|
|
|
|
// https://github.com/tinygo-org/bluetooth
|
|
|
|
|
2024-10-28 20:10:55 +01:00
|
|
|
var adapter = bluetooth.DefaultAdapter
|
|
|
|
|
2024-11-12 13:57:14 +01:00
|
|
|
var ledColor = [3]byte{0x00, 0x00, 0xff}
|
2024-10-28 20:10:55 +01:00
|
|
|
var leds = [3]machine.Pin{machine.LED_RED, machine.LED_GREEN, machine.LED_BLUE}
|
|
|
|
var hasColorChange = true
|
|
|
|
|
|
|
|
var senseAccelerationData string = "-0.000 -0.000 -0.000"
|
|
|
|
|
2024-11-12 13:57:14 +01:00
|
|
|
var senseRotationData string = "-0.000 -0.000 -0.000"
|
|
|
|
|
|
|
|
var bleConnected bool = false
|
|
|
|
|
2024-10-28 20:10:55 +01:00
|
|
|
var (
|
2024-11-12 13:57:14 +01:00
|
|
|
LSM6DS3TRService = [16]byte{0x4C, 0x53, 0x4D, 0x36, 0x44, 0x53, 0x33, 0x54, 0x52, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65}
|
|
|
|
accelerationData = [16]byte{0x61, 0x63, 0x63, 0x65, 0x6C, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6F, 0x6E, 0x44, 0x61, 0x74, 0x61}
|
|
|
|
tempSenseService = [16]byte{0x74, 0x65, 0x6D, 0x70, 0x53, 0x65, 0x6E, 0x73, 0x65, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65}
|
|
|
|
temperatureSense = [16]byte{0x74, 0x65, 0x6D, 0x70, 0x65, 0x72, 0x61, 0x74, 0x75, 0x72, 0x65, 0x53, 0x65, 0x6E, 0x73, 0x65}
|
2024-10-28 20:10:55 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
// Configure LSM6DS3TR
|
|
|
|
machine.I2C0.Configure(machine.I2CConfig{})
|
|
|
|
|
|
|
|
accel := lsm6ds3tr.New(machine.I2C0)
|
|
|
|
err := accel.Configure(lsm6ds3tr.Configuration{})
|
|
|
|
if err != nil {
|
|
|
|
for {
|
|
|
|
println("Failed to configure", err.Error())
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
}
|
2024-11-12 13:57:14 +01:00
|
|
|
//
|
|
|
|
// for _, led := range leds {
|
|
|
|
// led.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
|
|
|
// }
|
2024-10-28 20:10:55 +01:00
|
|
|
|
|
|
|
// Configure Bluetooth
|
|
|
|
must("enable BLE stack", adapter.Enable())
|
|
|
|
adv := adapter.DefaultAdvertisement()
|
|
|
|
must("config adv", adv.Configure(bluetooth.AdvertisementOptions{
|
|
|
|
LocalName: "Go Bluetooth",
|
|
|
|
ManufacturerData: []bluetooth.ManufacturerDataElement{
|
2024-11-12 13:57:14 +01:00
|
|
|
// 0xFFFF: Special Use/Default ID
|
|
|
|
// Bluetooth Company Identifiers:
|
|
|
|
// https://gist.github.com/angorb/f92f76108b98bb0d81c74f60671e9c67
|
2024-10-28 20:10:55 +01:00
|
|
|
{CompanyID: 0xffff, Data: []byte{0x01, 0x02}},
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
|
|
|
|
adapter.SetConnectHandler(func(device bluetooth.Device, connected bool) {
|
|
|
|
if connected {
|
|
|
|
println("connected, not advertising...")
|
2024-11-12 13:57:14 +01:00
|
|
|
// leds[0].Low()
|
|
|
|
// leds[1].High()
|
|
|
|
// ledColor[0] = 0
|
|
|
|
// ledColor[1] = 1
|
|
|
|
// ledColor[2] = 0
|
|
|
|
// hasColorChange = true
|
|
|
|
bleConnected = true
|
2024-10-28 20:10:55 +01:00
|
|
|
} else {
|
|
|
|
println("disconnected, advertising...")
|
2024-11-12 13:57:14 +01:00
|
|
|
// leds[0].High()
|
|
|
|
// leds[1].Low()
|
|
|
|
// ledColor[0] = 0
|
|
|
|
// ledColor[1] = 0
|
|
|
|
// ledColor[2] = 1
|
|
|
|
// hasColorChange = true
|
|
|
|
bleConnected = false
|
2024-10-28 20:10:55 +01:00
|
|
|
}
|
|
|
|
})
|
2024-11-12 13:57:14 +01:00
|
|
|
//
|
|
|
|
// Start Bluetooth advertisment
|
2024-10-28 20:10:55 +01:00
|
|
|
must("start adv", adv.Start())
|
|
|
|
|
|
|
|
var senseCharacteristic bluetooth.Characteristic
|
|
|
|
must("add sense service", adapter.AddService(&bluetooth.Service{
|
2024-11-12 13:57:14 +01:00
|
|
|
UUID: bluetooth.NewUUID(LSM6DS3TRService),
|
2024-10-28 20:10:55 +01:00
|
|
|
Characteristics: []bluetooth.CharacteristicConfig{
|
|
|
|
{
|
|
|
|
Handle: &senseCharacteristic,
|
2024-11-12 13:57:14 +01:00
|
|
|
UUID: bluetooth.NewUUID(accelerationData),
|
2024-10-28 20:10:55 +01:00
|
|
|
Value: []byte(senseAccelerationData),
|
|
|
|
Flags: bluetooth.CharacteristicNotifyPermission | bluetooth.CharacteristicReadPermission | bluetooth.CharacteristicWritePermission | bluetooth.CharacteristicWriteWithoutResponsePermission,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
|
2024-11-12 13:57:14 +01:00
|
|
|
// var tempCharacteristic bluetooth.Characteristic
|
|
|
|
// must("add temperature service", adapter.AddService(&bluetooth.Service{
|
|
|
|
// UUID: bluetooth.NewUUID(tempSenseService),
|
|
|
|
// Characteristics: []bluetooth.CharacteristicConfig{
|
|
|
|
// {
|
|
|
|
// Handle: &tempCharacteristic,
|
|
|
|
// UUID: bluetooth.NewUUID(temperatureSense),
|
|
|
|
// Value: []byte(tempData),
|
|
|
|
// Flags: bluetooth.CharacteristicNotifyPermission | bluetooth.CharacteristicReadPermission,
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// }))
|
|
|
|
|
|
|
|
// Main Loop
|
2024-10-28 20:10:55 +01:00
|
|
|
for {
|
2024-11-12 13:57:14 +01:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
2024-10-28 20:10:55 +01:00
|
|
|
// for !hasColorChange {
|
2024-11-12 13:57:14 +01:00
|
|
|
// for i, led := range leds {
|
|
|
|
// led.Set(ledColor[i] == 0)
|
|
|
|
// }
|
2024-10-28 20:10:55 +01:00
|
|
|
// time.Sleep(10 * time.Millisecond)
|
|
|
|
// }
|
|
|
|
// hasColorChange = false
|
|
|
|
|
2024-11-12 13:57:14 +01:00
|
|
|
// Only read and update sensor data
|
|
|
|
// with an active bluetooth connection
|
|
|
|
if bleConnected {
|
|
|
|
X, Y, Z, _ := accel.ReadRotation()
|
|
|
|
rotX := fmt.Sprintf("rX=%f", float32(X)/100000000)
|
|
|
|
rotY := fmt.Sprintf("rY=%f", float32(Y)/100000000)
|
|
|
|
rotZ := fmt.Sprintf("rZ=%f", float32(Z)/100000000)
|
|
|
|
|
|
|
|
x, y, z, _ := accel.ReadAcceleration()
|
|
|
|
accel := fmt.Sprintf("%.3f,%.3f,%.3f", float32(x)/1000000, float32(y)/1000000, float32(z)/1000000)
|
|
|
|
//senseAccelerationData = dataTest
|
|
|
|
|
|
|
|
//tempTest, _ := accel.ReadTemperature()
|
|
|
|
//tempTestBytes := []byte{}
|
|
|
|
//tempData = fmt.Sprintf("%i", tempTest)
|
|
|
|
|
|
|
|
//senseCharacteristic.Write([]byte(dataRotation))
|
|
|
|
//senseCharacteristic.Write([]byte("-0.0,-1.2,-5.6"))
|
|
|
|
senseCharacteristic.Write([]byte(rotX))
|
|
|
|
senseCharacteristic.Write([]byte(rotY))
|
|
|
|
senseCharacteristic.Write([]byte(rotZ))
|
|
|
|
senseCharacteristic.Write([]byte(accel))
|
|
|
|
|
|
|
|
// bs := make([]byte, 4)
|
|
|
|
// binary.LittleEndian.PutUint32(bs, X)
|
|
|
|
|
|
|
|
//binary.LittleEndian.AppendUint32(tempTestBytes, uint32(tempTest))
|
|
|
|
//tempCharacteristic.Write(tempTestBytes)
|
|
|
|
}
|
2024-10-28 20:10:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func must(action string, err error) {
|
|
|
|
if err != nil {
|
|
|
|
panic("failed to " + action + ": " + err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns an int >= min, < max
|
|
|
|
func randomInt(min, max int) uint8 {
|
|
|
|
return uint8(min + rand.Intn(max-min))
|
|
|
|
}
|