microbit i/o using tinygo
Micro:Bit v2 is a fun microcontroller for interesting educational projects. This repo aims to build simple I/O using tinygo as a backend.
You will need tinygo
to build and flash this code. Connect micro:bit
and identify
the port:
tinygo ports
Port ID Boards
/dev/ttyACM0 ****:****
Flash the code specifying target and the port. Of course, change the port to what you see on your computer
tinygo flash -target microbit-v2 -port /dev/ttyACMO
Different actions, such as display, buzzer and temperature readout, are performed
asynchronously and tied together using context
. These context
values are
cancelled on events such as button press.
Example code below displays three different patterns on LED matrix along with buzzer buzzing at different tones that change on button press.
package main
import (
"context"
"github.com/kubetrail/microbit"
)
func main() {
// create a device handle
device := microbit.NewDevice()
// get context that will cancel on either button A or button B press
ctx := device.OnButtonPress()
device.SetMatrix(microbit.DisplayHeart).Display(ctx).Buzz(ctx, 400).Wait(ctx)
ctx = device.OnButtonPress()
device.SetMatrix(microbit.DisplaySquare).Display(ctx).Buzz(ctx, 200).Wait(ctx)
ctx = device.OnButtonPress()
device.SetMatrix(microbit.Display{
{1, 1, 0, 1, 1},
{1, 1, 0, 1, 1},
{1, 1, 1, 1, 1},
{1, 1, 0, 1, 1},
{1, 1, 0, 1, 1},
},
).Display(ctx).Wait(ctx)
ctx = context.Background()
device.SetMatrix(microbit.DisplayZeros).Display(ctx)
}
package main
import (
"github.com/kubetrail/microbit"
)
func main() {
device := microbit.NewDevice()
ctx := device.OnButtonPress()
device.SetMatrix(microbit.DisplayLeftArrow).Display(ctx).Wait(ctx)
for {
ctx = device.OnButtonPress()
microbit.NewDevice().DisplayTemp(ctx).Wait(ctx).Wait(device.OnButtonPress())
}
}