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

     1  //go:build microbit
     2  
     3  // Package microbitmatrix implements a driver for the BBC micro:bit's LED matrix.
     4  //
     5  // Schematic: https://github.com/bbcmicrobit/hardware/blob/master/SCH_BBC-Microbit_V1.3B.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}, {1, 3}, {0, 1}, {1, 4}, {0, 2}},
    19  		{{2, 3}, {2, 4}, {2, 5}, {2, 6}, {2, 7}},
    20  		{{1, 1}, {0, 8}, {1, 2}, {2, 8}, {1, 0}},
    21  		{{0, 7}, {0, 6}, {0, 5}, {0, 4}, {0, 3}},
    22  		{{2, 2}, {1, 6}, {2, 0}, {1, 5}, {2, 1}},
    23  	},
    24  	{ // 90 CW
    25  		{{0, 2}, {2, 7}, {1, 0}, {0, 3}, {2, 1}},
    26  		{{1, 4}, {2, 6}, {2, 8}, {0, 4}, {1, 5}},
    27  		{{0, 1}, {2, 5}, {1, 2}, {0, 5}, {2, 0}},
    28  		{{1, 3}, {2, 4}, {0, 8}, {0, 6}, {1, 6}},
    29  		{{0, 0}, {2, 3}, {1, 1}, {0, 7}, {2, 2}},
    30  	},
    31  	{ // 180
    32  		{{2, 1}, {1, 5}, {2, 0}, {1, 6}, {2, 2}},
    33  		{{0, 3}, {0, 4}, {0, 5}, {0, 6}, {0, 7}},
    34  		{{1, 0}, {2, 8}, {1, 2}, {0, 8}, {1, 1}},
    35  		{{2, 7}, {2, 6}, {2, 5}, {2, 4}, {2, 3}},
    36  		{{0, 2}, {1, 4}, {0, 1}, {1, 3}, {0, 0}},
    37  	},
    38  	{ // 270
    39  		{{2, 2}, {0, 7}, {1, 1}, {2, 3}, {0, 0}},
    40  		{{1, 6}, {0, 6}, {0, 8}, {2, 4}, {1, 3}},
    41  		{{2, 0}, {0, 5}, {1, 2}, {2, 5}, {0, 1}},
    42  		{{1, 5}, {0, 4}, {2, 8}, {2, 6}, {1, 4}},
    43  		{{2, 1}, {0, 3}, {1, 0}, {2, 7}, {0, 2}},
    44  	},
    45  }
    46  
    47  const (
    48  	ledRows = 3
    49  	ledCols = 9
    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  	d.pin[5] = machine.LED_COL_6
    65  	d.pin[6] = machine.LED_COL_7
    66  	d.pin[7] = machine.LED_COL_8
    67  	d.pin[8] = machine.LED_COL_9
    68  
    69  	d.pin[9] = machine.LED_ROW_1
    70  	d.pin[10] = machine.LED_ROW_2
    71  	d.pin[11] = machine.LED_ROW_3
    72  
    73  	for i := 0; i < len(d.pin); i++ {
    74  		d.pin[i].Configure(machine.PinConfig{Mode: machine.PinOutput})
    75  	}
    76  }