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

     1  //go:build microbit_v2
     2  
     3  // Package microbitmatrix implements a driver for the BBC micro:bit version 2 LED matrix.
     4  //
     5  // Schematic: https://github.com/microbit-foundation/microbit-v2-hardware/blob/main/V2.00/MicroBit_V2.0.0_S_schematic.PDF
     6  package microbitmatrix // import "tinygo.org/x/drivers/microbitmatrix"
     7  
     8  import (
     9  	"machine"
    10  )
    11  
    12  // 4 rotation orientations (0, 90, 180, 270), CW (clock wise)
    13  // 5 rows
    14  // 5 cols
    15  // target coordinates in machine rows (y) and cols (x)
    16  var matrixRotations = [4][5][5][2]uint8{
    17  	{ // 0
    18  		{{0, 0}, {0, 1}, {0, 2}, {0, 3}, {0, 4}},
    19  		{{1, 0}, {1, 1}, {1, 2}, {1, 3}, {1, 4}},
    20  		{{2, 0}, {2, 1}, {2, 2}, {2, 3}, {2, 4}},
    21  		{{3, 0}, {3, 1}, {3, 2}, {3, 3}, {3, 4}},
    22  		{{4, 0}, {4, 1}, {4, 2}, {4, 3}, {4, 4}},
    23  	},
    24  	{ // 90 CW
    25  		{{0, 4}, {1, 4}, {2, 4}, {3, 4}, {4, 4}},
    26  		{{0, 3}, {1, 3}, {2, 3}, {3, 3}, {4, 3}},
    27  		{{0, 2}, {1, 2}, {2, 2}, {3, 2}, {4, 2}},
    28  		{{0, 1}, {1, 1}, {2, 1}, {3, 1}, {4, 1}},
    29  		{{0, 0}, {1, 0}, {2, 0}, {3, 0}, {4, 0}},
    30  	},
    31  	{ // 180
    32  		{{4, 4}, {4, 3}, {4, 2}, {4, 1}, {4, 0}},
    33  		{{3, 4}, {3, 3}, {3, 2}, {3, 1}, {3, 0}},
    34  		{{2, 4}, {2, 3}, {2, 2}, {2, 1}, {2, 0}},
    35  		{{1, 4}, {1, 3}, {1, 2}, {1, 1}, {1, 0}},
    36  		{{0, 4}, {0, 3}, {0, 2}, {0, 1}, {0, 0}},
    37  	},
    38  	{ // 270
    39  		{{4, 0}, {3, 0}, {2, 0}, {1, 0}, {0, 0}},
    40  		{{4, 1}, {3, 1}, {2, 1}, {1, 1}, {0, 1}},
    41  		{{4, 2}, {3, 2}, {2, 2}, {1, 2}, {0, 2}},
    42  		{{4, 3}, {3, 3}, {2, 3}, {1, 3}, {0, 3}},
    43  		{{4, 4}, {3, 4}, {2, 4}, {1, 4}, {0, 4}},
    44  	},
    45  }
    46  
    47  const (
    48  	ledRows = 5
    49  	ledCols = 5
    50  )
    51  
    52  type Device struct {
    53  	pin      [ledCols + ledRows]machine.Pin
    54  	buffer   [ledRows][ledCols]int8
    55  	rotation uint8
    56  }
    57  
    58  func (d *Device) assignPins() {
    59  	d.pin[0] = machine.LED_COL_1
    60  	d.pin[1] = machine.LED_COL_2
    61  	d.pin[2] = machine.LED_COL_3
    62  	d.pin[3] = machine.LED_COL_4
    63  	d.pin[4] = machine.LED_COL_5
    64  
    65  	d.pin[5] = machine.LED_ROW_1
    66  	d.pin[6] = machine.LED_ROW_2
    67  	d.pin[7] = machine.LED_ROW_3
    68  	d.pin[8] = machine.LED_ROW_4
    69  	d.pin[9] = machine.LED_ROW_5
    70  
    71  	for i := 0; i < len(d.pin); i++ {
    72  		d.pin[i].Configure(machine.PinConfig{Mode: machine.PinOutput})
    73  	}
    74  }