Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
TinyGoでIoTを始めよう
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
irieda
April 20, 2019
Technology
910
2
Share
TinyGoでIoTを始めよう
TinyGoというGo言語サブセットを使って
PC-OSの無い環境の組み込み開発を始める方法の紹介。
irieda
April 20, 2019
More Decks by irieda
See All by irieda
SpaGoでSPAを作ろう!
nobonobo
0
650
Goだけでロビーサーバーを作ってみた
nobonobo
2
570
GopherJS+Vecty
nobonobo
1
1.1k
Other Decks in Technology
See All in Technology
マルチプロダクトの信頼性を効率良く保っていくために
kworkdev
PRO
0
160
Master Dataグループ紹介資料
sansan33
PRO
1
4.6k
AIでAIをテストする - 音声AIエージェントの品質保証戦略
morix1500
1
120
[最強DB講義]推薦システム | 基礎編
recsyslab
PRO
1
170
小説執筆のハーネスエンジニアリング
yoshitetsu
0
690
AIを共同作業者にして書籍を執筆する方法 / How to Write a Book with AI as a Co-Creator
ama_ch
2
130
マルチエージェント × ハーネスエンジニアリング × GitLab Duo Agent Platformで実現する「AIエージェントに仕事をさせる時代へ。」 / 20260421 GitLab Duo Agent Platform
n11sh1
0
160
Do Ruby::Box dream of Modular Monolith?
joker1007
1
340
Good Enough Types: Heuristic Type Inference for Ruby
riseshia
1
220
Eight Engineering Unit 紹介資料
sansan33
PRO
3
7.3k
ハーネスエンジニアリングをやりすぎた話 ~そのハーネスは解体された~
gotalab555
4
1.7k
Keeping Ruby Running on Cygwin
fd0
0
160
Featured
See All Featured
The browser strikes back
jonoalderson
0
980
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
410
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
1
3.5k
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
130
The untapped power of vector embeddings
frankvandijk
2
1.7k
The Cult of Friendly URLs
andyhume
79
6.8k
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
1
330
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
3.8k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
340
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
1
520
Java REST API Framework Comparison - PWX 2021
mraible
34
9.3k
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
170
Transcript
TinyGoͰIoTΛ࢝ΊΑ͏
͓લ୭Αʁ ϝΧτϩιϑτ Pythonista -> Gopher झຯͰGoʹ͔ͳ͍ͷ։ Goྺ̒ऑ͘Β͍ αΠτ: ձࣾ: 144Lab(2017/07/01
͔Β৽໊ࣾ) HN: @nobonobo http://golang.rdy.jp/
ΈࠐΈ։ൃͬͯʁ
ΈࠐΈͱݴͬͯʁ େผͯ͠ PC͚OSͷ্ͰΈࠐΈ PC͚OSແ͠ͰΈࠐΈ ʢΈࠐΈ͚OS·ͨOSແ͠ʣ ͱ͍͏̎छྨ͕͋Γ·͢ɻ
PC͚OSͷ্ͰΈࠐΈ PCʹ͍ۙখܕϘʔυʢRaspberryPiͳͲʣͰ LinuxWindows10ͷ্ʹ ΞϓϦΛ࣮͢ΔελΠϧ ͕ૉΒ͍͠ ͨͩ͠ɺిݯ֬อ͕ඞਢ Ϟϊͷબࢶ࣮࣭̎ RaspberryPiܥ ߴՁͳϘʔυͷྨ https://gobot.io
PC͚OSແ͠ͰΈࠐΈ Goͷෆಘҙ Goͷཪํͷػೳηοτ͕OSʹґଘ goroutineͷεέδϡʔϥGCͳͲ OSͷແ͍ڥʹҠ২͢Δίετ͕ߴ͍ ϨΠϠͷI/Oࢧԉػೳ͕ͱͱແ͍ PCͱҧ͍ཧతʹଟ༷͗͢Δڥ
ΈࠐΈ։ൃͷੈք ιϑτΣΞͷ͕ࣝٻΊΒΕΔ ϨΠϠʔͷࣝٻΊΒΕΔ ϋʔυΣΞͷࣝগ͠ٻΊΒΕΔ ։ൃڥΛ͑Δͷʹίετ͕͔͔Δ ΈࠐΈ is Hard!
ͦͯ͠ɾɾɾ C/C++͕σϑΝΫτελϯμʔυ C͚ͩͳΒ·ͩ͠C++πϥΠ ॲཧͷ࣮ମΛӅ͢ͷ্͕ख͗͢Δ ࠔͳґଘղܾ includeࠈͱIFDEFࠈ ΦʔόʔϩʔυʹςϯϓϨʔτ ΈࠐΈ is Hard!
C/C++ͷΘΓʹGoͳΒʁ ϦʔμϏϦςΟ࠷ߴʂ ΤϥʔΛపఈͯ͠νΣοΫ͢ΔจԽʂ undefined-behavierʹ·͞Εͳ͍ʂ ґଘղܾʹgo-get͕͑Δʂ ΈࠐΈ͕গۙ͠ʹͳΔ͔ʁʂ
ͱ͍͏Θ͚Ͱ TinyGoͰ͢Αʂʂ
ઌ݄ϗοςϯτϦೖΓ https://tech.144lab.com/entry/tinygo
λʔήοτྫ
micro:bit ରԠ։ൃπʔϧͷ͕τοϓΫϥε ϒϩοΫํࣜͰখֶੜϓϩάϥϛϯάOK ϒϩοΫʹ͖ͨΒJS,TS ArduinoͰPythonͰRuby,Jacvascript,LuaͳͲͳͲ ϓϩ͚SEGGER Embedded StudioͰOK
circuit playground express ࡌΪϛοΫ͕τοϓΫϥε ϑϧΧϥʔ֊ௐ͖ͭLED̍̌ݸ εϐʔΧʔʹϚΠΫ ޫྔηϯαɺԹɾ࣪ηϯα ̒࣠ʢՃɾ֯ʣηϯα IRϦϞίϯૹड৴ λονηϯγϯά
UART/USB/SPI/I2C/etc...
࣮ࡍʹ͍ΖΜͳίʔυΛ ίϯύΠϧͯ͜͠ΕΒͰ ಈ͔ͯ͠Έ·͠ΐ͏ʂ
TinyGoͷ͍ํ dockerϕʔε͕ࠓΦεεϝͰ͢ɻ ʢdevϒϥϯνϏϧυେม ˍ ߴසͰґଘมߋ͞Ε Δʣ $ docker run -it
--rm tinygo/tinygo tinygo version tinygo version 0.4.1 linux/amd64
ͨ·ʹΞοϓσʔτͯ͠Έ·͠ΐ͏ $ docker pull tinygo/tinygo:latest latest: Pulling from tinygo/tinygo Digest:
sha256:821ad71e7d9b3fe46a84a5818e52d3d3596e0d8f3855f8407b917a37371b2b23 Status: Image is up to date for tinygo/tinygo:latest
sample̍ ͍ΘΏΔLνΧʢΈࠐΈͰͷHelloWorldʣ package main import ( "machine" "time" ) func
main() { led := machine.GPIO{machine.LED} led.Configure(machine.GPIOConfig{Mode: machine.GPIO_OUTPUT}) for { led.Low() time.Sleep(time.Millisecond * 500) led.High() time.Sleep(time.Millisecond * 500) } }
Ϗϧυ output.uf2 --Drag&Drop--> CIRCUITPYσόΠε ͜Ε͚ͩͰλʔήοτʹॻ͖ࠐ·ΕΔ $ go mod init github.com/nobonobo/examples/sample1
$ PACKAGE_PATH = github.com/nobonobo/examples/sample1 $ PROJECT_PATH = /go/src/$PACKAGE_PATH $ docker run -it --rm $PWD:$PROJECT_PATH -w $PROJECT_PATH \ tinygo/tinygo tinygo build -target=circuitplay-express -o output.uf2 .
TinyGoͷఏڙόε࣮(1) ڞ௨Ͱఏڙ͢Δόε GPIO(൚༻ೖग़ྗϐϯ) ADCʢΞφϩάܭଌʣ PWMʢαʔϘɺύϫʔίϯτϩʔϧ͚ʣ UARTʢPCͱͷ࿈ܞ༻ʣ SPIʢখ͞ͳσόΠεͱͷ࿈ܞ༻ʣ I2Cʢখ͞ͳσόΠεͱͷ࿈ܞ༻ʣ
TinyGoͷఏڙόε࣮(2) Ұ෦ͷλʔήοτ DACʢΞφϩάిѹൃੜʣ
ΞʔΩςΫνϟผͷఏڙػೳ ϨδελΞΫηε ΠϯϥΠϯΞηϯϒϥ λΠϚʔɺUARTϋϯυϦϯά ΄ͱΜͲͷλʔήοτͰ ʮprintlnʯͰ γϦΞϧϙʔτʹσόοάϩάΛग़ྗͰ͖Δɻ
पลσόΠεผυϥΠόʔ Device Name Interface Type SPI I2C I2C I2C GPIO
UART I2C I2C I2C GPIO https://github.com/tinygo-org/drivers APA102 RGB LED BH1750 ambient light sensor BlinkM RGB LED BMP180 barometer "Easystepper" stepper motor controller ESP8266/ESP32 AT Command set for WiFi/TCP/UDP MAG3110 magnetometer MMA8653 accelerometer MPU6050 accelerometer/gyroscope WS2812 RGB LED
֎෦ύοέʔδͱdocker ґଘ͢ΔύοέʔδΛՃ͍ͨ͠ʂʂ ͋ΕʁdockerϕʔεͩͱͲ͏͢Δʁʂ ͦ͜Ͱgo modͰ͢Α $ export GO111MODULE=on $ go
mod github.com/..../sample1 $ go get github.com/tinygo-org/drivers/ws2812 $ go mod vendor
͢Δͱʁ vendor/ github.com/ tinygo-org/ drivers/ ws2812/ main.go ͖ͬࣔͨ͞͠dockerίϚϯυͰίϯςφ͔Β ws2812ύοέʔδ͕ࢀরՄೳʹͳΔɻ
LEDετϦοϓυϥΠόΛ࡞Δ package main import ( "device/arm" "image/color" "machine" "github.com/tinygo-org/drivers/ws2812" )
// NeopixelDriver represents a connection to a NeoPixel type NeopixelDriver struct { pin machine.GPIO device ws2812.Device pixelCount int pixels []color.RGBA } // NewNeopixelDriver returns a new NeopixelDriver
ͦͯ͠ಈ͔ͯ͠ΈΔ package main import ( "image/color" "machine" "time" ) const
NUM_PIXELS = 10 func main() { neo := NewNeopixelDriver(machine.GPIO{machine.NEOPIXELS}, NUM_PIXELS) n := 0 var pows = []uint8{255, 127, 63, 31, 15, 7, 3, 1, 0, 0} for { for i := 0; i < NUM_PIXELS; i++ { pow := pows[NUM_PIXELS-1-i] neo.SetPixel((n+i)%NUM_PIXELS, color.RGBA{R: pow, G: pow, B: pow}) }
DEMO ͕ʔɾɾ͕ʔʂʂ
໌Δ͞ʹԠͯ͡LEDΛ౮͢ package main import ( "image/color" "machine" "time" ) const
NUM_PIXELS = 10 func main() { machine.InitADC() sensor := machine.ADC{machine.A8} sensor.Configure() neo := NewNeopixelDriver(machine.GPIO{machine.NEOPIXELS}, NUM_PIXELS) for { for i := 0; i < NUM_PIXELS; i++ { pow := uint8(sensor.Get() / 256) neo.SetPixel(i, color.RGBA{R: pow, G: pow, B: pow})
DEMO
GoͬΆ͘ॻ͘ package main import ( "image/color" "machine" "time" ) const
NUM_PIXELS = 10
Ϙλϯࢹؔ func observer(event chan int) { btnA := machine.GPIO{machine.BUTTONA} btnA.Configure(machine.GPIOConfig{Mode:
machine.GPIO_INPUT_PULLDOWN}) btnB := machine.GPIO{machine.BUTTONB} btnB.Configure(machine.GPIOConfig{Mode: machine.GPIO_INPUT_PULLDOWN}) next := make([]bool, 2) prev := make([]bool, 2) prev[0], prev[1] = btnA.Get(), btnB.Get() for { time.Sleep(20 * time.Millisecond) next[0], next[1] = btnA.Get(), btnB.Get() for i := range next { if !prev[i] && next[i] { event <- i } } prev[0], prev[1] = next[0], next[1] } }
ϨϯμϦϯάؔ func renderer(ch chan *NeopixelDriver) { for { v :=
<-ch v.Show() } }
mainؔ func main() { events := make(chan int) go observer(events)
// ϘλϯࢹλεΫىಈ ɹneo := NewNeopixelDriver(machine.GPIO{machine.NEOPIXELS}, NUM_PIXELS) ch := make(chan *NeopixelDriver) go renderer(ch) // ϨϯμϥʔλεΫىಈ prev, next := 0, 0 neo.SetPixel(prev, color.RGBA{R: 10, G: 10, B: 10}) ch <- neo // ϨϯμϦϯάΛൃՐ for { select { case v := <-events: switch v { case 0: println("buttonA pressed") next++ if next >= NUM_PIXELS {
DEMO
·ͱΊ ฒߦॲཧ͕ॻ͖͍͢ʂ ݱঢ়ͷϨΠϠ֦ுखஈ ΠϯϥΠΞηϯϒϥ SVCall CGOαϙʔτߴϨϕϧαϙʔτ͕ෆ TinyGoͷϏϧυͰgo-mod·ͩ͑ͳ͍ ͕ɺdockerͱͷซ༻ͰͳΜͱ͔ͳΔ
ࠓޙͷൃల
׆ൃͳ׆ಈ BLEϥΠϒϥϦରԠ CGOαϙʔτ ՃηϯαͳͲͷυϥΠόʔ ESPγϦʔζͷαϙʔτ
ݱঢ়ͷωοτػೳ ҎԼͷΞΠςϜΛ͏͜ͱͰ࣮ݱՄೳ ESP8266 or ESP32 ͱ͍͏֨҆Wi-Fiػೳ͖ϚΠίϯ driversʹ͋Δ ࠓճͷλʔήοτΛESPϞδϡʔϧͱUARTͱ͍͏̎৴߸ Λͭͳ͙ TCP/UDP௨৴͕ՄೳʹͳΔ
ESP8266/ESP32 AT Command set for WiFi/TCP/UDP
ࠓޙͷωοτଓ LINE ThingsͷΑ͏ʹࠓ୭͕࣋ͭεϚϗΛ BLEtoΠϯλʔωοτͷϋϒʹͪ͠Ό͏ΞΠσΟΞ LoRaɺNB-IoTϞδϡʔϧ΅ͪ΅ͪೖखՄೳʹͳΓͭͭ ͋Γ·͢
WebAssemblyग़ྗ ຊՈΑΓޮతͳՌΛग़ͤΔ syscall/jsαϙʔτͳͲ࢝·ͬͯΔ
IoT։ൃͲΜͲΜۙʹ ͳ͖ͬͯͯ·͢Αʂ
͓ΘΓ