tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/is31fl3731/is31fl3731_acw15x7.go (about)

     1  package is31fl3731
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"tinygo.org/x/drivers"
     7  	"tinygo.org/x/drivers/internal/legacy"
     8  )
     9  
    10  // DeviceAdafruitCharlieWing15x7 implements TinyGo driver for Lumissil
    11  // IS31FL3731 matrix LED driver on Adafruit 15x7 CharliePlex LED Matrix
    12  // FeatherWing (CharlieWing) board: https://www.adafruit.com/product/3163
    13  type DeviceAdafruitCharlieWing15x7 struct {
    14  	Device
    15  }
    16  
    17  // enableLEDs enables only LEDs that are soldered on the Adafruit CharlieWing
    18  // board. The board has following LEDs matrix layout:
    19  //
    20  //	"o" - connected (soldered) LEDs
    21  //	"x" - not connected LEDs
    22  //
    23  //	  + - - - - - - - - - - - - - - +
    24  //	  | + - - - - - - - - - - - - + |
    25  //	  | |                         | |
    26  //	  | |                         v v
    27  //	+---------------------------------+
    28  //	| o o o o o o o o o o o o o o o x |
    29  //	| o o o o o o o o o o o o o o o x |
    30  //	| o o o o o o o o o o o o o o o x |
    31  //	| o o o o o o o o o o o o o o o x |
    32  //	| o o o o o o o o o o o o o o o x |
    33  //	| o o o o o o o o o o o o o o o x |
    34  //	| o o o o o o o o o o o o o o o x |
    35  //	| x x x x x x x x x x x x x x x x |
    36  //	+---------------------------------+
    37  //	  ^ ^                         | |
    38  //	  | |                 ... - - + |
    39  //	  | + - - - - - - - - - - - - - +
    40  //	  |
    41  //	  start (address 0x00)
    42  func (d *DeviceAdafruitCharlieWing15x7) enableLEDs() (err error) {
    43  	for frame := FRAME_0; frame <= FRAME_7; frame++ {
    44  		err = d.selectCommand(frame)
    45  		if err != nil {
    46  			return err
    47  		}
    48  
    49  		// Enable left half
    50  		for i := uint8(0); i < 16; i += 2 {
    51  			err = legacy.WriteRegister(d.bus, d.Address, i, []byte{0b11111110})
    52  			if err != nil {
    53  				return err
    54  			}
    55  		}
    56  		// Enable right half
    57  		for i := uint8(3); i < 16; i += 2 {
    58  			err = legacy.WriteRegister(d.bus, d.Address, i, []byte{0b01111111})
    59  			if err != nil {
    60  				return err
    61  			}
    62  		}
    63  		// Disable invisible column on the right side
    64  		err = legacy.WriteRegister(d.bus, d.Address, 1, []byte{0b00000000})
    65  		if err != nil {
    66  			return err
    67  		}
    68  	}
    69  
    70  	return nil
    71  }
    72  
    73  // DrawPixelXY draws a single pixel on the selected frame by its XY coordinates
    74  // with provided PWM value [0-255]
    75  func (d *DeviceAdafruitCharlieWing15x7) DrawPixelXY(frame, x, y, value uint8) (err error) {
    76  	var index uint8
    77  
    78  	if x >= 15 {
    79  		return fmt.Errorf("invalid value: X is out of range [0, 15]")
    80  	} else if y >= 7 {
    81  		return fmt.Errorf("invalid value: Y is out of range [0, 7]")
    82  	}
    83  
    84  	// Board is one pixel shorter (7 vs 8 supported pixels)
    85  	if x < 8 {
    86  		index = 16*x + y + 1
    87  	} else {
    88  		index = 16*(16-x) - y - 1 - 1
    89  	}
    90  
    91  	return d.setPixelPWD(frame, index, value)
    92  }
    93  
    94  // NewAdafruitCharlieWing15x7 creates a new driver with Adafruit 15x7
    95  // CharliePlex LED Matrix FeatherWing (CharlieWing) layout.
    96  // Available addresses:
    97  // - 0x74 (default)
    98  // - 0x77 (when the address jumper soldered)
    99  func NewAdafruitCharlieWing15x7(bus drivers.I2C, address uint8) DeviceAdafruitCharlieWing15x7 {
   100  	return DeviceAdafruitCharlieWing15x7{
   101  		Device: Device{
   102  			Address: address,
   103  			bus:     bus,
   104  		},
   105  	}
   106  }