Open2

tinygo goroutine and channel 試作

jtmy7203jtmy7203

動くわけないと思いつつ、TinyGoでGoroutineとChannelを試しに動かしてみる。
あっさり動いてびっくり

// You can edit this code!
package main

import (
	"machine"
	"time"
)

func main() {
    messages := make(chan string)
    go ledcontrol(messages)
    for {
        time.Sleep(time.Second*1)
        messages <- "High"
        time.Sleep(time.Second*1)
        messages <- "Low"		
    }
}

func ledcontrol(msgchan chan string) {
    led := machine.LED
    led.Configure(machine.PinConfig{Mode: machine.PinOutput})
    for {
        select {
            case control := <- msgchan :
    		    if control == "High" {
                    led.High()
                } else {
                    led.Low()
                }
        }
    }
}