gobot.io/x/gobot@v1.16.0/platforms/beaglebone/beaglebone_adaptor_test.go (about) 1 package beaglebone 2 3 import ( 4 "errors" 5 "strings" 6 "testing" 7 8 "gobot.io/x/gobot" 9 "gobot.io/x/gobot/drivers/aio" 10 "gobot.io/x/gobot/drivers/gpio" 11 "gobot.io/x/gobot/drivers/i2c" 12 "gobot.io/x/gobot/drivers/spi" 13 "gobot.io/x/gobot/gobottest" 14 "gobot.io/x/gobot/sysfs" 15 ) 16 17 // make sure that this Adaptor fullfills all the required interfaces 18 var _ gobot.Adaptor = (*Adaptor)(nil) 19 var _ gpio.DigitalReader = (*Adaptor)(nil) 20 var _ gpio.DigitalWriter = (*Adaptor)(nil) 21 var _ aio.AnalogReader = (*Adaptor)(nil) 22 var _ gpio.PwmWriter = (*Adaptor)(nil) 23 var _ gpio.ServoWriter = (*Adaptor)(nil) 24 var _ sysfs.DigitalPinnerProvider = (*Adaptor)(nil) 25 var _ sysfs.PWMPinnerProvider = (*Adaptor)(nil) 26 var _ i2c.Connector = (*Adaptor)(nil) 27 var _ spi.Connector = (*Adaptor)(nil) 28 29 func initBBBTestAdaptor() (*Adaptor, error) { 30 a := NewAdaptor() 31 a.findPin = func(pinPath string) (string, error) { 32 switch pinPath { 33 case "/sys/devices/platform/ocp/48304000.epwmss/48304200.pwm/pwm/pwmchip*": 34 return "/sys/devices/platform/ocp/48304000.epwmss/48304200.pwm/pwm/pwmchip4", nil 35 case "/sys/devices/platform/ocp/48302000.epwmss/48302200.pwm/pwm/pwmchip*": 36 return "/sys/devices/platform/ocp/48302000.epwmss/48302200.pwm/pwm/pwmchip2", nil 37 case "/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip*": 38 return "/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0", nil 39 case "/sys/devices/platform/ocp/48300000.epwmss/48300100.ecap/pwm/pwmchip*": 40 return "/sys/devices/platform/ocp/48300000.epwmss/48300100.ecap/pwm/pwmchip0", nil 41 default: 42 return pinPath, nil 43 } 44 } 45 46 err := a.Connect() 47 48 return a, err 49 } 50 51 func TestBeagleboneAdaptor(t *testing.T) { 52 fs := sysfs.NewMockFilesystem([]string{ 53 "/dev/i2c-2", 54 "/sys/devices/platform/bone_capemgr", 55 "/sys/devices/platform/ocp/ocp:P8_07_pinmux/state", 56 "/sys/devices/platform/ocp/ocp:P9_11_pinmux/state", 57 "/sys/devices/platform/ocp/ocp:P9_12_pinmux/state", 58 "/sys/devices/platform/ocp/ocp:P9_22_pinmux/state", 59 "/sys/devices/platform/ocp/ocp:P9_21_pinmux/state", 60 "/sys/class/leds/beaglebone:green:usr1/brightness", 61 "/sys/bus/iio/devices/iio:device0/in_voltage1_raw", 62 "/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/export", 63 "/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/unexport", 64 "/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/pwm0/enable", 65 "/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/pwm0/period", 66 "/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/pwm0/duty_cycle", 67 "/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/pwm0/polarity", 68 "/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/pwm1/enable", 69 "/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/pwm1/period", 70 "/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/pwm1/duty_cycle", 71 "/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/pwm1/polarity", 72 "/sys/class/gpio/export", 73 "/sys/class/gpio/unexport", 74 "/sys/class/gpio/gpio60/value", 75 "/sys/class/gpio/gpio60/direction", 76 "/sys/class/gpio/gpio66/value", 77 "/sys/class/gpio/gpio66/direction", 78 "/sys/class/gpio/gpio10/value", 79 "/sys/class/gpio/gpio10/direction", 80 "/sys/class/gpio/gpio30/value", 81 "/sys/class/gpio/gpio30/direction", 82 }) 83 84 sysfs.SetFilesystem(fs) 85 86 a, _ := initBBBTestAdaptor() 87 88 // PWM 89 gobottest.Assert(t, a.PwmWrite("P9_99", 175), errors.New("Not a valid PWM pin")) 90 a.PwmWrite("P9_21", 175) 91 gobottest.Assert( 92 t, 93 fs.Files["/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/pwm1/period"].Contents, 94 "500000", 95 ) 96 gobottest.Assert( 97 t, 98 fs.Files["/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/pwm1/duty_cycle"].Contents, 99 "343137", 100 ) 101 102 a.ServoWrite("P9_21", 100) 103 gobottest.Assert( 104 t, 105 fs.Files["/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/pwm1/period"].Contents, 106 "500000", 107 ) 108 gobottest.Assert( 109 t, 110 fs.Files["/sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/pwmchip0/pwm1/duty_cycle"].Contents, 111 "66666", 112 ) 113 gobottest.Assert(t, a.ServoWrite("P9_99", 175), errors.New("Not a valid PWM pin")) 114 115 fs.WithReadError = true 116 gobottest.Assert(t, a.PwmWrite("P9_21", 175), errors.New("read error")) 117 fs.WithReadError = false 118 119 fs.WithWriteError = true 120 gobottest.Assert(t, a.PwmWrite("P9_22", 175), errors.New("write error")) 121 fs.WithWriteError = false 122 123 // Analog 124 fs.Files["/sys/bus/iio/devices/iio:device0/in_voltage1_raw"].Contents = "567\n" 125 i, err := a.AnalogRead("P9_40") 126 gobottest.Assert(t, i, 567) 127 gobottest.Assert(t, err, nil) 128 129 _, err = a.AnalogRead("P9_99") 130 gobottest.Assert(t, err, errors.New("Not a valid analog pin")) 131 132 fs.WithReadError = true 133 _, err = a.AnalogRead("P9_40") 134 gobottest.Assert(t, err, errors.New("read error")) 135 fs.WithReadError = false 136 137 // DigitalIO 138 a.DigitalWrite("usr1", 1) 139 gobottest.Assert(t, 140 fs.Files["/sys/class/leds/beaglebone:green:usr1/brightness"].Contents, 141 "1", 142 ) 143 144 // no such LED 145 err = a.DigitalWrite("usr10101", 1) 146 gobottest.Refute(t, err, nil) 147 148 a.DigitalWrite("P9_12", 1) 149 gobottest.Assert(t, fs.Files["/sys/class/gpio/gpio60/value"].Contents, "1") 150 151 gobottest.Assert(t, a.DigitalWrite("P9_99", 1), errors.New("Not a valid pin")) 152 153 _, err = a.DigitalRead("P9_99") 154 gobottest.Assert(t, err, errors.New("Not a valid pin")) 155 156 fs.Files["/sys/class/gpio/gpio66/value"].Contents = "1" 157 i, err = a.DigitalRead("P8_07") 158 gobottest.Assert(t, i, 1) 159 gobottest.Assert(t, err, nil) 160 161 fs.WithReadError = true 162 _, err = a.DigitalRead("P8_07") 163 gobottest.Assert(t, err, errors.New("read error")) 164 fs.WithReadError = false 165 166 fs.WithWriteError = true 167 _, err = a.DigitalRead("P9_11") 168 gobottest.Assert(t, err, errors.New("write error")) 169 fs.WithWriteError = false 170 171 // I2c 172 sysfs.SetSyscall(&sysfs.MockSyscall{}) 173 174 con, err := a.GetConnection(0xff, 2) 175 gobottest.Assert(t, err, nil) 176 177 con.Write([]byte{0x00, 0x01}) 178 data := []byte{42, 42} 179 con.Read(data) 180 gobottest.Assert(t, data, []byte{0x00, 0x01}) 181 182 gobottest.Assert(t, a.Finalize(), nil) 183 } 184 185 func TestBeagleboneAdaptorName(t *testing.T) { 186 a := NewAdaptor() 187 gobottest.Assert(t, strings.HasPrefix(a.Name(), "Beaglebone"), true) 188 a.SetName("NewName") 189 gobottest.Assert(t, a.Name(), "NewName") 190 } 191 192 func TestBeagleboneDefaultBus(t *testing.T) { 193 a := NewAdaptor() 194 gobottest.Assert(t, a.GetDefaultBus(), 2) 195 } 196 197 func TestBeagleboneGetConnectionInvalidBus(t *testing.T) { 198 a := NewAdaptor() 199 _, err := a.GetConnection(0x01, 99) 200 gobottest.Assert(t, err, errors.New("Bus number 99 out of range")) 201 } 202 203 func TestBeagleboneAnalogReadFileError(t *testing.T) { 204 fs := sysfs.NewMockFilesystem([]string{ 205 "/sys/devices/platform/whatever", 206 }) 207 sysfs.SetFilesystem(fs) 208 209 a, _ := initBBBTestAdaptor() 210 211 _, err := a.AnalogRead("P9_40") 212 gobottest.Assert(t, strings.Contains(err.Error(), "/sys/bus/iio/devices/iio:device0/in_voltage1_raw: No such file."), true) 213 } 214 215 func TestBeagleboneDigitalPinDirectionFileError(t *testing.T) { 216 fs := sysfs.NewMockFilesystem([]string{ 217 "/sys/class/gpio/export", 218 "/sys/class/gpio/gpio60/value", 219 "/sys/devices/platform/ocp/ocp:P9_12_pinmux/state", 220 }) 221 sysfs.SetFilesystem(fs) 222 223 a, _ := initBBBTestAdaptor() 224 225 err := a.DigitalWrite("P9_12", 1) 226 gobottest.Assert(t, strings.Contains(err.Error(), "/sys/class/gpio/gpio60/direction: No such file."), true) 227 228 err = a.Finalize() 229 gobottest.Assert(t, strings.Contains(err.Error(), "/sys/class/gpio/unexport: No such file."), true) 230 } 231 232 func TestBeagleboneDigitalPinFinalizeFileError(t *testing.T) { 233 fs := sysfs.NewMockFilesystem([]string{ 234 "/sys/class/gpio/export", 235 "/sys/class/gpio/gpio60/value", 236 "/sys/class/gpio/gpio60/direction", 237 "/sys/devices/platform/ocp/ocp:P9_12_pinmux/state", 238 }) 239 sysfs.SetFilesystem(fs) 240 241 a, _ := initBBBTestAdaptor() 242 243 err := a.DigitalWrite("P9_12", 1) 244 gobottest.Assert(t, err, nil) 245 246 err = a.Finalize() 247 gobottest.Assert(t, strings.Contains(err.Error(), "/sys/class/gpio/unexport: No such file."), true) 248 } 249 250 func TestPocketBeagleAdaptorName(t *testing.T) { 251 a := NewPocketBeagleAdaptor() 252 gobottest.Assert(t, strings.HasPrefix(a.Name(), "PocketBeagle"), true) 253 }