first commit

This commit is contained in:
pandacraft 2025-03-21 16:04:17 +01:00
commit a5a0434432
1126 changed files with 439481 additions and 0 deletions

19
Software/Go/LICENSE Normal file
View file

@ -0,0 +1,19 @@
Copyright (c) <2014> <Falco Tomasetti>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

11
Software/Go/README.md Executable file
View file

@ -0,0 +1,11 @@
# Go library for GrovePi
## Introduction
You will need to install mrmorphic hwio library
go get github.com/mrmorphic/hwio
## Features
Allows Digital Write for switching Relais or a LED, and DHT Read allows to read e.g. Temperature: 25.000000 - Humidity: 95.000000
## Testing
The Package contains now GO Testing Packages, they can be used to directly execute them on Raspberry Pi within Visual Studio Code.

View file

@ -0,0 +1,154 @@
package grovepi
import (
"fmt"
"time"
"unsafe"
"github.com/mrmorphic/hwio"
)
const (
//Pins
A0 = 0
A1 = 1
A2 = 2
D2 = 2
D3 = 3
D4 = 4
D5 = 5
D6 = 6
D7 = 7
D8 = 8
//Cmd format
DIGITAL_READ = 1
DIGITAL_WRITE = 2
ANALOG_READ = 3
ANALOG_WRITE = 4
PIN_MODE = 5
DHT_READ = 40
)
type GrovePi struct {
i2cmodule hwio.I2CModule
i2cDevice hwio.I2CDevice
}
func InitGrovePi(address int) *GrovePi {
grovePi := new(GrovePi)
m, err := hwio.GetModule("i2c")
if err != nil {
fmt.Printf("could not get i2c module: %s\n", err)
return nil
}
grovePi.i2cmodule = m.(hwio.I2CModule)
grovePi.i2cmodule.Enable()
grovePi.i2cDevice = grovePi.i2cmodule.GetDevice(address)
return grovePi
}
func (grovePi *GrovePi) CloseDevice() {
grovePi.i2cmodule.Disable()
}
func (grovePi *GrovePi) AnalogRead(pin byte) (int, error) {
b := []byte{ANALOG_READ, pin, 0, 0}
err := grovePi.i2cDevice.Write(1, b)
if err != nil {
return 0, err
}
time.Sleep(100 * time.Millisecond)
grovePi.i2cDevice.ReadByte(1)
val, err2 := grovePi.i2cDevice.Read(1, 4)
if err2 != nil {
return 0, err
}
var v1 int = int(val[1])
var v2 int = int(val[2])
return ((v1 * 256) + v2), nil
}
func (grovePi *GrovePi) AnalogWrite(pin byte, val byte) error {
b := []byte{ANALOG_WRITE, pin, val, 0}
err := grovePi.i2cDevice.Write(1, b)
time.Sleep(100 * time.Millisecond)
if err != nil {
return err
}
return nil
}
func (grovePi *GrovePi) DigitalRead(pin byte) (byte, error) {
b := []byte{DIGITAL_READ, pin, 0, 0}
err := grovePi.i2cDevice.Write(1, b)
if err != nil {
return 0, err
}
time.Sleep(100 * time.Millisecond)
val, err2 := grovePi.i2cDevice.ReadByte(1)
if err2 != nil {
return 0, err2
}
return val, nil
}
func (grovePi *GrovePi) DigitalWrite(pin byte, val byte) error {
b := []byte{DIGITAL_WRITE, pin, val, 0}
err := grovePi.i2cDevice.Write(1, b)
time.Sleep(100 * time.Millisecond)
if err != nil {
return err
}
return nil
}
func (grovePi *GrovePi) PinMode(pin byte, mode string) error {
var b []byte
if mode == "output" {
b = []byte{PIN_MODE, pin, 1, 0}
} else {
b = []byte{PIN_MODE, pin, 0, 0}
}
err := grovePi.i2cDevice.Write(1, b)
time.Sleep(100 * time.Millisecond)
if err != nil {
return err
}
return nil
}
func (grovePi *GrovePi) ReadDHT(pin byte) (float32, float32, error) {
b := []byte{DHT_READ, pin, 0, 0}
rawdata, err := grovePi.readDHTRawData(b)
if err != nil {
return 0, 0, err
}
temperatureData := rawdata[1:5]
tInt := int32(temperatureData[0]) | int32(temperatureData[1])<<8 | int32(temperatureData[2])<<16 | int32(temperatureData[3])<<24
t := (*(*float32)(unsafe.Pointer(&tInt)))
humidityData := rawdata[5:9]
humInt := int32(humidityData[0]) | int32(humidityData[1])<<8 | int32(humidityData[2])<<16 | int32(humidityData[3])<<24
h := (*(*float32)(unsafe.Pointer(&humInt)))
return t, h, nil
}
func (grovePi *GrovePi) readDHTRawData(cmd []byte) ([]byte, error) {
err := grovePi.i2cDevice.Write(1, cmd)
if err != nil {
return nil, err
}
time.Sleep(600 * time.Millisecond)
grovePi.i2cDevice.ReadByte(1)
time.Sleep(100 * time.Millisecond)
raw, err := grovePi.i2cDevice.Read(1, 9)
if err != nil {
return nil, err
}
return raw, nil
}

View file

@ -0,0 +1,23 @@
package main
import (
"github.com/JGrotex/GrovePi/Software/Go/grovepi"
"fmt"
)
func runDHT() string {
var g grovepi.GrovePi
g = *grovepi.InitGrovePi(0x04)
defer g.CloseDevice()
t, h, err := g.ReadDHT(grovepi.D4)
if err != nil {
panic(err)
}
fmt.Printf("Temperature: %f - Humidity: %f\n", t, h)
return "done"
}

View file

@ -0,0 +1,28 @@
package main
import (
"fmt"
"time"
"github.com/JGrotex/GrovePi/Software/Go/grovepi"
)
func runLED() string {
var g grovepi.GrovePi
g = *grovepi.InitGrovePi(0x04)
err := g.PinMode(grovepi.D3, "output")
if err != nil {
fmt.Println(err)
}
g.DigitalWrite(grovepi.D3, 1)
time.Sleep(500 * time.Millisecond)
g.DigitalWrite(grovepi.D3, 0)
time.Sleep(500 * time.Millisecond)
return "done"
}
func main() {
runLED()
}

View file

@ -0,0 +1,20 @@
package main
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestLED(t *testing.T) {
fmt.Println("start test now ...")
var result = runLED()
assert.Equal(t, "done", result, "should return 'done'")
}
func TestDHT(t *testing.T) {
fmt.Println("start test now ...")
var result = runDHT()
assert.Equal(t, "done", result, "should return 'done'")
}