Skip to content

Commit

Permalink
SPI using GPIO's plus driver for MFRC522
Browse files Browse the repository at this point in the history
  • Loading branch information
gen2thomas authored Jan 5, 2023
1 parent fdf4f4b commit 6c7ecbe
Show file tree
Hide file tree
Showing 42 changed files with 2,594 additions and 228 deletions.
37 changes: 27 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
[![Go Report Card](https://goreportcard.com/badge/hybridgroup/gobot)](https://goreportcard.com/report/hybridgroup/gobot)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://github.com/hybridgroup/gobot/blob/master/LICENSE.txt)

Gobot (https://gobot.io/) is a framework using the Go programming language (https://golang.org/) for robotics, physical computing, and the Internet of Things.
Gobot (https://gobot.io/) is a framework using the Go programming language (https://golang.org/) for robotics, physical
computing, and the Internet of Things.

It provides a simple, yet powerful way to create solutions that incorporate multiple, different hardware devices at the same time.
It provides a simple, yet powerful way to create solutions that incorporate multiple, different hardware devices at the
same time.

Want to run Go directly on microcontrollers? Check out our sister project TinyGo (https://tinygo.org/)

Expand Down Expand Up @@ -87,7 +89,8 @@ func main() {

#### "Metal" Gobot

You can use the entire Gobot framework as shown in the examples above ("Classic" Gobot), or you can pick and choose from the various Gobot packages to control hardware with nothing but pure idiomatic Golang code ("Metal" Gobot). For example:
You can use the entire Gobot framework as shown in the examples above ("Classic" Gobot), or you can pick and choose from
the various Gobot packages to control hardware with nothing but pure idiomatic Golang code ("Metal" Gobot). For example:

```go
package main
Expand All @@ -114,7 +117,8 @@ func main() {

#### "Master" Gobot

You can also use the full capabilities of the framework aka "Master Gobot" to control swarms of robots or other features such as the built-in API server. For example:
You can also use the full capabilities of the framework aka "Master Gobot" to control swarms of robots or other features
such as the built-in API server. For example:

```go
package main
Expand Down Expand Up @@ -180,7 +184,9 @@ func main() {
```

## Hardware Support
Gobot has a extensible system for connecting to hardware devices. The following robotics and physical computing platforms are currently supported:

Gobot has a extensible system for connecting to hardware devices. The following robotics and physical computing
platforms are currently supported:

- [Arduino](http://www.arduino.cc/) <=> [Package](https://github.com/hybridgroup/gobot/tree/master/platforms/firmata)
- Audio <=> [Package](https://github.com/hybridgroup/gobot/tree/master/platforms/audio)
Expand Down Expand Up @@ -311,13 +317,15 @@ a shared set of drivers provided using the `gobot/drivers/spi` package:
- MCP3204 Analog/Digital Converter
- MCP3208 Analog/Digital Converter
- MCP3304 Analog/Digital Converter
- MFRC522 RFID Card Reader
- SSD1306 OLED Display Controller

More platforms and drivers are coming soon...

## API:
## API

Gobot includes a RESTful API to query the status of any robot running within a group, including the connection and device status, and execute device commands.
Gobot includes a RESTful API to query the status of any robot running within a group, including the connection and
device status, and execute device commands.

To activate the API, import the `gobot.io/x/gobot/api` package and instantiate the `API` like this:

Expand All @@ -327,6 +335,7 @@ To activate the API, import the `gobot.io/x/gobot/api` package and instantiate t
```

You can also specify the api host and port, and turn on authentication:

```go
master := gobot.NewMaster()
server := api.NewAPI(master)
Expand All @@ -339,29 +348,37 @@ You may access the [robeaux](https://github.com/hybridgroup/robeaux) React.js in

## CLI

Gobot uses the Gort [http://gort.io](http://gort.io) Command Line Interface (CLI) so you can access important features right from the command line. We call it "RobotOps", aka "DevOps For Robotics". You can scan, connect, update device firmware, and more!
Gobot uses the Gort [http://gort.io](http://gort.io) Command Line Interface (CLI) so you can access important features
right from the command line. We call it "RobotOps", aka "DevOps For Robotics". You can scan, connect, update device
firmware, and more!

Gobot also has its own CLI to generate new platforms, adaptors, and drivers. You can check it out in the `/cli` directory.

## Documentation

We're always adding documentation to our web site at https://gobot.io/ please check there as we continue to work on Gobot

Thank you!

## Need help?

* Issues: https://github.com/hybridgroup/gobot/issues
* Twitter: [@gobotio](https://twitter.com/gobotio)
* Slack: [https://gophers.slack.com/messages/C0N5HDB08](https://gophers.slack.com/messages/C0N5HDB08)
* Mailing list: https://groups.google.com/forum/#!forum/gobotio

## Contributing

For our contribution guidelines, please go to [https://github.com/hybridgroup/gobot/blob/master/CONTRIBUTING.md
](https://github.com/hybridgroup/gobot/blob/master/CONTRIBUTING.md
).

Gobot is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms. [You can read about it here](https://github.com/hybridgroup/gobot/tree/master/CODE_OF_CONDUCT.md).
Gobot is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.
[You can read about it here](https://github.com/hybridgroup/gobot/tree/master/CODE_OF_CONDUCT.md).

## License

Copyright (c) 2013-2020 The Hybrid Group. Licensed under the Apache 2.0 license.

The Contributor Covenant is released under the Creative Commons Attribution 4.0 International Public License, which requires that attribution be included.
The Contributor Covenant is released under the Creative Commons Attribution 4.0 International Public License, which
requires that attribution be included.
45 changes: 29 additions & 16 deletions adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,33 @@ type PWMPinnerProvider interface {
PWMPin(id string) (PWMPinner, error)
}

// I2cSystemDevicer is the interface to a i2c bus at system level.
type I2cSystemDevicer interface {
I2cOperations
SetAddress(int) error
}

// SpiSystemDevicer is the interface to a SPI bus at system level.
type SpiSystemDevicer interface {
TxRx(tx []byte, rx []byte) error
// Close the SPI connection.
Close() error
}

// BusOperations are functions provided by a bus device, e.g. SPI, i2c.
type BusOperations interface {
// ReadByteData reads a byte from the given register of bus device.
ReadByteData(reg uint8) (uint8, error)
// ReadBlockData fills the given buffer with reads starting from the given register of bus device.
ReadBlockData(reg uint8, data []byte) error
// WriteByteData writes the given byte value to the given register of bus device.
WriteByteData(reg uint8, val uint8) error
// WriteBlockData writes the given data starting from the given register of bus device.
WriteBlockData(reg uint8, data []byte) error
// WriteByte writes the given byte value to the current register of a bus device.
// WriteByte writes the given byte value to the current register of bus device.
WriteByte(val byte) error
// WriteBytes writes the given data starting from the current register of an bus device.
// WriteBytes writes the given data starting from the current register of bus device.
WriteBytes(data []byte) error
// Close the connection.
Close() error
}

// I2cOperations represents the i2c methods according to I2C/SMBus specification.
Expand All @@ -105,8 +122,14 @@ type BusOperations interface {
// Count (8 bits): A data byte containing the length of a block operation.
// [..]: Data sent by I2C device, as opposed to data sent by the host adapter.
//
// ReadByteData must be implemented as the sequence:
// "S Addr Wr [A] Comm [A] Sr Addr Rd [A] [Data] NA P"
// ReadBlockData must be implemented as the sequence:
// "S Addr Wr [A] Comm [A] Sr Addr Rd [A] [Count] A [Data] A [Data] A ... A [Data] NA P"
// WriteByte must be implemented as the sequence:
// "S Addr Wr [A] Data [A] P"
// WriteByteData must be implemented as the sequence:
// "S Addr Wr [A] Comm [A] Data [A] P"
// WriteBlockData must be implemented as the sequence:
// "S Addr Wr [A] Comm [A] Count [A] Data [A] Data [A] ... [A] Data [A] P"
type I2cOperations interface {
Expand All @@ -117,22 +140,10 @@ type I2cOperations interface {
// "S Addr Rd [A] [Data] NA P"
ReadByte() (byte, error)

// ReadByteData must be implemented as the sequence:
// "S Addr Wr [A] Comm [A] Sr Addr Rd [A] [Data] NA P"
ReadByteData(reg uint8) (uint8, error)

// ReadWordData must be implemented as the sequence:
// "S Addr Wr [A] Comm [A] Sr Addr Rd [A] [DataLow] A [DataHigh] NA P"
ReadWordData(reg uint8) (uint16, error)

// ReadBlockData must be implemented as the sequence:
// "S Addr Wr [A] Comm [A] Sr Addr Rd [A] [Count] A [Data] A [Data] A ... A [Data] NA P"
ReadBlockData(reg uint8, b []byte) error

// WriteByteData must be implemented as the sequence:
// "S Addr Wr [A] Comm [A] Data [A] P"
WriteByteData(reg uint8, val uint8) error

// WriteWordData must be implemented as the sequence:
// "S Addr Wr [A] Comm [A] DataLow [A] DataHigh [A] P"
WriteWordData(reg uint8, val uint16) error
Expand All @@ -143,6 +154,8 @@ type SpiOperations interface {
BusOperations
// ReadCommandData uses the SPI device TX to send/receive data.
ReadCommandData(command []byte, data []byte) error
// Close the connection.
Close() error
}

// Adaptor is the interface that describes an adaptor in gobot
Expand Down
39 changes: 39 additions & 0 deletions drivers/common/mfrc522/mfrc522_connectionwrapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package mfrc522

import "fmt"

func (d *MFRC522Common) readByteData(reg uint8) (uint8, error) {
if d.connection == nil {
return 0, fmt.Errorf("not connected")
}
return d.connection.ReadByteData(reg)
}

func (d *MFRC522Common) writeByteData(reg uint8, data uint8) error {
if d.connection == nil {
return fmt.Errorf("not connected")
}
return d.connection.WriteByteData(reg, data)
}

func (d *MFRC522Common) setRegisterBitMask(reg uint8, mask uint8) error {
val, err := d.readByteData(reg)
if err != nil {
return err
}
if err := d.writeByteData(reg, val|mask); err != nil {
return err
}
return nil
}

func (d *MFRC522Common) clearRegisterBitMask(reg uint8, mask uint8) error {
val, err := d.readByteData(reg)
if err != nil {
return err
}
if err := d.writeByteData(reg, val&^mask); err != nil {
return err
}
return nil
}
Loading

0 comments on commit 6c7ecbe

Please sign in to comment.