Skip to content

Commit

Permalink
examples: golang
Browse files Browse the repository at this point in the history
Signed-off-by: Douglas Schilling Landgraf <[email protected]>
  • Loading branch information
dougsland committed Sep 25, 2024
1 parent 68ea493 commit a74a929
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go.sum
send_signal
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.
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
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.")
}
}

0 comments on commit a74a929

Please sign in to comment.