Skip to content

Commit 87138e9

Browse files
committed
feat: forked from 'github.com/ralvarezdev/tinygo-pullup
1 parent 1d77eea commit 87138e9

File tree

7 files changed

+180
-0
lines changed

7 files changed

+180
-0
lines changed

enabler/interfaces.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//go:build tinygo && (rp2040 || rp2350)
2+
3+
package enabler
4+
5+
import (
6+
tinygopullup "github.com/ralvarezdev/tinygo-pullup"
7+
)
8+
9+
type (
10+
// Handler is the interface to handle pull-up resistor operations for enabling features
11+
Handler interface {
12+
tinygopullup.Handler
13+
IsEnabled() bool
14+
IsDisabled() bool
15+
}
16+
)

enabler/types.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//go:build tinygo && (rp2040 || rp2350)
2+
3+
package enabler
4+
5+
import (
6+
"machine"
7+
8+
tinygopullup "github.com/ralvarezdev/tinygo-pullup"
9+
tinygotypes "github.com/ralvarezdev/tinygo-types"
10+
)
11+
12+
type (
13+
// DefaultHandler is the default implementation of the Handler interface.
14+
DefaultHandler struct {
15+
tinygopullup.Handler
16+
}
17+
)
18+
19+
// NewDefaultHandler creates a new default debug handler with the given pull-up handler.
20+
//
21+
// Parameters:
22+
//
23+
// pullUpHandler: The pull-up handler to use
24+
//
25+
// Returns:
26+
//
27+
// An instance of DefaultHandler, or an error if the pull-up handler is nil
28+
func NewDefaultHandler(
29+
pullUpHandler tinygopullup.Handler,
30+
) (*DefaultHandler, tinygotypes.ErrorCode) {
31+
if pullUpHandler == nil {
32+
return nil, tinygopullup.ErrorCodePullUpResistorNilHandler
33+
}
34+
35+
return &DefaultHandler{
36+
pullUpHandler,
37+
}, tinygotypes.ErrorCodeNil
38+
}
39+
40+
// NewDefaultHandlerFromPin creates a new default debug handler with a pull-up handler created from the given pin.
41+
func NewDefaultHandlerFromPin(pin machine.Pin) *DefaultHandler {
42+
pullUpHandler := tinygopullup.NewDefaultHandler(pin)
43+
44+
return &DefaultHandler{
45+
pullUpHandler,
46+
}
47+
}
48+
49+
// IsEnabled checks if the debug mode is enabled.
50+
func (d *DefaultHandler) IsEnabled() bool {
51+
return d.IsShorted()
52+
}
53+
54+
// IsDisabled checks if the debug mode is disabled.
55+
func (d *DefaultHandler) IsDisabled() bool {
56+
return d.IsOpen()
57+
}

errors.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//go:build tinygo && (rp2040 || rp2350)
2+
3+
package tinygo_pullup
4+
5+
import (
6+
tinygotypes "github.com/ralvarezdev/tinygo-types"
7+
)
8+
9+
const (
10+
// ErrorCodePullUpResistorStartNumber is the starting number for Pull-Up Resistor-related error codes.
11+
ErrorCodePullUpResistorStartNumber uint16 = 5200
12+
)
13+
14+
const (
15+
ErrorCodePullUpResistorNilHandler = tinygotypes.ErrorCode(iota + ErrorCodePullUpResistorStartNumber)
16+
)

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/ralvarezdev/tinygo-pullup
2+
3+
go 1.25.0
4+
5+
require github.com/ralvarezdev/tinygo-types v0.0.3

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/ralvarezdev/tinygo-types v0.0.3 h1:h8ogOD2lOVxR+w5i6o+v/8qTROFA2KBjVXBM7NT3SMc=
2+
github.com/ralvarezdev/tinygo-types v0.0.3/go.mod h1:2u3ApwAtIqGZ6wDsmUYt44bIk1DEn06INNjtfWndCr4=

interfaces.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//go:build tinygo && (rp2040 || rp2350)
2+
3+
package tinygo_pullup
4+
5+
type (
6+
// Handler is the interface to handle pull-up resistor operations
7+
Handler interface {
8+
Setup()
9+
IsHigh() bool
10+
IsLow() bool
11+
IsOpen() bool
12+
IsShorted() bool
13+
}
14+
)

types.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//go:build tinygo && (rp2040 || rp2350)
2+
3+
package tinygo_pullup
4+
5+
import (
6+
"machine"
7+
)
8+
9+
type (
10+
// DefaultHandler is the default implementation to handle pull-up resistor.
11+
DefaultHandler struct {
12+
inputPin machine.Pin
13+
}
14+
)
15+
16+
// NewDefaultHandler creates a new instance of DefaultHandler
17+
//
18+
// Parameters:
19+
//
20+
// inputPin: The GPIO pin to be used as the input pin.
21+
//
22+
// Returns:
23+
//
24+
// An instance of DefaultHandler.
25+
func NewDefaultHandler(inputPin machine.Pin) *DefaultHandler {
26+
return &DefaultHandler{
27+
inputPin,
28+
}
29+
}
30+
31+
// Setup initializes the input pin for the DefaultHandler
32+
func (d *DefaultHandler) Setup() {
33+
d.inputPin.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
34+
}
35+
36+
// IsHigh checks if the input pin is high.
37+
//
38+
// Returns:
39+
//
40+
// True if the input pin is high, otherwise false.
41+
func (d *DefaultHandler) IsHigh() bool {
42+
return d.inputPin.Get()
43+
}
44+
45+
// IsLow checks if the input pin is low.
46+
//
47+
// Returns:
48+
//
49+
// True if the input pin is low, otherwise false.
50+
func (d *DefaultHandler) IsLow() bool {
51+
return !d.inputPin.Get()
52+
}
53+
54+
// IsOpen checks if the input pin is open (not connected).
55+
//
56+
// Returns:
57+
//
58+
// True if the input pin is open, otherwise false.
59+
func (d *DefaultHandler) IsOpen() bool {
60+
return d.IsHigh()
61+
}
62+
63+
// IsShorted checks if the input pin is shorted (connected to ground).
64+
//
65+
// Returns:
66+
//
67+
// True if the input pin is shorted, otherwise false.
68+
func (d *DefaultHandler) IsShorted() bool {
69+
return d.IsLow()
70+
}

0 commit comments

Comments
 (0)