mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-09 17:33:43 +00:00
Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ce0ce03cfe | |||
| 5bd814c9de | |||
| 41b7f06d05 | |||
| 749b2f6726 | |||
| 71c77d4b53 | |||
| 1f54c7dd23 | |||
| d0163c67f5 | |||
| 80cd4d9dac | |||
| 594ba1dab2 | |||
| 625c565c0b | |||
| b64d4ec3a0 | |||
| 171366698b |
@@ -1,3 +1,37 @@
|
|||||||
|
0.22.0
|
||||||
|
---
|
||||||
|
- **new devices**
|
||||||
|
- epd: add waveshare 2.9in (v1)
|
||||||
|
- makeybutton: add driver for MakeyMakey-like button
|
||||||
|
|
||||||
|
- **enhancements**
|
||||||
|
- **rtl8720dn**
|
||||||
|
- add UDP close function
|
||||||
|
- improve error handling
|
||||||
|
- **net/http**
|
||||||
|
- improve header parsing
|
||||||
|
- add last-will-and-testament to MQTT
|
||||||
|
- **net/mqtt**
|
||||||
|
- adds keepalive pinging, disconnect, and graceful goroutine cleanup
|
||||||
|
- support for cookies when https
|
||||||
|
- add support for retained messsages
|
||||||
|
|
||||||
|
- **bugfixes**
|
||||||
|
- irremote: Fix irremote reporting incorrect NEC addresses and command codes (#422)
|
||||||
|
- net/http: Fix http.Get() with port specification
|
||||||
|
|
||||||
|
- **build**
|
||||||
|
- Makefile recursively finds unit-tests
|
||||||
|
- switching to GHA
|
||||||
|
|
||||||
|
- **updates**
|
||||||
|
- update tinyfont to v0.3.0
|
||||||
|
- update tinyfs to v0.2.0
|
||||||
|
|
||||||
|
- **examples**
|
||||||
|
- rtl8720dn: add ./examples/rtl8720dn/version
|
||||||
|
|
||||||
|
|
||||||
0.21.0
|
0.21.0
|
||||||
---
|
---
|
||||||
- **new devices**
|
- **new devices**
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ func main() {
|
|||||||
|
|
||||||
## Currently supported devices
|
## Currently supported devices
|
||||||
|
|
||||||
The following 81 devices are supported.
|
The following 82 devices are supported.
|
||||||
|
|
||||||
| Device Name | Interface Type |
|
| Device Name | Interface Type |
|
||||||
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------|
|
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------|
|
||||||
@@ -137,6 +137,7 @@ The following 81 devices are supported.
|
|||||||
| [VL53L1X time-of-flight distance sensor](https://www.st.com/resource/en/datasheet/vl53l1x.pdf) | I2C |
|
| [VL53L1X time-of-flight distance sensor](https://www.st.com/resource/en/datasheet/vl53l1x.pdf) | I2C |
|
||||||
| [Waveshare 2.13" (B & C) e-paper display](https://www.waveshare.com/w/upload/d/d3/2.13inch-e-paper-b-Specification.pdf) | SPI |
|
| [Waveshare 2.13" (B & C) e-paper display](https://www.waveshare.com/w/upload/d/d3/2.13inch-e-paper-b-Specification.pdf) | SPI |
|
||||||
| [Waveshare 2.13" e-paper display](https://www.waveshare.com/w/upload/e/e6/2.13inch_e-Paper_Datasheet.pdf) | SPI |
|
| [Waveshare 2.13" e-paper display](https://www.waveshare.com/w/upload/e/e6/2.13inch_e-Paper_Datasheet.pdf) | SPI |
|
||||||
|
| [Waveshare 2.9" e-paper display (V1)](https://www.waveshare.com/w/upload/e/e6/2.9inch_e-Paper_Datasheet.pdf) | SPI |
|
||||||
| [Waveshare 4.2" e-paper B/W display](https://www.waveshare.com/w/upload/6/6a/4.2inch-e-paper-specification.pdf) | SPI |
|
| [Waveshare 4.2" e-paper B/W display](https://www.waveshare.com/w/upload/6/6a/4.2inch-e-paper-specification.pdf) | SPI |
|
||||||
| [WS2812 RGB LED](https://cdn-shop.adafruit.com/datasheets/WS2812.pdf) | GPIO |
|
| [WS2812 RGB LED](https://cdn-shop.adafruit.com/datasheets/WS2812.pdf) | GPIO |
|
||||||
| [XPT2046 touch controller](http://grobotronics.com/images/datasheets/xpt2046-datasheet.pdf) | GPIO |
|
| [XPT2046 touch controller](http://grobotronics.com/images/datasheets/xpt2046-datasheet.pdf) | GPIO |
|
||||||
|
|||||||
@@ -15,8 +15,8 @@ var (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
led.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
led.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||||
button.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
|
|
||||||
key = makeybutton.NewButton(button)
|
key = makeybutton.NewButton(button)
|
||||||
|
key.Configure()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
switch key.Get() {
|
switch key.Get() {
|
||||||
@@ -25,6 +25,6 @@ func main() {
|
|||||||
case makeybutton.Released:
|
case makeybutton.Released:
|
||||||
led.Low()
|
led.Low()
|
||||||
}
|
}
|
||||||
time.Sleep(30 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,84 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"machine"
|
||||||
|
|
||||||
|
"image/color"
|
||||||
|
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"tinygo.org/x/drivers/waveshare-epd/epd2in9"
|
||||||
|
)
|
||||||
|
|
||||||
|
var display epd2in9.Device
|
||||||
|
|
||||||
|
const (
|
||||||
|
width = 128
|
||||||
|
height = 296
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
machine.SPI0.Configure(machine.SPIConfig{
|
||||||
|
Frequency: 8000000,
|
||||||
|
Mode: 0,
|
||||||
|
})
|
||||||
|
|
||||||
|
display = epd2in9.New(machine.SPI0, machine.GPIO2, machine.GPIO3, machine.GPIO4, machine.GPIO5)
|
||||||
|
display.Configure(epd2in9.Config{
|
||||||
|
Width: width,
|
||||||
|
LogicalWidth: width,
|
||||||
|
Height: height,
|
||||||
|
})
|
||||||
|
|
||||||
|
black := color.RGBA{1, 1, 1, 255}
|
||||||
|
white := color.RGBA{0, 0, 0, 255}
|
||||||
|
|
||||||
|
display.ClearBuffer()
|
||||||
|
println("Clear the display")
|
||||||
|
display.ClearDisplay()
|
||||||
|
display.WaitUntilIdle()
|
||||||
|
println("Waiting for 2 seconds")
|
||||||
|
time.Sleep(2 * time.Second)
|
||||||
|
|
||||||
|
// Show a checkered board
|
||||||
|
for i := int16(0); i < width/8; i++ {
|
||||||
|
for j := int16(0); j < height/8; j++ {
|
||||||
|
if (i+j)%2 == 0 {
|
||||||
|
showRect(i*8, j*8, 8, 8, black)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
println("Show checkered board")
|
||||||
|
display.Display()
|
||||||
|
display.WaitUntilIdle()
|
||||||
|
println("Waiting for 2 seconds")
|
||||||
|
time.Sleep(2 * time.Second)
|
||||||
|
|
||||||
|
println("Set partial lut")
|
||||||
|
display.SetLUT(false) // partial updates (faster, but with some ghosting)
|
||||||
|
println("Show smaller striped area")
|
||||||
|
for i := int16(40); i < 88; i++ {
|
||||||
|
for j := int16(83); j < 166; j++ {
|
||||||
|
if (i+j)%4 == 0 || (i+j)%4 == 1 {
|
||||||
|
display.SetPixel(i, j, black)
|
||||||
|
} else {
|
||||||
|
display.SetPixel(i, j, white)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
display.Display()
|
||||||
|
display.WaitUntilIdle()
|
||||||
|
|
||||||
|
display.DeepSleep()
|
||||||
|
|
||||||
|
println("You could remove power now")
|
||||||
|
}
|
||||||
|
|
||||||
|
func showRect(x int16, y int16, w int16, h int16, c color.RGBA) {
|
||||||
|
for i := x; i < x+w; i++ {
|
||||||
|
for j := y; j < y+h; j++ {
|
||||||
|
display.SetPixel(i, j, c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,8 +5,8 @@ go 1.15
|
|||||||
require (
|
require (
|
||||||
github.com/eclipse/paho.mqtt.golang v1.2.0
|
github.com/eclipse/paho.mqtt.golang v1.2.0
|
||||||
github.com/frankban/quicktest v1.10.2
|
github.com/frankban/quicktest v1.10.2
|
||||||
golang.org/x/net v0.0.0-20210614182718-04defd469f4e // indirect
|
golang.org/x/net v0.0.0-20210614182718-04defd469f4e
|
||||||
tinygo.org/x/tinyfont v0.2.1
|
tinygo.org/x/tinyfont v0.3.0
|
||||||
tinygo.org/x/tinyfs v0.1.0
|
tinygo.org/x/tinyfs v0.2.0
|
||||||
tinygo.org/x/tinyterm v0.1.0
|
tinygo.org/x/tinyterm v0.1.0
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -5,12 +5,15 @@ github.com/frankban/quicktest v1.10.2 h1:19ARM85nVi4xH7xPXuc5eM/udya5ieh7b/Sv+d8
|
|||||||
github.com/frankban/quicktest v1.10.2/go.mod h1:K+q6oSqb0W0Ininfk863uOk1lMy69l/P6txr3mVT54s=
|
github.com/frankban/quicktest v1.10.2/go.mod h1:K+q6oSqb0W0Ininfk863uOk1lMy69l/P6txr3mVT54s=
|
||||||
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
|
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
|
||||||
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
|
github.com/hajimehoshi/go-jisx0208 v1.0.0/go.mod h1:yYxEStHL7lt9uL+AbdWgW9gBumwieDoZCiB1f/0X0as=
|
||||||
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
|
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
|
||||||
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||||
|
github.com/sago35/go-bdf v0.0.0-20200313142241-6c17821c91c4/go.mod h1:rOebXGuMLsXhZAC6mF/TjxONsm45498ZyzVhel++6KM=
|
||||||
github.com/valyala/fastjson v1.6.3/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY=
|
github.com/valyala/fastjson v1.6.3/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY=
|
||||||
|
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
|
||||||
golang.org/x/net v0.0.0-20210614182718-04defd469f4e h1:XpT3nA5TvE525Ne3hInMh6+GETgn27Zfm9dxsThnX2Q=
|
golang.org/x/net v0.0.0-20210614182718-04defd469f4e h1:XpT3nA5TvE525Ne3hInMh6+GETgn27Zfm9dxsThnX2Q=
|
||||||
golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
@@ -24,9 +27,12 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T
|
|||||||
tinygo.org/x/drivers v0.14.0/go.mod h1:uT2svMq3EpBZpKkGO+NQHjxjGf1f42ra4OnMMwQL2aI=
|
tinygo.org/x/drivers v0.14.0/go.mod h1:uT2svMq3EpBZpKkGO+NQHjxjGf1f42ra4OnMMwQL2aI=
|
||||||
tinygo.org/x/drivers v0.15.1/go.mod h1:uT2svMq3EpBZpKkGO+NQHjxjGf1f42ra4OnMMwQL2aI=
|
tinygo.org/x/drivers v0.15.1/go.mod h1:uT2svMq3EpBZpKkGO+NQHjxjGf1f42ra4OnMMwQL2aI=
|
||||||
tinygo.org/x/drivers v0.16.0/go.mod h1:uT2svMq3EpBZpKkGO+NQHjxjGf1f42ra4OnMMwQL2aI=
|
tinygo.org/x/drivers v0.16.0/go.mod h1:uT2svMq3EpBZpKkGO+NQHjxjGf1f42ra4OnMMwQL2aI=
|
||||||
tinygo.org/x/tinyfont v0.2.1 h1:FAaemBzw8wsfhAtG6fWW+QjyWw/K8YqEeiWo4N1pv4o=
|
tinygo.org/x/drivers v0.19.0/go.mod h1:uJD/l1qWzxzLx+vcxaW0eY464N5RAgFi1zTVzASFdqI=
|
||||||
tinygo.org/x/tinyfont v0.2.1/go.mod h1:eLqnYSrFRjt5STxWaMeOWJTzrKhXqpWw7nU3bPfKOAM=
|
tinygo.org/x/tinyfont v0.2.1/go.mod h1:eLqnYSrFRjt5STxWaMeOWJTzrKhXqpWw7nU3bPfKOAM=
|
||||||
tinygo.org/x/tinyfs v0.1.0 h1:yx1Tq9L60rpCm6HURo45x+Tnag+O9RGSbQfgeCb6XYU=
|
tinygo.org/x/tinyfont v0.3.0 h1:HIRLQoI3oc+2CMhPcfv+Ig88EcTImE/5npjqOnMD4lM=
|
||||||
|
tinygo.org/x/tinyfont v0.3.0/go.mod h1:+TV5q0KpwSGRWnN+ITijsIhrWYJkoUCp9MYELjKpAXk=
|
||||||
tinygo.org/x/tinyfs v0.1.0/go.mod h1:ysc8Y92iHfhTXeyEM9+c7zviUQ4fN9UCFgSOFfMWv20=
|
tinygo.org/x/tinyfs v0.1.0/go.mod h1:ysc8Y92iHfhTXeyEM9+c7zviUQ4fN9UCFgSOFfMWv20=
|
||||||
|
tinygo.org/x/tinyfs v0.2.0 h1:M0lwZC/dEGFt16XYN5GTQsif/qCkAN2qUVNxELVD1xg=
|
||||||
|
tinygo.org/x/tinyfs v0.2.0/go.mod h1:6ZHYdvB3sFYeMB3ypmXZCNEnFwceKc61ADYTYHpep1E=
|
||||||
tinygo.org/x/tinyterm v0.1.0 h1:80i+j+KWoxCFa/Xfp6pWbh79x+8zUdMXC1vaKj2QhkY=
|
tinygo.org/x/tinyterm v0.1.0 h1:80i+j+KWoxCFa/Xfp6pWbh79x+8zUdMXC1vaKj2QhkY=
|
||||||
tinygo.org/x/tinyterm v0.1.0/go.mod h1:/DDhNnGwNF2/tNgHywvyZuCGnbH3ov49Z/6e8LPLRR4=
|
tinygo.org/x/tinyterm v0.1.0/go.mod h1:/DDhNnGwNF2/tNgHywvyZuCGnbH3ov49Z/6e8LPLRR4=
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package makeybutton
|
package makeybutton
|
||||||
|
|
||||||
const bufferSize = 10
|
const bufferSize = 6
|
||||||
|
|
||||||
// Buffer is a buffer to keep track of the most recent readings for a button.
|
// Buffer is a buffer to keep track of the most recent readings for a button.
|
||||||
type Buffer struct {
|
type Buffer struct {
|
||||||
|
|||||||
+17
-5
@@ -6,7 +6,10 @@
|
|||||||
//
|
//
|
||||||
package makeybutton
|
package makeybutton
|
||||||
|
|
||||||
import "machine"
|
import (
|
||||||
|
"machine"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
// ButtonState represents the state of a MakeyButton.
|
// ButtonState represents the state of a MakeyButton.
|
||||||
type ButtonState int
|
type ButtonState int
|
||||||
@@ -36,9 +39,6 @@ type Button struct {
|
|||||||
|
|
||||||
// NewButton creates a new Button.
|
// NewButton creates a new Button.
|
||||||
func NewButton(pin machine.Pin) *Button {
|
func NewButton(pin machine.Pin) *Button {
|
||||||
pin.Configure(machine.PinConfig{Mode: machine.PinInput})
|
|
||||||
pin.Set(false)
|
|
||||||
|
|
||||||
return &Button{
|
return &Button{
|
||||||
pin: pin,
|
pin: pin,
|
||||||
state: NeverPressed,
|
state: NeverPressed,
|
||||||
@@ -47,6 +47,18 @@ func NewButton(pin machine.Pin) *Button {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Configure configures the Makey Button pin to have the correct settings to detect touches.
|
||||||
|
func (b *Button) Configure() error {
|
||||||
|
// Note that we have to first turn on the pullup, and then turn off the pullup,
|
||||||
|
// in order for the pin to be properly floating.
|
||||||
|
b.pin.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
|
||||||
|
time.Sleep(10 * time.Millisecond)
|
||||||
|
b.pin.Configure(machine.PinConfig{Mode: machine.PinInput})
|
||||||
|
b.pin.Set(false)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Get returns a ButtonEvent based on the most recent state of the button,
|
// Get returns a ButtonEvent based on the most recent state of the button,
|
||||||
// and if it has changed by being pressed or released.
|
// and if it has changed by being pressed or released.
|
||||||
func (b *Button) Get() ButtonEvent {
|
func (b *Button) Get() ButtonEvent {
|
||||||
@@ -61,7 +73,7 @@ func (b *Button) Get() ButtonEvent {
|
|||||||
b.readings.Put(pressed)
|
b.readings.Put(pressed)
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case pressed && avg > 0:
|
case pressed && avg > -1*bufferSize+2:
|
||||||
if b.state == Press {
|
if b.state == Press {
|
||||||
return NotChanged
|
return NotChanged
|
||||||
}
|
}
|
||||||
|
|||||||
+25
-10
@@ -20,6 +20,11 @@ func SetBuf(b []byte) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Do(req *Request) (*Response, error) {
|
func (c *Client) Do(req *Request) (*Response, error) {
|
||||||
|
if c.Jar != nil {
|
||||||
|
for _, cookie := range c.Jar.Cookies(req.URL) {
|
||||||
|
req.AddCookie(cookie)
|
||||||
|
}
|
||||||
|
}
|
||||||
switch req.URL.Scheme {
|
switch req.URL.Scheme {
|
||||||
case "http":
|
case "http":
|
||||||
return c.doHTTP(req)
|
return c.doHTTP(req)
|
||||||
@@ -31,15 +36,17 @@ func (c *Client) Do(req *Request) (*Response, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) doHTTP(req *Request) (*Response, error) {
|
func (c *Client) doHTTP(req *Request) (*Response, error) {
|
||||||
if c.Jar != nil {
|
|
||||||
for _, cookie := range c.Jar.Cookies(req.URL) {
|
|
||||||
req.AddCookie(cookie)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// make TCP connection
|
// make TCP connection
|
||||||
ip := net.ParseIP(req.URL.Host)
|
ip := net.ParseIP(req.URL.Hostname())
|
||||||
raddr := &net.TCPAddr{IP: ip, Port: 80}
|
port := 80
|
||||||
|
if req.URL.Port() != "" {
|
||||||
|
p, err := strconv.ParseUint(req.URL.Port(), 0, 64)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
port = int(p)
|
||||||
|
}
|
||||||
|
raddr := &net.TCPAddr{IP: ip, Port: port}
|
||||||
laddr := &net.TCPAddr{Port: 8080}
|
laddr := &net.TCPAddr{Port: 8080}
|
||||||
|
|
||||||
conn, err := net.DialTCP("tcp", laddr, raddr)
|
conn, err := net.DialTCP("tcp", laddr, raddr)
|
||||||
@@ -178,12 +185,17 @@ func (c *Client) doResp(conn net.Conn, req *Request) (*Response, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
println("Read error: " + err.Error())
|
println("Read error: " + err.Error())
|
||||||
} else {
|
} else {
|
||||||
idx := bytes.Index(buf[ofs:ofs+n], []byte("\r\n\r\n"))
|
// Take care of the case where "\r\n\r\n" is on the boundary of a buffer
|
||||||
|
start := ofs
|
||||||
|
if start > 3 {
|
||||||
|
start -= 3
|
||||||
|
}
|
||||||
|
idx := bytes.Index(buf[start:ofs+n], []byte("\r\n\r\n"))
|
||||||
if idx == -1 {
|
if idx == -1 {
|
||||||
ofs += n
|
ofs += n
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
idx += ofs + 4
|
idx += start + 4
|
||||||
|
|
||||||
scanner = bufio.NewScanner(bytes.NewReader(buf[0 : ofs+n]))
|
scanner = bufio.NewScanner(bytes.NewReader(buf[0 : ofs+n]))
|
||||||
if resp.Status == "" && scanner.Scan() {
|
if resp.Status == "" && scanner.Scan() {
|
||||||
@@ -261,6 +273,9 @@ func (c *Client) doResp(conn net.Conn, req *Request) (*Response, error) {
|
|||||||
return nil, fmt.Errorf("slice out of range : use http.SetBuf() to change the allocation to %d bytes or more", end)
|
return nil, fmt.Errorf("slice out of range : use http.SetBuf() to change the allocation to %d bytes or more", end)
|
||||||
}
|
}
|
||||||
n, err := conn.Read(buf[ofs : ofs+0x400])
|
n, err := conn.Read(buf[ofs : ofs+0x400])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
if n == 0 {
|
if n == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|||||||
+116
-30
@@ -5,6 +5,7 @@ package mqtt
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/eclipse/paho.mqtt.golang/packets"
|
"github.com/eclipse/paho.mqtt.golang/packets"
|
||||||
@@ -19,20 +20,32 @@ import (
|
|||||||
func NewClient(o *ClientOptions) Client {
|
func NewClient(o *ClientOptions) Client {
|
||||||
c := &mqttclient{opts: o, adaptor: o.Adaptor}
|
c := &mqttclient{opts: o, adaptor: o.Adaptor}
|
||||||
c.msgRouter, c.stopRouter = newRouter()
|
c.msgRouter, c.stopRouter = newRouter()
|
||||||
|
|
||||||
|
c.inboundPacketChan = make(chan packets.ControlPacket, 10)
|
||||||
|
c.stopInbound = make(chan struct{})
|
||||||
|
c.incomingPubChan = make(chan *packets.PublishPacket, 10)
|
||||||
|
// this launches a goroutine, so only call once per client:
|
||||||
|
c.msgRouter.matchAndDispatch(c.incomingPubChan, c.opts.Order, c)
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
type mqttclient struct {
|
type mqttclient struct {
|
||||||
adaptor net.Adapter
|
adaptor net.Adapter
|
||||||
conn net.Conn
|
conn net.Conn
|
||||||
connected bool
|
connected bool
|
||||||
opts *ClientOptions
|
opts *ClientOptions
|
||||||
mid uint16
|
mid uint16
|
||||||
inbound chan packets.ControlPacket
|
inboundPacketChan chan packets.ControlPacket
|
||||||
stop chan struct{}
|
stopInbound chan struct{}
|
||||||
msgRouter *router
|
msgRouter *router
|
||||||
stopRouter chan bool
|
stopRouter chan bool
|
||||||
incomingPubChan chan *packets.PublishPacket
|
incomingPubChan chan *packets.PublishPacket
|
||||||
|
// stats for keepalive
|
||||||
|
lastReceive time.Time
|
||||||
|
lastSend time.Time
|
||||||
|
// keep track of routines and signal a shutdown
|
||||||
|
workers sync.WaitGroup
|
||||||
|
shutdown bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddRoute allows you to add a handler for messages on a specific topic
|
// AddRoute allows you to add a handler for messages on a specific topic
|
||||||
@@ -56,6 +69,9 @@ func (c *mqttclient) IsConnectionOpen() bool {
|
|||||||
|
|
||||||
// Connect will create a connection to the message broker.
|
// Connect will create a connection to the message broker.
|
||||||
func (c *mqttclient) Connect() Token {
|
func (c *mqttclient) Connect() Token {
|
||||||
|
if c.IsConnected() {
|
||||||
|
return &mqtttoken{}
|
||||||
|
}
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
// make connection
|
// make connection
|
||||||
@@ -77,10 +93,6 @@ func (c *mqttclient) Connect() Token {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.mid = 1
|
c.mid = 1
|
||||||
c.inbound = make(chan packets.ControlPacket, 10)
|
|
||||||
c.stop = make(chan struct{})
|
|
||||||
c.incomingPubChan = make(chan *packets.PublishPacket, 10)
|
|
||||||
c.msgRouter.matchAndDispatch(c.incomingPubChan, c.opts.Order, c)
|
|
||||||
|
|
||||||
// send the MQTT connect message
|
// send the MQTT connect message
|
||||||
connectPkt := packets.NewControlPacket(packets.Connect).(*packets.ConnectPacket)
|
connectPkt := packets.NewControlPacket(packets.Connect).(*packets.ConnectPacket)
|
||||||
@@ -98,7 +110,7 @@ func (c *mqttclient) Connect() Token {
|
|||||||
connectPkt.ClientIdentifier = c.opts.ClientID
|
connectPkt.ClientIdentifier = c.opts.ClientID
|
||||||
connectPkt.ProtocolVersion = byte(c.opts.ProtocolVersion)
|
connectPkt.ProtocolVersion = byte(c.opts.ProtocolVersion)
|
||||||
connectPkt.ProtocolName = "MQTT"
|
connectPkt.ProtocolName = "MQTT"
|
||||||
connectPkt.Keepalive = 60
|
connectPkt.Keepalive = uint16(c.opts.KeepAlive)
|
||||||
|
|
||||||
connectPkt.WillFlag = c.opts.WillEnabled
|
connectPkt.WillFlag = c.opts.WillEnabled
|
||||||
connectPkt.WillTopic = c.opts.WillTopic
|
connectPkt.WillTopic = c.opts.WillTopic
|
||||||
@@ -110,6 +122,7 @@ func (c *mqttclient) Connect() Token {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return &mqtttoken{err: err}
|
return &mqtttoken{err: err}
|
||||||
}
|
}
|
||||||
|
c.lastSend = time.Now()
|
||||||
|
|
||||||
// TODO: handle timeout as ReadPacket blocks until it gets a packet.
|
// TODO: handle timeout as ReadPacket blocks until it gets a packet.
|
||||||
// CONNECT response.
|
// CONNECT response.
|
||||||
@@ -127,20 +140,36 @@ func (c *mqttclient) Connect() Token {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
go readMessages(c)
|
|
||||||
go processInbound(c)
|
go processInbound(c)
|
||||||
|
go readMessages(c)
|
||||||
|
go keepAlive(c)
|
||||||
|
|
||||||
return &mqtttoken{}
|
return &mqtttoken{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Disconnect will end the connection with the server, but not before waiting
|
// Disconnect will end the connection with the server, but not before waiting
|
||||||
// the specified number of milliseconds to wait for existing work to be
|
// the specified number of milliseconds to wait for existing work to be
|
||||||
// completed.
|
// completed. Blocks until disconnected.
|
||||||
func (c *mqttclient) Disconnect(quiesce uint) {
|
func (c *mqttclient) Disconnect(quiesce uint) {
|
||||||
c.conn.Close()
|
c.shutdownRoutines()
|
||||||
|
// block until all done
|
||||||
|
for c.connected {
|
||||||
|
time.Sleep(time.Millisecond * 10)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// shutdownRoutines will disconnect and shut down all processes. If you want to trigger a
|
||||||
|
// disconnect internally, make sure you call this instead of Disconnect() to avoid deadlocks
|
||||||
|
func (c *mqttclient) shutdownRoutines() {
|
||||||
|
if c.shutdown {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.shutdown = true
|
||||||
|
c.conn.Close()
|
||||||
|
c.stopInbound <- struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
// Publish will publish a message with the specified QoS and content
|
// Publish will publish a message with the specified QoS and content
|
||||||
// to the specified topic.
|
// to the specified topic.
|
||||||
// Returns a token to track delivery of the message to the broker
|
// Returns a token to track delivery of the message to the broker
|
||||||
@@ -153,6 +182,7 @@ func (c *mqttclient) Publish(topic string, qos byte, retained bool, payload inte
|
|||||||
pub.Qos = qos
|
pub.Qos = qos
|
||||||
pub.TopicName = topic
|
pub.TopicName = topic
|
||||||
pub.Retain = retained
|
pub.Retain = retained
|
||||||
|
|
||||||
switch payload.(type) {
|
switch payload.(type) {
|
||||||
case string:
|
case string:
|
||||||
pub.Payload = []byte(payload.(string))
|
pub.Payload = []byte(payload.(string))
|
||||||
@@ -168,6 +198,8 @@ func (c *mqttclient) Publish(topic string, qos byte, retained bool, payload inte
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return &mqtttoken{err: err}
|
return &mqtttoken{err: err}
|
||||||
}
|
}
|
||||||
|
// update this for every control message that is sent successfully, for keepalive
|
||||||
|
c.lastSend = time.Now()
|
||||||
|
|
||||||
return &mqtttoken{}
|
return &mqtttoken{}
|
||||||
}
|
}
|
||||||
@@ -195,6 +227,7 @@ func (c *mqttclient) Subscribe(topic string, qos byte, callback MessageHandler)
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return &mqtttoken{err: err}
|
return &mqtttoken{err: err}
|
||||||
}
|
}
|
||||||
|
c.lastSend = time.Now()
|
||||||
|
|
||||||
return &mqtttoken{}
|
return &mqtttoken{}
|
||||||
}
|
}
|
||||||
@@ -220,12 +253,13 @@ func (c *mqttclient) OptionsReader() ClientOptionsReader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func processInbound(c *mqttclient) {
|
func processInbound(c *mqttclient) {
|
||||||
|
PROCESS:
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case msg := <-c.inbound:
|
case msg := <-c.inboundPacketChan:
|
||||||
switch m := msg.(type) {
|
switch m := msg.(type) {
|
||||||
case *packets.PingrespPacket:
|
case *packets.PingrespPacket:
|
||||||
// TODO: handle this
|
// println("pong")
|
||||||
case *packets.SubackPacket:
|
case *packets.SubackPacket:
|
||||||
// TODO: handle this
|
// TODO: handle this
|
||||||
case *packets.UnsubackPacket:
|
case *packets.UnsubackPacket:
|
||||||
@@ -242,33 +276,85 @@ func processInbound(c *mqttclient) {
|
|||||||
case *packets.PubcompPacket:
|
case *packets.PubcompPacket:
|
||||||
// TODO: handle this
|
// TODO: handle this
|
||||||
}
|
}
|
||||||
case <-c.stop:
|
case <-c.stopInbound:
|
||||||
return
|
break PROCESS
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// as this routine could be the last to finish (if a lot of messages are queued in the
|
||||||
|
// channel), it is the last to turn out the lights
|
||||||
|
|
||||||
|
c.workers.Wait()
|
||||||
|
c.connected = false
|
||||||
|
c.shutdown = false
|
||||||
}
|
}
|
||||||
|
|
||||||
// readMessages reads incoming messages off the wire.
|
// readMessages reads incoming messages off the wire.
|
||||||
// incoming messages are then send into inbound channel.
|
// incoming messages are then send into inbound buffered channel.
|
||||||
func readMessages(c *mqttclient) {
|
func readMessages(c *mqttclient) {
|
||||||
|
c.workers.Add(1)
|
||||||
|
defer c.workers.Done()
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
var cp packets.ControlPacket
|
var cp packets.ControlPacket
|
||||||
|
|
||||||
PROCESS:
|
for !c.shutdown {
|
||||||
for {
|
|
||||||
if cp, err = c.ReadPacket(); err != nil {
|
if cp, err = c.ReadPacket(); err != nil {
|
||||||
break PROCESS
|
c.shutdownRoutines()
|
||||||
|
return
|
||||||
}
|
}
|
||||||
if cp != nil {
|
if cp != nil {
|
||||||
c.inbound <- cp
|
c.inboundPacketChan <- cp
|
||||||
// TODO: Notify keepalive logic that we recently received a packet
|
// notify keepalive logic that we recently received a packet
|
||||||
|
c.lastReceive = time.Now()
|
||||||
}
|
}
|
||||||
|
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: handle if we received an error on read.
|
// keepAlive is a goroutine to handle sending ping requests according to the MQTT spec. If the keepalive time has
|
||||||
// If disconnect is in progress, swallow error and return
|
// been reached with no messages being sent, we will send a ping request and check back to see if we've
|
||||||
|
// had any activity by the timeout. If not, disconnect.
|
||||||
|
func keepAlive(c *mqttclient) {
|
||||||
|
c.workers.Add(1)
|
||||||
|
defer c.workers.Done()
|
||||||
|
|
||||||
|
var err error
|
||||||
|
var ping *packets.PingreqPacket
|
||||||
|
var timeout, pingsent time.Time
|
||||||
|
|
||||||
|
for !c.shutdown {
|
||||||
|
// As long as we haven't reached the keepalive value...
|
||||||
|
if time.Since(c.lastSend) < time.Duration(c.opts.KeepAlive)*time.Second {
|
||||||
|
// ...sleep and check shutdown status again
|
||||||
|
time.Sleep(time.Millisecond * 100)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// value has been reached, so send a ping request
|
||||||
|
ping = packets.NewControlPacket(packets.Pingreq).(*packets.PingreqPacket)
|
||||||
|
if err = ping.Write(c.conn); err != nil {
|
||||||
|
// if connection is lost, report disconnect
|
||||||
|
c.shutdownRoutines()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// println("ping")
|
||||||
|
|
||||||
|
c.lastSend = time.Now()
|
||||||
|
pingsent = time.Now()
|
||||||
|
timeout = pingsent.Add(c.opts.PingTimeout)
|
||||||
|
|
||||||
|
// as long as we are still connected and haven't received anything after the ping...
|
||||||
|
for !c.shutdown && c.lastReceive.Before(pingsent) {
|
||||||
|
// if the timeout has passed, disconnect
|
||||||
|
if time.Now().After(timeout) {
|
||||||
|
c.shutdownRoutines()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
time.Sleep(time.Millisecond * 100)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *mqttclient) ackFunc(packet *packets.PublishPacket) func() {
|
func (c *mqttclient) ackFunc(packet *packets.PublishPacket) func() {
|
||||||
|
|||||||
+18
-1
@@ -210,7 +210,7 @@ type ClientOptions struct {
|
|||||||
|
|
||||||
// NewClientOptions returns a new ClientOptions struct.
|
// NewClientOptions returns a new ClientOptions struct.
|
||||||
func NewClientOptions() *ClientOptions {
|
func NewClientOptions() *ClientOptions {
|
||||||
return &ClientOptions{Adaptor: net.ActiveDevice, ProtocolVersion: 4}
|
return &ClientOptions{Adaptor: net.ActiveDevice, ProtocolVersion: 4, KeepAlive: 60, PingTimeout: time.Second * 10}
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddBroker adds a broker URI to the list of brokers to be used. The format should be
|
// AddBroker adds a broker URI to the list of brokers to be used. The format should be
|
||||||
@@ -257,6 +257,23 @@ func (o *ClientOptions) SetPassword(p string) *ClientOptions {
|
|||||||
return o
|
return o
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetKeepAlive will set the amount of time (in seconds) that the client
|
||||||
|
// should wait before sending a PING request to the broker. This will
|
||||||
|
// allow the client to know that a connection has not been lost with the
|
||||||
|
// server.
|
||||||
|
func (o *ClientOptions) SetKeepAlive(k time.Duration) *ClientOptions {
|
||||||
|
o.KeepAlive = int64(k / time.Second)
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetPingTimeout will set the amount of time (in seconds) that the client
|
||||||
|
// will wait after sending a PING request to the broker, before deciding
|
||||||
|
// that the connection has been lost. Default is 10 seconds.
|
||||||
|
func (o *ClientOptions) SetPingTimeout(k time.Duration) *ClientOptions {
|
||||||
|
o.PingTimeout = k
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
// SetWill accepts a string will message to be set. When the client connects,
|
// SetWill accepts a string will message to be set. When the client connects,
|
||||||
// it will give this will message to the broker, which will then publish the
|
// it will give this will message to the broker, which will then publish the
|
||||||
// provided payload (the will) to any clients that are subscribed to the provided
|
// provided payload (the will) to any clients that are subscribed to the provided
|
||||||
|
|||||||
@@ -118,6 +118,9 @@ func (r *RTL8720DN) ConnectSSLSocket(addr, port string) error {
|
|||||||
if r.debug {
|
if r.debug {
|
||||||
fmt.Printf("ConnectSSLSocket(%q, %q)\r\n", addr, port)
|
fmt.Printf("ConnectSSLSocket(%q, %q)\r\n", addr, port)
|
||||||
}
|
}
|
||||||
|
if r.root_ca == nil {
|
||||||
|
return fmt.Errorf("root_ca is not set")
|
||||||
|
}
|
||||||
|
|
||||||
client, err := r.Rpc_wifi_ssl_client_create()
|
client, err := r.Rpc_wifi_ssl_client_create()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -224,7 +227,7 @@ func (r *RTL8720DN) DisconnectSocket() error {
|
|||||||
fmt.Printf("DisconnectSocket()\r\n")
|
fmt.Printf("DisconnectSocket()\r\n")
|
||||||
}
|
}
|
||||||
switch r.connectionType {
|
switch r.connectionType {
|
||||||
case ConnectionTypeTCP:
|
case ConnectionTypeTCP, ConnectionTypeUDP:
|
||||||
_, err := r.Rpc_lwip_close(r.socket)
|
_, err := r.Rpc_lwip_close(r.socket)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
+1
-1
@@ -2,4 +2,4 @@ package drivers
|
|||||||
|
|
||||||
// Version returns a user-readable string showing the version of the drivers package for support purposes.
|
// Version returns a user-readable string showing the version of the drivers package for support purposes.
|
||||||
// Update this value before release of new version of software.
|
// Update this value before release of new version of software.
|
||||||
const Version = "0.21.0"
|
const Version = "0.22.0"
|
||||||
|
|||||||
@@ -0,0 +1,291 @@
|
|||||||
|
// Package epd2in9 implements a driver for Waveshare 2.9in black and white e-paper device.
|
||||||
|
//
|
||||||
|
// Note: this is for the V1 device (using IL3820), the V2 device uses a different chipset.
|
||||||
|
//
|
||||||
|
// Datasheets:
|
||||||
|
// https://www.waveshare.com/w/upload/e/e6/2.9inch_e-Paper_Datasheet.pdf
|
||||||
|
// https://www.smart-prototyping.com/image/data/9_Modules/EinkDisplay/GDE029A1/IL3820.pdf
|
||||||
|
//
|
||||||
|
// This implementation is essentially a copy of the 2in13 driver with the correct LUTs and
|
||||||
|
// default size for the 2.9in device.
|
||||||
|
//
|
||||||
|
package epd2in9 // import "tinygo.org/x/drivers/waveshare-epd/epd2in9"
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image/color"
|
||||||
|
"machine"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"tinygo.org/x/drivers"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
Width int16 // Width is the display resolution
|
||||||
|
Height int16
|
||||||
|
LogicalWidth int16 // LogicalWidth must be a multiple of 8 and same size or bigger than Width
|
||||||
|
Rotation Rotation // Rotation is clock-wise
|
||||||
|
}
|
||||||
|
|
||||||
|
type Device struct {
|
||||||
|
bus drivers.SPI
|
||||||
|
cs machine.Pin
|
||||||
|
dc machine.Pin
|
||||||
|
rst machine.Pin
|
||||||
|
busy machine.Pin
|
||||||
|
logicalWidth int16
|
||||||
|
width int16
|
||||||
|
height int16
|
||||||
|
buffer []uint8
|
||||||
|
bufferLength uint32
|
||||||
|
rotation Rotation
|
||||||
|
}
|
||||||
|
|
||||||
|
type Rotation uint8
|
||||||
|
|
||||||
|
// Look up table for full updates
|
||||||
|
var lutFullUpdate = [30]uint8{
|
||||||
|
0x50, 0xAA, 0x55, 0xAA, 0x11, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0xFF, 0xFF, 0x1F, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Look up table for partial updates, faster but there will be some ghosting
|
||||||
|
var lutPartialUpdate = [30]uint8{
|
||||||
|
0x10, 0x18, 0x18, 0x08, 0x18, 0x18,
|
||||||
|
0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x13, 0x14, 0x44, 0x12,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
}
|
||||||
|
|
||||||
|
// New returns a new epd2in9 driver. Pass in a fully configured SPI bus.
|
||||||
|
func New(bus drivers.SPI, csPin, dcPin, rstPin, busyPin machine.Pin) Device {
|
||||||
|
csPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||||
|
dcPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||||
|
rstPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||||
|
busyPin.Configure(machine.PinConfig{Mode: machine.PinInput})
|
||||||
|
return Device{
|
||||||
|
bus: bus,
|
||||||
|
cs: csPin,
|
||||||
|
dc: dcPin,
|
||||||
|
rst: rstPin,
|
||||||
|
busy: busyPin,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Configure sets up the device.
|
||||||
|
func (d *Device) Configure(cfg Config) {
|
||||||
|
if cfg.LogicalWidth != 0 {
|
||||||
|
d.logicalWidth = cfg.LogicalWidth
|
||||||
|
} else {
|
||||||
|
d.logicalWidth = 128
|
||||||
|
}
|
||||||
|
if cfg.Width != 0 {
|
||||||
|
d.width = cfg.Width
|
||||||
|
} else {
|
||||||
|
d.width = 128
|
||||||
|
}
|
||||||
|
if cfg.Height != 0 {
|
||||||
|
d.height = cfg.Height
|
||||||
|
} else {
|
||||||
|
d.height = 296
|
||||||
|
}
|
||||||
|
d.rotation = cfg.Rotation
|
||||||
|
d.bufferLength = (uint32(d.logicalWidth) * uint32(d.height)) / 8
|
||||||
|
d.buffer = make([]uint8, d.bufferLength)
|
||||||
|
for i := uint32(0); i < d.bufferLength; i++ {
|
||||||
|
d.buffer[i] = 0xFF
|
||||||
|
}
|
||||||
|
|
||||||
|
d.cs.Low()
|
||||||
|
d.dc.Low()
|
||||||
|
d.rst.Low()
|
||||||
|
|
||||||
|
d.Reset()
|
||||||
|
|
||||||
|
d.SendCommand(DRIVER_OUTPUT_CONTROL)
|
||||||
|
d.SendData(uint8((d.height - 1) & 0xFF))
|
||||||
|
d.SendData(uint8(((d.height - 1) >> 8) & 0xFF))
|
||||||
|
d.SendData(0x00) // GD = 0; SM = 0; TB = 0;
|
||||||
|
d.SendCommand(BOOSTER_SOFT_START_CONTROL)
|
||||||
|
d.SendData(0xD7)
|
||||||
|
d.SendData(0xD6)
|
||||||
|
d.SendData(0x9D)
|
||||||
|
d.SendCommand(WRITE_VCOM_REGISTER)
|
||||||
|
d.SendData(0xA8) // VCOM 7C
|
||||||
|
d.SendCommand(SET_DUMMY_LINE_PERIOD)
|
||||||
|
d.SendData(0x1A) // 4 dummy lines per gate
|
||||||
|
d.SendCommand(SET_GATE_TIME)
|
||||||
|
d.SendData(0x08) // 2us per line
|
||||||
|
d.SendCommand(DATA_ENTRY_MODE_SETTING)
|
||||||
|
d.SendData(0x03) // X increment; Y increment
|
||||||
|
|
||||||
|
d.SetLUT(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset resets the device
|
||||||
|
func (d *Device) Reset() {
|
||||||
|
d.rst.Low()
|
||||||
|
time.Sleep(200 * time.Millisecond)
|
||||||
|
d.rst.High()
|
||||||
|
time.Sleep(200 * time.Millisecond)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeepSleep puts the display into deepsleep
|
||||||
|
func (d *Device) DeepSleep() {
|
||||||
|
d.SendCommand(DEEP_SLEEP_MODE)
|
||||||
|
d.WaitUntilIdle()
|
||||||
|
}
|
||||||
|
|
||||||
|
// SendCommand sends a command to the display
|
||||||
|
func (d *Device) SendCommand(command uint8) {
|
||||||
|
d.sendDataCommand(true, command)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SendData sends a data byte to the display
|
||||||
|
func (d *Device) SendData(data uint8) {
|
||||||
|
d.sendDataCommand(false, data)
|
||||||
|
}
|
||||||
|
|
||||||
|
// sendDataCommand sends image data or a command to the screen
|
||||||
|
func (d *Device) sendDataCommand(isCommand bool, data uint8) {
|
||||||
|
if isCommand {
|
||||||
|
d.dc.Low()
|
||||||
|
} else {
|
||||||
|
d.dc.High()
|
||||||
|
}
|
||||||
|
d.cs.Low()
|
||||||
|
d.bus.Transfer(data)
|
||||||
|
d.cs.High()
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetLUT sets the look up tables for full or partial updates
|
||||||
|
func (d *Device) SetLUT(fullUpdate bool) {
|
||||||
|
d.SendCommand(WRITE_LUT_REGISTER)
|
||||||
|
if fullUpdate {
|
||||||
|
for i := 0; i < 30; i++ {
|
||||||
|
d.SendData(lutFullUpdate[i])
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for i := 0; i < 30; i++ {
|
||||||
|
d.SendData(lutPartialUpdate[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetPixel modifies the internal buffer in a single pixel.
|
||||||
|
// The display have 2 colors: black and white
|
||||||
|
// We use RGBA(0,0,0, 255) as white (transparent)
|
||||||
|
// Anything else as black
|
||||||
|
func (d *Device) SetPixel(x int16, y int16, c color.RGBA) {
|
||||||
|
x, y = d.xy(x, y)
|
||||||
|
if x < 0 || x >= d.logicalWidth || y < 0 || y >= d.height {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
byteIndex := (int32(x) + int32(y)*int32(d.logicalWidth)) / 8
|
||||||
|
if c.R == 0 && c.G == 0 && c.B == 0 { // TRANSPARENT / WHITE
|
||||||
|
d.buffer[byteIndex] |= 0x80 >> uint8(x%8)
|
||||||
|
} else { // WHITE / EMPTY
|
||||||
|
d.buffer[byteIndex] &^= 0x80 >> uint8(x%8)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Display sends the buffer to the screen.
|
||||||
|
func (d *Device) Display() error {
|
||||||
|
d.setMemoryArea(0, 0, d.logicalWidth-1, d.height-1)
|
||||||
|
for j := int16(0); j < d.height; j++ {
|
||||||
|
d.setMemoryPointer(0, j)
|
||||||
|
d.SendCommand(WRITE_RAM)
|
||||||
|
for i := int16(0); i < d.logicalWidth/8; i++ {
|
||||||
|
d.SendData(d.buffer[i+j*(d.logicalWidth/8)])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
d.SendCommand(DISPLAY_UPDATE_CONTROL_2)
|
||||||
|
d.SendData(0xC4)
|
||||||
|
d.SendCommand(MASTER_ACTIVATION)
|
||||||
|
d.SendCommand(TERMINATE_FRAME_READ_WRITE)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ClearDisplay erases the device SRAM
|
||||||
|
func (d *Device) ClearDisplay() {
|
||||||
|
d.setMemoryArea(0, 0, d.logicalWidth-1, d.height-1)
|
||||||
|
d.setMemoryPointer(0, 0)
|
||||||
|
d.SendCommand(WRITE_RAM)
|
||||||
|
for i := uint32(0); i < d.bufferLength; i++ {
|
||||||
|
d.SendData(0xFF)
|
||||||
|
}
|
||||||
|
d.Display()
|
||||||
|
}
|
||||||
|
|
||||||
|
// setMemoryArea sets the area of the display that will be updated
|
||||||
|
func (d *Device) setMemoryArea(x0 int16, y0 int16, x1 int16, y1 int16) {
|
||||||
|
d.SendCommand(SET_RAM_X_ADDRESS_START_END_POSITION)
|
||||||
|
d.SendData(uint8((x0 >> 3) & 0xFF))
|
||||||
|
d.SendData(uint8((x1 >> 3) & 0xFF))
|
||||||
|
d.SendCommand(SET_RAM_Y_ADDRESS_START_END_POSITION)
|
||||||
|
d.SendData(uint8(y0 & 0xFF))
|
||||||
|
d.SendData(uint8((y0 >> 8) & 0xFF))
|
||||||
|
d.SendData(uint8(y1 & 0xFF))
|
||||||
|
d.SendData(uint8((y1 >> 8) & 0xFF))
|
||||||
|
}
|
||||||
|
|
||||||
|
// setMemoryPointer moves the internal pointer to the speficied coordinates
|
||||||
|
func (d *Device) setMemoryPointer(x int16, y int16) {
|
||||||
|
d.SendCommand(SET_RAM_X_ADDRESS_COUNTER)
|
||||||
|
d.SendData(uint8((x >> 3) & 0xFF))
|
||||||
|
d.SendCommand(SET_RAM_Y_ADDRESS_COUNTER)
|
||||||
|
d.SendData(uint8(y & 0xFF))
|
||||||
|
d.SendData(uint8((y >> 8) & 0xFF))
|
||||||
|
d.WaitUntilIdle()
|
||||||
|
}
|
||||||
|
|
||||||
|
// WaitUntilIdle waits until the display is ready
|
||||||
|
func (d *Device) WaitUntilIdle() {
|
||||||
|
for d.busy.Get() {
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsBusy returns the busy status of the display
|
||||||
|
func (d *Device) IsBusy() bool {
|
||||||
|
return d.busy.Get()
|
||||||
|
}
|
||||||
|
|
||||||
|
// ClearBuffer sets the buffer to 0xFF (white)
|
||||||
|
func (d *Device) ClearBuffer() {
|
||||||
|
for i := uint32(0); i < d.bufferLength; i++ {
|
||||||
|
d.buffer[i] = 0xFF
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Size returns the current size of the display.
|
||||||
|
func (d *Device) Size() (w, h int16) {
|
||||||
|
if d.rotation == ROTATION_90 || d.rotation == ROTATION_270 {
|
||||||
|
return d.height, d.logicalWidth
|
||||||
|
}
|
||||||
|
return d.logicalWidth, d.height
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetRotation changes the rotation (clock-wise) of the device
|
||||||
|
func (d *Device) SetRotation(rotation Rotation) {
|
||||||
|
d.rotation = rotation
|
||||||
|
}
|
||||||
|
|
||||||
|
// xy chages the coordinates according to the rotation
|
||||||
|
func (d *Device) xy(x, y int16) (int16, int16) {
|
||||||
|
switch d.rotation {
|
||||||
|
case NO_ROTATION:
|
||||||
|
return x, y
|
||||||
|
case ROTATION_90:
|
||||||
|
return d.width - y - 1, x
|
||||||
|
case ROTATION_180:
|
||||||
|
return d.width - x - 1, d.height - y - 1
|
||||||
|
case ROTATION_270:
|
||||||
|
return y, d.height - x - 1
|
||||||
|
}
|
||||||
|
return x, y
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package epd2in9
|
||||||
|
|
||||||
|
// Registers
|
||||||
|
const (
|
||||||
|
DRIVER_OUTPUT_CONTROL = 0x01
|
||||||
|
BOOSTER_SOFT_START_CONTROL = 0x0C
|
||||||
|
GATE_SCAN_START_POSITION = 0x0F
|
||||||
|
DEEP_SLEEP_MODE = 0x10
|
||||||
|
DATA_ENTRY_MODE_SETTING = 0x11
|
||||||
|
SW_RESET = 0x12
|
||||||
|
TEMPERATURE_SENSOR_CONTROL = 0x1A
|
||||||
|
MASTER_ACTIVATION = 0x20
|
||||||
|
DISPLAY_UPDATE_CONTROL_1 = 0x21
|
||||||
|
DISPLAY_UPDATE_CONTROL_2 = 0x22
|
||||||
|
WRITE_RAM = 0x24
|
||||||
|
WRITE_VCOM_REGISTER = 0x2C
|
||||||
|
WRITE_LUT_REGISTER = 0x32
|
||||||
|
SET_DUMMY_LINE_PERIOD = 0x3A
|
||||||
|
SET_GATE_TIME = 0x3B
|
||||||
|
BORDER_WAVEFORM_CONTROL = 0x3C
|
||||||
|
SET_RAM_X_ADDRESS_START_END_POSITION = 0x44
|
||||||
|
SET_RAM_Y_ADDRESS_START_END_POSITION = 0x45
|
||||||
|
SET_RAM_X_ADDRESS_COUNTER = 0x4E
|
||||||
|
SET_RAM_Y_ADDRESS_COUNTER = 0x4F
|
||||||
|
TERMINATE_FRAME_READ_WRITE = 0xFF
|
||||||
|
|
||||||
|
NO_ROTATION Rotation = 0
|
||||||
|
ROTATION_90 Rotation = 1 // 90 degrees clock-wise rotation
|
||||||
|
ROTATION_180 Rotation = 2
|
||||||
|
ROTATION_270 Rotation = 3
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user