Skip to content

Instantly share code, notes, and snippets.

@zankich
Last active February 16, 2019 11:48
Show Gist options
  • Save zankich/a71839f3b48fab2c8c59198599d9ff58 to your computer and use it in GitHub Desktop.
Save zankich/a71839f3b48fab2c8c59198599d9ff58 to your computer and use it in GitHub Desktop.
Santa Monica Golang
package main
import (
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/goiot/devices/piglow"
"golang.org/x/exp/io/i2c"
)
type ColorNotifier interface {
Yellow(int) error
Orange(int) error
Red(int) error
Green(int) error
Blue(int) error
}
func check(notifier ColorNotifier) {
req, err := http.NewRequest("GET", "https://api.travis-ci.org/repos/zankich/shipit", nil)
if err != nil {
panic(err)
}
req.Header.Add("Accept", "application/vnd.travis-ci.2+json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
var travis struct {
Repo struct {
LastBuildState string `json:"last_build_state"`
} `json:"repo"`
}
if err := json.NewDecoder(resp.Body).Decode(&travis); err != nil {
panic(err)
}
switch travis.Repo.LastBuildState {
case "passed":
notifier.Green(255)
fmt.Println("green")
case "failed":
notifier.Red(255)
fmt.Println("red")
case "started":
notifier.Blue(255)
fmt.Println("blue")
case "created":
notifier.Yellow(255)
fmt.Println("yellow")
default:
notifier.Orange(255)
fmt.Println("orange")
}
}
func main() {
p, err := piglow.Open(&i2c.Devfs{Dev: "/dev/i2c-1"})
if err != nil {
panic(err)
}
if err := p.Setup(); err != nil {
panic(err)
}
for {
p.SetBrightness(0)
check(p)
time.Sleep(3 * time.Second)
}
}
Go + Robots
Adrian Zankich
[email protected]
@adzankich
* Go for hardware?
- Strongly typed language
- Strong concurrency primitives
- Fantastic deployment story
- Cross compilation
* Can Go be used for "Bare Metal" programming?
- Short answer: no
- Requires some sort of embedded version of supported OS
- Maybe an RTOS eventually
.link https://devel.rtems.org/wiki/Projects/GCCGoRTEMS
* How does Go communicate with hardware?
- devfs
- sysfs
- OS primitives
- remote serial interface
- http
* Current landscape
- One off libraries
- Frameworks (gobot, embd, hwio)
- Varying levels of portability and idiomaticity
.link https://github.com/rakyll/go-hardware
* Peripheral IO
- Base of common interfaces
- Common platform implementations: sysfs, devfs, firmata, etc
- Suite of device drivers
- Targetting core IO operations: i2c, spi, gpio, pwm, etc
.link https://golang.org/x/exp/io
.link https://github.com/goiot/devices
.link https://github.com/zankich/hal
* DEMOS!
.image robot-fire.jpg
package main
import (
"fmt"
"net/url"
"os"
"time"
"github.com/ChimeraCoder/anaconda"
"github.com/goiot/devices/lcdrgbbacklight"
"github.com/zankich/hal/firmata"
)
func main() {
anaconda.SetConsumerKey(os.Getenv("CONSUMER_KEY"))
anaconda.SetConsumerSecret(os.Getenv("CONSUMER_SECRET"))
api := anaconda.NewTwitterApi(os.Getenv("ACCESS_TOKEN"), os.Getenv("ACCESS_TOKEN_SECRET"))
display, err := lcdrgbbacklight.Open(
firmata.New("/dev/cu.usbmodem1421", 57600),
)
if err != nil {
panic(err)
}
display.SetRGB(255, 255, 255)
for {
v := url.Values{}
v.Set("count", "1")
searchResult, _ := api.GetSearch("DemsInPhilly", v)
display.Clear()
display.Home()
for _, tweet := range searchResult.Statuses {
fmt.Println(tweet.Text)
fmt.Println(fmt.Sprintf("@%s", tweet.User.ScreenName))
for i, rvalue := range tweet.Text {
if (i % 15) == 0 {
display.Clear()
display.SetPosition(16)
display.Write(fmt.Sprintf("@%s", tweet.User.ScreenName))
display.SetPosition(0)
}
display.Write(string(rvalue))
time.Sleep(100 * time.Millisecond)
}
}
time.Sleep(2 * time.Second)
}
}
package main
import (
"fmt"
"net/url"
"os"
"time"
"github.com/ChimeraCoder/anaconda"
"github.com/goiot/devices/lcdrgbbacklight"
"github.com/zankich/hal/firmata"
)
func main() {
anaconda.SetConsumerKey(os.Getenv("CONSUMER_KEY"))
anaconda.SetConsumerSecret(os.Getenv("CONSUMER_SECRET"))
api := anaconda.NewTwitterApi(os.Getenv("ACCESS_TOKEN"), os.Getenv("ACCESS_TOKEN_SECRET"))
display, err := lcdrgbbacklight.Open(firmata.New("/dev/cu.usbmodem1411", 57600))
if err != nil {
panic(err)
}
display.SetRGB(255, 255, 255)
tweets := map[int64]anaconda.Tweet{}
go func() {
for {
v := url.Values{}
v.Set("count", "10")
searchResult, _ := api.GetSearch("DemsInPhilly", v)
for _, tweet := range searchResult.Statuses {
tweets[tweet.Id] = tweet
}
time.Sleep(10 * time.Second)
}
}()
for {
for _, tweet := range tweets {
fmt.Println(tweet.Text)
fmt.Println(fmt.Sprintf("@%s", tweet.User.ScreenName))
for i, rvalue := range tweet.Text {
if (i % 15) == 0 {
display.Clear()
display.SetPosition(16)
display.Write(fmt.Sprintf("@%s", tweet.User.ScreenName))
display.SetPosition(0)
}
display.Write(string(rvalue))
time.Sleep(100 * time.Millisecond)
}
time.Sleep(1 * time.Second)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment