Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/jiecang/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func isValidData(buf []byte) bool {
}

// Check preamble and last byte
if buf[0] != 0xf2 || buf[1] != 0xf2 || buf[len(buf)-1] != 0x7e {
if buf[0] != ProtocolResponse1 || buf[1] != ProtocolResponse2 || buf[len(buf)-1] != ProtocolTerminator {
return false
}

Expand Down
18 changes: 9 additions & 9 deletions pkg/jiecang/height.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ func (j *Jiecang) GoToHeight(ctx context.Context, height uint8) error {
if height > j.HighestHeight || height < j.LowestHeight {
return fmt.Errorf("height %d is out of range (low: %d, high: %d)", height, j.LowestHeight, j.HighestHeight)
}
data0 := byte((int(height) * 10) / 256)
data1 := byte((int(height) * 10) % 256)
data0 := byte((int(height) * HeightConversionFactor) / 256)
data1 := byte((int(height) * HeightConversionFactor) % 256)
command := []byte{
0xf1,
0xf1,
ProtocolPreamble1,
ProtocolPreamble2,
0x1b,
0x02,
data0,
data1,
byte((int(0x1b) + int(0x02) + int(data0) + int(data1)) % 256),
0x7e,
ProtocolTerminator,
}

ticker := time.NewTicker(200 * time.Millisecond)
ticker := time.NewTicker(PollingInterval)
defer ticker.Stop()

for {
Expand Down Expand Up @@ -107,7 +107,7 @@ func readHeight(buf []byte) uint8 {
if buf[3] == 0x03 {
height := int(buf[4])*256 + int(buf[5])
// Hack to round the value
return uint8(math.Round(float64(height / 10.0)))
return uint8(math.Round(float64(height / HeightConversionFactor)))
}
return 0
}
Expand All @@ -117,8 +117,8 @@ func readHeightRange(buf []byte) (uint8, uint8) {
if buf[3] == 0x04 {
highestHeight := int(buf[4])*256 + int(buf[5])
lowestHeight := int(buf[6])*256 + int(buf[7])
return uint8(math.Round(float64(highestHeight / 10.0))),
uint8(math.Round(float64(lowestHeight / 10.0)))
return uint8(math.Round(float64(highestHeight / HeightConversionFactor))),
uint8(math.Round(float64(lowestHeight / HeightConversionFactor)))
}
return 0, 0
}
23 changes: 22 additions & 1 deletion pkg/jiecang/jiecang.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"sync"
"time"

"tinygo.org/x/bluetooth"
)
Expand All @@ -26,9 +27,29 @@ var commands = map[string][]byte{
}

const (
// BLE service and characteristic IDs
BLEDeviceId = 0xFE60
BLECharDataInId = 0xFE61
BLECharDataOutId = 0xFE62

// Protocol constants
ProtocolPreamble1 = 0xf1 // Command message preamble byte 1
ProtocolPreamble2 = 0xf1 // Command message preamble byte 2
ProtocolResponse1 = 0xf2 // Response message preamble byte 1
ProtocolResponse2 = 0xf2 // Response message preamble byte 2
ProtocolTerminator = 0x7e // Message terminator

// Conversion factors
HeightConversionFactor = 10 // Multiply height by 10 for protocol

// Memory preset constants
MemoryPresetModulo = 0x24 // Modulo for memory preset calculation

// Timing constants
PollingInterval = 200 * time.Millisecond // Polling interval for height operations
SaveMemoryDelay = 200 * time.Millisecond // Delay after saving memory preset
OperationTimeout = 60 * time.Second // Default timeout for operations
InitializationDelay = 200 * time.Millisecond // Delay during initialization
)

type Jiecang struct {
Expand Down Expand Up @@ -196,7 +217,7 @@ func (j *Jiecang) characteristicReceiver(buf []byte) {
j.HighestHeight, j.LowestHeight = readHeightRange(msg[i])
j.mu.Unlock()
case 0x25, 0x26, 0x27, 0x28: // Data contains height for each memory preset (1-4). Memory 4 is currently 0
memory := int(msg[i][2] % 0x24)
memory := int(msg[i][2] % MemoryPresetModulo)
memoryName := fmt.Sprintf("memory%d", memory)
j.mu.Lock()
j.presets[memoryName] = readMemoryPreset(msg[i])
Expand Down
14 changes: 7 additions & 7 deletions pkg/jiecang/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (j *Jiecang) GoToMemory1(ctx context.Context) error {
return fmt.Errorf("failed to send goto memory1 command: %w", err)
}

ticker := time.NewTicker(200 * time.Millisecond)
ticker := time.NewTicker(PollingInterval)
defer ticker.Stop()

for {
Expand Down Expand Up @@ -48,7 +48,7 @@ func (j *Jiecang) GoToMemory2(ctx context.Context) error {
return fmt.Errorf("failed to send goto memory2 command: %w", err)
}

ticker := time.NewTicker(200 * time.Millisecond)
ticker := time.NewTicker(PollingInterval)
defer ticker.Stop()

for {
Expand Down Expand Up @@ -81,7 +81,7 @@ func (j *Jiecang) GoToMemory3(ctx context.Context) error {
return fmt.Errorf("failed to send goto memory3 command: %w", err)
}

ticker := time.NewTicker(200 * time.Millisecond)
ticker := time.NewTicker(PollingInterval)
defer ticker.Stop()

for {
Expand Down Expand Up @@ -117,7 +117,7 @@ func (j *Jiecang) SaveMemory1() error {
}

log.Printf("Height: %d cm", j.currentHeight)
time.Sleep(200 * time.Millisecond)
time.Sleep(SaveMemoryDelay)
return nil
}

Expand All @@ -128,7 +128,7 @@ func (j *Jiecang) SaveMemory2() error {
}

log.Printf("Height: %d cm", j.currentHeight)
time.Sleep(200 * time.Millisecond)
time.Sleep(SaveMemoryDelay)
return nil
}

Expand All @@ -139,7 +139,7 @@ func (j *Jiecang) SaveMemory3() error {
}

log.Printf("Height: %d cm", j.currentHeight)
time.Sleep(200 * time.Millisecond)
time.Sleep(SaveMemoryDelay)
return nil
}

Expand All @@ -149,7 +149,7 @@ func readMemoryPreset(buf []byte) uint8 {
if buf[3] == 0x02 {
preset := int(buf[4])*256 + int(buf[5])
// Hack to round the value
return uint8(math.Round(float64(preset / 10.0)))
return uint8(math.Round(float64(preset / HeightConversionFactor)))
}
return 0
}
Loading