Skip to content
Merged
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
15 changes: 1 addition & 14 deletions cmd/gotoMemory.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,7 @@ var gotoMemoryCmd = &cobra.Command{
opCtx, cancel := context.WithTimeout(ctx, 60*time.Second)
defer cancel()

var err error
switch memoryNum {
case 1:
err = j.GoToMemory1(opCtx)
case 2:
err = j.GoToMemory2(opCtx)
case 3:
err = j.GoToMemory3(opCtx)
default:
// We should never reach this state as we validate this argument with PreRun hook.
fmt.Fprintf(os.Stderr, "Memory %d is not within boundaries (1-3)\n", memoryNum)
os.Exit(1)
}
if err != nil {
if err := j.GoToMemory(opCtx, memoryNum); err != nil {
fmt.Fprintf(os.Stderr, "Failed to go to memory %d: %v\n", memoryNum, err)
os.Exit(1)
}
Expand Down
4 changes: 2 additions & 2 deletions hack/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ func onScan(adapter *bluetooth.Adapter, device bluetooth.ScanResult) {
defer cancel()

// Go to Memory1
if err := j.GoToMemory1(ctx); err != nil {
if err := j.GoToMemory(ctx, 1); err != nil {
log.Printf("Failed to go to memory1: %v\n", err)
return
}
time.Sleep(5 * time.Second)
// Go to Memory2
if err := j.GoToMemory2(ctx); err != nil {
if err := j.GoToMemory(ctx, 2); err != nil {
log.Printf("Failed to go to memory2: %v\n", err)
return
}
Expand Down
135 changes: 47 additions & 88 deletions pkg/jiecang/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,19 @@ import (
"time"
)

// GoToMemoryX functions
func (j *Jiecang) GoToMemory1(ctx context.Context) error {
// Send the command twice the first time
if err := j.sendCommand(commands["goto_memory1"]); err != nil {
return fmt.Errorf("failed to send goto memory1 command: %w", err)
// GoToMemory moves the desk to the specified memory preset (1-3).
// The operation can be cancelled via the provided context.
func (j *Jiecang) GoToMemory(ctx context.Context, memoryNum int) error {
if memoryNum < 1 || memoryNum > 3 {
return fmt.Errorf("invalid memory number %d (must be 1-3)", memoryNum)
}

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

for {
j.mu.RLock()
currentHeight := j.currentHeight
targetHeight := j.presets["memory1"]
j.mu.RUnlock()

if currentHeight == targetHeight {
break
}

select {
case <-ctx.Done():
// Context cancelled, return
fmt.Printf("Operation cancelled at height %d cm\n", currentHeight)
return nil
case <-ticker.C:
if err := j.sendCommand(commands["goto_memory1"]); err != nil {
return fmt.Errorf("failed to send goto memory1 command: %w", err)
}
}
}
return nil
}
commandKey := fmt.Sprintf("goto_memory%d", memoryNum)
memoryKey := fmt.Sprintf("memory%d", memoryNum)

func (j *Jiecang) GoToMemory2(ctx context.Context) error {
// Send the command twice the first time
if err := j.sendCommand(commands["goto_memory2"]); err != nil {
return fmt.Errorf("failed to send goto memory2 command: %w", err)
if err := j.sendCommand(commands[commandKey]); err != nil {
return fmt.Errorf("failed to send goto memory%d command: %w", memoryNum, err)
}

ticker := time.NewTicker(200 * time.Millisecond)
Expand All @@ -54,7 +29,7 @@ func (j *Jiecang) GoToMemory2(ctx context.Context) error {
for {
j.mu.RLock()
currentHeight := j.currentHeight
targetHeight := j.presets["memory2"]
targetHeight := j.presets[memoryKey]
j.mu.RUnlock()

if currentHeight == targetHeight {
Expand All @@ -67,80 +42,64 @@ func (j *Jiecang) GoToMemory2(ctx context.Context) error {
fmt.Printf("Operation cancelled at height %d cm\n", currentHeight)
return nil
case <-ticker.C:
if err := j.sendCommand(commands["goto_memory2"]); err != nil {
return fmt.Errorf("failed to send goto memory2 command: %w", err)
if err := j.sendCommand(commands[commandKey]); err != nil {
return fmt.Errorf("failed to send goto memory%d command: %w", memoryNum, err)
}
}
}
return nil
}

func (j *Jiecang) GoToMemory3(ctx context.Context) error {
// Send the command twice the first time
if err := j.sendCommand(commands["goto_memory3"]); err != nil {
return fmt.Errorf("failed to send goto memory3 command: %w", err)
}

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

for {
j.mu.RLock()
currentHeight := j.currentHeight
targetHeight := j.presets["memory3"]
j.mu.RUnlock()
// GoToMemory1 moves the desk to memory preset 1.
// Deprecated: Use GoToMemory(ctx, 1) instead.
func (j *Jiecang) GoToMemory1(ctx context.Context) error {
return j.GoToMemory(ctx, 1)
}

if currentHeight == targetHeight {
break
}
// GoToMemory2 moves the desk to memory preset 2.
// Deprecated: Use GoToMemory(ctx, 2) instead.
func (j *Jiecang) GoToMemory2(ctx context.Context) error {
return j.GoToMemory(ctx, 2)
}

select {
case <-ctx.Done():
// Context cancelled, return
fmt.Printf("Operation cancelled at height %d cm\n", currentHeight)
return nil
case <-ticker.C:
if err := j.sendCommand(commands["goto_memory3"]); err != nil {
return fmt.Errorf("failed to send goto memory3 command: %w", err)
}
}
}
return nil
// GoToMemory3 moves the desk to memory preset 3.
// Deprecated: Use GoToMemory(ctx, 3) instead.
func (j *Jiecang) GoToMemory3(ctx context.Context) error {
return j.GoToMemory(ctx, 3)
}

// Save memory commands
// SaveMemory saves the current height to the specified memory preset (1-3).
func (j *Jiecang) SaveMemory(memoryNum int) error {
if memoryNum < 1 || memoryNum > 3 {
return fmt.Errorf("invalid memory number %d (must be 1-3)", memoryNum)
}

func (j *Jiecang) SaveMemory1() error {
//Save memory
if err := j.sendCommand(commands["save_memory1"]); err != nil {
return fmt.Errorf("failed to save memory1: %w", err)
commandKey := fmt.Sprintf("save_memory%d", memoryNum)
if err := j.sendCommand(commands[commandKey]); err != nil {
return fmt.Errorf("failed to save memory%d: %w", memoryNum, err)
}

log.Printf("Height: %d cm", j.currentHeight)
log.Printf("Saved height %d cm to memory %d", j.currentHeight, memoryNum)
time.Sleep(200 * time.Millisecond)
return nil
}

func (j *Jiecang) SaveMemory2() error {
//Save memory
if err := j.sendCommand(commands["save_memory2"]); err != nil {
return fmt.Errorf("failed to save memory2: %w", err)
}
// SaveMemory1 saves the current height to memory preset 1.
// Deprecated: Use SaveMemory(1) instead.
func (j *Jiecang) SaveMemory1() error {
return j.SaveMemory(1)
}

log.Printf("Height: %d cm", j.currentHeight)
time.Sleep(200 * time.Millisecond)
return nil
// SaveMemory2 saves the current height to memory preset 2.
// Deprecated: Use SaveMemory(2) instead.
func (j *Jiecang) SaveMemory2() error {
return j.SaveMemory(2)
}

// SaveMemory3 saves the current height to memory preset 3.
// Deprecated: Use SaveMemory(3) instead.
func (j *Jiecang) SaveMemory3() error {
//Save memory
if err := j.sendCommand(commands["save_memory3"]); err != nil {
return fmt.Errorf("failed to save memory3: %w", err)
}

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

// Reads the response of the controller containing height settings of
Expand Down
Loading