gobot.io/x/gobot@v1.16.0/drivers/gpio/easy_driver_test.go (about)

     1  package gpio
     2  
     3  import (
     4  	"gobot.io/x/gobot/gobottest"
     5  	"strings"
     6  	"testing"
     7  	"time"
     8  )
     9  
    10  const (
    11  	stepAngle   = 0.5 // use non int step angle to check int math
    12  	stepsPerRev = 720
    13  )
    14  
    15  var adapter *gpioTestAdaptor
    16  
    17  func initEasyDriver() *EasyDriver {
    18  	adapter = newGpioTestAdaptor()
    19  	return NewEasyDriver(adapter, stepAngle, "1", "2", "3", "4")
    20  }
    21  
    22  func TestEasyDriver_Connection(t *testing.T) {
    23  	d := initEasyDriver()
    24  	gobottest.Assert(t, d.Connection(), adapter)
    25  }
    26  
    27  func TestEasyDriverDefaultName(t *testing.T) {
    28  	d := initEasyDriver()
    29  	gobottest.Assert(t, strings.HasPrefix(d.Name(), "EasyDriver"), true)
    30  }
    31  
    32  func TestEasyDriverSetName(t *testing.T) {
    33  	d := initEasyDriver()
    34  	d.SetName("OtherDriver")
    35  	gobottest.Assert(t, strings.HasPrefix(d.Name(), "OtherDriver"), true)
    36  }
    37  
    38  func TestEasyDriverStart(t *testing.T) {
    39  	d := initEasyDriver()
    40  	d.Start()
    41  	// noop - no error occurred
    42  }
    43  
    44  func TestEasyDriverHalt(t *testing.T) {
    45  	d := initEasyDriver()
    46  	d.Run()
    47  	gobottest.Assert(t, d.IsMoving(), true)
    48  	d.Halt()
    49  	gobottest.Assert(t, d.IsMoving(), false)
    50  }
    51  
    52  func TestEasyDriverMove(t *testing.T) {
    53  	d := initEasyDriver()
    54  	d.Move(2)
    55  	time.Sleep(2 * time.Millisecond)
    56  	gobottest.Assert(t, d.GetCurrentStep(), 4)
    57  	gobottest.Assert(t, d.IsMoving(), false)
    58  }
    59  
    60  func TestEasyDriverRun(t *testing.T) {
    61  	d := initEasyDriver()
    62  	d.Run()
    63  	gobottest.Assert(t, d.IsMoving(), true)
    64  	d.Run()
    65  	gobottest.Assert(t, d.IsMoving(), true)
    66  }
    67  
    68  func TestEasyDriverStop(t *testing.T) {
    69  	d := initEasyDriver()
    70  	d.Run()
    71  	gobottest.Assert(t, d.IsMoving(), true)
    72  	d.Stop()
    73  	gobottest.Assert(t, d.IsMoving(), false)
    74  }
    75  
    76  func TestEasyDriverStep(t *testing.T) {
    77  	d := initEasyDriver()
    78  	d.Step()
    79  	gobottest.Assert(t, d.GetCurrentStep(), 1)
    80  	d.Step()
    81  	d.Step()
    82  	d.Step()
    83  	gobottest.Assert(t, d.GetCurrentStep(), 4)
    84  	d.SetDirection("ccw")
    85  	d.Step()
    86  	gobottest.Assert(t, d.GetCurrentStep(), 3)
    87  }
    88  
    89  func TestEasyDriverSetDirection(t *testing.T) {
    90  	d := initEasyDriver()
    91  	gobottest.Assert(t, d.dir, int8(1))
    92  	d.SetDirection("cw")
    93  	gobottest.Assert(t, d.dir, int8(1))
    94  	d.SetDirection("ccw")
    95  	gobottest.Assert(t, d.dir, int8(-1))
    96  	d.SetDirection("nothing")
    97  	gobottest.Assert(t, d.dir, int8(1))
    98  }
    99  
   100  func TestEasyDriverSetDirectionNoPin(t *testing.T) {
   101  	d := initEasyDriver()
   102  	d.dirPin = ""
   103  	err := d.SetDirection("cw")
   104  	gobottest.Refute(t, err, nil)
   105  }
   106  
   107  func TestEasyDriverSetSpeed(t *testing.T) {
   108  	d := initEasyDriver()
   109  	gobottest.Assert(t, d.rpm, uint(stepsPerRev/4)) // default speed of 720/4
   110  	d.SetSpeed(0)
   111  	gobottest.Assert(t, d.rpm, uint(1))
   112  	d.SetSpeed(200)
   113  	gobottest.Assert(t, d.rpm, uint(200))
   114  	d.SetSpeed(1000)
   115  	gobottest.Assert(t, d.rpm, uint(stepsPerRev))
   116  }
   117  
   118  func TestEasyDriverGetMaxSpeed(t *testing.T) {
   119  	d := initEasyDriver()
   120  	gobottest.Assert(t, d.GetMaxSpeed(), uint(stepsPerRev))
   121  }
   122  
   123  func TestEasyDriverSleep(t *testing.T) {
   124  	// let's test basic functionality
   125  	d := initEasyDriver()
   126  	d.Sleep()
   127  	gobottest.Assert(t, d.IsSleeping(), true)
   128  
   129  	// let's make sure it stops first
   130  	d = initEasyDriver()
   131  	d.Run()
   132  	d.Sleep()
   133  	gobottest.Assert(t, d.IsSleeping(), true)
   134  	gobottest.Assert(t, d.IsMoving(), false)
   135  }
   136  
   137  func TestEasyDriverSleepNoPin(t *testing.T) {
   138  	d := initEasyDriver()
   139  	d.sleepPin = ""
   140  	err := d.Sleep()
   141  	gobottest.Refute(t, err, nil)
   142  	err = d.Wake()
   143  	gobottest.Refute(t, err, nil)
   144  }
   145  
   146  func TestEasyDriverWake(t *testing.T) {
   147  	// let's test basic functionality
   148  	d := initEasyDriver()
   149  	d.Sleep()
   150  	gobottest.Assert(t, d.IsSleeping(), true)
   151  	d.Wake()
   152  	gobottest.Assert(t, d.IsSleeping(), false)
   153  }
   154  
   155  func TestEasyDriverEnable(t *testing.T) {
   156  	// let's test basic functionality
   157  	d := initEasyDriver()
   158  	d.Disable()
   159  	gobottest.Assert(t, d.IsEnabled(), false)
   160  	d.Enable()
   161  	gobottest.Assert(t, d.IsEnabled(), true)
   162  }
   163  
   164  func TestEasyDriverEnableNoPin(t *testing.T) {
   165  	d := initEasyDriver()
   166  	d.enPin = ""
   167  	err := d.Disable()
   168  	gobottest.Refute(t, err, nil)
   169  	err = d.Enable()
   170  	gobottest.Refute(t, err, nil)
   171  }
   172  
   173  func TestEasyDriverDisable(t *testing.T) {
   174  	// let's test basic functionality
   175  	d := initEasyDriver()
   176  	d.Disable()
   177  	gobottest.Assert(t, d.IsEnabled(), false)
   178  
   179  	// let's make sure it stops first
   180  	d = initEasyDriver()
   181  	d.Run()
   182  	d.Disable()
   183  	gobottest.Assert(t, d.IsEnabled(), false)
   184  	gobottest.Assert(t, d.IsMoving(), false)
   185  }
   186