Browse Source

Cleaned up

master
Tim Schuster 2 years ago
parent
commit
339d02c22d
Signed by: Tim Schuster <mail@timschuster.info> GPG Key ID: F9E27097EFB77F61
2 changed files with 16 additions and 34 deletions
  1. 1
    0
      example/.gitignore
  2. 15
    34
      example/main.go

+ 1
- 0
example/.gitignore View File

@@ -0,0 +1 @@
1
+example

+ 15
- 34
example/main.go View File

@@ -7,11 +7,8 @@ import (
7 7
 	"go.rls.moe/misc/adafruitPCA9685"
8 8
 	"log"
9 9
 	"os"
10
-	"os/signal"
11 10
 	"strconv"
12 11
 	"strings"
13
-	"sync"
14
-	"syscall"
15 12
 )
16 13
 
17 14
 var bufin = bufio.NewReader(os.Stdin)
@@ -33,45 +30,29 @@ func main() {
33 30
 		return
34 31
 	}
35 32
 	fmt.Printf("Current Angle: %d °\n", servo.CurrentAngle())
36
-	exitTrap := make(chan os.Signal, 1)
37
-	servoCtrl := sync.Mutex{}
38
-	signal.Notify(exitTrap, os.Interrupt, syscall.SIGKILL, syscall.SIGINT)
39 33
 
40 34
 	defer func() {
41
-		servoCtrl.Lock()
42 35
 		servo.DisableSignal()
43
-		os.Exit(1)
44
-		os.Stdin.Close()
45 36
 		return
46 37
 	}()
47
-
48
-	go func() {
49
-		defer os.Exit(0)
50
-		for {
51
-			err := func() error {
52
-				servoCtrl.Lock()
53
-				defer servoCtrl.Unlock()
54
-				angle, err := parseKeyboardInput(stepsize, servo)
55
-				if err != nil {
56
-					fmt.Printf("Error: %s", err)
57
-					return err
58
-				}
59
-				err = servo.TurnTo(angle)
60
-				if err != nil {
61
-					fmt.Printf("Error: %s", err)
62
-					return err
63
-				}
64
-				fmt.Printf("Current Angle: %d °\n", servo.CurrentAngle())
65
-				return nil
66
-			}()
38
+	for {
39
+		if err := func() error {
40
+			angle, err := parseKeyboardInput(stepsize, servo)
41
+			if err != nil {
42
+				fmt.Printf("Error: %s", err)
43
+				return err
44
+			}
45
+			err = servo.TurnTo(angle)
67 46
 			if err != nil {
68
-				return
47
+				fmt.Printf("Error: %s", err)
48
+				return err
69 49
 			}
50
+			fmt.Printf("Current Angle: %d °\n", servo.CurrentAngle())
51
+			return nil
52
+		}(); err != nil {
53
+			return
70 54
 		}
71
-	}()
72
-
73
-	<-exitTrap
74
-	return
55
+	}
75 56
 }
76 57
 
77 58
 func parseKeyboardInput(step int16, s *adafruitPCA9685.Servo) (int16, error) {

Loading…
Cancel
Save