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