github.com/aykevl/tinygo@v0.5.0/src/machine/board_microbit.go (about)

     1  // +build nrf51,microbit
     2  
     3  package machine
     4  
     5  import (
     6  	"device/nrf"
     7  	"errors"
     8  )
     9  
    10  // The micro:bit does not have a 32kHz crystal on board.
    11  const HasLowFrequencyCrystal = false
    12  
    13  // Buttons on the micro:bit (A and B)
    14  const (
    15  	BUTTON  = BUTTONA
    16  	BUTTONA = 17
    17  	BUTTONB = 26
    18  )
    19  
    20  // UART pins
    21  const (
    22  	UART_TX_PIN = 24
    23  	UART_RX_PIN = 25
    24  )
    25  
    26  // ADC pins
    27  const (
    28  	ADC0 = 3 // P0 on the board
    29  	ADC1 = 2 // P1 on the board
    30  	ADC2 = 1 // P2 on the board
    31  )
    32  
    33  // I2C pins
    34  const (
    35  	SDA_PIN = 30 // P20 on the board
    36  	SCL_PIN = 0  // P19 on the board
    37  )
    38  
    39  // SPI pins
    40  const (
    41  	SPI0_SCK_PIN  = 23 // P13 on the board
    42  	SPI0_MOSI_PIN = 21 // P15 on the board
    43  	SPI0_MISO_PIN = 22 // P14 on the board
    44  )
    45  
    46  // GPIO/Analog pins
    47  const (
    48  	P0  = 3
    49  	P1  = 2
    50  	P2  = 1
    51  	P3  = 4
    52  	P4  = 5
    53  	P5  = 17
    54  	P6  = 12
    55  	P7  = 11
    56  	P8  = 18
    57  	P9  = 10
    58  	P10 = 6
    59  	P11 = 26
    60  	P12 = 20
    61  	P13 = 23
    62  	P14 = 22
    63  	P15 = 21
    64  	P16 = 16
    65  )
    66  
    67  // LED matrix pins
    68  const (
    69  	LED_COL_1 = 4
    70  	LED_COL_2 = 5
    71  	LED_COL_3 = 6
    72  	LED_COL_4 = 7
    73  	LED_COL_5 = 8
    74  	LED_COL_6 = 9
    75  	LED_COL_7 = 10
    76  	LED_COL_8 = 11
    77  	LED_COL_9 = 12
    78  	LED_ROW_1 = 13
    79  	LED_ROW_2 = 14
    80  	LED_ROW_3 = 15
    81  )
    82  
    83  // matrixSettings has the legs of the LED grid in the form {row, column} for each LED position.
    84  var matrixSettings = [5][5][2]uint8{
    85  	{{LED_ROW_1, LED_COL_1}, {LED_ROW_2, LED_COL_4}, {LED_ROW_1, LED_COL_2}, {LED_ROW_2, LED_COL_5}, {LED_ROW_1, LED_COL_3}},
    86  	{{LED_ROW_3, LED_COL_4}, {LED_ROW_3, LED_COL_5}, {LED_ROW_3, LED_COL_6}, {LED_ROW_3, LED_COL_7}, {LED_ROW_3, LED_COL_8}},
    87  	{{LED_ROW_2, LED_COL_2}, {LED_ROW_1, LED_COL_9}, {LED_ROW_2, LED_COL_3}, {LED_ROW_3, LED_COL_9}, {LED_ROW_2, LED_COL_1}},
    88  	{{LED_ROW_1, LED_COL_8}, {LED_ROW_1, LED_COL_7}, {LED_ROW_1, LED_COL_6}, {LED_ROW_1, LED_COL_5}, {LED_ROW_1, LED_COL_4}},
    89  	{{LED_ROW_3, LED_COL_3}, {LED_ROW_2, LED_COL_7}, {LED_ROW_3, LED_COL_1}, {LED_ROW_2, LED_COL_6}, {LED_ROW_3, LED_COL_2}}}
    90  
    91  // InitLEDMatrix initializes the LED matrix, by setting all of the row/col pins to output
    92  // then calling ClearLEDMatrix.
    93  func InitLEDMatrix() {
    94  	set := 0
    95  	for i := LED_COL_1; i <= LED_ROW_3; i++ {
    96  		set |= 1 << uint8(i)
    97  	}
    98  	nrf.GPIO.DIRSET = nrf.RegValue(set)
    99  	ClearLEDMatrix()
   100  }
   101  
   102  // ClearLEDMatrix clears the entire LED matrix.
   103  func ClearLEDMatrix() {
   104  	set := 0
   105  	for i := LED_COL_1; i <= LED_COL_9; i++ {
   106  		set |= 1 << uint8(i)
   107  	}
   108  	nrf.GPIO.OUTSET = nrf.RegValue(set)
   109  	nrf.GPIO.OUTCLR = (1 << LED_ROW_1) | (1 << LED_ROW_2) | (1 << LED_ROW_3)
   110  }
   111  
   112  // SetLEDMatrix turns on a single LED on the LED matrix.
   113  // Currently limited to a single LED at a time, it will clear the matrix before setting it.
   114  func SetLEDMatrix(x, y uint8) error {
   115  	if x > 4 || y > 4 {
   116  		return errors.New("Invalid LED matrix row or column")
   117  	}
   118  
   119  	// Clear matrix
   120  	ClearLEDMatrix()
   121  
   122  	nrf.GPIO.OUTSET = (1 << matrixSettings[y][x][0])
   123  	nrf.GPIO.OUTCLR = (1 << matrixSettings[y][x][1])
   124  
   125  	return nil
   126  }
   127  
   128  // SetEntireLEDMatrixOn turns on all of the LEDs on the LED matrix.
   129  func SetEntireLEDMatrixOn() error {
   130  	set := 0
   131  	for i := LED_ROW_1; i <= LED_ROW_3; i++ {
   132  		set |= 1 << uint8(i)
   133  	}
   134  	nrf.GPIO.OUTSET = nrf.RegValue(set)
   135  
   136  	set = 0
   137  	for i := LED_COL_1; i <= LED_COL_9; i++ {
   138  		set |= 1 << uint8(i)
   139  	}
   140  	nrf.GPIO.OUTCLR = nrf.RegValue(set)
   141  
   142  	return nil
   143  }