2023-01-06 02:04:32 +08:00
|
|
|
package spi
|
|
|
|
|
|
|
|
import (
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2/drivers/common/mfrc522"
|
2023-01-06 02:04:32 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// MFRC522Driver is a wrapper for SPI bus usage. Please refer to the mfrc522.MFRC522Common package
|
|
|
|
// for implementation details.
|
|
|
|
type MFRC522Driver struct {
|
|
|
|
*Driver
|
|
|
|
*mfrc522.MFRC522Common
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMFRC522Driver creates a new Gobot Driver for MFRC522 RFID with SPI connection
|
|
|
|
//
|
|
|
|
// Params:
|
2023-05-20 20:25:21 +08:00
|
|
|
//
|
|
|
|
// a *Adaptor - the Adaptor to use with this Driver
|
2023-01-06 02:04:32 +08:00
|
|
|
//
|
|
|
|
// Optional params:
|
|
|
|
//
|
2023-05-20 20:25:21 +08:00
|
|
|
// spi.WithBusNumber(int): bus to use with this driver
|
|
|
|
// spi.WithChipNumber(int): chip to use with this driver
|
|
|
|
// spi.WithMode(int): mode to use with this driver
|
|
|
|
// spi.WithBitCount(int): number of bits to use with this driver
|
|
|
|
// spi.WithSpeed(int64): speed in Hz to use with this driver
|
2023-01-06 02:04:32 +08:00
|
|
|
func NewMFRC522Driver(a Connector, options ...func(Config)) *MFRC522Driver {
|
|
|
|
d := &MFRC522Driver{
|
|
|
|
Driver: NewDriver(a, "MFRC522"),
|
|
|
|
}
|
|
|
|
d.MFRC522Common = mfrc522.NewMFRC522Common()
|
|
|
|
d.afterStart = d.initialize
|
|
|
|
for _, option := range options {
|
|
|
|
option(d)
|
|
|
|
}
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *MFRC522Driver) initialize() error {
|
|
|
|
wrapper := &conWrapper{origCon: d.connection}
|
|
|
|
return d.MFRC522Common.Initialize(wrapper)
|
|
|
|
}
|
|
|
|
|
|
|
|
// this is necessary due to special behavior of shift bytes and set first bit
|
|
|
|
type conWrapper struct {
|
|
|
|
origCon Connection
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *conWrapper) ReadByteData(reg uint8) (uint8, error) {
|
|
|
|
// MSBit=1 for reading, LSBit not used for first byte (address/register)
|
|
|
|
return w.origCon.ReadByteData(0x80 | (reg << 1))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *conWrapper) WriteByteData(reg uint8, val uint8) error {
|
|
|
|
// LSBit not used for first byte (address/register)
|
|
|
|
return w.origCon.WriteByteData(reg<<1, val)
|
|
|
|
}
|