gobot.io/x/gobot/v2@v2.1.0/platforms/digispark/digispark_adaptor.go (about)

     1  package digispark
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strconv"
     7  
     8  	"gobot.io/x/gobot/v2"
     9  	"gobot.io/x/gobot/v2/drivers/i2c"
    10  )
    11  
    12  // ErrConnection is the error resulting of a connection error with the digispark
    13  var ErrConnection = errors.New("connection error")
    14  
    15  // Adaptor is the Gobot Adaptor for the Digispark
    16  type Adaptor struct {
    17  	name       string
    18  	littleWire lw
    19  	servo      bool
    20  	pwm        bool
    21  	i2c        bool
    22  	connect    func(*Adaptor) (err error)
    23  }
    24  
    25  // NewAdaptor returns a new Digispark Adaptor
    26  func NewAdaptor() *Adaptor {
    27  	return &Adaptor{
    28  		name: gobot.DefaultName("Digispark"),
    29  		connect: func(d *Adaptor) (err error) {
    30  			d.littleWire = littleWireConnect()
    31  			if d.littleWire.(*littleWire).lwHandle == nil {
    32  				return ErrConnection
    33  			}
    34  			return
    35  		},
    36  	}
    37  }
    38  
    39  // Name returns the Digispark Adaptors name
    40  func (d *Adaptor) Name() string { return d.name }
    41  
    42  // SetName sets the Digispark Adaptors name
    43  func (d *Adaptor) SetName(n string) { d.name = n }
    44  
    45  // Connect starts a connection to the digispark
    46  func (d *Adaptor) Connect() (err error) {
    47  	err = d.connect(d)
    48  	return
    49  }
    50  
    51  // Finalize implements the Adaptor interface
    52  func (d *Adaptor) Finalize() (err error) { return }
    53  
    54  // DigitalWrite writes a value to the pin. Acceptable values are 1 or 0.
    55  func (d *Adaptor) DigitalWrite(pin string, level byte) (err error) {
    56  	p, err := strconv.Atoi(pin)
    57  
    58  	if err != nil {
    59  		return
    60  	}
    61  
    62  	if err = d.littleWire.pinMode(uint8(p), 0); err != nil {
    63  		return
    64  	}
    65  
    66  	return d.littleWire.digitalWrite(uint8(p), level)
    67  }
    68  
    69  // PwmWrite writes the 0-254 value to the specified pin
    70  func (d *Adaptor) PwmWrite(pin string, value byte) (err error) {
    71  	if !d.pwm {
    72  		if err = d.littleWire.pwmInit(); err != nil {
    73  			return
    74  		}
    75  
    76  		if err = d.littleWire.pwmUpdatePrescaler(1); err != nil {
    77  			return
    78  		}
    79  		d.pwm = true
    80  	}
    81  
    82  	return d.littleWire.pwmUpdateCompare(value, value)
    83  }
    84  
    85  // ServoWrite writes the 0-180 degree val to the specified pin.
    86  func (d *Adaptor) ServoWrite(pin string, angle uint8) (err error) {
    87  	if !d.servo {
    88  		if err = d.littleWire.servoInit(); err != nil {
    89  			return
    90  		}
    91  		d.servo = true
    92  	}
    93  	return d.littleWire.servoUpdateLocation(angle, angle)
    94  }
    95  
    96  // GetI2cConnection returns an i2c connection to a device on a specified bus.
    97  // Only supports bus number 0
    98  func (d *Adaptor) GetI2cConnection(address int, bus int) (connection i2c.Connection, err error) {
    99  	if bus != 0 {
   100  		return nil, fmt.Errorf("Invalid bus number %d, only 0 is supported", bus)
   101  	}
   102  	c := NewDigisparkI2cConnection(d, uint8(address))
   103  	if err := c.Init(); err != nil {
   104  		return nil, err
   105  	}
   106  	return i2c.Connection(c), nil
   107  }
   108  
   109  // DefaultI2cBus returns the default i2c bus for this platform
   110  func (d *Adaptor) DefaultI2cBus() int {
   111  	return 0
   112  }