gobot.io/x/gobot/v2@v2.1.0/drivers/gpio/button_driver.go (about)

     1  package gpio
     2  
     3  import (
     4  	"time"
     5  
     6  	"gobot.io/x/gobot/v2"
     7  )
     8  
     9  // ButtonDriver Represents a digital Button
    10  type ButtonDriver struct {
    11  	Active       bool
    12  	DefaultState int
    13  	pin          string
    14  	name         string
    15  	halt         chan bool
    16  	interval     time.Duration
    17  	connection   DigitalReader
    18  	gobot.Eventer
    19  }
    20  
    21  // NewButtonDriver returns a new ButtonDriver with a polling interval of
    22  // 10 Milliseconds given a DigitalReader and pin.
    23  //
    24  // Optionally accepts:
    25  //
    26  //	time.Duration: Interval at which the ButtonDriver is polled for new information
    27  func NewButtonDriver(a DigitalReader, pin string, v ...time.Duration) *ButtonDriver {
    28  	b := &ButtonDriver{
    29  		name:         gobot.DefaultName("Button"),
    30  		connection:   a,
    31  		pin:          pin,
    32  		Active:       false,
    33  		DefaultState: 0,
    34  		Eventer:      gobot.NewEventer(),
    35  		interval:     10 * time.Millisecond,
    36  		halt:         make(chan bool),
    37  	}
    38  
    39  	if len(v) > 0 {
    40  		b.interval = v[0]
    41  	}
    42  
    43  	b.AddEvent(ButtonPush)
    44  	b.AddEvent(ButtonRelease)
    45  	b.AddEvent(Error)
    46  
    47  	return b
    48  }
    49  
    50  // Start starts the ButtonDriver and polls the state of the button at the given interval.
    51  //
    52  // Emits the Events:
    53  //
    54  //	Push int - On button push
    55  //	Release int - On button release
    56  //	Error error - On button error
    57  func (b *ButtonDriver) Start() (err error) {
    58  	state := b.DefaultState
    59  	go func() {
    60  		for {
    61  			newValue, err := b.connection.DigitalRead(b.Pin())
    62  			if err != nil {
    63  				b.Publish(Error, err)
    64  			} else if newValue != state && newValue != -1 {
    65  				state = newValue
    66  				b.update(newValue)
    67  			}
    68  			select {
    69  			case <-time.After(b.interval):
    70  			case <-b.halt:
    71  				return
    72  			}
    73  		}
    74  	}()
    75  	return
    76  }
    77  
    78  // Halt stops polling the button for new information
    79  func (b *ButtonDriver) Halt() (err error) {
    80  	b.halt <- true
    81  	return
    82  }
    83  
    84  // Name returns the ButtonDrivers name
    85  func (b *ButtonDriver) Name() string { return b.name }
    86  
    87  // SetName sets the ButtonDrivers name
    88  func (b *ButtonDriver) SetName(n string) { b.name = n }
    89  
    90  // Pin returns the ButtonDrivers pin
    91  func (b *ButtonDriver) Pin() string { return b.pin }
    92  
    93  // Connection returns the ButtonDrivers Connection
    94  func (b *ButtonDriver) Connection() gobot.Connection { return b.connection.(gobot.Connection) }
    95  
    96  func (b *ButtonDriver) update(newValue int) {
    97  	if newValue != b.DefaultState {
    98  		b.Active = true
    99  		b.Publish(ButtonPush, newValue)
   100  	} else {
   101  		b.Active = false
   102  		b.Publish(ButtonRelease, newValue)
   103  	}
   104  }