├── .gitignore ├── Makefile ├── README.md ├── accel └── main.go ├── arduino-colorlamp └── main.go ├── microbit-blink └── main.go ├── microbit-pixelbuttons └── main.go └── nrf-colorlamp └── main.go /.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | # aliases 3 | .PHONY: clean arduino-colorlamp build-arduino-colorlamp flash-arduino-colorlamp microbit-blink build-microbit-blink flash-microbit-blink microbit-pixelbuttons build-microbit-pixelbuttons flash-microbit-pixelbuttons nrf-colorlamp build-nrf-colorlamp flash-nrf-colorlamp microbit-accel build-microbit-accel flash-microbit-accel reelboard-accel build-reelboard-accel flash-reelboard-accel 4 | 5 | clean: 6 | mkdir -p build 7 | rm -rf build/** 8 | 9 | build-arduino-colorlamp: 10 | docker run --rm -v "$(PWD):/src" -v "$(GOPATH):/gohost" -e "GOPATH=$(GOPATH):/gohost" tinygo/tinygo:0.7.1 tinygo build -o /src/build/arduino-colorlamp.hex -target arduino /src/arduino-colorlamp/main.go 11 | 12 | flash-arduino-colorlamp: 13 | avrdude -c arduino -p atmega328p -P /dev/ttyACM0 -U flash:w:build/arduino-colorlamp.hex 14 | 15 | arduino-colorlamp: 16 | make clean 17 | make build-arduino-colorlamp 18 | make flash-arduino-colorlamp 19 | 20 | build-microbit-blink: 21 | docker run --rm -v "$(PWD):/src" -v "$(GOPATH):/gohost" -e "GOPATH=$(GOPATH):/gohost" tinygo/tinygo:0.7.1 tinygo build -o /src/build/microbit-blink.hex -target microbit /src/microbit-blink/main.go 22 | 23 | flash-microbit-blink: 24 | openocd -f interface/cmsis-dap.cfg -f target/nrf51.cfg -c 'program build/microbit-blink.hex reset exit' 25 | 26 | microbit-blink: 27 | make clean 28 | make build-microbit-blink 29 | make flash-microbit-blink 30 | 31 | build-microbit-pixelbuttons: 32 | docker run --rm -v "$(PWD):/src" -v "$(GOPATH):/gohost" -e "GOPATH=$(GOPATH):/gohost" tinygo/tinygo:0.7.1 tinygo build -o /src/build/microbit-pixelbuttons.hex -target microbit /src/microbit-pixelbuttons/main.go 33 | 34 | flash-microbit-pixelbuttons: 35 | openocd -f interface/cmsis-dap.cfg -f target/nrf51.cfg -c 'program build/microbit-pixelbuttons.hex reset exit' 36 | 37 | microbit-pixelbuttons: 38 | make clean 39 | make build-microbit-pixelbuttons 40 | make flash-microbit-pixelbuttons 41 | 42 | build-microbit-accel: 43 | docker run --rm -v "$(PWD):/src" -v "$(GOPATH):/gohost" -e "GOPATH=$(GOPATH):/gohost" tinygo/tinygo:0.7.1 tinygo build -o /src/build/microbit-accel.hex -target microbit /src/accel/main.go 44 | 45 | flash-microbit-accel: 46 | openocd -f interface/cmsis-dap.cfg -f target/nrf51.cfg -c 'program build/microbit-accel.hex reset exit' 47 | 48 | microbit-accel: 49 | make clean 50 | make build-microbit-accel 51 | make flash-microbit-accel 52 | 53 | build-reelboard-accel: 54 | docker run --rm -v "$(PWD):/src" -v "$(GOPATH):/gohost" -e "GOPATH=$(GOPATH):/gohost" tinygo/tinygo:0.7.1 tinygo build -o /src/build/reelboard-accel.hex -target reelboard /src/accel/main.go 55 | 56 | flash-reelboard-accel: 57 | openocd -f interface/cmsis-dap.cfg -f target/nrf51.cfg -c 'program build/reelboard-accel.hex reset exit' 58 | 59 | reelboard-accel: 60 | make clean 61 | make build-reelboard-accel 62 | make flash-reelboard-accel 63 | 64 | build-nrf-colorlamp: 65 | docker run --rm -v "$(PWD):/src" -v "$(GOPATH):/gohost" -e "GOPATH=$(GOPATH):/gohost" tinygo/tinygo:0.7.1 tinygo build -o /src/build/nrf-colorlamp.hex -target pca10040 /src/nrf-colorlamp/main.go 66 | 67 | flash-nrf-colorlamp: 68 | nrfjprog -f nrf52 --sectorerase --program build/nrf-colorlamp.hex --reset 69 | 70 | nrf-colorlamp: 71 | make clean 72 | make build-nrf-colorlamp 73 | make flash-nrf-colorlamp 74 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TinyGo Zoo 2 | 3 | Various sample programs for microcontrollers using TinyGo (http://tinygo.org) 4 | 5 | ## Installation requirements 6 | 7 | ### Install Docker image 8 | 9 | docker pull tinygo/tinygo 10 | 11 | ### Install tinygo-drivers into your LOCAL golang installation 12 | 13 | go get -d github.com/tinygo-org/drivers 14 | 15 | ### Install flashing tools 16 | 17 | - BBC micro:bit 18 | 19 | sudo apt-get install openocd 20 | 21 | - Arduino 22 | 23 | sudo apt-get install avrdude 24 | 25 | - PCA10040 26 | Install nrfjprog as follows. 27 | 28 | First install the J-Link Software and Documentation Pack from Segger: 29 | https://www.segger.com/downloads/jlink/#J-LinkSoftwareAndDocumentationPack 30 | 31 | Then install the nRF5x Command-Line Tools: 32 | https://docs.zephyrproject.org/latest/tools/nordic_segger.html#nordic-segger 33 | 34 | 35 | ## Blink for BBC micro:bit 36 | 37 | make microbit-blink 38 | 39 | ## Pixel buttons for BBC micro:bit 40 | 41 | make microbit-pixelbuttons 42 | 43 | ## Color lamp for Arduino Uno 44 | 45 | make arduino-colorlamp 46 | 47 | ## Color lamp for PCA10040 (NRF52-DK) 48 | 49 | make nrf-colorlamp 50 | 51 | ## Built-in accelerometer on BBC micro:bit 52 | 53 | make microbit-accel 54 | 55 | ## Built-in accelerometer on reel board 56 | 57 | make reelboard-accel 58 | -------------------------------------------------------------------------------- /accel/main.go: -------------------------------------------------------------------------------- 1 | // program for the BBC micro:bit displays the mma8653 accelerometer via the serial port 2 | package main 3 | 4 | import ( 5 | "machine" 6 | "time" 7 | 8 | "tinygo.org/x/drivers/mma8653" 9 | ) 10 | 11 | func main() { 12 | machine.I2C0.Configure(machine.I2CConfig{}) 13 | 14 | // Init mma8653 15 | accel := mma8653.New(machine.I2C0) 16 | accel.Configure(mma8653.DataRate200Hz, mma8653.Sensitivity2G) 17 | 18 | for { 19 | x, y, z, _ := accel.ReadAcceleration() 20 | println(x, y, z) 21 | time.Sleep(time.Millisecond * 500) 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /arduino-colorlamp/main.go: -------------------------------------------------------------------------------- 1 | // This program runs on an Arduino that has the following four devices connected: 2 | // - Button connected to D2 3 | // - Rotary analog dial connected to A0 4 | // - RGB LED connected to D3, D5, and D6 used as PWM pins 5 | // - BlinkM I2C RGB LED 6 | // 7 | // Pushing the button switches which color is selected. 8 | // Rotating the dial changes the value for the currently selected color. 9 | // Changing the color value updates the color displayed on both the 10 | // PWM-controlled RGB LED and the I2C-controlled BlinkM. 11 | package main 12 | 13 | import ( 14 | "machine" 15 | "time" 16 | 17 | "tinygo.org/x/drivers/blinkm" 18 | ) 19 | 20 | const ( 21 | buttonPin = 2 22 | redPin = 3 23 | greenPin = 5 24 | bluePin = 6 25 | 26 | red = 0 27 | green = 1 28 | blue = 2 29 | ) 30 | 31 | func main() { 32 | machine.InitADC() 33 | machine.InitPWM() 34 | machine.I2C0.Configure(machine.I2CConfig{}) 35 | 36 | // Init BlinkM 37 | blm := blinkm.New(machine.I2C0) 38 | blm.StopScript() 39 | 40 | button := machine.Pin(buttonPin) 41 | button.Configure(machine.PinConfig{Mode: machine.PinInput}) 42 | 43 | dial := machine.ADC{machine.ADC0} 44 | dial.Configure() 45 | 46 | redLED := machine.PWM{redPin} 47 | redLED.Configure() 48 | 49 | greenLED := machine.PWM{greenPin} 50 | greenLED.Configure() 51 | 52 | blueLED := machine.PWM{bluePin} 53 | blueLED.Configure() 54 | 55 | selectedColor := red 56 | colors := []uint16{0, 0, 0} 57 | 58 | for { 59 | // If we pushed the button, switch active color. 60 | if !button.Get() { 61 | if selectedColor == blue { 62 | selectedColor = red 63 | } else { 64 | selectedColor++ 65 | } 66 | } 67 | 68 | // Change the intensity for the currently selected color based on the dial setting. 69 | colors[selectedColor] = (dial.Get()) 70 | 71 | // Update the RGB LED. 72 | redLED.Set(colors[red]) 73 | greenLED.Set(colors[green]) 74 | blueLED.Set(colors[blue]) 75 | 76 | // Update the BlinkM. 77 | blm.SetRGB(byte(colors[red]>>8), byte(colors[green]>>8), byte(colors[blue]>>8)) 78 | 79 | time.Sleep(time.Millisecond * 100) 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /microbit-blink/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "image/color" 5 | "math/rand" 6 | "time" 7 | 8 | "tinygo.org/x/drivers/microbitmatrix" 9 | ) 10 | 11 | var display microbitmatrix.Device 12 | 13 | func main() { 14 | display = microbitmatrix.New() 15 | display.Configure(microbitmatrix.Config{}) 16 | 17 | display.ClearDisplay() 18 | 19 | x := int16(1) 20 | y := int16(2) 21 | deltaX := int16(1) 22 | deltaY := int16(1) 23 | then := time.Now() 24 | c := color.RGBA{255, 255, 255, 255} 25 | 26 | for { 27 | if time.Since(then).Nanoseconds() > 80000000 { 28 | then = time.Now() 29 | 30 | pixel := display.GetPixel(x, y) 31 | if pixel { 32 | display.ClearDisplay() 33 | x = 1 + int16(rand.Int31n(3)) 34 | y = 1 + int16(rand.Int31n(3)) 35 | deltaX = 1 36 | deltaY = 1 37 | if rand.Int31n(2) == 0 { 38 | deltaX = -1 39 | } 40 | if rand.Int31n(2) == 0 { 41 | deltaY = -1 42 | } 43 | } 44 | display.SetPixel(x, y, c) 45 | 46 | x += deltaX 47 | y += deltaY 48 | 49 | if x == 0 || x == 4 { 50 | deltaX = -deltaX 51 | } 52 | 53 | if y == 0 || y == 4 { 54 | deltaY = -deltaY 55 | } 56 | } 57 | display.Display() 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /microbit-pixelbuttons/main.go: -------------------------------------------------------------------------------- 1 | // Example using the buttons and LED matrix on a BBC:Microbit 2 | package main 3 | 4 | import ( 5 | "image/color" 6 | "machine" 7 | "time" 8 | 9 | "tinygo.org/x/drivers/microbitmatrix" 10 | ) 11 | 12 | func main() { 13 | display := microbitmatrix.New() 14 | display.Configure(microbitmatrix.Config{}) 15 | display.ClearDisplay() 16 | 17 | left := machine.BUTTONA 18 | left.Configure(machine.PinConfig{Mode: machine.PinInput}) 19 | 20 | right := machine.BUTTONB 21 | right.Configure(machine.PinConfig{Mode: machine.PinInput}) 22 | 23 | var ( 24 | x int16 = 2 25 | y int16 = 2 26 | c = color.RGBA{255, 255, 255, 255} 27 | ) 28 | 29 | then := time.Now() 30 | for { 31 | if time.Since(then).Nanoseconds() > 80000000 { 32 | then = time.Now() 33 | 34 | if !left.Get() { 35 | switch { 36 | case x > 0: 37 | x-- 38 | case x == 0: 39 | if y > 0 { 40 | x = 4 41 | y-- 42 | } 43 | } 44 | } 45 | 46 | if !right.Get() { 47 | switch { 48 | case x < 4: 49 | x++ 50 | case x == 4: 51 | if y < 4 { 52 | x = 0 53 | y++ 54 | } 55 | } 56 | } 57 | 58 | display.ClearDisplay() 59 | display.SetPixel(x, y, c) 60 | } 61 | display.Display() 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /nrf-colorlamp/main.go: -------------------------------------------------------------------------------- 1 | // This program runs on an PCA10040 that has the following four devices connected: 2 | // - Button connected to P0.11 3 | // - Rotary analog dial connected to P0.03 (A0) 4 | // - RGB LED connected to P0.23, P0.24, and P0.25 used as PWM pins 5 | // - BlinkM I2C RGB LED 6 | // 7 | // Pushing the button switches which color is selected. 8 | // Rotating the dial changes the value for the currently selected color. 9 | // Changing the color value updates the color displayed on both the 10 | // PWM-controlled RGB LED and the I2C-controlled BlinkM. 11 | package main 12 | 13 | import ( 14 | "machine" 15 | "time" 16 | 17 | "tinygo.org/x/drivers/blinkm" 18 | ) 19 | 20 | const ( 21 | buttonPin = 11 22 | redPin = 24 23 | greenPin = 25 24 | bluePin = 23 25 | 26 | red = 0 27 | green = 1 28 | blue = 2 29 | ) 30 | 31 | func main() { 32 | machine.InitADC() 33 | machine.InitPWM() 34 | machine.I2C0.Configure(machine.I2CConfig{}) 35 | 36 | // Init BlinkM 37 | blm := blinkm.New(machine.I2C0) 38 | blm.StopScript() 39 | 40 | button := machine.Pin(buttonPin) 41 | button.Configure(machine.PinConfig{Mode: machine.PinInput}) 42 | 43 | dial := machine.ADC{machine.ADC0} 44 | dial.Configure() 45 | 46 | redLED := machine.PWM{redPin} 47 | redLED.Configure() 48 | 49 | greenLED := machine.PWM{greenPin} 50 | greenLED.Configure() 51 | 52 | blueLED := machine.PWM{bluePin} 53 | blueLED.Configure() 54 | 55 | selectedColor := red 56 | colors := []uint16{0, 0, 0} 57 | 58 | for { 59 | // If we pushed the button, switch active color. 60 | if !button.Get() { 61 | if selectedColor == blue { 62 | selectedColor = red 63 | } else { 64 | selectedColor++ 65 | } 66 | } 67 | 68 | // Change the intensity for the currently selected color based on the dial setting. 69 | colors[selectedColor] = (dial.Get()) 70 | 71 | // Update the RGB LED. 72 | redLED.Set(colors[red]) 73 | greenLED.Set(colors[green]) 74 | blueLED.Set(colors[blue]) 75 | 76 | // Update the BlinkM. 77 | blm.SetRGB(byte(colors[red]>>8), byte(colors[green]>>8), byte(colors[blue]>>8)) 78 | 79 | time.Sleep(time.Millisecond * 100) 80 | } 81 | } 82 | --------------------------------------------------------------------------------