Skip to content

kubetrail/microbit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

microbit

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.

installation

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

usage

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.

display patterns and buzzer

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)
}

display temperature

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())
	}
}

About

microbit i/o using tinygo

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages