-
Notifications
You must be signed in to change notification settings - Fork 0
User/xavierruttendion/raspidriver #119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6a3c8d7
draft for raspi io driver (untested)
ruttendx 50402c7
initial commit, untested
ruttendx 605de18
fixed issues; set correct pin out
ruttendx 9c43e13
added test app
ruttendx 11228a7
pi test
ruttendx 61221f1
updated pin assignmnent from bcm to periph compatible
ruttendx a65cf36
fix test build
xavierruttendion be79909
new line fix
xavierruttendion 21bf036
go version check
ruttendx 127db26
go version update
ruttendx cfaab61
working version
xavierruttendion fc4c1d3
note on driver permissions added
ruttendx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 @@ | ||
| raspitest |
This file contains hidden or 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,137 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "context" | ||
| "flag" | ||
| "fmt" | ||
| "log" | ||
| "os" | ||
| "strings" | ||
|
|
||
| "go.uber.org/zap" | ||
|
|
||
| "github.com/macformula/hil/iocontrol/raspi" | ||
| ) | ||
|
|
||
| const helpText = "commands: high | low | read | help | exit" | ||
|
|
||
| func main() { | ||
| boardPin := flag.Uint("pin", 0, "Raspberry Pi board pin number (1-40)") | ||
| // one-shot mode (optional) | ||
| oneSet := flag.String("set", "", "one-shot: set pin: high|low") | ||
| oneGet := flag.Bool("get", false, "one-shot: read pin") | ||
| flag.Parse() | ||
|
|
||
| if *boardPin == 0 || *boardPin > 40 { | ||
| log.Fatalf("pin is required and must be between 1 and 40: --pin N (board numbering)") | ||
| } | ||
|
|
||
| logger, err := zap.NewDevelopment() | ||
| if err != nil { | ||
| log.Fatalf("init logger: %v", err) | ||
| } | ||
| defer logger.Sync() | ||
|
|
||
| ctrl := raspi.NewController(logger) | ||
| if err := ctrl.Open(context.Background()); err != nil { | ||
| log.Fatalf("open raspi controller: %v", err) | ||
| } | ||
| defer func() { | ||
| if err := ctrl.Close(); err != nil { | ||
| logger.Warn("failed to close raspi controller", zap.Error(err)) | ||
| } | ||
| }() | ||
|
|
||
| pin := raspi.NewDigitalPin(uint8(*boardPin)) | ||
|
|
||
| // One-shot path | ||
| if *oneSet != "" || *oneGet { | ||
| switch { | ||
| case *oneSet != "": | ||
| switch strings.ToLower(*oneSet) { | ||
| case "high", "1", "on": | ||
| if err := ctrl.SetDigital(pin, true); err != nil { | ||
| log.Fatalf("set high: %v", err) | ||
| } | ||
| fmt.Printf("P1-%d -> HIGH\n", *boardPin) | ||
| case "low", "0", "off": | ||
| if err := ctrl.SetDigital(pin, false); err != nil { | ||
| log.Fatalf("set low: %v", err) | ||
| } | ||
| fmt.Printf("P1-%d -> LOW\n", *boardPin) | ||
| default: | ||
| log.Fatalf("unknown --set value: %q (use high|low)", *oneSet) | ||
| } | ||
| case *oneGet: | ||
| level, err := ctrl.ReadDigital(pin) | ||
| if err != nil { | ||
| log.Fatalf("read: %v", err) | ||
| } | ||
| if level { | ||
| fmt.Printf("P1-%d <- HIGH\n", *boardPin) | ||
| } else { | ||
| fmt.Printf("P1-%d <- LOW\n", *boardPin) | ||
| } | ||
| } | ||
| return | ||
| } | ||
|
|
||
| // Interactive path: read from the controlling TTY | ||
| tty, err := os.OpenFile("/dev/tty", os.O_RDWR, 0) | ||
| if err != nil { | ||
| log.Fatalf("open /dev/tty: %v", err) | ||
| } | ||
| defer tty.Close() | ||
|
|
||
| fmt.Printf("raspitest ready on pin P1-%d\n%s\n", *boardPin, helpText) | ||
|
|
||
| reader := bufio.NewScanner(tty) | ||
| for { | ||
| fmt.Fprintf(tty, "P1-%d> ", *boardPin) | ||
| if !reader.Scan() { | ||
| break | ||
| } | ||
| cmd := strings.ToLower(strings.TrimSpace(reader.Text())) | ||
| if cmd == "" { | ||
| continue | ||
| } | ||
|
|
||
| switch cmd { | ||
| case "high", "1", "on": | ||
| if err := ctrl.SetDigital(pin, true); err != nil { | ||
| fmt.Fprintf(tty, "error setting HIGH: %v\n", err) | ||
| continue | ||
| } | ||
| fmt.Fprintln(tty, "-> HIGH") | ||
| case "low", "0", "off": | ||
| if err := ctrl.SetDigital(pin, false); err != nil { | ||
| fmt.Fprintf(tty, "error setting LOW: %v\n", err) | ||
| continue | ||
| } | ||
| fmt.Fprintln(tty, "-> LOW") | ||
| case "read", "get": | ||
| level, err := ctrl.ReadDigital(pin) | ||
| if err != nil { | ||
| fmt.Fprintf(tty, "error reading pin: %v\n", err) | ||
| continue | ||
| } | ||
| if level { | ||
| fmt.Fprintln(tty, "<- HIGH") | ||
| } else { | ||
| fmt.Fprintln(tty, "<- LOW") | ||
| } | ||
| case "help", "?": | ||
| fmt.Fprintln(tty, helpText) | ||
| case "exit", "quit": | ||
| fmt.Fprintln(tty, "bye") | ||
| return | ||
| default: | ||
| fmt.Fprintf(tty, "unknown command %q (%s)\n", cmd, helpText) | ||
| } | ||
| } | ||
|
|
||
| if err := reader.Err(); err != nil { | ||
| log.Fatalf("tty read error: %v", err) | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.