mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-07-29 03:58:43 +00:00
Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 592f66e1ca | |||
| 71c77d4b53 | |||
| 1f54c7dd23 | |||
| d0163c67f5 | |||
| 80cd4d9dac | |||
| 594ba1dab2 | |||
| 625c565c0b | |||
| b64d4ec3a0 | |||
| 171366698b | |||
| ca34b93e5d | |||
| 2cfc08b560 | |||
| 1850c5effb | |||
| 810888ce42 | |||
| cafe8df620 | |||
| 13c43350a3 | |||
| 3edd8e413b |
@@ -1,20 +0,0 @@
|
||||
# Golang CircleCI 2.0 configuration file
|
||||
#
|
||||
# Check https://circleci.com/docs/2.0/language-go/ for more details
|
||||
version: 2
|
||||
jobs:
|
||||
build:
|
||||
docker:
|
||||
- image: tinygo/tinygo-dev
|
||||
steps:
|
||||
- checkout
|
||||
- run: tinygo version
|
||||
- run:
|
||||
name: "Enforce Go Formatted Code"
|
||||
command: make fmt-check
|
||||
- run:
|
||||
name: "Run unit tests"
|
||||
command: make unit-test
|
||||
- run:
|
||||
name: "Run build and smoke tests"
|
||||
command: make smoke-test
|
||||
@@ -0,0 +1,25 @@
|
||||
name: Build
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
push:
|
||||
branches:
|
||||
- dev
|
||||
- release
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
container: tinygo/tinygo-dev
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v2
|
||||
- name: TinyGo version check
|
||||
run: tinygo version
|
||||
- name: Enforce Go Formatted Code
|
||||
run: make fmt-check
|
||||
- name: Run unit tests
|
||||
run: make unit-test
|
||||
- name: Run build and smoke tests
|
||||
run: make smoke-test
|
||||
@@ -239,14 +239,17 @@ endif
|
||||
@md5sum ./build/test.hex
|
||||
tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/scd4x/main.go
|
||||
@md5sum ./build/test.uf2
|
||||
tinygo build -size short -o ./build/test.uf2 -target=circuitplay-express ./examples/makeybutton/main.go
|
||||
@md5sum ./build/test.uf2
|
||||
|
||||
DRIVERS = $(wildcard */)
|
||||
NOTESTS = build examples flash semihosting pcd8544 shiftregister st7789 microphone mcp3008 gps microbitmatrix \
|
||||
hcsr04 ssd1331 ws2812 thermistor apa102 easystepper ssd1351 ili9341 wifinina shifter hub75 \
|
||||
hd44780 buzzer ssd1306 espat l9110x st7735 bmi160 l293x keypad4x4 max72xx p1am tone tm1637 \
|
||||
pcf8563 mcp2515 servo sdcard rtl8720dn image cmd i2csoft hts221 lps22hb apds9960 axp192 xpt2046 \
|
||||
ft6336 sx126x ssd1289 irremote uc8151
|
||||
TESTS = $(filter-out $(addsuffix /%,$(NOTESTS)),$(DRIVERS))
|
||||
# rwildcard is a recursive version of $(wildcard)
|
||||
# https://blog.jgc.org/2011/07/gnu-make-recursive-wildcard-function.html
|
||||
rwildcard=$(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2) $(filter $(subst *,%,$2),$d))
|
||||
# Recursively find all *_test.go files from cwd & reduce to unique dir names
|
||||
HAS_TESTS = $(sort $(dir $(call rwildcard,,*_test.go)))
|
||||
# Exclude anything we explicitly don't want to test for whatever reason
|
||||
EXCLUDE_TESTS = image
|
||||
TESTS = $(filter-out $(addsuffix /%,$(EXCLUDE_TESTS)),$(HAS_TESTS))
|
||||
|
||||
unit-test:
|
||||
@go test -v $(addprefix ./,$(TESTS))
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# TinyGo Drivers
|
||||
|
||||
[](https://pkg.go.dev/tinygo.org/x/drivers) [](https://circleci.com/gh/tinygo-org/drivers/tree/dev)
|
||||
[](https://pkg.go.dev/tinygo.org/x/drivers) [](https://github.com/tinygo-org/drivers/actions/workflows/build.yml)
|
||||
|
||||
|
||||
This package provides a collection of hardware drivers for devices such as sensors and displays that can be used together with [TinyGo](https://tinygo.org).
|
||||
@@ -52,7 +52,7 @@ func main() {
|
||||
|
||||
## Currently supported devices
|
||||
|
||||
The following 80 devices are supported.
|
||||
The following 81 devices are supported.
|
||||
|
||||
| Device Name | Interface Type |
|
||||
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------|
|
||||
@@ -100,6 +100,7 @@ The following 80 devices are supported.
|
||||
| [LSM6DS3TR accelerometer](https://www.st.com/resource/en/datasheet/lsm6ds3tr.pdf) | I2C |
|
||||
| [LSM303AGR accelerometer](https://www.st.com/resource/en/datasheet/lsm303agr.pdf) | I2C |
|
||||
| [LSM9DS1 accelerometer](https://www.st.com/resource/en/datasheet/lsm9ds1.pdf) | I2C |
|
||||
| [Makey Button](https://makeymakey.com/) | GPIO |
|
||||
| [MAG3110 magnetometer](https://www.nxp.com/docs/en/data-sheet/MAG3110.pdf) | I2C |
|
||||
| [MAX7219 & MAX7221 display driver](https://datasheets.maximintegrated.com/en/ds/MAX7219-MAX7221.pdf) | SPI |
|
||||
| [MCP2515 Stand-Alone CAN Controller with SPI Interface](https://ww1.microchip.com/downloads/en/DeviceDoc/MCP2515-Family-Data-Sheet-DS20001801K.pdf) | SPI |
|
||||
|
||||
+18
-18
@@ -8,27 +8,27 @@ import (
|
||||
)
|
||||
|
||||
var irCmdButtons = map[uint16]string{
|
||||
0xA2: "POWER",
|
||||
0xE2: "FUNC/STOP",
|
||||
0x62: "VOL+",
|
||||
0x22: "FAST BACK",
|
||||
0x02: "PAUSE",
|
||||
0xC2: "FAST FORWARD",
|
||||
0xE0: "DOWN",
|
||||
0xA8: "VOL-",
|
||||
0x90: "UP",
|
||||
0x98: "EQ",
|
||||
0xB0: "ST/REPT",
|
||||
0x68: "0",
|
||||
0x30: "1",
|
||||
0x45: "POWER",
|
||||
0x47: "FUNC/STOP",
|
||||
0x46: "VOL+",
|
||||
0x44: "FAST BACK",
|
||||
0x40: "PAUSE",
|
||||
0x43: "FAST FORWARD",
|
||||
0x07: "DOWN",
|
||||
0x15: "VOL-",
|
||||
0x09: "UP",
|
||||
0x19: "EQ",
|
||||
0x0D: "ST/REPT",
|
||||
0x16: "0",
|
||||
0x0C: "1",
|
||||
0x18: "2",
|
||||
0x7A: "3",
|
||||
0x10: "4",
|
||||
0x38: "5",
|
||||
0x5E: "3",
|
||||
0x08: "4",
|
||||
0x1C: "5",
|
||||
0x5A: "6",
|
||||
0x42: "7",
|
||||
0x4A: "8",
|
||||
0x52: "9",
|
||||
0x52: "8",
|
||||
0x4A: "9",
|
||||
}
|
||||
|
||||
var (
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"machine"
|
||||
"time"
|
||||
|
||||
"tinygo.org/x/drivers/makeybutton"
|
||||
)
|
||||
|
||||
var (
|
||||
led machine.Pin = machine.LED
|
||||
button machine.Pin = machine.BUTTON
|
||||
key *makeybutton.Button
|
||||
)
|
||||
|
||||
func main() {
|
||||
led.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
key = makeybutton.NewButton(button)
|
||||
key.Configure()
|
||||
|
||||
for {
|
||||
switch key.Get() {
|
||||
case makeybutton.Pressed:
|
||||
led.High()
|
||||
case makeybutton.Released:
|
||||
led.Low()
|
||||
}
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"tinygo.org/x/drivers/examples/rtl8720dn"
|
||||
)
|
||||
|
||||
var (
|
||||
debug = false
|
||||
)
|
||||
|
||||
func main() {
|
||||
err := run()
|
||||
for err != nil {
|
||||
fmt.Printf("error: %s\r\n", err.Error())
|
||||
time.Sleep(5 * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
func run() error {
|
||||
//rtl8720dn.Debug(true)
|
||||
rtl, err := rtl8720dn.Setup()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ver, err := rtl.Version()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
for {
|
||||
fmt.Printf("RTL8270DN Firmware Version: %s\r\n", ver)
|
||||
time.Sleep(10 * time.Second)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
//go:build wioterminal
|
||||
// +build wioterminal
|
||||
|
||||
package rtl8720dn
|
||||
|
||||
import (
|
||||
"device/sam"
|
||||
"machine"
|
||||
"runtime/interrupt"
|
||||
"time"
|
||||
|
||||
"tinygo.org/x/drivers/rtl8720dn"
|
||||
)
|
||||
|
||||
var (
|
||||
uart UARTx
|
||||
debug bool
|
||||
)
|
||||
|
||||
func handleInterrupt(interrupt.Interrupt) {
|
||||
// should reset IRQ
|
||||
uart.Receive(byte((uart.Bus.DATA.Get() & 0xFF)))
|
||||
uart.Bus.INTFLAG.SetBits(sam.SERCOM_USART_INT_INTFLAG_RXC)
|
||||
}
|
||||
|
||||
func Setup() (*rtl8720dn.RTL8720DN, error) {
|
||||
machine.RTL8720D_CHIP_PU.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
machine.RTL8720D_CHIP_PU.Low()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
machine.RTL8720D_CHIP_PU.High()
|
||||
time.Sleep(1000 * time.Millisecond)
|
||||
if debug {
|
||||
waitSerial()
|
||||
}
|
||||
|
||||
uart = UARTx{
|
||||
UART: &machine.UART{
|
||||
Buffer: machine.NewRingBuffer(),
|
||||
Bus: sam.SERCOM0_USART_INT,
|
||||
SERCOM: 0,
|
||||
},
|
||||
}
|
||||
|
||||
uart.Interrupt = interrupt.New(sam.IRQ_SERCOM0_2, handleInterrupt)
|
||||
uart.Configure(machine.UARTConfig{TX: machine.PB24, RX: machine.PC24, BaudRate: 614400})
|
||||
|
||||
rtl := rtl8720dn.New(uart)
|
||||
rtl.Debug(debug)
|
||||
|
||||
_, err := rtl.Rpc_tcpip_adapter_init_with_timeout(10 * time.Second)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return rtl, nil
|
||||
}
|
||||
|
||||
// Wait for user to open serial console
|
||||
func waitSerial() {
|
||||
for !machine.Serial.DTR() {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
type UARTx struct {
|
||||
*machine.UART
|
||||
}
|
||||
|
||||
func (u UARTx) Read(p []byte) (n int, err error) {
|
||||
if u.Buffered() == 0 {
|
||||
time.Sleep(1 * time.Millisecond)
|
||||
return 0, nil
|
||||
}
|
||||
return u.UART.Read(p)
|
||||
}
|
||||
|
||||
func Debug(b bool) {
|
||||
debug = b
|
||||
}
|
||||
@@ -5,8 +5,8 @@ go 1.15
|
||||
require (
|
||||
github.com/eclipse/paho.mqtt.golang v1.2.0
|
||||
github.com/frankban/quicktest v1.10.2
|
||||
golang.org/x/net v0.0.0-20210614182718-04defd469f4e // indirect
|
||||
tinygo.org/x/tinyfont v0.2.1
|
||||
tinygo.org/x/tinyfs v0.1.0
|
||||
golang.org/x/net v0.0.0-20210614182718-04defd469f4e
|
||||
tinygo.org/x/tinyfont v0.3.0
|
||||
tinygo.org/x/tinyfs v0.2.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/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/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/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||
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/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=
|
||||
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/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
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.15.1/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/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.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/go.mod h1:/DDhNnGwNF2/tNgHywvyZuCGnbH3ov49Z/6e8LPLRR4=
|
||||
|
||||
@@ -151,7 +151,7 @@ func (ir *ReceiverDevice) pinChange(pin machine.Pin) {
|
||||
ir.resetStateMachine()
|
||||
} else {
|
||||
// 562.5µs OR 1687.5µs space detected
|
||||
mask := uint32((1 << (31 - ir.bitIndex)))
|
||||
mask := uint32(1 << ir.bitIndex)
|
||||
if duration > time.Microsecond*1000 {
|
||||
// 1687.5µs space detected (logic 1) - Set bit
|
||||
ir.data.Code |= mask
|
||||
@@ -201,16 +201,16 @@ const (
|
||||
|
||||
func (ir *ReceiverDevice) decode() irDecodeError {
|
||||
// Decode cmd and inverse cmd and perform validation check
|
||||
cmd := uint8((ir.data.Code & 0xff00) >> 8)
|
||||
invCmd := uint8(ir.data.Code & 0xff)
|
||||
cmd := uint8((ir.data.Code & 0x00ff0000) >> 16)
|
||||
invCmd := uint8((ir.data.Code & 0xff000000) >> 24)
|
||||
if cmd != ^invCmd {
|
||||
// Validation failure. cmd and inverse cmd do not match
|
||||
return irDecodeErrorInverseCheckFail
|
||||
}
|
||||
// cmd validation pass, decode address
|
||||
ir.data.Command = uint16(cmd)
|
||||
addrLow := uint8((ir.data.Code & 0xff000000) >> 24)
|
||||
addrHigh := uint8((ir.data.Code & 0x00ff0000) >> 16)
|
||||
addrLow := uint8(ir.data.Code & 0xff)
|
||||
addrHigh := uint8((ir.data.Code & 0xff00) >> 8)
|
||||
if addrHigh == ^addrLow {
|
||||
// addrHigh is inverse of addrLow. This is not a valid 16-bit address in extended NEC coding
|
||||
// since it is indistinguishable from 8-bit address with inverse validation. Use the 8-bit address
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package makeybutton
|
||||
|
||||
const bufferSize = 6
|
||||
|
||||
// Buffer is a buffer to keep track of the most recent readings for a button.
|
||||
type Buffer struct {
|
||||
readings [bufferSize]bool
|
||||
index int
|
||||
}
|
||||
|
||||
// NewBuffer returns a new buffer.
|
||||
func NewBuffer() *Buffer {
|
||||
return &Buffer{}
|
||||
}
|
||||
|
||||
// Used returns how many bytes in buffer have been used.
|
||||
func (b *Buffer) Used() int {
|
||||
return b.index
|
||||
}
|
||||
|
||||
// Put stores a boolean in the buffer.
|
||||
func (b *Buffer) Put(val bool) bool {
|
||||
b.index++
|
||||
if b.index >= bufferSize {
|
||||
b.index = 0
|
||||
}
|
||||
|
||||
b.readings[b.index] = val
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// Avg returns the "average" of all the readings in the buffer, by
|
||||
// treating a true as 1 and a false as -1.
|
||||
func (b *Buffer) Avg() int {
|
||||
avg := 0
|
||||
for i := 0; i < bufferSize; i++ {
|
||||
if b.readings[i] {
|
||||
avg += 1
|
||||
} else {
|
||||
avg -= 1
|
||||
}
|
||||
}
|
||||
|
||||
return avg
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
// Package makeybutton providers a driver for a button that can be triggered
|
||||
// by anything that is conductive by using an ultra high value resistor.
|
||||
//
|
||||
// Inspired by the amazing MakeyMakey
|
||||
// https://makeymakey.com/
|
||||
//
|
||||
package makeybutton
|
||||
|
||||
import (
|
||||
"machine"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ButtonState represents the state of a MakeyButton.
|
||||
type ButtonState int
|
||||
|
||||
const (
|
||||
NeverPressed ButtonState = 0
|
||||
Press = 1
|
||||
Release = 2
|
||||
)
|
||||
|
||||
// ButtonEvent represents when the state of a Button changes.
|
||||
type ButtonEvent int
|
||||
|
||||
const (
|
||||
NotChanged ButtonEvent = 0
|
||||
Pressed = 1
|
||||
Released = 2
|
||||
)
|
||||
|
||||
// Button is a "button"-like device that acts like a MakeyMakey.
|
||||
type Button struct {
|
||||
pin machine.Pin
|
||||
state ButtonState
|
||||
readings *Buffer
|
||||
HighMeansPressed bool
|
||||
}
|
||||
|
||||
// NewButton creates a new Button.
|
||||
func NewButton(pin machine.Pin) *Button {
|
||||
return &Button{
|
||||
pin: pin,
|
||||
state: NeverPressed,
|
||||
readings: NewBuffer(),
|
||||
HighMeansPressed: false,
|
||||
}
|
||||
}
|
||||
|
||||
// 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,
|
||||
// and if it has changed by being pressed or released.
|
||||
func (b *Button) Get() ButtonEvent {
|
||||
// if pin is pulled up, a low value means the key is pressed
|
||||
pressed := !b.pin.Get()
|
||||
if b.HighMeansPressed {
|
||||
// otherwise, a high value means the key is pressed
|
||||
pressed = !pressed
|
||||
}
|
||||
|
||||
avg := b.readings.Avg()
|
||||
b.readings.Put(pressed)
|
||||
|
||||
switch {
|
||||
case pressed && avg > -1*bufferSize+2:
|
||||
if b.state == Press {
|
||||
return NotChanged
|
||||
}
|
||||
|
||||
b.state = Press
|
||||
return Pressed
|
||||
case !pressed:
|
||||
if b.state == Press {
|
||||
b.state = Release
|
||||
return Released
|
||||
}
|
||||
}
|
||||
|
||||
return NotChanged
|
||||
}
|
||||
+25
-10
@@ -20,6 +20,11 @@ func SetBuf(b []byte) {
|
||||
}
|
||||
|
||||
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 {
|
||||
case "http":
|
||||
return c.doHTTP(req)
|
||||
@@ -31,15 +36,17 @@ func (c *Client) Do(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
|
||||
ip := net.ParseIP(req.URL.Host)
|
||||
raddr := &net.TCPAddr{IP: ip, Port: 80}
|
||||
ip := net.ParseIP(req.URL.Hostname())
|
||||
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}
|
||||
|
||||
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 {
|
||||
println("Read error: " + err.Error())
|
||||
} 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 {
|
||||
ofs += n
|
||||
continue
|
||||
}
|
||||
idx += ofs + 4
|
||||
idx += start + 4
|
||||
|
||||
scanner = bufio.NewScanner(bytes.NewReader(buf[0 : ofs+n]))
|
||||
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)
|
||||
}
|
||||
n, err := conn.Read(buf[ofs : ofs+0x400])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if n == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -100,6 +100,12 @@ func (c *mqttclient) Connect() Token {
|
||||
connectPkt.ProtocolName = "MQTT"
|
||||
connectPkt.Keepalive = 60
|
||||
|
||||
connectPkt.WillFlag = c.opts.WillEnabled
|
||||
connectPkt.WillTopic = c.opts.WillTopic
|
||||
connectPkt.WillMessage = c.opts.WillPayload
|
||||
connectPkt.WillQos = c.opts.WillQos
|
||||
connectPkt.WillRetain = c.opts.WillRetained
|
||||
|
||||
err = connectPkt.Write(c.conn)
|
||||
if err != nil {
|
||||
return &mqtttoken{err: err}
|
||||
@@ -146,6 +152,7 @@ func (c *mqttclient) Publish(topic string, qos byte, retained bool, payload inte
|
||||
pub := packets.NewControlPacket(packets.Publish).(*packets.PublishPacket)
|
||||
pub.Qos = qos
|
||||
pub.TopicName = topic
|
||||
pub.Retain = retained
|
||||
switch payload.(type) {
|
||||
case string:
|
||||
pub.Payload = []byte(payload.(string))
|
||||
|
||||
@@ -118,6 +118,9 @@ func (r *RTL8720DN) ConnectSSLSocket(addr, port string) error {
|
||||
if r.debug {
|
||||
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()
|
||||
if err != nil {
|
||||
|
||||
+32
-1
@@ -1,6 +1,10 @@
|
||||
package rtl8720dn
|
||||
|
||||
import "io"
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"time"
|
||||
)
|
||||
|
||||
const maxUartRecvSize = 128
|
||||
|
||||
@@ -38,6 +42,33 @@ func New(r io.ReadWriter) *RTL8720DN {
|
||||
return ret
|
||||
}
|
||||
|
||||
func (r *RTL8720DN) Rpc_tcpip_adapter_init_with_timeout(d time.Duration) (int32, error) {
|
||||
timeout := make(chan bool)
|
||||
go func() {
|
||||
time.Sleep(d)
|
||||
timeout <- true
|
||||
}()
|
||||
|
||||
var ret int32
|
||||
var err error
|
||||
done := make(chan bool)
|
||||
go func() {
|
||||
ret, err = r.Rpc_tcpip_adapter_init()
|
||||
done <- true
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-timeout:
|
||||
return ret, fmt.Errorf("Rpc_tcpip_adapter_init: timeout")
|
||||
case <-done:
|
||||
if err != nil {
|
||||
return ret, err
|
||||
}
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (r *RTL8720DN) SetSeq(s uint64) {
|
||||
r.seq = s
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user