gobot.io/x/gobot@v1.16.0/platforms/microbit/led_driver.go (about)

     1  package microbit
     2  
     3  import (
     4  	"gobot.io/x/gobot"
     5  	"gobot.io/x/gobot/platforms/ble"
     6  )
     7  
     8  // LEDDriver is the Gobot driver for the Microbit's LED array
     9  type LEDDriver struct {
    10  	name       string
    11  	connection gobot.Connection
    12  	gobot.Eventer
    13  }
    14  
    15  const (
    16  	// BLE services
    17  	ledService = "e95dd91d251d470aa062fa1922dfa9a8"
    18  
    19  	// BLE characteristics
    20  	ledMatrixStateCharacteristic    = "e95d7b77251d470aa062fa1922dfa9a8"
    21  	ledTextCharacteristic           = "e95d93ee251d470aa062fa1922dfa9a8"
    22  	ledScrollingDelayCharacteristic = "e95d0d2d251d470aa062fa1922dfa9a8"
    23  )
    24  
    25  // NewLEDDriver creates a Microbit LEDDriver
    26  func NewLEDDriver(a ble.BLEConnector) *LEDDriver {
    27  	n := &LEDDriver{
    28  		name:       gobot.DefaultName("Microbit LED"),
    29  		connection: a,
    30  		Eventer:    gobot.NewEventer(),
    31  	}
    32  
    33  	return n
    34  }
    35  
    36  // Connection returns the BLE connection
    37  func (b *LEDDriver) Connection() gobot.Connection { return b.connection }
    38  
    39  // Name returns the Driver Name
    40  func (b *LEDDriver) Name() string { return b.name }
    41  
    42  // SetName sets the Driver Name
    43  func (b *LEDDriver) SetName(n string) { b.name = n }
    44  
    45  // adaptor returns BLE adaptor
    46  func (b *LEDDriver) adaptor() ble.BLEConnector {
    47  	return b.Connection().(ble.BLEConnector)
    48  }
    49  
    50  // Start tells driver to get ready to do work
    51  func (b *LEDDriver) Start() (err error) {
    52  	return
    53  }
    54  
    55  // Halt stops LED driver (void)
    56  func (b *LEDDriver) Halt() (err error) {
    57  	return
    58  }
    59  
    60  // ReadMatrix read the current LED matrix state
    61  func (b *LEDDriver) ReadMatrix() (data []byte, err error) {
    62  	data, err = b.adaptor().ReadCharacteristic(ledMatrixStateCharacteristic)
    63  	return
    64  }
    65  
    66  // WriteMatrix writes an array of 5 bytes to set the LED matrix
    67  func (b *LEDDriver) WriteMatrix(data []byte) (err error) {
    68  	err = b.adaptor().WriteCharacteristic(ledMatrixStateCharacteristic, data)
    69  	return
    70  }
    71  
    72  // WriteText writes a text message to the Microbit LED matrix
    73  func (b *LEDDriver) WriteText(msg string) (err error) {
    74  	err = b.adaptor().WriteCharacteristic(ledTextCharacteristic, []byte(msg))
    75  	return err
    76  }
    77  
    78  func (b *LEDDriver) ReadScrollingDelay() (delay uint16, err error) {
    79  	return
    80  }
    81  
    82  func (b *LEDDriver) WriteScrollingDelay(delay uint16) (err error) {
    83  	buf := []byte{byte(delay)}
    84  	err = b.adaptor().WriteCharacteristic(ledScrollingDelayCharacteristic, buf)
    85  	return
    86  }
    87  
    88  // Blank clears the LEDs on the Microbit
    89  func (b *LEDDriver) Blank() (err error) {
    90  	buf := []byte{0x00, 0x00, 0x00, 0x00, 0x00}
    91  	err = b.WriteMatrix(buf)
    92  	return
    93  }
    94  
    95  // Solid turns on all of the Microbit LEDs
    96  func (b *LEDDriver) Solid() (err error) {
    97  	buf := []byte{0x1F, 0x1F, 0x1F, 0x1F, 0x1F}
    98  	err = b.WriteMatrix(buf)
    99  	return
   100  }
   101  
   102  // UpRightArrow displays an arrow pointing upwards and to the right on the Microbit LEDs
   103  func (b *LEDDriver) UpRightArrow() (err error) {
   104  	buf := []byte{0x0F, 0x03, 0x05, 0x09, 0x10}
   105  	err = b.WriteMatrix(buf)
   106  	return
   107  }
   108  
   109  // UpLeftArrow displays an arrow pointing upwards and to the left on the Microbit LEDs
   110  func (b *LEDDriver) UpLeftArrow() (err error) {
   111  	buf := []byte{0x1E, 0x18, 0x14, 0x12, 0x01}
   112  	err = b.WriteMatrix(buf)
   113  	return
   114  }
   115  
   116  // DownRightArrow displays an arrow pointing down and to the right on the Microbit LEDs
   117  func (b *LEDDriver) DownRightArrow() (err error) {
   118  	buf := []byte{0x10, 0x09, 0x05, 0x03, 0x0F}
   119  	err = b.WriteMatrix(buf)
   120  	return
   121  }
   122  
   123  // DownLeftArrow displays an arrow pointing down and to the left on the Microbit LEDs
   124  func (b *LEDDriver) DownLeftArrow() (err error) {
   125  	buf := []byte{0x01, 0x12, 0x14, 0x18, 0x1E}
   126  	err = b.WriteMatrix(buf)
   127  	return
   128  }
   129  
   130  // Dimond displays a dimond on the Microbit LEDs
   131  func (b *LEDDriver) Dimond() (err error) {
   132  	buf := []byte{0x04, 0x0A, 0x11, 0x0A, 0x04}
   133  	err = b.WriteMatrix(buf)
   134  	return
   135  }
   136  
   137  // Smile displays a smile on the Microbit LEDs
   138  func (b *LEDDriver) Smile() (err error) {
   139  	buf := []byte{0x0A, 0x0A, 0x00, 0x11, 0x0E}
   140  	err = b.WriteMatrix(buf)
   141  	return
   142  }
   143  
   144  // Wink displays a wink on the Microbit LEDs
   145  func (b *LEDDriver) Wink() (err error) {
   146  	buf := []byte{0x08, 0x0B, 0x00, 0x11, 0x0E}
   147  	err = b.WriteMatrix(buf)
   148  	return
   149  }