gobot.io/x/gobot@v1.16.0/platforms/chip/chip_adaptor_test.go (about)

     1  package chip
     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 initTestChipAdaptor() (*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/gpio50/value",
    31  		"/sys/class/gpio/gpio50/direction",
    32  		"/sys/class/gpio/gpio139/value",
    33  		"/sys/class/gpio/gpio139/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/duty_cycle",
    38  		"/sys/class/pwm/pwmchip0/pwm0/polarity",
    39  		"/sys/class/pwm/pwmchip0/pwm0/period",
    40  	})
    41  
    42  	sysfs.SetFilesystem(fs)
    43  	return a, fs
    44  }
    45  
    46  func initTestChipProAdaptor() (*Adaptor, *sysfs.MockFilesystem) {
    47  	a := NewProAdaptor()
    48  	fs := sysfs.NewMockFilesystem([]string{
    49  		"/sys/class/gpio/export",
    50  		"/sys/class/gpio/unexport",
    51  		"/sys/class/gpio/gpio50/value",
    52  		"/sys/class/gpio/gpio50/direction",
    53  		"/sys/class/gpio/gpio139/value",
    54  		"/sys/class/gpio/gpio139/direction",
    55  		"/sys/class/pwm/pwmchip0/export",
    56  		"/sys/class/pwm/pwmchip0/unexport",
    57  		"/sys/class/pwm/pwmchip0/pwm0/enable",
    58  		"/sys/class/pwm/pwmchip0/pwm0/duty_cycle",
    59  		"/sys/class/pwm/pwmchip0/pwm0/polarity",
    60  		"/sys/class/pwm/pwmchip0/pwm0/period",
    61  	})
    62  
    63  	sysfs.SetFilesystem(fs)
    64  	return a, fs
    65  }
    66  
    67  func TestChipAdaptorName(t *testing.T) {
    68  	a := NewAdaptor()
    69  	gobottest.Assert(t, strings.HasPrefix(a.Name(), "CHIP"), true)
    70  	a.SetName("NewName")
    71  	gobottest.Assert(t, a.Name(), "NewName")
    72  }
    73  
    74  func TestChipAdaptorBoard(t *testing.T) {
    75  	a := NewAdaptor()
    76  	a.SetBoard("pro")
    77  	gobottest.Assert(t, a.board, "pro")
    78  
    79  	gobottest.Assert(t, a.SetBoard("bad"), errors.New("Invalid board type"))
    80  }
    81  
    82  func TestAdaptorFinalizeErrorAfterGPIO(t *testing.T) {
    83  	a, fs := initTestChipAdaptor()
    84  	gobottest.Assert(t, a.Connect(), nil)
    85  	gobottest.Assert(t, a.DigitalWrite("CSID7", 1), nil)
    86  
    87  	fs.WithWriteError = true
    88  
    89  	err := a.Finalize()
    90  	gobottest.Assert(t, strings.Contains(err.Error(), "write error"), true)
    91  }
    92  
    93  func TestAdaptorFinalizeErrorAfterPWM(t *testing.T) {
    94  	a, fs := initTestChipAdaptor()
    95  	gobottest.Assert(t, a.Connect(), nil)
    96  	gobottest.Assert(t, a.PwmWrite("PWM0", 100), nil)
    97  
    98  	fs.WithWriteError = true
    99  
   100  	err := a.Finalize()
   101  	gobottest.Assert(t, strings.Contains(err.Error(), "write error"), true)
   102  }
   103  
   104  func TestChipAdaptorDigitalIO(t *testing.T) {
   105  	a, fs := initTestChipAdaptor()
   106  	a.Connect()
   107  
   108  	a.DigitalWrite("CSID7", 1)
   109  	gobottest.Assert(t, fs.Files["/sys/class/gpio/gpio139/value"].Contents, "1")
   110  
   111  	fs.Files["/sys/class/gpio/gpio50/value"].Contents = "1"
   112  	i, _ := a.DigitalRead("TWI2-SDA")
   113  	gobottest.Assert(t, i, 1)
   114  
   115  	gobottest.Assert(t, a.DigitalWrite("XIO-P10", 1), errors.New("Not a valid pin"))
   116  	gobottest.Assert(t, a.Finalize(), nil)
   117  }
   118  
   119  func TestChipProAdaptorDigitalIO(t *testing.T) {
   120  	a, fs := initTestChipProAdaptor()
   121  	a.Connect()
   122  
   123  	a.DigitalWrite("CSID7", 1)
   124  	gobottest.Assert(t, fs.Files["/sys/class/gpio/gpio139/value"].Contents, "1")
   125  
   126  	fs.Files["/sys/class/gpio/gpio50/value"].Contents = "1"
   127  	i, _ := a.DigitalRead("TWI2-SDA")
   128  	gobottest.Assert(t, i, 1)
   129  
   130  	gobottest.Assert(t, a.DigitalWrite("XIO-P0", 1), errors.New("Not a valid pin"))
   131  	gobottest.Assert(t, a.Finalize(), nil)
   132  }
   133  
   134  func TestAdaptorDigitalWriteError(t *testing.T) {
   135  	a, fs := initTestChipAdaptor()
   136  	fs.WithWriteError = true
   137  
   138  	err := a.DigitalWrite("CSID7", 1)
   139  	gobottest.Assert(t, err, errors.New("write error"))
   140  }
   141  
   142  func TestAdaptorDigitalReadWriteError(t *testing.T) {
   143  	a, fs := initTestChipAdaptor()
   144  	fs.WithWriteError = true
   145  
   146  	_, err := a.DigitalRead("CSID7")
   147  	gobottest.Assert(t, err, errors.New("write error"))
   148  }
   149  
   150  func TestChipAdaptorI2c(t *testing.T) {
   151  	a := NewAdaptor()
   152  	a.Connect()
   153  
   154  	fs := sysfs.NewMockFilesystem([]string{
   155  		"/dev/i2c-1",
   156  	})
   157  	sysfs.SetFilesystem(fs)
   158  	sysfs.SetSyscall(&sysfs.MockSyscall{})
   159  
   160  	con, err := a.GetConnection(0xff, 1)
   161  	gobottest.Assert(t, err, nil)
   162  
   163  	con.Write([]byte{0x00, 0x01})
   164  	data := []byte{42, 42}
   165  	con.Read(data)
   166  	gobottest.Assert(t, data, []byte{0x00, 0x01})
   167  
   168  	gobottest.Assert(t, a.Finalize(), nil)
   169  }
   170  
   171  func TestChipAdaptorInvalidPWMPin(t *testing.T) {
   172  	a, _ := initTestChipAdaptor()
   173  	a.Connect()
   174  
   175  	err := a.PwmWrite("LCD-D2", 42)
   176  	gobottest.Refute(t, err, nil)
   177  
   178  	err = a.ServoWrite("LCD-D2", 120)
   179  	gobottest.Refute(t, err, nil)
   180  }
   181  
   182  func TestChipAdaptorPWM(t *testing.T) {
   183  	a, fs := initTestChipAdaptor()
   184  	a.Connect()
   185  
   186  	err := a.PwmWrite("PWM0", 100)
   187  	gobottest.Assert(t, err, nil)
   188  
   189  	gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/export"].Contents, "0")
   190  	gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/enable"].Contents, "1")
   191  	gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/duty_cycle"].Contents, "3921568")
   192  	gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/polarity"].Contents, "normal")
   193  
   194  	err = a.ServoWrite("PWM0", 0)
   195  	gobottest.Assert(t, err, nil)
   196  
   197  	gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/duty_cycle"].Contents, "500000")
   198  
   199  	err = a.ServoWrite("PWM0", 180)
   200  	gobottest.Assert(t, err, nil)
   201  
   202  	gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm0/duty_cycle"].Contents, "2000000")
   203  	gobottest.Assert(t, a.Finalize(), nil)
   204  }
   205  
   206  func TestAdaptorPwmWriteError(t *testing.T) {
   207  	a, fs := initTestChipAdaptor()
   208  	fs.WithWriteError = true
   209  
   210  	err := a.PwmWrite("PWM0", 100)
   211  	gobottest.Assert(t, err, errors.New("write error"))
   212  }
   213  
   214  func TestAdaptorPwmReadError(t *testing.T) {
   215  	a, fs := initTestChipAdaptor()
   216  	fs.WithReadError = true
   217  
   218  	err := a.PwmWrite("PWM0", 100)
   219  	gobottest.Assert(t, err, errors.New("read error"))
   220  }
   221  
   222  func TestChipDefaultBus(t *testing.T) {
   223  	a, _ := initTestChipAdaptor()
   224  	gobottest.Assert(t, a.GetDefaultBus(), 1)
   225  }
   226  
   227  func TestChipGetConnectionInvalidBus(t *testing.T) {
   228  	a, _ := initTestChipAdaptor()
   229  	_, err := a.GetConnection(0x01, 99)
   230  	gobottest.Assert(t, err, errors.New("Bus number 99 out of range"))
   231  }