gogrove

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 7, 2019 License: MIT Imports: 8 Imported by: 0

README

gogrove

godoc

Go library for interacting with GrovePi

Currently only tested with GrovePi firmware version 1.3.0

See examples in example_test.go

The Rasberry Pi communicates with GrovePi over I2C. The following kernel modules are needed to support this:

  • i2c_dev
  • i2c_bcm2835

To see if the Rasberry Pi is communicating with the Grove pi, run the following:

# sudo i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          03 04 -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- 3e --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- 62 -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

You should see the "04" for the GrovePi and the "3e" and "62" for the LCD.

If these are not showing up, try reloading the i2c_bcm2835 module:

# sudo modprobe i2c_dev
# sudo rmmod i2c_bcm2835
# sudo modprobe i2c_bcm2835

This package is goroutine safe within a session

To cross compile your go app for the Rasberry Pi:

# GOOS=linux GOARCH=arm go build

Some logic within is based on the Python library aviable here

Useful links:

Documentation

Overview

Example
package main

import (
	"log"
	"time"

	"github.com/benmcclelland/gogrove"
)

func main() {
	s, err := gogrove.New()
	if err != nil {
		log.Fatal(err)
	}
	defer s.Close()

	// blink LED on D6, errors ignored
	for i := 0; i < 5; i++ {
		s.TurnOn(gogrove.PortD6)
		time.Sleep(time.Second)
		s.TurnOff(gogrove.PortD6)
		time.Sleep(time.Second)
	}
}
Output:

Example (GreenTxtLCD)
package main

import (
	"log"

	"github.com/benmcclelland/gogrove"
)

func main() {
	l, err := gogrove.NewLCD()
	if err != nil {
		log.Fatal(err)
	}
	defer l.Close()

	l.ClearText()
	l.SetText("Hello,\nGo Example")
	l.SetRGB(0, 255, 0)
}
Output:

Example (OffClearLCD)
package main

import (
	"log"

	"github.com/benmcclelland/gogrove"
)

func main() {
	l, err := gogrove.NewLCD()
	if err != nil {
		log.Fatal(err)
	}
	defer l.Close()

	l.ClearText()
	l.SetRGB(0, 0, 0)
}
Output:

Index

Examples

Constants

View Source
const (

	// PortA0 is the value for GrovePi A0
	PortA0 uint8 = 0
	// PortA1 is the value for GrovePi A1
	PortA1 uint8 = 1
	// PortA2 is the value for GrovePi A2
	PortA2 uint8 = 2
	// PortD2 is the value for GrovePi D2
	PortD2 uint8 = 2
	// PortD3 is the value for GrovePi D3
	PortD3 uint8 = 3
	// PortD4 is the value for GrovePi D4
	PortD4 uint8 = 4
	// PortD5 is the value for GrovePi D5
	PortD5 uint8 = 5
	// PortD6 is the value for GrovePi D6
	PortD6 uint8 = 6
	// PortD7 is the value for GrovePi D7
	PortD7 uint8 = 7
	// PortD8 is the value for GrovePi D8
	PortD8 uint8 = 8

	// ModeInput is used for SetPortMode to input
	ModeInput uint8 = 0
	// ModeOutput is used for SetPortMode to output
	ModeOutput uint8 = 1

	// BlueDHTSensor is the DHT sensor that comes with base kit
	BlueDHTSensor uint8 = 0
	// WhiteDHTSensor is the separate white DHT sensor
	WhiteDHTSensor uint8 = 1
)

Variables

This section is empty.

Functions

This section is empty.

Types

type LCD

type LCD struct {
	sync.Mutex
	// contains filtered or unexported fields
}

LCD holds session info for interacting with GrovePi LCD

func NewLCD

func NewLCD() (*LCD, error)

NewLCD initializes a new session with the LCD

func (*LCD) ClearText

func (l *LCD) ClearText() error

ClearText clears the display text

func (*LCD) Close

func (l *LCD) Close() error

Close closes session with LCD

func (*LCD) SetRGB

func (l *LCD) SetRGB(r, g, b uint8) error

SetRGB sets the background RGB LCD color

func (*LCD) SetText

func (l *LCD) SetText(str string) error

SetText clears display text, and sets text

type Session

type Session struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Session holds session info for interacting with GrovePi

func New

func New() (*Session, error)

New initializes new session with default GrovePi address

func NewWithAddress

func NewWithAddress(address uint16) (*Session, error)

NewWithAddress initializes new session with given GrovePi address

func (*Session) AnalogRead

func (s *Session) AnalogRead(port uint8) (uint16, error)

AnalogRead reads analog value from port this is only valid for PortA0, PortA1, or PortA2 the returned value will be between 0-1023 inclusive

func (*Session) AnalogWrite

func (s *Session) AnalogWrite(port, value uint8) error

AnalogWrite writes value 0-255 inclusive to given port This appears to only be valid for PortD3, PortD5, and PortD6 using PWM write

func (*Session) Close

func (s *Session) Close() error

Close closes GrovePi session

func (*Session) DigitalRead

func (s *Session) DigitalRead(port uint8) (uint8, error)

DigitalRead return the value from a digital port on success, this will be either 0 or 1

func (*Session) DigitalWrite

func (s *Session) DigitalWrite(port, value uint8) error

DigitalWrite sets the value for the given port The value must be 0 or 1

func (*Session) GetFirmwareVersion

func (s *Session) GetFirmwareVersion() (string, error)

GetFirmwareVersion returns the GrovePi firmware version

Example
package main

import (
	"fmt"
	"log"

	"github.com/benmcclelland/gogrove"
)

func main() {
	s, err := gogrove.New()
	if err != nil {
		log.Fatal(err)
	}
	defer s.Close()

	vers, err := s.GetFirmwareVersion()
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(vers)
}
Output:

func (*Session) IsOn

func (s *Session) IsOn(port uint8) bool

IsOn is shorthand for DigitalRead on a digital port returning true if the port is 1 and false if the port is 0 this ignores errors from DigitalRead for easier inlining

func (*Session) ReadDHT

func (s *Session) ReadDHT(port, sensor uint8) (float32, float32, error)

ReadDHT returns temp (C), humidity (%), error must pass the sensort type, one of: gogrove.BlueDHTSensor gogrove.WhiteDHTSensor

func (*Session) ReadUltraSonic

func (s *Session) ReadUltraSonic(port uint8) (uint16, error)

ReadUltraSonic returns distance in cm Sensor spec: measuring range 2-350cm, resolution 1cm

func (*Session) SetPortMode

func (s *Session) SetPortMode(port, mode uint8) error

SetPortMode sets port to mode, for example: SetPortMode(gogrove.PortA0, gogrove.ModeOutput) SetPortMode(gogrove.PortD3, gogrove.ModeInput)

Example
package main

import (
	"log"

	"github.com/benmcclelland/gogrove"
)

func main() {
	s, err := gogrove.New()
	if err != nil {
		log.Fatal(err)
	}
	defer s.Close()

	// Set port D3 to Input
	err = s.SetPortMode(gogrove.PortD3, gogrove.ModeInput)
	if err != nil {
		log.Fatal(err)
	}
}
Output:

func (*Session) TurnOff

func (s *Session) TurnOff(port uint8) error

TurnOff is shorthand for DigitalWrite(port, 0)

func (*Session) TurnOn

func (s *Session) TurnOn(port uint8) error

TurnOn is shorthand for DigitalWrite(port, 1)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL