소스 검색

pipoint: add a driver for the Raspberry Pi servoblaster.

Michael Hope 9 달 전
부모
커밋
58684dc1f6
1개의 변경된 파일43개의 추가작업 그리고 0개의 파일을 삭제
  1. 43 0
      servoblaster.go

+ 43 - 0
servoblaster.go 파일 보기

@@ -0,0 +1,43 @@
1
+// Copyright 2017 Google Inc.
2
+//
3
+// Licensed under the Apache License, Version 2.0 (the "License");
4
+// you may not use this file except in compliance with the License.
5
+// You may obtain a copy of the License at
6
+//
7
+//     http://www.apache.org/licenses/LICENSE-2.0
8
+//
9
+// Unless required by applicable law or agreed to in writing, software
10
+// distributed under the License is distributed on an "AS IS" BASIS,
11
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+// See the License for the specific language governing permissions and
13
+// limitations under the License.
14
+//
15
+
16
+package pipoint
17
+
18
+import (
19
+	"fmt"
20
+	"os"
21
+
22
+	"gobot.io/x/gobot/sysfs"
23
+)
24
+
25
+const (
26
+	path = "/dev/servoblaster"
27
+)
28
+
29
+type ServoBlaster struct {
30
+	Pin int
31
+}
32
+
33
+// SetDuty sets the servo period in ns.
34
+func (s *ServoBlaster) SetDuty(period int) (err error) {
35
+	file, err := sysfs.OpenFile(path, os.O_WRONLY, 0644)
36
+	defer file.Close()
37
+	if err != nil {
38
+		return
39
+	}
40
+
41
+	_, err = file.Write([]byte(fmt.Sprintf("%d=%dus\n", s.Pin, period/1000)))
42
+	return err
43
+}