|
| 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