mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-06 20:13:40 +00:00
Add goroutines and function pointers
This commit is contained in:
@@ -7,15 +7,34 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
led := machine.GPIO{17} // LED 1 on the PCA10040
|
||||
go led1()
|
||||
led2()
|
||||
}
|
||||
|
||||
func led1() {
|
||||
led := machine.GPIO{machine.LED}
|
||||
led.Configure(machine.GPIOConfig{Mode: machine.GPIO_OUTPUT})
|
||||
for {
|
||||
println("LED on")
|
||||
led.Set(false)
|
||||
runtime.Sleep(runtime.Millisecond * 500)
|
||||
println("+")
|
||||
led.Low()
|
||||
runtime.Sleep(runtime.Millisecond * 1000)
|
||||
|
||||
println("LED off")
|
||||
led.Set(true)
|
||||
runtime.Sleep(runtime.Millisecond * 500)
|
||||
println("-")
|
||||
led.High()
|
||||
runtime.Sleep(runtime.Millisecond * 1000)
|
||||
}
|
||||
}
|
||||
|
||||
func led2() {
|
||||
led := machine.GPIO{machine.LED2}
|
||||
led.Configure(machine.GPIOConfig{Mode: machine.GPIO_OUTPUT})
|
||||
for {
|
||||
println(" +")
|
||||
led.Low()
|
||||
runtime.Sleep(runtime.Millisecond * 420)
|
||||
|
||||
println(" -")
|
||||
led.High()
|
||||
runtime.Sleep(runtime.Millisecond * 420)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,16 @@ func main() {
|
||||
printItf(5)
|
||||
printItf(byte('x'))
|
||||
printItf("foo")
|
||||
|
||||
runFunc(hello) // must be indirect to avoid obvious inlining
|
||||
}
|
||||
|
||||
func runFunc(f func()) {
|
||||
f()
|
||||
}
|
||||
|
||||
func hello() {
|
||||
println("hello from function pointer!")
|
||||
}
|
||||
|
||||
func strlen(s string) int {
|
||||
|
||||
Reference in New Issue
Block a user