Created
July 15, 2017 21:25
-
-
Save peterhellberg/1ca504231fd02f6b67d8e2550dcfea9b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"image" | |
"image/color" | |
"os" | |
"time" | |
"github.com/faiface/pixel" | |
"github.com/faiface/pixel/pixelgl" | |
"gobot.io/x/gobot" | |
"gobot.io/x/gobot/drivers/aio" | |
"gobot.io/x/gobot/platforms/firmata" | |
_ "image/png" | |
) | |
func main() { | |
pixelgl.Run(run) | |
} | |
func run() { | |
firmataAdaptor := firmata.NewAdaptor("/dev/cu.usbmodem14111") | |
xAxis := aio.NewAnalogSensorDriver(firmataAdaptor, "0") | |
yAxis := aio.NewAnalogSensorDriver(firmataAdaptor, "1") | |
var x, y int | |
work := func() { | |
xAxis.On(aio.Data, func(data interface{}) { | |
nx := data.(int) | |
if nx < x-5 || nx > x+5 { | |
x = nx | |
} | |
}) | |
yAxis.On(aio.Data, func(data interface{}) { | |
ny := data.(int) | |
if ny < y-5 || ny > y+5 { | |
y = ny | |
} | |
}) | |
gobot.Every(500*time.Millisecond, func() { | |
fmt.Printf("%dx%d\n", x, y) | |
}) | |
} | |
go gobot.NewRobot("curieBot", | |
[]gobot.Connection{firmataAdaptor}, | |
[]gobot.Device{xAxis, yAxis}, | |
work, | |
).Start() | |
win, err := pixelgl.NewWindow(pixelgl.WindowConfig{ | |
Bounds: pixel.R(0, 0, float64(1024), float64(1024)), | |
VSync: true, | |
Undecorated: true, | |
}) | |
if err != nil { | |
panic(err) | |
} | |
pic, err := loadPicture("gopher.png") | |
if err != nil { | |
panic(err) | |
} | |
sprite := pixel.NewSprite(pic, pic.Bounds()) | |
for !win.Closed() { | |
win.SetClosed(win.JustPressed(pixelgl.KeyEscape) || win.JustPressed(pixelgl.KeyQ)) | |
win.Clear(color.RGBA{0, 0, 0, 255}) | |
sprite.Draw(win, pixel.IM.Moved(pixel.Vec{float64(x), float64(y)}).ScaledXY(win.Bounds().Center(), pixel.V(-1, 1))) | |
win.Update() | |
} | |
} | |
func loadPicture(path string) (pixel.Picture, error) { | |
file, err := os.Open(path) | |
if err != nil { | |
return nil, err | |
} | |
defer file.Close() | |
img, _, err := image.Decode(file) | |
if err != nil { | |
return nil, err | |
} | |
return pixel.PictureDataFromImage(img), nil | |
} |
color | pin |
---|---|
grey | 2 |
green | 3 |
red | 4 |
blue | 5 |
white | 12 |
red | 5V |
black | GND |
yellow | A0 |
orange | A1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
gopher.png