-
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.
Signed-off-by: Douglas Schilling Landgraf <[email protected]>
- Loading branch information
Showing
4 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
examples/vendor-sending-real-hardware-signal-to-dbus/golang/.git_ignore
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,2 @@ | ||
go.sum | ||
send_signal |
8 changes: 8 additions & 0 deletions
8
examples/vendor-sending-real-hardware-signal-to-dbus/golang/README.md
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,8 @@ | ||
golang$ go mod init send_signal | ||
golang$ go mod tidy | ||
golang$ go get github.com/godbus/dbus/v5 | ||
golang$ go build -o send_signal main.go | ||
|
||
$ sudo ./send_signal | ||
Hardware signal 'Speed' with value 80.000000 sent to D-Bus. | ||
Signal sent successfully. |
5 changes: 5 additions & 0 deletions
5
examples/vendor-sending-real-hardware-signal-to-dbus/golang/go.mod
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,5 @@ | ||
module send_signal | ||
|
||
go 1.22.7 | ||
|
||
require github.com/godbus/dbus/v5 v5.1.0 |
55 changes: 55 additions & 0 deletions
55
examples/vendor-sending-real-hardware-signal-to-dbus/golang/main.go
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,55 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/godbus/dbus/v5" | ||
"log" | ||
) | ||
|
||
// sendHardwareSignal sends a hardware signal to the D-Bus service. | ||
// | ||
// signalName: The name of the signal (e.g., "Speed"). | ||
// value: The value of the signal (e.g., 80.0 for speed). | ||
func sendHardwareSignal(signalName string, value float64) error { | ||
// Connect to the system bus | ||
conn, err := dbus.SystemBus() | ||
if err != nil { | ||
return fmt.Errorf("failed to connect to the system bus: %v", err) | ||
} | ||
|
||
// Object to call the D-Bus method | ||
obj := conn.Object("com.vss_lib.VehicleSignals", "/com/vss_lib/VehicleSignals") | ||
|
||
// Call the EmitHardwareSignal method with signalName and value | ||
call := obj.Call("com.vss_lib.VehicleSignals.EmitHardwareSignal", 0, signalName, value) | ||
if call.Err != nil { | ||
return fmt.Errorf("failed to send D-Bus message: %v", call.Err) | ||
} | ||
|
||
fmt.Printf("Hardware signal '%s' with value %f sent to D-Bus.\n", signalName, value) | ||
return nil | ||
} | ||
|
||
func main() { | ||
// Example: Sending real-time speed signal from hardware | ||
err := sendHardwareSignal("Speed", 80.0) | ||
if err != nil { | ||
log.Fatalf("Error: Could not send the signal. Details: %v", err) | ||
} else { | ||
fmt.Println("Signal sent successfully.") | ||
} | ||
} |