gobot.io/x/gobot@v1.16.0/platforms/digispark/digispark_adaptor_test.go (about) 1 package digispark 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/gobottest" 11 ) 12 13 var _ gobot.Adaptor = (*Adaptor)(nil) 14 15 var _ gpio.DigitalWriter = (*Adaptor)(nil) 16 var _ gpio.PwmWriter = (*Adaptor)(nil) 17 var _ gpio.ServoWriter = (*Adaptor)(nil) 18 19 type mock struct { 20 locationA uint8 21 locationB uint8 22 pwmChannelA uint8 23 pwmChannelB uint8 24 pwmPrescalerValue uint 25 pin uint8 26 mode uint8 27 state uint8 28 } 29 30 // setup mock for GPIO, PWM and servo tests 31 func (l *mock) digitalWrite(pin uint8, state uint8) error { 32 l.pin = pin 33 l.state = state 34 return l.error() 35 } 36 func (l *mock) pinMode(pin uint8, mode uint8) error { 37 l.pin = pin 38 l.mode = mode 39 return l.error() 40 } 41 42 var pwmInitErrorFunc = func() error { return nil } 43 44 func (l *mock) pwmInit() error { return pwmInitErrorFunc() } 45 func (l *mock) pwmStop() error { return l.error() } 46 func (l *mock) pwmUpdateCompare(channelA uint8, channelB uint8) error { 47 l.pwmChannelA = channelA 48 l.pwmChannelB = channelB 49 return l.error() 50 } 51 func (l *mock) pwmUpdatePrescaler(value uint) error { 52 l.pwmPrescalerValue = value 53 return l.error() 54 } 55 func (l *mock) servoInit() error { return l.error() } 56 func (l *mock) servoUpdateLocation(locationA uint8, locationB uint8) error { 57 l.locationA = locationA 58 l.locationB = locationB 59 return l.error() 60 } 61 62 var errorFunc = func() error { return nil } 63 64 func (l *mock) error() error { return errorFunc() } 65 66 // i2c functions unused in this test scenarios 67 func (l *mock) i2cInit() error { return nil } 68 func (l *mock) i2cStart(address7bit uint8, direction uint8) error { return nil } 69 func (l *mock) i2cWrite(sendBuffer []byte, length int, endWithStop uint8) error { return nil } 70 func (l *mock) i2cRead(readBuffer []byte, length int, endWithStop uint8) error { return nil } 71 func (l *mock) i2cUpdateDelay(duration uint) error { return nil } 72 73 func initTestAdaptor() *Adaptor { 74 a := NewAdaptor() 75 a.connect = func(a *Adaptor) (err error) { return nil } 76 a.littleWire = new(mock) 77 errorFunc = func() error { return nil } 78 pwmInitErrorFunc = func() error { return nil } 79 return a 80 } 81 82 func TestDigisparkAdaptorName(t *testing.T) { 83 a := NewAdaptor() 84 gobottest.Assert(t, strings.HasPrefix(a.Name(), "Digispark"), true) 85 a.SetName("NewName") 86 gobottest.Assert(t, a.Name(), "NewName") 87 } 88 89 func TestDigisparkAdaptorConnect(t *testing.T) { 90 a := initTestAdaptor() 91 gobottest.Assert(t, a.Connect(), nil) 92 } 93 94 func TestDigisparkAdaptorFinalize(t *testing.T) { 95 a := initTestAdaptor() 96 gobottest.Assert(t, a.Finalize(), nil) 97 } 98 99 func TestDigisparkAdaptorDigitalWrite(t *testing.T) { 100 a := initTestAdaptor() 101 err := a.DigitalWrite("0", uint8(1)) 102 gobottest.Assert(t, err, nil) 103 gobottest.Assert(t, a.littleWire.(*mock).pin, uint8(0)) 104 gobottest.Assert(t, a.littleWire.(*mock).state, uint8(1)) 105 106 err = a.DigitalWrite("?", uint8(1)) 107 gobottest.Refute(t, err, nil) 108 109 errorFunc = func() error { return errors.New("pin mode error") } 110 err = a.DigitalWrite("0", uint8(1)) 111 gobottest.Assert(t, err, errors.New("pin mode error")) 112 } 113 114 func TestDigisparkAdaptorServoWrite(t *testing.T) { 115 a := initTestAdaptor() 116 err := a.ServoWrite("2", uint8(80)) 117 gobottest.Assert(t, err, nil) 118 gobottest.Assert(t, a.littleWire.(*mock).locationA, uint8(80)) 119 gobottest.Assert(t, a.littleWire.(*mock).locationB, uint8(80)) 120 121 a = initTestAdaptor() 122 errorFunc = func() error { return errors.New("servo error") } 123 err = a.ServoWrite("2", uint8(80)) 124 gobottest.Assert(t, err, errors.New("servo error")) 125 } 126 127 func TestDigisparkAdaptorPwmWrite(t *testing.T) { 128 a := initTestAdaptor() 129 err := a.PwmWrite("1", uint8(100)) 130 gobottest.Assert(t, err, nil) 131 gobottest.Assert(t, a.littleWire.(*mock).pwmChannelA, uint8(100)) 132 gobottest.Assert(t, a.littleWire.(*mock).pwmChannelB, uint8(100)) 133 134 a = initTestAdaptor() 135 pwmInitErrorFunc = func() error { return errors.New("pwminit error") } 136 err = a.PwmWrite("1", uint8(100)) 137 gobottest.Assert(t, err, errors.New("pwminit error")) 138 139 a = initTestAdaptor() 140 errorFunc = func() error { return errors.New("pwm error") } 141 err = a.PwmWrite("1", uint8(100)) 142 gobottest.Assert(t, err, errors.New("pwm error")) 143 }