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

bring the matrix4x3 library up to date

This commit is contained in:
Karan Misra 2014-03-03 00:55:20 +05:30
parent 36f2c0486d
commit ca6eb80458
2 changed files with 17 additions and 18 deletions

View File

@ -6,7 +6,7 @@ import (
"sync" "sync"
"time" "time"
"github.com/kidoman/embd/gpio" "github.com/kidoman/embd"
) )
type Key int type Key int
@ -68,7 +68,7 @@ func init() {
// A Matrix4x3 struct represents access to the keypad. // A Matrix4x3 struct represents access to the keypad.
type Matrix4x3 struct { type Matrix4x3 struct {
rowPins, colPins []gpio.DigitalPin rowPins, colPins []embd.DigitalPin
initialized bool initialized bool
mu sync.RWMutex mu sync.RWMutex
@ -82,20 +82,20 @@ type Matrix4x3 struct {
// New creates a new interface for matrix4x3. // New creates a new interface for matrix4x3.
func New(rowPins, colPins []int) (*Matrix4x3, error) { func New(rowPins, colPins []int) (*Matrix4x3, error) {
m := &Matrix4x3{ m := &Matrix4x3{
rowPins: make([]gpio.DigitalPin, rows), rowPins: make([]embd.DigitalPin, rows),
colPins: make([]gpio.DigitalPin, cols), colPins: make([]embd.DigitalPin, cols),
poll: pollDelay, poll: pollDelay,
} }
var err error var err error
for i := 0; i < rows; i++ { for i := 0; i < rows; i++ {
m.rowPins[i], err = gpio.NewDigitalPin(rowPins[i]) m.rowPins[i], err = embd.NewDigitalPin(rowPins[i])
if err != nil { if err != nil {
return nil, err return nil, err
} }
} }
for i := 0; i < cols; i++ { for i := 0; i < cols; i++ {
m.colPins[i], err = gpio.NewDigitalPin(colPins[i]) m.colPins[i], err = embd.NewDigitalPin(colPins[i])
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -121,7 +121,7 @@ func (d *Matrix4x3) setup() error {
defer d.mu.Unlock() defer d.mu.Unlock()
for i := 0; i < rows; i++ { for i := 0; i < rows; i++ {
if err := d.rowPins[i].SetDirection(gpio.In); err != nil { if err := d.rowPins[i].SetDirection(embd.In); err != nil {
return err return err
} }
if err := d.rowPins[i].PullUp(); err != nil { if err := d.rowPins[i].PullUp(); err != nil {
@ -130,10 +130,10 @@ func (d *Matrix4x3) setup() error {
} }
for i := 0; i < cols; i++ { for i := 0; i < cols; i++ {
if err := d.colPins[i].SetDirection(gpio.Out); err != nil { if err := d.colPins[i].SetDirection(embd.Out); err != nil {
return err return err
} }
if err := d.colPins[i].Write(gpio.High); err != nil { if err := d.colPins[i].Write(embd.High); err != nil {
return err return err
} }
} }
@ -149,7 +149,7 @@ func (d *Matrix4x3) findPressedKey() (Key, error) {
} }
for col := 0; col < cols; col++ { for col := 0; col < cols; col++ {
if err := d.colPins[col].Write(gpio.Low); err != nil { if err := d.colPins[col].Write(embd.Low); err != nil {
return KNone, err return KNone, err
} }
for row := 0; row < rows; row++ { for row := 0; row < rows; row++ {
@ -157,22 +157,22 @@ func (d *Matrix4x3) findPressedKey() (Key, error) {
if err != nil { if err != nil {
return KNone, err return KNone, err
} }
if value == gpio.Low { if value == embd.Low {
time.Sleep(debounce) time.Sleep(debounce)
value, err = d.rowPins[row].Read() value, err = d.rowPins[row].Read()
if err != nil { if err != nil {
return KNone, err return KNone, err
} }
if value == gpio.Low { if value == embd.Low {
if err := d.colPins[col].Write(gpio.High); err != nil { if err := d.colPins[col].Write(embd.High); err != nil {
return KNone, err return KNone, err
} }
return keyMap[row][col], nil return keyMap[row][col], nil
} }
} }
} }
if err := d.colPins[col].Write(gpio.High); err != nil { if err := d.colPins[col].Write(embd.High); err != nil {
return KNone, err return KNone, err
} }
} }

View File

@ -5,8 +5,7 @@ package main
import ( import (
"fmt" "fmt"
"time" "time"
"github.com/kidoman/embd"
"github.com/kidoman/embd/gpio"
"github.com/kidoman/embd/interface/keypad/matrix4x3" "github.com/kidoman/embd/interface/keypad/matrix4x3"
) )
@ -14,10 +13,10 @@ func main() {
rowPins := []int{4, 17, 27, 22} rowPins := []int{4, 17, 27, 22}
colPins := []int{23, 24, 25} colPins := []int{23, 24, 25}
if err := gpio.Open(); err != nil { if err := embd.InitGPIO(); err != nil {
panic(err) panic(err)
} }
defer gpio.Close() defer embd.CloseGPIO()
keypad, err := matrix4x3.New(rowPins, colPins) keypad, err := matrix4x3.New(rowPins, colPins)
if err != nil { if err != nil {