Skip to content

Commit 94b8214

Browse files
aykevldeadprogram
authored andcommitted
machine: refactor pins to be of Pin type
1 parent 421ef04 commit 94b8214

38 files changed

+592
-607
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import (
1717
)
1818

1919
func main() {
20-
led := machine.GPIO{machine.LED}
21-
led.Configure(machine.GPIOConfig{Mode: machine.GPIO_OUTPUT})
20+
led := machine.LED
21+
led.Configure(machine.PinConfig{Mode: machine.PinOutput})
2222
for {
2323
led.Low()
2424
time.Sleep(time.Millisecond * 1000)

src/examples/adc/adc.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
func main() {
1212
machine.InitADC()
1313

14-
led := machine.GPIO{machine.LED}
15-
led.Configure(machine.GPIOConfig{Mode: machine.GPIO_OUTPUT})
14+
led := machine.LED
15+
led.Configure(machine.PinConfig{Mode: machine.PinOutput})
1616

1717
sensor := machine.ADC{machine.ADC2}
1818
sensor.Configure()

src/examples/blinky1/blinky1.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
)
99

1010
func main() {
11-
led := machine.GPIO{machine.LED}
12-
led.Configure(machine.GPIOConfig{Mode: machine.GPIO_OUTPUT})
11+
led := machine.LED
12+
led.Configure(machine.PinConfig{Mode: machine.PinOutput})
1313
for {
1414
led.Low()
1515
time.Sleep(time.Millisecond * 500)

src/examples/blinky2/blinky2.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ func main() {
1616
}
1717

1818
func led1() {
19-
led := machine.GPIO{machine.LED}
20-
led.Configure(machine.GPIOConfig{Mode: machine.GPIO_OUTPUT})
19+
led := machine.LED1
20+
led.Configure(machine.PinConfig{Mode: machine.PinOutput})
2121
for {
2222
println("+")
2323
led.Low()
@@ -30,8 +30,8 @@ func led1() {
3030
}
3131

3232
func led2() {
33-
led := machine.GPIO{machine.LED2}
34-
led.Configure(machine.GPIOConfig{Mode: machine.GPIO_OUTPUT})
33+
led := machine.LED2
34+
led.Configure(machine.PinConfig{Mode: machine.PinOutput})
3535
for {
3636
println(" +")
3737
led.Low()

src/examples/button/button.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import (
77

88
// This example assumes that the button is connected to pin 8. Change the value
99
// below to use a different pin.
10-
const buttonPin = 8
10+
const (
11+
led = machine.LED
12+
button = machine.Pin(8)
13+
)
1114

1215
func main() {
13-
led := machine.GPIO{machine.LED}
14-
led.Configure(machine.GPIOConfig{Mode: machine.GPIO_OUTPUT})
15-
16-
button := machine.GPIO{buttonPin}
17-
button.Configure(machine.GPIOConfig{Mode: machine.GPIO_INPUT})
16+
led.Configure(machine.PinConfig{Mode: machine.PinOutput})
17+
button.Configure(machine.PinConfig{Mode: machine.PinInput})
1818

1919
for {
2020
if button.Get() {

src/examples/button2/button2.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,29 @@ import (
88
// This example assumes that you are using the pca10040 board
99

1010
func main() {
11-
led1 := machine.GPIO{machine.LED1}
12-
led1.Configure(machine.GPIOConfig{Mode: machine.GPIO_OUTPUT})
11+
led1 := machine.LED1
12+
led1.Configure(machine.PinConfig{Mode: machine.PinOutput})
1313

14-
led2 := machine.GPIO{machine.LED2}
15-
led2.Configure(machine.GPIOConfig{Mode: machine.GPIO_OUTPUT})
14+
led2 := machine.LED2
15+
led2.Configure(machine.PinConfig{Mode: machine.PinOutput})
1616

17-
led3 := machine.GPIO{machine.LED3}
18-
led3.Configure(machine.GPIOConfig{Mode: machine.GPIO_OUTPUT})
17+
led3 := machine.LED3
18+
led3.Configure(machine.PinConfig{Mode: machine.PinOutput})
1919

20-
led4 := machine.GPIO{machine.LED4}
21-
led4.Configure(machine.GPIOConfig{Mode: machine.GPIO_OUTPUT})
20+
led4 := machine.LED4
21+
led4.Configure(machine.PinConfig{Mode: machine.PinOutput})
2222

23-
button1 := machine.GPIO{machine.BUTTON1}
24-
button1.Configure(machine.GPIOConfig{Mode: machine.GPIO_INPUT_PULLUP})
23+
button1 := machine.BUTTON1
24+
button1.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
2525

26-
button2 := machine.GPIO{machine.BUTTON2}
27-
button2.Configure(machine.GPIOConfig{Mode: machine.GPIO_INPUT_PULLUP})
26+
button2 := machine.BUTTON2
27+
button2.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
2828

29-
button3 := machine.GPIO{machine.BUTTON3}
30-
button3.Configure(machine.GPIOConfig{Mode: machine.GPIO_INPUT_PULLUP})
29+
button3 := machine.BUTTON3
30+
button3.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
3131

32-
button4 := machine.GPIO{machine.BUTTON4}
33-
button4.Configure(machine.GPIOConfig{Mode: machine.GPIO_INPUT_PULLUP})
32+
button4 := machine.BUTTON4
33+
button4.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
3434

3535
for {
3636
led1.Set(button1.Get())

src/examples/echo/echo.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import (
99

1010
// change these to test a different UART or pins if available
1111
var (
12-
uart = machine.UART0
13-
tx uint8 = machine.UART_TX_PIN
14-
rx uint8 = machine.UART_RX_PIN
12+
uart = machine.UART0
13+
tx = machine.UART_TX_PIN
14+
rx = machine.UART_RX_PIN
1515
)
1616

1717
func main() {

src/examples/mcp3008/mcp3008.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,17 @@ import (
88
"time"
99
)
1010

11-
// CS_PIN is the pin used for Chip Select (CS). Change to whatever is in use on your board.
12-
const CS_PIN = 3
11+
// cs is the pin used for Chip Select (CS). Change to whatever is in use on your board.
12+
const cs = machine.Pin(3)
1313

1414
var (
1515
tx []byte
1616
rx []byte
1717
val, result uint16
18-
cs machine.GPIO
1918
)
2019

2120
func main() {
22-
cs = machine.GPIO{CS_PIN}
23-
cs.Configure(machine.GPIOConfig{Mode: machine.GPIO_OUTPUT})
21+
cs.Configure(machine.PinConfig{Mode: machine.PinOutput})
2422

2523
machine.SPI0.Configure(machine.SPIConfig{
2624
Frequency: 4000000,

src/examples/microbit-blink/microbit-blink.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import (
99
// The LED matrix in the micro:bit is a multiplexed display: https://en.wikipedia.org/wiki/Multiplexed_display
1010
// Driver for easier control: https://github.com/tinygo-org/drivers/tree/master/microbitmatrix
1111
func main() {
12-
ledrow := machine.GPIO{machine.LED_ROW_1}
13-
ledrow.Configure(machine.GPIOConfig{Mode: machine.GPIO_OUTPUT})
14-
ledcol := machine.GPIO{machine.LED_COL_1}
15-
ledcol.Configure(machine.GPIOConfig{Mode: machine.GPIO_OUTPUT})
12+
ledrow := machine.LED_ROW_1
13+
ledrow.Configure(machine.PinConfig{Mode: machine.PinOutput})
14+
ledcol := machine.LED_COL_1
15+
ledcol.Configure(machine.PinConfig{Mode: machine.PinOutput})
1616
ledcol.Low()
1717
for {
1818
ledrow.Low()

src/machine/board_arduino.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ package machine
55
const CPU_FREQUENCY = 16000000
66

77
// LED on the Arduino
8-
const LED = 13
8+
const LED Pin = 13
99

1010
// ADC on the Arduino
1111
const (
12-
ADC0 = 0
13-
ADC1 = 1
14-
ADC2 = 2
15-
ADC3 = 3
16-
ADC4 = 4 // Used by TWI for SDA
17-
ADC5 = 5 // Used by TWI for SCL
12+
ADC0 Pin = 0
13+
ADC1 Pin = 1
14+
ADC2 Pin = 2
15+
ADC3 Pin = 3
16+
ADC4 Pin = 4 // Used by TWI for SDA
17+
ADC5 Pin = 5 // Used by TWI for SCL
1818
)
1919

2020
// UART pins
2121
const (
22-
UART_TX_PIN = 1
23-
UART_RX_PIN = 0
22+
UART_TX_PIN Pin = 1
23+
UART_RX_PIN Pin = 0
2424
)

0 commit comments

Comments
 (0)