forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c8335aa
commit f6bcb17
Showing
11 changed files
with
1,061 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// +build example | ||
// | ||
// Do not build by default. | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"gobot.io/x/gobot" | ||
"gobot.io/x/gobot/drivers/gpio" | ||
"gobot.io/x/gobot/platforms/adaptors" | ||
"gobot.io/x/gobot/platforms/nanopi" | ||
) | ||
|
||
// Wiring | ||
// PWR NanoPi: 1, 17 (+3.3V, VCC); 2, 4 (+5V, VDD); 6, 9, 14, 20 (GND) | ||
// GPIO NanoPi: header pin 22 is input, pin 23 is normal output, pin 24 is inverted output | ||
// Button: the input pin is wired with a button to GND, the internal pull up resistor is used | ||
// LED's: the output pins are wired to the cathode of a LED, the anode is wired with a resistor (70-130Ohm for 20mA) to VCC | ||
// Expected behavior: always one LED is on, the other in opposite state, if button is pressed for >2 seconds the state changes | ||
func main() { | ||
const ( | ||
inPinNum = "22" // 7, 8, 10, 11, 12, 13, 15, 16, 18, 22 | ||
outPinNum = "23" | ||
outPinInvertedNum = "24" | ||
debounceTime = 2 * time.Second | ||
) | ||
// note: WithGpiosOpenDrain() is optional, if using WithGpiosOpenSource() the LED's will not light up | ||
board := nanopi.NewNeoAdaptor(adaptors.WithGpiosActiveLow(outPinInvertedNum), | ||
adaptors.WithGpiosOpenDrain(outPinNum, outPinInvertedNum), | ||
adaptors.WithGpiosPullUp(inPinNum), | ||
adaptors.WithGpioDebounce(inPinNum, debounceTime)) | ||
|
||
inPin := gpio.NewDirectPinDriver(board, inPinNum) | ||
outPin := gpio.NewDirectPinDriver(board, outPinNum) | ||
outPinInverted := gpio.NewDirectPinDriver(board, outPinInvertedNum) | ||
|
||
work := func() { | ||
level := byte(1) | ||
|
||
gobot.Every(500*time.Millisecond, func() { | ||
read, err := inPin.DigitalRead() | ||
fmt.Printf("pin %s state is %d\n", inPinNum, read) | ||
if err != nil { | ||
fmt.Println(err) | ||
} else { | ||
level = byte(read) | ||
} | ||
|
||
err = outPin.DigitalWrite(level) | ||
fmt.Printf("pin %s is now %d\n", outPinNum, level) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
|
||
err = outPinInverted.DigitalWrite(level) | ||
fmt.Printf("pin %s is now not %d\n", outPinInvertedNum, level) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
|
||
if level == 1 { | ||
level = 0 | ||
} else { | ||
level = 1 | ||
} | ||
}) | ||
} | ||
|
||
robot := gobot.NewRobot("pinBot", | ||
[]gobot.Connection{board}, | ||
[]gobot.Device{inPin, outPin, outPinInverted}, | ||
work, | ||
) | ||
|
||
robot.Start() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// +build example | ||
// | ||
// Do not build by default. | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"gobot.io/x/gobot" | ||
"gobot.io/x/gobot/platforms/nanopi" | ||
"gobot.io/x/gobot/system" | ||
) | ||
|
||
const ( | ||
inPinNum = "22" // 7, 8, 10, 11, 12, 13, 15, 16, 18, 22 | ||
outPinNum = "23" | ||
outPinInvertedNum = "24" | ||
debounceTime = 2 * time.Second | ||
) | ||
|
||
var ( | ||
outPin gobot.DigitalPinner | ||
outPinInverted gobot.DigitalPinner | ||
) | ||
|
||
// Wiring | ||
// PWR NanoPi: 1, 17 (+3.3V, VCC); 2, 4 (+5V, VDD); 6, 9, 14, 20 (GND) | ||
// GPIO NanoPi: header pin 22 is input, pin 23 is normal output, pin 24 is inverted output | ||
// Button: the input pin is wired with a button to GND, the internal pull up resistor is used | ||
// LED's: the output pins are wired to the cathode of a LED, the anode is wired with a resistor (70-130Ohm for 20mA) to VCC | ||
// Expected behavior: always one LED is on, the other in opposite state, if button is pressed for >2 seconds the state changes | ||
func main() { | ||
|
||
board := nanopi.NewNeoAdaptor() | ||
|
||
work := func() { | ||
inPin, err := board.DigitalPin(inPinNum) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
if err := inPin.ApplyOptions(system.WithPinDirectionInput(), system.WithPinPullUp(), | ||
system.WithPinDebounce(debounceTime), system.WithPinEventOnBothEdges(buttonEventHandler)); err != nil { | ||
fmt.Println(err) | ||
} | ||
|
||
// note: WithPinOpenDrain() is optional, if using WithPinOpenSource() the LED's will not light up | ||
outPin, err = board.DigitalPin(outPinNum) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
if err := outPin.ApplyOptions(system.WithPinDirectionOutput(1), system.WithPinOpenDrain()); err != nil { | ||
fmt.Println(err) | ||
} | ||
|
||
outPinInverted, err = board.DigitalPin(outPinInvertedNum) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
if err := outPinInverted.ApplyOptions(system.WithPinActiveLow(), system.WithPinDirectionOutput(1), | ||
system.WithPinOpenDrain()); err != nil { | ||
fmt.Println(err) | ||
} | ||
|
||
fmt.Printf("\nPlease press and hold the button for at least %s\n", debounceTime) | ||
} | ||
|
||
robot := gobot.NewRobot("pinEdgeBot", | ||
[]gobot.Connection{board}, | ||
[]gobot.Device{}, | ||
work, | ||
) | ||
|
||
robot.Start() | ||
} | ||
|
||
func buttonEventHandler(offset int, t time.Duration, et string, sn uint32, lsn uint32) { | ||
fmt.Printf("%s: %s detected on line %d with total sequence %d and line sequence %d\n", t, et, offset, sn, lsn) | ||
level := 1 | ||
|
||
if et == "falling edge" { | ||
level = 0 | ||
} | ||
|
||
if outPin != nil { | ||
err := outPin.Write(level) | ||
fmt.Printf("pin %s is now %d\n", outPinNum, level) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
} | ||
|
||
if outPinInverted != nil { | ||
err := outPinInverted.Write(level) | ||
fmt.Printf("pin %s is now not %d\n", outPinInvertedNum, level) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// +build example | ||
// | ||
// Do not build by default. | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"gobot.io/x/gobot" | ||
"gobot.io/x/gobot/drivers/gpio" | ||
"gobot.io/x/gobot/platforms/nanopi" | ||
) | ||
|
||
// Wiring | ||
// PWR NanoPi: 1, 17 (+3.3V, VCC); 2, 4 (+5V, VDD); 6, 9, 14, 20 (GND) | ||
// GPIO NanoPi: the fourth header pin at inner USB side, count from USB side, is the PWM output | ||
// LED: the PWM output is NOT able to drive a 20mA LED with full brightness, so a custom driver or low current LED is needed | ||
// Expected behavior: the LED fades in and out | ||
func main() { | ||
r := nanopi.NewNeoAdaptor() | ||
led := gpio.NewLedDriver(r, "PWM") | ||
|
||
work := func() { | ||
brightness := uint8(0) | ||
fadeAmount := uint8(15) | ||
|
||
gobot.Every(100*time.Millisecond, func() { | ||
if err := led.Brightness(brightness); err != nil { | ||
fmt.Println(err) | ||
} | ||
brightness = brightness + fadeAmount | ||
if brightness == 0 || brightness == 255 { | ||
fadeAmount = -fadeAmount | ||
} | ||
}) | ||
} | ||
|
||
robot := gobot.NewRobot("pwmBot", | ||
[]gobot.Connection{r}, | ||
[]gobot.Device{led}, | ||
work, | ||
) | ||
|
||
robot.Start() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// +build example | ||
// | ||
// Do not build by default. | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"gobot.io/x/gobot" | ||
"gobot.io/x/gobot/drivers/i2c" | ||
"gobot.io/x/gobot/platforms/nanopi" | ||
) | ||
|
||
// Wiring | ||
// PWR NanoPi: 1, 17 (+3.3V, VCC); 2, 4 (+5V, VDD); 6, 9, 14, 20 (GND) | ||
// I2C0 NanoPi: 3 (SDA), 5 (SCL) | ||
// PCA9533: 8 (VDD, +2.3..5.5V), 4 (VSS, GND), 7 (SDA), 6 (SCL) | ||
// LED pins: 1 (LED0), 2 (LED1), 3 (LED2), 5 (LED3) | ||
// LED's directly driven with pull-up resistors to VDD, e.g. 180 Ohm | ||
// I2C addresses: 0x62 (PCA9533/01), 0x63 (PCA9533/02) | ||
func main() { | ||
board := nanopi.NewNeoAdaptor() | ||
pca := i2c.NewPCA953xDriver(board, i2c.WithAddress(0x63)) | ||
|
||
led := uint8(0) // index of LED | ||
wVal := uint8(1) // start with LED is "off" | ||
rVal := uint8(0) | ||
|
||
work := func() { | ||
// LED 2 with 5 Hz 1:1, LED 3 with 1 Hz 1:10 | ||
initialize(pca, 5, 1) | ||
|
||
gobot.Every(2000*time.Millisecond, func() { | ||
fmt.Printf("set LED%d output to %d", led, wVal) | ||
err := pca.WriteGPIO(led, wVal) | ||
if err != nil { | ||
fmt.Println("errW:", err) | ||
} | ||
|
||
rVal, err = pca.ReadGPIO(led) | ||
if err != nil { | ||
fmt.Println("errR:", err) | ||
} | ||
if rVal == 0 { | ||
fmt.Printf(" - LED%d is ON\n", led) | ||
} else { | ||
fmt.Printf(" - LED%d is OFF\n", led) | ||
} | ||
|
||
led = led + 1 | ||
if led > 1 { | ||
led = 0 | ||
if wVal == 1 { | ||
wVal = 0 | ||
} else { | ||
wVal = 1 | ||
} | ||
} | ||
}) | ||
} | ||
|
||
robot := gobot.NewRobot("ledI2c", | ||
[]gobot.Connection{board}, | ||
[]gobot.Device{pca}, | ||
work, | ||
) | ||
|
||
err := robot.Start() | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
} | ||
|
||
func initialize(pca *i2c.PCA953xDriver, led2FrequHz float32, led3FrequHz float32) { | ||
// prepare PWM0 | ||
err := pca.WriteFrequency(0, led2FrequHz) | ||
if err != nil { | ||
fmt.Println("errWF0:", err) | ||
} | ||
frq, err := pca.ReadFrequency(0) | ||
if err != nil { | ||
fmt.Println("errRF0:", err) | ||
} | ||
fmt.Println("get Frq0:", frq) | ||
|
||
err = pca.WriteDutyCyclePercent(0, 50) | ||
if err != nil { | ||
fmt.Println("errWD0:", err) | ||
} | ||
dc, err := pca.ReadDutyCyclePercent(0) | ||
if err != nil { | ||
fmt.Println("errRD0:", err) | ||
} | ||
fmt.Println("get dc0:", dc) | ||
|
||
// prepare PWM1 | ||
err = pca.WriteFrequency(1, led3FrequHz) | ||
if err != nil { | ||
fmt.Println("errWF1:", err) | ||
} | ||
frq, err = pca.ReadFrequency(1) | ||
if err != nil { | ||
fmt.Println("errRF1:", err) | ||
} | ||
fmt.Println("get Frq1:", frq) | ||
|
||
err = pca.WriteDutyCyclePercent(1, 10) | ||
if err != nil { | ||
fmt.Println("errWD1:", err) | ||
} | ||
dc, err = pca.ReadDutyCyclePercent(1) | ||
if err != nil { | ||
fmt.Println("errRD1:", err) | ||
} | ||
fmt.Println("get dc1:", dc) | ||
|
||
// LED 2 | ||
fmt.Println("set LED: 2 to: pwm0") | ||
err = pca.SetLED(2, i2c.PCA953xModePwm0) | ||
if err != nil { | ||
fmt.Println("errW:", err) | ||
} | ||
fmt.Println("set LED: 3 to: pwm1") | ||
err = pca.SetLED(3, i2c.PCA953xModePwm1) | ||
if err != nil { | ||
fmt.Println("errW:", err) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Copyright (c) 2014-2018 The Hybrid Group | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. |
Oops, something went wrong.