174 lines
5.5 KiB
Go
Raw Permalink Normal View History

2024-10-28 20:10:55 +01:00
package main
import (
2024-11-12 13:57:14 +01:00
"encoding/binary"
2024-11-18 23:59:40 +01:00
"fmt"
2024-10-28 20:10:55 +01:00
"machine"
"time"
"tinygo.org/x/bluetooth"
"tinygo.org/x/drivers/lsm6ds3tr"
)
var adapter = bluetooth.DefaultAdapter
2024-11-13 21:22:29 +01:00
var isBleConnected bool = false
2024-10-28 20:10:55 +01:00
2024-11-13 21:22:29 +01:00
var isCapturing bool = false
2024-11-12 13:57:14 +01:00
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}
2024-11-13 21:22:29 +01:00
unixTimeStampRst = [16]byte{0x75, 0x6E, 0x69, 0x78, 0x54, 0x69, 0x6D, 0x65, 0x53, 0x74, 0x61, 0x6D, 0x70, 0x52, 0x73, 0x74}
capturingService = [16]byte{0x63, 0x61, 0x70, 0x74, 0x75, 0x72, 0x69, 0x6E, 0x67, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65}
//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
)
var currentTimeStamp time.Time = time.Now()
var lastTimeStamp time.Time = time.Now()
2024-11-13 21:22:29 +01:00
const sleepDuration time.Duration = time.Millisecond * 100
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)
}
}
// Configure Bluetooth
must("enable BLE stack", adapter.Enable())
adv := adapter.DefaultAdvertisement()
must("config adv", adv.Configure(bluetooth.AdvertisementOptions{
2024-12-16 15:42:44 +01:00
LocalName: "Go Bluetooth 2",
2024-10-28 20:10:55 +01:00
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 {
2024-11-13 21:22:29 +01:00
isBleConnected = true
adv.Stop()
2024-10-28 20:10:55 +01:00
} else {
2024-11-13 21:22:29 +01:00
isBleConnected = false
isCapturing = false
adv.Start()
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-11-13 21:22:29 +01:00
// can only send a max amount of 20 bytes in one packet
//Value: []byte{},
Flags: bluetooth.CharacteristicNotifyPermission | bluetooth.CharacteristicReadPermission | bluetooth.CharacteristicWritePermission | bluetooth.CharacteristicWriteWithoutResponsePermission,
},
{
UUID: bluetooth.NewUUID(unixTimeStampRst),
Flags: bluetooth.CharacteristicNotifyPermission | bluetooth.CharacteristicReadPermission | bluetooth.CharacteristicWritePermission | bluetooth.CharacteristicWriteWithoutResponsePermission,
WriteEvent: func(client bluetooth.Connection, offset int, value []byte) {
if len(value) != 8 {
return
}
millisFromEpoch := binary.BigEndian.Uint64(value)
currentTimeStamp = time.Unix(0, int64(millisFromEpoch)*int64(time.Millisecond))
2024-11-13 21:22:29 +01:00
},
},
{
UUID: bluetooth.NewUUID(capturingService),
Flags: bluetooth.CharacteristicNotifyPermission | bluetooth.CharacteristicReadPermission | bluetooth.CharacteristicWritePermission | bluetooth.CharacteristicWriteWithoutResponsePermission,
WriteEvent: func(client bluetooth.Connection, offset int, value []byte) {
if len(value) != 1 {
return
}
if value[0] == 1 {
isCapturing = true
} else {
isCapturing = false
}
},
2024-10-28 20:10:55 +01:00
},
},
}))
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
// Only read and update sensor data
// with an active bluetooth connection
2024-11-13 21:22:29 +01:00
// and when `isCapturing` is set to true
2024-11-13 21:59:59 +01:00
if isBleConnected && isCapturing {
2024-11-12 13:57:14 +01:00
X, Y, Z, _ := accel.ReadRotation()
x, y, z, _ := accel.ReadAcceleration()
2024-11-13 21:22:29 +01:00
arrRot := valuesToByteArray(X, Y, Z, int8(1))
arrAcc := valuesToByteArray(x, y, z, int8(2))
2024-11-12 13:57:14 +01:00
currentTimeStamp = currentTimeStamp.Add(time.Now().Sub(lastTimeStamp))
2024-11-18 23:59:40 +01:00
fmt.Println("TIME: ", time.Now().Sub(lastTimeStamp))
arrTime := timeStampToByteArray(currentTimeStamp.UnixMilli(), int8(3))
senseCharacteristic.Write(arrRot)
senseCharacteristic.Write(arrAcc)
2024-11-13 21:22:29 +01:00
senseCharacteristic.Write(arrTime)
}
if isCapturing {
lastTimeStamp = time.Now()
2024-11-12 13:57:14 +01:00
}
time.Sleep(sleepDuration)
2024-10-28 20:10:55 +01:00
}
}
2024-11-13 21:22:29 +01:00
func valuesToByteArray(x int32, y int32, z int32, p int8) []byte {
2024-11-18 23:59:40 +01:00
byteSlice := make([]byte, 13)
binary.LittleEndian.PutUint32(byteSlice, uint32(x))
binary.LittleEndian.PutUint32(byteSlice[4:], uint32(y))
binary.LittleEndian.PutUint32(byteSlice[8:], uint32(z))
byteSlice[12] = byte(p)
return byteSlice
2024-11-13 21:22:29 +01:00
}
func timeStampToByteArray(value int64, p int8) []byte {
2024-11-18 23:59:40 +01:00
byteSlice := make([]byte, 9)
binary.LittleEndian.PutUint64(byteSlice, uint64(value))
byteSlice[8] = byte(p)
return byteSlice
2024-11-13 21:22:29 +01:00
}
2024-10-28 20:10:55 +01:00
func must(action string, err error) {
if err != nil {
panic("failed to " + action + ": " + err.Error())
}
}