1
0
mirror of https://github.com/kidoman/embd synced 2024-12-22 12:50:19 +01:00

fix a bunch of build errors

This commit is contained in:
Karan Misra 2014-03-02 12:09:57 +05:30
parent 026f5fe3f3
commit 4e67e7eb11
7 changed files with 105 additions and 67 deletions

View File

@ -1,6 +1,7 @@
package gpio package gpio
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
"path" "path"
@ -18,7 +19,7 @@ type digitalPin struct {
} }
func newDigitalPin(n int) (*digitalPin, error) { func newDigitalPin(n int) (*digitalPin, error) {
p = &digitalPin{n: n} p := &digitalPin{n: n}
if err := p.init(); err != nil { if err := p.init(); err != nil {
return nil, err return nil, err
} }
@ -66,7 +67,7 @@ func (p *digitalPin) SetDirection(dir gpio.Direction) error {
str = "out" str = "out"
} }
_, err := p.dir.WriteString(str) _, err := p.dir.WriteString(str)
return return err
} }
func (p *digitalPin) Read() (int, error) { func (p *digitalPin) Read() (int, error) {
@ -99,6 +100,14 @@ func (p *digitalPin) ActiveLow(b bool) error {
return err return err
} }
func (p *digitalPin) PullUp() error {
return errors.New("not implemented")
}
func (p *digitalPin) PullDown() error {
return errors.New("not implemented")
}
func (p *digitalPin) Close() error { func (p *digitalPin) Close() error {
if err := p.dir.Close(); err != nil { if err := p.dir.Close(); err != nil {
return err return err

View File

@ -99,20 +99,20 @@ func (io *GPIO) unexport(n int) error {
func (io *GPIO) digitalPin(key interface{}) (*digitalPin, error) { func (io *GPIO) digitalPin(key interface{}) (*digitalPin, error) {
pd, found := io.lookupKey(key) pd, found := io.lookupKey(key)
if !found { if !found {
err = fmt.Errorf("gpio: could not find pin matching %q", key) err := fmt.Errorf("gpio: could not find pin matching %q", key)
return return nil, err
} }
n := pd.N n := pd.N
p, ok := io.initializedPins[n] p, ok := io.initializedPins[n]
if ok { if ok {
return return p, nil
} }
if pd.Caps&Normal == 0 { if pd.Caps&Normal == 0 {
err = fmt.Errorf("gpio: sorry, pin %q cannot be used for GPIO", key) err := fmt.Errorf("gpio: sorry, pin %q cannot be used for GPIO", key)
return return nil, err
} }
if pd.Caps != Normal { if pd.Caps != Normal {

View File

@ -21,6 +21,9 @@ type DigitalPin interface {
SetDirection(dir Direction) error SetDirection(dir Direction) error
ActiveLow(b bool) error ActiveLow(b bool) error
PullUp() error
PullDown() error
Close() error Close() error
} }

View File

@ -15,6 +15,8 @@ func describer(rev int) *host.Descriptor {
GPIO: func() interface{} { GPIO: func() interface{} {
return lgpio.New(pins) return lgpio.New(pins)
}, },
I2C: li2c.New, I2C: func() interface{} {
return li2c.New()
},
} }
} }

View File

@ -12,7 +12,7 @@ func init() {
func describer(rev int) *host.Descriptor { func describer(rev int) *host.Descriptor {
var pins = rev1Pins var pins = rev1Pins
if d.rev > 1 { if rev > 1 {
pins = rev2Pins pins = rev2Pins
} }
@ -20,6 +20,8 @@ func describer(rev int) *host.Descriptor {
GPIO: func() interface{} { GPIO: func() interface{} {
return lgpio.New(pins) return lgpio.New(pins)
}, },
I2C: li2c.New, I2C: func() interface{} {
return li2c.New()
},
} }
} }

View File

@ -2,12 +2,11 @@
package matrix4x3 package matrix4x3
import ( import (
"errors"
"strconv" "strconv"
"sync" "sync"
"time" "time"
"github.com/stianeikeland/go-rpio" "github.com/kidoman/embd/gpio"
) )
type Key int type Key int
@ -19,12 +18,13 @@ func (k Key) String() string {
case KHash: case KHash:
return "#" return "#"
default: default:
return strconv.Itoa(int(k)) return strconv.Itoa(int(k) - 1)
} }
} }
const ( const (
K0 Key = iota KNone Key = iota
K0
K1 K1
K2 K2
K3 K3
@ -66,23 +66,9 @@ func init() {
keyMap[3][2] = KHash keyMap[3][2] = KHash
} }
// A Matrix4x3 interface implements access to the keypad. // A Matrix4x3 struct represents access to the keypad.
type Matrix4x3 interface { type Matrix4x3 struct {
// Run starts the continuous key scan loop. rowPins, colPins []gpio.DigitalPin
Run()
// SetPollDelay sets the delay between runs of key scan acquisition loop.
SetPollDelay(delay int)
// Pressed key returns the current key pressed on the keypad.
PressedKey() (Key, error)
// Close.
Close()
}
type matrix4x3 struct {
rpioRowPins, rpioColPins []rpio.Pin
initialized bool initialized bool
mu sync.RWMutex mu sync.RWMutex
@ -94,33 +80,40 @@ type matrix4x3 struct {
} }
// New creates a new interface for matrix4x3. // New creates a new interface for matrix4x3.
func New(rowPins, colPins []int) Matrix4x3 { func New(rowPins, colPins []int) (*Matrix4x3, error) {
m := &matrix4x3{ m := &Matrix4x3{
rpioRowPins: make([]rpio.Pin, rows), rowPins: make([]gpio.DigitalPin, rows),
rpioColPins: make([]rpio.Pin, cols), colPins: make([]gpio.DigitalPin, cols),
poll: pollDelay, poll: pollDelay,
} }
var err error
for i := 0; i < rows; i++ { for i := 0; i < rows; i++ {
m.rpioRowPins[i] = rpio.Pin(rowPins[i]) m.rowPins[i], err = gpio.NewDigitalPin(rowPins[i])
if err != nil {
return nil, err
}
} }
for i := 0; i < cols; i++ { for i := 0; i < cols; i++ {
m.rpioColPins[i] = rpio.Pin(colPins[i]) m.colPins[i], err = gpio.NewDigitalPin(colPins[i])
if err != nil {
return nil, err
}
} }
return m return m, nil
} }
// SetPollDelay sets the delay between run of key scan acquisition loop. // SetPollDelay sets the delay between run of key scan acquisition loop.
func (d *matrix4x3) SetPollDelay(delay int) { func (d *Matrix4x3) SetPollDelay(delay int) {
d.poll = delay d.poll = delay
} }
func (d *matrix4x3) setup() (err error) { func (d *Matrix4x3) setup() error {
d.mu.RLock() d.mu.RLock()
if d.initialized { if d.initialized {
d.mu.RUnlock() d.mu.RUnlock()
return return nil
} }
d.mu.RUnlock() d.mu.RUnlock()
@ -128,47 +121,67 @@ func (d *matrix4x3) setup() (err error) {
defer d.mu.Unlock() defer d.mu.Unlock()
for i := 0; i < rows; i++ { for i := 0; i < rows; i++ {
d.rpioRowPins[i].Input() if err := d.rowPins[i].SetDirection(gpio.In); err != nil {
d.rpioRowPins[i].PullUp() return err
}
if err := d.rowPins[i].PullUp(); err != nil {
return err
}
} }
for i := 0; i < cols; i++ { for i := 0; i < cols; i++ {
d.rpioColPins[i].Output() if err := d.colPins[i].SetDirection(gpio.Out); err != nil {
d.rpioColPins[i].High() return err
}
if err := d.colPins[i].Write(gpio.High); err != nil {
return err
}
} }
d.initialized = true d.initialized = true
return return nil
} }
func (d *matrix4x3) findPressedKey() (key Key, err error) { func (d *Matrix4x3) findPressedKey() (Key, error) {
if err = d.setup(); err != nil { if err := d.setup(); err != nil {
return return 0, err
} }
err = errors.New("no key pressed")
for col := 0; col < cols; col++ { for col := 0; col < cols; col++ {
d.rpioColPins[col].Low() if err := d.colPins[col].Write(gpio.Low); err != nil {
return KNone, err
}
for row := 0; row < rows; row++ { for row := 0; row < rows; row++ {
if d.rpioRowPins[row].Read() == rpio.Low { value, err := d.rowPins[row].Read()
if err != nil {
return KNone, err
}
if value == gpio.Low {
time.Sleep(debounce) time.Sleep(debounce)
if d.rpioRowPins[row].Read() == rpio.Low { value, err = d.rowPins[row].Read()
key = keyMap[row][col] if err != nil {
err = nil return KNone, err
}
if value == gpio.Low {
if err := d.colPins[col].Write(gpio.High); err != nil {
return KNone, err
}
return keyMap[row][col], nil
} }
} }
} }
d.rpioColPins[col].High() if err := d.colPins[col].Write(gpio.High); err != nil {
return KNone, err
}
} }
return return KNone, nil
} }
// Pressed key returns the current key pressed on the keypad. // Pressed key returns the current key pressed on the keypad.
func (d *matrix4x3) PressedKey() (key Key, err error) { func (d *Matrix4x3) PressedKey() (key Key, err error) {
select { select {
case key = <-d.keyPressed: case key = <-d.keyPressed:
return return
@ -178,7 +191,7 @@ func (d *matrix4x3) PressedKey() (key Key, err error) {
} }
// Run starts the continuous key scan loop. // Run starts the continuous key scan loop.
func (d *matrix4x3) Run() { func (d *Matrix4x3) Run() {
d.quit = make(chan bool) d.quit = make(chan bool)
go func() { go func() {
@ -206,7 +219,7 @@ func (d *matrix4x3) Run() {
} }
// Close. // Close.
func (d *matrix4x3) Close() { func (d *Matrix4x3) Close() {
if d.quit != nil { if d.quit != nil {
d.quit <- true d.quit <- true
} }

View File

@ -4,21 +4,30 @@ import (
"fmt" "fmt"
"time" "time"
"github.com/kidoman/embd/gpio"
"github.com/kidoman/embd/interface/keypad/matrix4x3" "github.com/kidoman/embd/interface/keypad/matrix4x3"
"github.com/stianeikeland/go-rpio"
) )
func main() { func main() {
rowPins := []int{4, 17, 27, 22} rowPins := []int{4, 17, 27, 22}
colPins := []int{23, 24, 25} colPins := []int{23, 24, 25}
rpio.Open() if err := gpio.Open(); err != nil {
defer rpio.Close() panic(err)
}
defer gpio.Close()
keypad := matrix4x3.New(rowPins, colPins) keypad, err := matrix4x3.New(rowPins, colPins)
if err != nil {
panic(err)
}
for { for {
if key, err := keypad.PressedKey(); err == nil { key, err := keypad.PressedKey()
if err != nil {
panic(err)
}
if key != matrix4x3.KNone {
fmt.Printf("Key Pressed = %v\n", key) fmt.Printf("Key Pressed = %v\n", key)
} }