Cleanup
This commit is contained in:
parent
280265c566
commit
d9d9b0837e
7 changed files with 33 additions and 248 deletions
33
device_test.go
Normal file
33
device_test.go
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
package rfm69
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/davecheney/gpio"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
irqPin = gpio.GPIO25
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
log.Print("Start")
|
||||||
|
|
||||||
|
pin, err := gpio.OpenPin(irqPin, gpio.ModeInput)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
defer pin.Close()
|
||||||
|
|
||||||
|
spiBus, err := NewSPIDevice("/dev/spidev0.0")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
defer spiBus.Close()
|
||||||
|
|
||||||
|
rfm, err := NewDevice(spiBus, pin, 1, 10, false)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
log.Print(rfm)
|
||||||
|
}
|
|
@ -1,40 +0,0 @@
|
||||||
package rfm69
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
|
||||||
|
|
||||||
_ "github.com/kidoman/embd/host/rpi"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestRfm69(t *testing.T) {
|
|
||||||
t.Log("Test")
|
|
||||||
if err := embd.InitGPIO(); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
defer embd.CloseGPIO()
|
|
||||||
|
|
||||||
gpio, err := embd.NewDigitalPin(25)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
defer gpio.Close()
|
|
||||||
|
|
||||||
if err := gpio.SetDirection(embd.In); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
gpio.ActiveLow(false)
|
|
||||||
|
|
||||||
spiBus, err := NewSPIDevice()
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
defer spiBus.Close()
|
|
||||||
|
|
||||||
rfm, err := NewDevice(spiBus, gpio, 1, 10, true)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
t.Log(rfm)
|
|
||||||
}
|
|
|
@ -1,62 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
"os/signal"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/davecheney/gpio"
|
|
||||||
"github.com/fulr/rfm69"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
log.Print("Start")
|
|
||||||
|
|
||||||
pin, err := gpio.OpenPin(gpio.GPIO25, gpio.ModeInput)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
defer pin.Close()
|
|
||||||
|
|
||||||
spiBus, err := rfm69.NewSPIDevice()
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
defer spiBus.Close()
|
|
||||||
|
|
||||||
rfm, err := rfm69.NewDevice(spiBus, pin, 1, 10, false)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
log.Print(rfm)
|
|
||||||
|
|
||||||
err = rfm.Encrypt([]byte("0123456789012345"))
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
rxChan, txChan, quit := rfm.Loop()
|
|
||||||
|
|
||||||
sigint := make(chan os.Signal, 1)
|
|
||||||
signal.Notify(sigint, os.Interrupt, os.Kill)
|
|
||||||
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case data := <-rxChan:
|
|
||||||
log.Print("main got data")
|
|
||||||
log.Print(data)
|
|
||||||
case <-sigint:
|
|
||||||
quit <- 1
|
|
||||||
<-quit
|
|
||||||
return
|
|
||||||
case <-time.After(3 * time.Second):
|
|
||||||
txChan <- rfm69.Data{
|
|
||||||
ToAddress: 99,
|
|
||||||
FromAddress: 1,
|
|
||||||
Data: []byte{1, 2, 3},
|
|
||||||
RequestAck: true,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
123
spi_linux.go
123
spi_linux.go
|
@ -1,123 +0,0 @@
|
||||||
package rfm69
|
|
||||||
|
|
||||||
/*
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <sys/ioctl.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <linux/types.h>
|
|
||||||
#include <linux/spi/spidev.h>
|
|
||||||
|
|
||||||
#define SPI_SPEED 4000000
|
|
||||||
|
|
||||||
uint8_t mode=0;
|
|
||||||
uint8_t bits=8;
|
|
||||||
uint32_t speed=SPI_SPEED;
|
|
||||||
uint16_t delay=5;
|
|
||||||
|
|
||||||
int spi_open(const char *device) {
|
|
||||||
int fd = open(device, O_RDWR);
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
if (fd < 0) {
|
|
||||||
printf("can't open device");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
|
|
||||||
if (ret == -1) {
|
|
||||||
printf("can't set spi mode");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
|
|
||||||
if (ret == -1) {
|
|
||||||
printf("can't get spi mode");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
|
|
||||||
if (ret == -1) {
|
|
||||||
printf("can't set bits per word");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
|
|
||||||
if (ret == -1) {
|
|
||||||
printf("can't get bits per word");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
|
|
||||||
if (ret == -1) {
|
|
||||||
printf("can't set max speed hz");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
|
|
||||||
if (ret == -1) {
|
|
||||||
printf("can't get max speed hz");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return fd;
|
|
||||||
}
|
|
||||||
|
|
||||||
int spi_xfer(int fd, char* tx, char* rx, int length) {
|
|
||||||
struct spi_ioc_transfer tr = {
|
|
||||||
.tx_buf = (unsigned long)tx,
|
|
||||||
.rx_buf = (unsigned long)rx,
|
|
||||||
.len = length,
|
|
||||||
.delay_usecs = delay,
|
|
||||||
.speed_hz = speed,
|
|
||||||
.bits_per_word = bits,
|
|
||||||
};
|
|
||||||
|
|
||||||
int ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
|
|
||||||
if (ret < 1)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
import "C"
|
|
||||||
import "unsafe"
|
|
||||||
|
|
||||||
import "errors"
|
|
||||||
|
|
||||||
// SPIDevice device
|
|
||||||
type SPIDevice struct {
|
|
||||||
fd C.int
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewSPIDevice creates a new device
|
|
||||||
func NewSPIDevice() (*SPIDevice, error) {
|
|
||||||
name := C.CString("/dev/spidev0.0")
|
|
||||||
defer C.free(unsafe.Pointer(name))
|
|
||||||
i := C.spi_open(name)
|
|
||||||
if i < 0 {
|
|
||||||
return nil, errors.New("could not open")
|
|
||||||
}
|
|
||||||
return &SPIDevice{i}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Xfer cross transfer
|
|
||||||
func (d *SPIDevice) Xfer(tx []byte) ([]byte, error) {
|
|
||||||
length := len(tx)
|
|
||||||
rx := make([]byte, length)
|
|
||||||
//log.Print("sending", tx)
|
|
||||||
ret := C.spi_xfer(d.fd, (*C.char)(unsafe.Pointer(&tx[0])), (*C.char)(unsafe.Pointer(&rx[0])), C.int(length))
|
|
||||||
//log.Print("got", rx)
|
|
||||||
if ret < 0 {
|
|
||||||
return nil, errors.New("could not xfer")
|
|
||||||
}
|
|
||||||
return rx, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close closes the fd
|
|
||||||
func (d *SPIDevice) Close() {
|
|
||||||
C.close(d.fd)
|
|
||||||
}
|
|
23
spidummy.go
23
spidummy.go
|
@ -1,23 +0,0 @@
|
||||||
// +build !linux
|
|
||||||
|
|
||||||
package rfm69
|
|
||||||
|
|
||||||
// SPIDevice is a device
|
|
||||||
type SPIDevice struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewSPIDevice creates a new device
|
|
||||||
func NewSPIDevice() (*SPIDevice, error) {
|
|
||||||
return &SPIDevice{}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Xfer cross transfer
|
|
||||||
func (d *SPIDevice) Xfer(tx []byte) ([]byte, error) {
|
|
||||||
length := len(tx)
|
|
||||||
rx := make([]byte, length)
|
|
||||||
return rx, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close closes the fd
|
|
||||||
func (d *SPIDevice) Close() {
|
|
||||||
}
|
|
Loading…
Add table
Reference in a new issue