Skip to content

Commit

Permalink
Merge pull request #6 from balazsgrill/issues/4
Browse files Browse the repository at this point in the history
#4 pca9685 integration
  • Loading branch information
balazsgrill authored Feb 23, 2020
2 parents 4350831 + 13be52e commit d2a8b5c
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 26 deletions.
11 changes: 9 additions & 2 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
variables:
REPO_NAME: gitlab.com/grill-tamasi.hu/wscgo
GO111MODULE: "on"
VERSION: 0.3.3-beta6
VERSION: 0.4.0-beta3

# The problem is that to be able to use go get, one needs to put
# the repository in the $GOPATH. So for example if your gitlab domain
Expand All @@ -22,7 +22,10 @@ test:rpizw:
stage: test
variables:
GO111MODULE: "on"
GOARM: 5
before_script:
- curl -L https://dl.bintray.com/balazsgrill/home-build/libwiringPiPca9685.a-rpizw --output libwiringPiPca9685.a
- curl -L https://dl.bintray.com/balazsgrill/home-build/pca9685.h --output wiringpi/pca9685.h
- echo $REPO_NAME
- mkdir -p $GOPATH/src/
- ln -svf $CI_PROJECT_DIR $GOPATH/src/$(dirname $REPO_NAME)
Expand All @@ -41,6 +44,8 @@ compile:rpizw:
GO111MODULE: "on"
GOARM: 5
before_script:
- curl -L https://dl.bintray.com/balazsgrill/home-build/libwiringPiPca9685.a-rpizw --output libwiringPiPca9685.a
- curl -L https://dl.bintray.com/balazsgrill/home-build/pca9685.h --output wiringpi/pca9685.h
- mkdir -p $GOPATH/src/
- ln -svf $CI_PROJECT_DIR $GOPATH/src/$(dirname $REPO_NAME)
- cd $GOPATH/src/$REPO_NAME
Expand All @@ -61,6 +66,8 @@ compile:opiz:
GO111MODULE: "on"
GOARM: 7
before_script:
- curl -L https://dl.bintray.com/balazsgrill/home-build/libwiringPiPca9685.a-opiz --output libwiringPiPca9685.a
- curl -L https://dl.bintray.com/balazsgrill/home-build/pca9685.h --output wiringpi/pca9685.h
- mkdir -p $GOPATH/src/
- ln -svf $CI_PROJECT_DIR $GOPATH/src/$(dirname $REPO_NAME)
- cd $GOPATH/src/$REPO_NAME
Expand All @@ -80,7 +87,7 @@ package:opiz:
dependencies:
- compile:opiz
script:
- curl -L https://github.com/balazsgrill/WiringOP-Zero/releases/download/v2.0-softpwm/libwiringPi.so.2.0 --output libwiringPi.so.2.0
- curl -L https://github.com/balazsgrill/WiringOP-Zero/releases/download/v2.0-pwmfix/libwiringPi.so.2.0 --output libwiringPi.so.2.0
- ln -s libwiringPi.so.2.0 libwiringPi.so
- cp wscgo-opiz wscgo
- envsubst < wscgo-opiz.cfg.tpl > wscgo-opiz.cfg
Expand Down
9 changes: 8 additions & 1 deletion config/iniconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ func (conf *WscgoConfiguration) processConfig(category string, id string, sectio
conf.Configs = append(conf.Configs, func(wiringpi.IoContext) {
wiringpi.Mcp23017Setup(c)
})
case "pca9685":
c := &wiringpi.Pca9685Config{}
section.MapTo(c)
conf.Configs = append(conf.Configs, func(wiringpi.IoContext) {
wiringpi.Pca9685Setup(c)
})
case "shutter":
s := &devices.ShutterConfig{}
section.MapTo(s)
Expand All @@ -44,7 +50,8 @@ func (conf *WscgoConfiguration) processConfig(category string, id string, sectio
})
case "light":
s := &devices.DimmerConfig{
OnPin: -1,
OnPin: -1,
Resolution: 1024,
}
section.MapTo(s)
c := protocol.CreateLightConfig(id)
Expand Down
26 changes: 15 additions & 11 deletions devices/dimmer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package devices

import "gitlab.com/grill-tamasi/wscgo/wiringpi"

const DimmerMaxValue int = 1023

type DimmerConfig struct {
PwmPin int `ini:"pwmpin"`
OnPin int `ini:"onpin"`
Speed int `ini:"speed"`
OnDelay int `ini:"ondelay"`
Inverted bool `ini:"inverted"`
PwmPin int `ini:"pwmpin"`
OnPin int `ini:"onpin"`
Speed int `ini:"speed"`
OnDelay int `ini:"ondelay"`
Inverted bool `ini:"inverted"`
Resolution int `ini:"resolution"`
}

type dimmer struct {
Expand All @@ -25,6 +24,7 @@ type IDimmer interface {
On()
Off()
SetBrightness(value int)
BrightnessResolution() int
}

func CreateDimmer(io wiringpi.IoContext, config *DimmerConfig) IDimmer {
Expand All @@ -44,17 +44,21 @@ func (dimmer *dimmer) Initialize() {
}
}

func (dimmer *dimmer) BrightnessResolution() int {
return dimmer.Resolution
}

func (dimmer *dimmer) On() {
dimmer.SetBrightness(DimmerMaxValue)
dimmer.SetBrightness(dimmer.Resolution - 1)
}

func (dimmer *dimmer) Off() {
dimmer.SetBrightness(0)
}

func (dimmer *dimmer) SetBrightness(target int) {
if target > DimmerMaxValue {
dimmer.target = DimmerMaxValue
if target > dimmer.Resolution-1 {
dimmer.target = dimmer.Resolution - 1
} else {
if target < 0 {
dimmer.target = 0
Expand Down Expand Up @@ -100,7 +104,7 @@ func (dimmer *dimmer) adjustCurrent() {
func (dimmer *dimmer) actuate() {
pwmvalue := dimmer.current
if dimmer.Inverted {
pwmvalue = DimmerMaxValue - pwmvalue
pwmvalue = (dimmer.Resolution - 1) - pwmvalue
}
dimmer.PwmWrite(dimmer.PwmPin, pwmvalue)
if dimmer.OnPin >= 0 {
Expand Down
22 changes: 13 additions & 9 deletions devices/dimmer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"gitlab.com/grill-tamasi/wscgo/wiringpi"
)

const DimmerMaxValue int = 1023

func checkDimmerPins(msg string, t *testing.T, io *tests.TestIo, on bool, pwm int) {
if io.Values[0] != on || io.Pwm[1] != pwm {
t.Errorf("%s ON[exp-actal]: %t - %t, PWM[exp-actal]: %d - %d\n", msg, on, io.Values[0], pwm, io.Pwm[1])
Expand All @@ -16,10 +18,11 @@ func checkDimmerPins(msg string, t *testing.T, io *tests.TestIo, on bool, pwm in
func createDimmerForTest() (*dimmer, *tests.TestIo) {
io := tests.CreateTestIo(3)
c := &DimmerConfig{
OnPin: 0,
PwmPin: 1,
Speed: 10,
OnDelay: 5,
OnPin: 0,
PwmPin: 1,
Speed: 10,
OnDelay: 5,
Resolution: DimmerMaxValue + 1,
}
d, _ := CreateDimmer(io, c).(*dimmer)
return d, io
Expand All @@ -28,11 +31,12 @@ func createDimmerForTest() (*dimmer, *tests.TestIo) {
func createInvertedDimmerForTest() (*dimmer, *tests.TestIo) {
io := tests.CreateTestIo(3)
c := &DimmerConfig{
OnPin: 0,
PwmPin: 1,
Speed: 10,
OnDelay: 5,
Inverted: true,
OnPin: 0,
PwmPin: 1,
Speed: 10,
OnDelay: 5,
Inverted: true,
Resolution: DimmerMaxValue + 1,
}
d, _ := CreateDimmer(io, c).(*dimmer)
return d, io
Expand Down
4 changes: 2 additions & 2 deletions protocol/light.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (light *light) onMsgReceive(client mqtt.Client, msg mqtt.Message) {
switch cmd {
case "ON":
light.On()
light.fireBrightnessEvent(client, devices.DimmerMaxValue)
light.fireBrightnessEvent(client, light.BrightnessResolution()-1)
case "OFF":
light.Off()
light.fireBrightnessEvent(client, 0)
Expand Down Expand Up @@ -87,7 +87,7 @@ func (light *light) GetDiscoveryInfo(uniqueID string, device *DeviceDiscoveryInf
Name: light.Name,
CommandTopic: light.CommandTopic,
BrightnessCommandTopic: light.CommandTopic,
BrightnessScale: 1023,
BrightnessScale: light.BrightnessResolution() - 1,
BrightnessStateTopic: light.CommandTopic + "/brightness",
OnCommandType: "brightness",
}
Expand Down
7 changes: 7 additions & 0 deletions wiringpi/pca9685.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package wiringpi

type Pca9685Config struct {
Address int `ini:"address"`
ExpansionBase int `ini:"expansionBase"`
Frequency float32 `ini:"frequency"`
}
10 changes: 9 additions & 1 deletion wiringpi/wiringPi_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

package wiringpi

// #cgo LDFLAGS: -lwiringPi
// #cgo LDFLAGS: -L${SRCDIR}/.. -lwiringPiPca9685 -lwiringPi
// #include<wiringPi.h>
// #include<mcp23017.h>
// #include "pca9685.h"
// #include<softPwm.h>
// #ifdef PI_MODEL_BPR
// int setuprc = 0;
Expand Down Expand Up @@ -37,6 +38,13 @@ func Mcp23017Setup(config *Mcp23017Config) {
}
}

func Pca9685Setup(config *Pca9685Config) {
rc := C.pca9685Setup(C.int(config.ExpansionBase), C.int(config.Address), C.float(config.Frequency))
if rc < 0 {
log.Fatal("PCA9685 error: ", rc)
}
}

func (*WiringPiIO) Setup() {
//C.wiringPiDebug = (C.int)(1)
C.wiringPiSetup()
Expand Down
2 changes: 2 additions & 0 deletions wiringpi/wiringPi_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type WiringPiIO struct {

func Mcp23017Setup(config *Mcp23017Config) {}

func Pca9685Setup(config *Pca9685Config) {}

func (w *WiringPiIO) Setup() {
w.TestIo = *tests.CreateTestIo(12)
}

0 comments on commit d2a8b5c

Please sign in to comment.