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.
add advanced digital pin options (pull, bias, drive, debounce, event)
- Loading branch information
1 parent
6c7ecbe
commit 9ce45c0
Showing
18 changed files
with
1,344 additions
and
122 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,78 @@ | ||
// +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/raspi" | ||
) | ||
|
||
// Wiring | ||
// PWR Raspi: 1 (+3.3V, VCC), 2(+5V), 6, 9, 14, 20 (GND) | ||
// GPIO Raspi: header pin 21 (GPIO9) is input, pin 24 (GPIO8) is normal output, pin 26 (GPIO7) 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 = "21" | ||
outPinNum = "24" | ||
outPinInvertedNum = "26" | ||
) | ||
// note: WithGpiosOpenDrain() is optional, if using WithGpiosOpenSource() the LED's will not light up | ||
board := raspi.NewAdaptor(adaptors.WithGpiosActiveLow(outPinInvertedNum), | ||
adaptors.WithGpiosOpenDrain(outPinNum, outPinInvertedNum), | ||
adaptors.WithGpiosPullUp(inPinNum), | ||
adaptors.WithGpioDebounce(inPinNum, 2*time.Second)) | ||
|
||
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,97 @@ | ||
// +build example | ||
// | ||
// Do not build by default. | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"gobot.io/x/gobot" | ||
"gobot.io/x/gobot/platforms/raspi" | ||
"gobot.io/x/gobot/system" | ||
) | ||
|
||
const ( | ||
inPinNum = "21" | ||
outPinNum = "24" | ||
outPinInvertedNum = "26" | ||
debounceTime = 2 * time.Second | ||
) | ||
|
||
var ( | ||
outPin gobot.DigitalPinner | ||
outPinInverted gobot.DigitalPinner | ||
) | ||
|
||
// Wiring | ||
// PWR Raspi: 1 (+3.3V, VCC), 2(+5V), 6, 9, 14, 20 (GND) | ||
// GPIO Raspi: header pin 21 (GPIO9) is input, pin 24 (GPIO8) is normal output, pin 26 (GPIO7) 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 := raspi.NewAdaptor() | ||
|
||
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 | ||
} | ||
|
||
err := outPin.Write(level) | ||
fmt.Printf("pin %s is now %d\n", outPinNum, level) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
|
||
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
Oops, something went wrong.