Build real-world gaming hardware with TinyGo

2 February 2020

Daniel Esteban

Video

This talk was presented at the FOSDEM 2020 - Go Devroom

Watch the talk on Youtube

2

I'm a maker

"Used to describe those who love to create things in their spare time (often electronic, often with their own hands). Also called Hobbyists. The term is popular in the open hardware and hardware hacking electronics communities."

Urban Dictionary's top definition of Maker
3

TinyGo

TinyGo is a project to bring the Go programming language to microcontrollers and modern web browsers by creating a new compiler based on LLVM.

Version 0.12.0 released on January 31th
Fosdem 2019 TinyGo @deadprogram's talk
Now sponsored by Google ❤️
4

TinyGo compatible boards

You can compile and run TinyGo programs on several different microcontroller boards such as the BBC micro:bit, the Arduino Nano33 IoT, Adafruit Itsybitsy M0,...

5

PineTime

PineTime, an open source smartwatch from Pine64

Find them at their stand in building AW

6

TinyGo supported sensors & components

7

TinyGo displays

8

Tinygo Playground

Don't have a board yet? Don't worry, you can emulate several boards at https://play.tinygo.org/.

9

What are you going to see today?

Some fun to make projects you could do at home.

10

First project "Gopher says"

3 buttons with a different color and sound each. It generates a longer sequence of color-sound each time that the user has to repeat.

11

Gopher says - components

12

Gopher says - (1) code imports

  1. package main
  2. import (
  3. "machine" // special package for hardware
  4. "math/rand"
  5. "time"
  6. "tinygo.org/x/drivers/buzzer" // driver for the buzzer
  7. )
13

Gopher says - (2) setup the LEDs

  1. // Define the pin for the LED
  2. led := machine.A1
  3. // Configure the LED pin as output
  4. led.Configure(machine.PinConfig{Mode: machine.PinOutput})
  5. // enable a LED with .High()
  6. led.High()
  7. // disable a LED with .Low()
  8. led.Low()
14

Gopher says - (3) setup the buttons

  1. // Define the pin for the button
  2. button := machine.A5
  3. // Configure the button as input
  4. button.Configure(machine.PinConfig{Mode: machine.PinInput})
  5. // read if a button is pressed with .Get()
  6. if button.Get() {
  7. // do something ...
  8. }
15

Gopher says - (4) setup buzzer device

  1. // Configure the buzzer pin with the buzzer driver
  2. bzrPin := machine.A0
  3. bzrPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
  4. bzr := buzzer.New(bzrPin)
  5. // play a sound by calling buzzer.Tone(note, duration)
  6. game.bzr.Tone(buzzer.G3, 0.5)
16

Gopher says - (5) Write the rest of the code

17

Gopher says - (6) flash and run the code

tinygo flash -target=circuitplay-express github.com/conejoninja/gophersays
18

Second project "2 Players PONG"

Classic PONG -air hockey- game. Players control their rectangle on the screen with a knob (potentiometer). Additional real-time clock to show the time when not playing.

19

PONG - components

20

PONG - (1) code imports

  1. package main
  2. import (
  3. "machine" // special package for hardware
  4. "time"
  5. "image/color"
  6. "math/rand"
  7. "tinygo.org/x/drivers/hub75" // Driver for HUB75 RGB LED matrix
  8. "tinygo.org/x/drivers/ds3231" // Driver for Real-Time Clock DS3231
  9. "tinygo.org/x/tinyfont" // Package for showing text on displays
  10. )
21

PONG - (2) Analog pins - potentiometers

Set up analog pins

  1. // Initialize Analog pins
  2. machine.InitADC()
  3. player1 := machine.ADC{machine.A1}
  4. player1.Configure()

Read analog value with .Get() int32

  1. // Read analog value (and convert to a 0-24 y-coord.)
  2. posY := player1.Get() / 16384
22

PONG - (3) HUB75 display

  1. // SPI for the hub75
  2. machine.SPI0.Configure(machine.SPIConfig{
  3. Frequency: 8000000,
  4. Mode: 0})
  5. display := hub75.New(machine.SPI0, machine.D6, machine.D5, machine.D8, machine.D10, machine.D9, machine.D7)
  6. display.Configure(hub75.Config{
  7. ColorDepth: 2,
  8. FastUpdate: true,
  9. })

Change a pixel color with .SetPixel(x, y, color)

  1. display.SetPixel(ballX, ballY, color.RGBA{155, 183, 0, 255})
  2. display.Display()
23

PONG - (4) flash and run the code

tinygo flash -target=nano33-iot github.com/conejoninja/tinypong
24

PONG - (A) DS3231 RTC

  1. // I2C for the RTC DS3231
  2. machine.I2C0.Configure(machine.I2CConfig{})
  3. rtc := ds3231.New(machine.I2C0)
  4. rtc.Configure()
  5. // Read date-time from RTC
  6. dt, err = rtc.ReadTime()
  7. hour := dt.Hour()
  8. minute := dt.Minute()
25

Third project "Game console"

A portable game console that plays games

26

Game console - components

Option A (home made) Option B
Any screen (ST7735 in these examples)   Adafruit's PyBadge (or PyPortal, PyGamer*, EdgeBadge*,... )
Some buttons and maybe a joystick (analog input)
Buzzer optional
Any board supported (itsybitsy-m4) *not all supported, but similar hardware
27

Game console - components II

Option C

28

Tinydraw - tinygo.org/x/tinydraw

You could draw primitives (triangle, circle, lines,...) on any device that implements drivers.Displayer interface (all current displays supported by TinyGo do)

29

Tinyfont - tinygo.org/x/tinyfont

You could write text on any device that implements drivers.Displayer interface (all current displays supported by TinyGo do).

  1. tinyfont.WriteLine(&display, &tinyfont.Org01, 10, 12, []byte("PLAYER 2"), red)
30

Fourth project - Escornabot

Escornabot is an open source / open hardware educational robot (similar to Beebot or Cubetto). Great to introduce young children into programming.

video from CEIP Présaras
31

Escornabot - what does it do?

Program the robot with the buttons (move forward, turn left/right, backward). It will execute the steps one by one.

32

Escornabot - what does it do? (II)

There are different activities and resources at the project's wiki (mostly in Spanish).

video from @pablorubma
33

Escornabot - components

picture from @pablorubma
34

Escornabot - (1) code imports

  1. import (
  2. "machine"
  3. // The bot's code
  4. "github.com/conejoninja/go-escornabot/bot"
  5. "github.com/conejoninja/go-escornabot/input"
  6. "tinygo.org/x/drivers/buzzer" // buzzer driver
  7. "tinygo.org/x/drivers/easystepper" // motor driver
  8. )
35

Escornabot - (2) setup the motors

One motor

  1. motor := easystepper.New(machine.D1, machine.D2, machine.D3, machine.D4, 200, 60)
  2. motor.Configure()
  3. motor.Move(200)

But our robot has two!

  1. motors := easystepper.NewDual(machine.D1, machine.D2, machine.D3, machine.D4, machine.D5, machine.D6, machine.D7, machine.D8, 200, 60)
  2. motors.Configure()
  3. // Move both motors at the same time
  4. motors.Move(200,-400)
36

Escornabot - (3) flash and run the code

tinygo flash -target=itsybitsy-m0 github.com/conejoninja/go-escornabot
video from @pablorubma
37

Last project "Open LED race"

Simulate a race track with an addresable LED strip. Each color is a player and they must compete to win. It's a project started by Gerardo Barbarov at Arduino Day Sevilla 2019.

picture from M4Reactor Makerspace, Salisbury, USA
38

Open LED race - components

39

Open LED race - code imports

  1. import (
  2. "image/color"
  3. "machine"
  4. "time"
  5. "tinygo.org/x/drivers/buzzer"
  6. "tinygo.org/x/drivers/ws2812" // driver for ws2812 LED strip
  7. )
40

Open LED race - setup WS2812 LED strip

WS2812 LED strips are controlled by a single pin (in output mode)

  1. ledpin := machine.D2
  2. ledpin.Configure(machine.PinConfig{Mode: machine.PinOutput})
  3. track := ws2812.New(ledpin)

To change the color of its LEDs, send an array of the colors

  1. leds := make([]color.RGBA, TRACKLENGHT)
  2. leds[0] = color.RGBA{255, 0, 0, 255}
  3. track.WriteColors(leds)
41

Open LED race - input

You could use whatever you want as input, most common is to use some buttons, but here are some other ideas:

42

Open LED race - The track

We simulate the physics of the race and track, we could simulate a slope or ramp, special conditions like turbo, engine failure, pit stop, ...

On the left we could see a "force map", red part slows you down, green helps you go faster.

43

Open LED race - Telemetry

Why not add wifi and have some telemetry data like professionals?

  1. import (
  2. "tinygo.org/x/drivers/espat" // driver for wifi chips!
  3. "tinygo.org/x/drivers/net" // net package
  4. )
44

Open LED race - Any volunteers?

45

Projects links

All you need: source code, circuit schematics, 3D printed parts,...

46

Links

Join us at #TinyGo channel on gophers.slack.com

47

Thank you

Daniel Esteban

Use the left and right arrow keys or click the left and right edges of the page to navigate between slides.
(Press 'H' or navigate to hide this message.)