gobot.io/x/gobot@v1.16.0/platforms/tinkerboard/adaptor_test.go (about)

     1  package tinkerboard
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  	"testing"
     7  
     8  	"gobot.io/x/gobot"
     9  	"gobot.io/x/gobot/drivers/gpio"
    10  	"gobot.io/x/gobot/drivers/i2c"
    11  	"gobot.io/x/gobot/gobottest"
    12  	"gobot.io/x/gobot/sysfs"
    13  )
    14  
    15  // make sure that this Adaptor fullfills all the required interfaces
    16  var _ gobot.Adaptor = (*Adaptor)(nil)
    17  var _ gpio.DigitalReader = (*Adaptor)(nil)
    18  var _ gpio.DigitalWriter = (*Adaptor)(nil)
    19  var _ gpio.PwmWriter = (*Adaptor)(nil)
    20  var _ gpio.ServoWriter = (*Adaptor)(nil)
    21  var _ sysfs.DigitalPinnerProvider = (*Adaptor)(nil)
    22  var _ sysfs.PWMPinnerProvider = (*Adaptor)(nil)
    23  var _ i2c.Connector = (*Adaptor)(nil)
    24  
    25  func initTestTinkerboardAdaptor() (*Adaptor, *sysfs.MockFilesystem) {
    26  	a := NewAdaptor()
    27  	fs := sysfs.NewMockFilesystem([]string{
    28  		"/sys/class/gpio/export",
    29  		"/sys/class/gpio/unexport",
    30  		"/sys/class/gpio/gpio17/value",
    31  		"/sys/class/gpio/gpio17/direction",
    32  		"/sys/class/gpio/gpio160/value",
    33  		"/sys/class/gpio/gpio160/direction",
    34  		"/sys/class/pwm/pwmchip0/export",
    35  		"/sys/class/pwm/pwmchip0/unexport",
    36  		"/sys/class/pwm/pwmchip0/pwm0/enable",
    37  		"/sys/class/pwm/pwmchip0/pwm0/period",
    38  		"/sys/class/pwm/pwmchip0/pwm0/duty_cycle",
    39  		"/sys/class/pwm/pwmchip0/pwm0/polarity",
    40  	})
    41  
    42  	sysfs.SetFilesystem(fs)
    43  	return a, fs
    44  }
    45  
    46  func TestTinkerboardAdaptorName(t *testing.T) {
    47  	a := NewAdaptor()
    48  	gobottest.Assert(t, strings.HasPrefix(a.Name(), "Tinker Board"), true)
    49  	a.SetName("NewName")
    50  	gobottest.Assert(t, a.Name(), "NewName")
    51  }
    52  
    53  func TestTinkerboardAdaptorDigitalIO(t *testing.T) {
    54  	a, fs := initTestTinkerboardAdaptor()
    55  	a.Connect()
    56  
    57  	a.DigitalWrite("7", 1)
    58  	gobottest.Assert(t, fs.Files["/sys/class/gpio/gpio17/value"].Contents, "1")
    59  
    60  	fs.Files["/sys/class/gpio/gpio160/value"].Contents = "1"
    61  	i, _ := a.DigitalRead("10")
    62  	gobottest.Assert(t, i, 1)
    63  
    64  	gobottest.Assert(t, a.DigitalWrite("99", 1), errors.New("Not a valid pin"))
    65  	gobottest.Assert(t, a.Finalize(), nil)
    66  }
    67  
    68  func TestAdaptorDigitalWriteError(t *testing.T) {
    69  	a, fs := initTestTinkerboardAdaptor()
    70  	fs.WithWriteError = true
    71  
    72  	err := a.DigitalWrite("7", 1)
    73  	gobottest.Assert(t, err, errors.New("write error"))
    74  }
    75  
    76  func TestAdaptorDigitalReadWriteError(t *testing.T) {
    77  	a, fs := initTestTinkerboardAdaptor()
    78  	fs.WithWriteError = true
    79  
    80  	_, err := a.DigitalRead("7")
    81  	gobottest.Assert(t, err, errors.New("write error"))
    82  }
    83  
    84  func TestTinkerboardAdaptorI2c(t *testing.T) {
    85  	a := NewAdaptor()
    86  	fs := sysfs.NewMockFilesystem([]string{
    87  		"/dev/i2c-1",
    88  	})
    89  	sysfs.SetFilesystem(fs)
    90  	sysfs.SetSyscall(&sysfs.MockSyscall{})
    91  
    92  	con, err := a.GetConnection(0xff, 1)
    93  	gobottest.Assert(t, err, nil)
    94  
    95  	con.Write([]byte{0x00, 0x01})
    96  	data := []byte{42, 42}
    97  	con.Read(data)
    98  	gobottest.Assert(t, data, []byte{0x00, 0x01})
    99  
   100  	gobottest.Assert(t, a.Finalize(), nil)
   101  }
   102  
   103  func TestTinkerboardAdaptorInvalidPWMPin(t *testing.T) {
   104  	a, _ := initTestTinkerboardAdaptor()
   105  	a.Connect()
   106  
   107  	err := a.PwmWrite("666", 42)
   108  	gobottest.Refute(t, err, nil)
   109  
   110  	err = a.ServoWrite("666", 120)
   111  	gobottest.Refute(t, err, nil)
   112  
   113  	err = a.PwmWrite("3", 42)
   114  	gobottest.Refute(t, err, nil)
   115  
   116  	err = a.ServoWrite("3", 120)
   117  	gobottest.Refute(t, err, nil)
   118  }
   119  
   120  func TestTinkerboardAdaptorPWM(t *testing.T) {
   121  	a, fs := initTestTinkerboardAdaptor()
   122  
   123  	err := a.PwmWrite("33", 100)
   124  	gobottest.Assert(t, err, nil)
   125  
   126  	gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/export"].Contents, "0")
   127  	gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/enable"].Contents, "1")
   128  	gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/duty_cycle"].Contents, "3921568")
   129  	gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/polarity"].Contents, "normal")
   130  
   131  	err = a.ServoWrite("33", 0)
   132  	gobottest.Assert(t, err, nil)
   133  
   134  	gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/duty_cycle"].Contents, "500000")
   135  
   136  	err = a.ServoWrite("33", 180)
   137  	gobottest.Assert(t, err, nil)
   138  
   139  	gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/duty_cycle"].Contents, "2000000")
   140  	gobottest.Assert(t, a.Finalize(), nil)
   141  }
   142  
   143  func TestTinkerboardAdaptorPwmWriteError(t *testing.T) {
   144  	a, fs := initTestTinkerboardAdaptor()
   145  	fs.WithWriteError = true
   146  
   147  	err := a.PwmWrite("33", 100)
   148  	gobottest.Assert(t, err, errors.New("write error"))
   149  }
   150  
   151  func TestTinkerboardAdaptorPwmReadError(t *testing.T) {
   152  	a, fs := initTestTinkerboardAdaptor()
   153  	fs.WithReadError = true
   154  
   155  	err := a.PwmWrite("33", 100)
   156  	gobottest.Assert(t, err, errors.New("read error"))
   157  }
   158  
   159  func TestTinkerboardDefaultBus(t *testing.T) {
   160  	a, _ := initTestTinkerboardAdaptor()
   161  	gobottest.Assert(t, a.GetDefaultBus(), 1)
   162  }
   163  
   164  func TestTinkerboardGetConnectionInvalidBus(t *testing.T) {
   165  	a, _ := initTestTinkerboardAdaptor()
   166  	_, err := a.GetConnection(0x01, 99)
   167  	gobottest.Assert(t, err, errors.New("Bus number 99 out of range"))
   168  }
   169  
   170  func TestTinkerboardFinalizeErrorAfterGPIO(t *testing.T) {
   171  	a, fs := initTestTinkerboardAdaptor()
   172  
   173  	gobottest.Assert(t, a.Connect(), nil)
   174  	gobottest.Assert(t, a.DigitalWrite("7", 1), nil)
   175  
   176  	fs.WithWriteError = true
   177  
   178  	err := a.Finalize()
   179  	gobottest.Assert(t, strings.Contains(err.Error(), "write error"), true)
   180  }
   181  
   182  func TestTinkerboardFinalizeErrorAfterPWM(t *testing.T) {
   183  	a, fs := initTestTinkerboardAdaptor()
   184  
   185  	gobottest.Assert(t, a.Connect(), nil)
   186  	gobottest.Assert(t, a.PwmWrite("33", 1), nil)
   187  
   188  	fs.WithWriteError = true
   189  
   190  	err := a.Finalize()
   191  	gobottest.Assert(t, strings.Contains(err.Error(), "write error"), true)
   192  }