all: refactor all current drivers to use new machine.Pin interface

Signed-off-by: Ron Evans <ron@hybridgroup.com>
This commit is contained in:
Ron Evans
2019-05-26 17:01:47 +02:00
parent 2c0bde0dad
commit 3f946868e1
19 changed files with 134 additions and 159 deletions
+31 -36
View File
@@ -7,36 +7,31 @@ import (
)
type GPIO struct {
dataPins []machine.GPIO
e machine.GPIO
rw machine.GPIO
rs machine.GPIO
dataPins []machine.Pin
en machine.Pin
rw machine.Pin
rs machine.Pin
write func(data byte)
read func() byte
}
func newGPIO(dataPins []uint8, e, rs, rw uint8, mode byte) Device {
pins := make([]machine.GPIO, len(dataPins))
func newGPIO(dataPins []machine.Pin, en, rs, rw machine.Pin, mode byte) Device {
pins := make([]machine.Pin, len(dataPins))
for i := 0; i < len(dataPins); i++ {
m := machine.GPIO{Pin: dataPins[i]}
m.Configure(machine.GPIOConfig{Mode: machine.GPIO_OUTPUT})
pins[i] = m
dataPins[i].Configure(machine.PinConfig{Mode: machine.PinOutput})
pins[i] = dataPins[i]
}
enable := machine.GPIO{e}
enable.Configure(machine.GPIOConfig{Mode: machine.GPIO_OUTPUT})
registerSelect := machine.GPIO{rs}
registerSelect.Configure(machine.GPIOConfig{Mode: machine.GPIO_OUTPUT})
readWrite := machine.GPIO{rw}
readWrite.Configure(machine.GPIOConfig{Mode: machine.GPIO_OUTPUT})
readWrite.Low()
en.Configure(machine.PinConfig{Mode: machine.PinOutput})
rs.Configure(machine.PinConfig{Mode: machine.PinOutput})
rw.Configure(machine.PinConfig{Mode: machine.PinOutput})
rw.Low()
gpio := GPIO{
dataPins: pins,
e: enable,
rs: registerSelect,
rw: readWrite,
en: en,
rs: rs,
rw: rw,
}
if mode == DATA_LENGTH_4BIT {
@@ -73,19 +68,19 @@ func (g *GPIO) Write(data []byte) (n int, err error) {
}
func (g *GPIO) write8BitMode(data byte) {
g.e.High()
g.en.High()
g.setPins(data)
g.e.Low()
g.en.Low()
}
func (g *GPIO) write4BitMode(data byte) {
g.e.High()
g.en.High()
g.setPins(data >> 4)
g.e.Low()
g.en.Low()
g.e.High()
g.en.High()
g.setPins(data)
g.e.Low()
g.en.Low()
}
// Read reads len(data) bytes from display RAM to data starting from RAM address counter position
@@ -95,35 +90,35 @@ func (g *GPIO) Read(data []byte) (n int, err error) {
return 0, errors.New("Length greater than 0 is required")
}
g.rw.High()
g.reconfigureGPIOMode(machine.GPIO_INPUT)
g.reconfigureGPIOMode(machine.PinInput)
for i := 0; i < len(data); i++ {
data[i] = g.read()
n++
}
g.reconfigureGPIOMode(machine.GPIO_OUTPUT)
g.reconfigureGPIOMode(machine.PinInput)
return n, nil
}
func (g *GPIO) read4BitMode() byte {
g.e.High()
g.en.High()
data := (g.pins() << 4 & 0xF0)
g.e.Low()
g.e.High()
g.en.Low()
g.en.High()
data |= (g.pins() & 0x0F)
g.e.Low()
g.en.Low()
return data
}
func (g *GPIO) read8BitMode() byte {
g.e.High()
g.en.High()
data := g.pins()
g.e.Low()
g.en.Low()
return data
}
func (g *GPIO) reconfigureGPIOMode(mode machine.GPIOMode) {
func (g *GPIO) reconfigureGPIOMode(mode machine.PinMode) {
for i := 0; i < len(g.dataPins); i++ {
g.dataPins[i].Configure(machine.GPIOConfig{Mode: mode})
g.dataPins[i].Configure(machine.PinConfig{Mode: mode})
}
}
+3 -2
View File
@@ -3,6 +3,7 @@ package hd44780
import (
"errors"
"io"
"machine"
"time"
)
@@ -38,7 +39,7 @@ type Config struct {
}
// NewGPIO4Bit returns 4bit data length HD44780 driver. Datapins are LCD DB pins starting from DB4 to DB7
func NewGPIO4Bit(dataPins []uint8, e, rs, rw uint8) (Device, error) {
func NewGPIO4Bit(dataPins []machine.Pin, e, rs, rw machine.Pin) (Device, error) {
const fourBitMode = 4
if len(dataPins) != fourBitMode {
return Device{}, errors.New("4 pins are required in data slice (D4-D7) when HD44780 is used in 4 bit mode")
@@ -47,7 +48,7 @@ func NewGPIO4Bit(dataPins []uint8, e, rs, rw uint8) (Device, error) {
}
// NewGPIO8Bit returns 8bit data length HD44780 driver. Datapins are LCD DB pins starting from DB0 to DB7
func NewGPIO8Bit(dataPins []uint8, e, rs, rw uint8) (Device, error) {
func NewGPIO8Bit(dataPins []machine.Pin, e, rs, rw machine.Pin) (Device, error) {
const eightBitMode = 8
if len(dataPins) != eightBitMode {
return Device{}, errors.New("8 pins are required in data slice (D0-D7) when HD44780 is used in 8 bit mode")