Skip to content

Commit

Permalink
minor fixes and add NO-OP motor for testing purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
fallais committed Apr 2, 2024
1 parent 3e36d75 commit 1c81459
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ coop:
mode: "sun_based"
value: "30m"
door:
openening_duration: "65s"
opening_duration: "65s"
closing_duration: "60s"
motor:
type: l293d
Expand Down
3 changes: 3 additions & 0 deletions internal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/fallais/gocoop/pkg/motor"
"github.com/fallais/gocoop/pkg/motor/bts7960"
"github.com/fallais/gocoop/pkg/motor/l293d"
"github.com/fallais/gocoop/pkg/motor/noop"

auth "github.com/abbot/go-http-auth"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -56,6 +57,8 @@ func Run(cmd *cobra.Command, args []string) {
motor = l293d.NewL293D(viper.GetInt("door.motor.pin_1A"), viper.GetInt("door.motor.pin_1B"), viper.GetInt("door.motor.pin_enable1"))
case "bts7960":
motor = bts7960.NewBTS7960(viper.GetInt("door.motor.forward_PWM"), viper.GetInt("door.motor.backward_PWM"), viper.GetInt("door.motor.forward_enable"), viper.GetInt("door.motor.backward_enable"))
case "noop":
motor = noop.New()
default:
logrus.Fatalln("Motor type does not exist")
}
Expand Down
6 changes: 5 additions & 1 deletion pkg/door/door.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package door

import (
"context"
"fmt"
"time"

"github.com/fallais/gocoop/pkg/motor"
Expand Down Expand Up @@ -46,7 +47,10 @@ func (d *door) Open() error {
defer cancel()

// Run the motor in forward
d.motor.Forward(ctx)
err := d.motor.Forward(ctx)
if err != nil {
return fmt.Errorf("error while opening door: %v", err)
}

logrus.Infoln("Door has been opened")

Expand Down
22 changes: 22 additions & 0 deletions pkg/motor/noop/noop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package noop

import (
"github.com/fallais/gocoop/pkg/motor"
)

//------------------------------------------------------------------------------
// Structure
//------------------------------------------------------------------------------

// Motor driver for BTS7960.
type noop struct {
}

//------------------------------------------------------------------------------
// Factory
//------------------------------------------------------------------------------

// New returns a new NO-OP motor driver.
func New() motor.Motor {
return &noop{}
}
59 changes: 59 additions & 0 deletions pkg/motor/noop/noop_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//go:build linux
// +build linux

package noop

import (
"context"

"github.com/sirupsen/logrus"
)

// Forward turns the motor forward.
func (d *noop) Forward(ctx context.Context) error {
logrus.Infoln("Turn motor forward")

// Enable the motor
logrus.Infoln("Start the motor")

// Wait
until, _ := ctx.Deadline()
logrus.Infoln("Wait until", until)
<-ctx.Done()

// Disable the motor
logrus.Infoln("Stop the motor")
logrus.Infoln("Motor has been stopped")

return nil
}

// Backward turns the motor backward.
func (d *noop) Backward(ctx context.Context) error {
logrus.Infoln("Turn motor backward")

// Enable the motor
logrus.Infoln("Start the motor")

// Wait
until, _ := ctx.Deadline()
logrus.Infoln("Wait until", until)
<-ctx.Done()

// Disable the motor
logrus.Infoln("Stop the motor")
logrus.Infoln("Motor has been stopped")

return nil
}

// Stop the motor.
func (d *noop) Stop() error {
logrus.Infoln("Stopping the motor")

// Set to LOW
logrus.Infoln("Stop the motor")
logrus.Infoln("Motor has been stopped")

return nil
}

0 comments on commit 1c81459

Please sign in to comment.