Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed init routine , added configurable strobe delays #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions examples/datetime-16x2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"fmt"
"log"
"time"

hd44780 "github.com/adrianh-za/go-hd44780-rpi"
"github.com/d2r2/go-i2c"
logger "github.com/d2r2/go-logger"
)

func checkError(err error) {
if err != nil {
log.Fatal(err)
}
}

func main() {
//Stop the I2C module from spamming the console
logger.ChangePackageLogLevel("i2c", logger.InfoLevel)

//Init I2C
i2c, err := i2c.NewI2C(0x27, 1)
checkError(err)
defer i2c.Close()

//Init the display
lcd, err := hd44780.NewLcd(i2c, hd44780.LCD_16x2)
lcd.Startup()
lcd.SetupExit(true) //Setup CTRL-C to quit gracefully

//Display the time
for {
lcd.Home()
t := time.Now()
seconds := t.Second()

lcd.SetPosition(0, 0)
fmt.Fprint(lcd, t.Format("Monday Jan 2"))
lcd.SetPosition(1, 1)
fmt.Fprint(lcd, t.Format("15:04:05 2006"))

//Small sleeps until we need to update due to second change.
for {
if seconds != time.Now().Second() {
break
}

time.Sleep(200 * time.Millisecond)
}
}
}
57 changes: 57 additions & 0 deletions examples/datetime-20x4.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"fmt"
"log"
"time"

hd44780 "github.com/adrianh-za/go-hd44780-rpi"
"github.com/d2r2/go-i2c"
logger "github.com/d2r2/go-logger"
)

func checkError(err error) {
if err != nil {
log.Fatal(err)
}
}

func main() {
//Stop the I2C module from spamming the console
logger.ChangePackageLogLevel("i2c", logger.InfoLevel)

//Init I2C
i2c, err := i2c.NewI2C(0x27, 1)
checkError(err)
defer i2c.Close()

//Init the display
lcd, err := hd44780.NewLcd(i2c, hd44780.LCD_20x4)
lcd.Startup()
lcd.SetupExit(true) //Setup CTRL-C to quit gracefully

//Display the time
for {
lcd.Home()
t := time.Now()
seconds := t.Second()

lcd.SetPosition(0, 0)
fmt.Fprint(lcd, t.Format("Monday Jan 2"))
lcd.SetPosition(1, 1)
fmt.Fprint(lcd, t.Format("15:04:05 2006"))
lcd.SetPosition(2, 0)
fmt.Fprint(lcd, t.Format("Monday Jan 2"))
lcd.SetPosition(3, 1)
fmt.Fprint(lcd, t.Format("15:04:05 2006"))

//Small sleeps until we need to update due to second change.
for {
if seconds != time.Now().Second() {
break
}

time.Sleep(200 * time.Millisecond)
}
}
}
116 changes: 58 additions & 58 deletions examples/example1/example1.go
Original file line number Diff line number Diff line change
@@ -1,70 +1,70 @@
package main

import (
"bytes"
"fmt"
"log"
"time"
"bytes"
"fmt"
"log"
"time"

device "github.com/d2r2/go-hd44780"
"github.com/d2r2/go-i2c"
device "github.com/d2r2/go-hd44780"
"github.com/d2r2/go-i2c"
)

func checkError(err error) {
if err != nil {
log.Fatal(err)
}
if err != nil {
log.Fatal(err)
}
}

func main() {
i2c, err := i2c.NewI2C(0x27, 2)
checkError(err)
defer i2c.Close()
lcd, err := device.NewLcd(i2c, device.LCD_16x2)
checkError(err)
err = lcd.BacklightOn()
checkError(err)
err = lcd.ShowMessage("--=! Let's rock !=--", device.SHOW_LINE_1)
checkError(err)
err = lcd.ShowMessage("Welcome to RPi dude!", device.SHOW_LINE_2)
checkError(err)
// err = lcd.ShowMessage("I'm lazy to be lazy.", device.SHOW_LINE_3)
// checkError(err)
// err = lcd.ShowMessage("R2D2, where are you?", device.SHOW_LINE_4)
// checkError(err)
time.Sleep(5 * time.Second)
for i := 0; i <= 12; i++ {
var buf bytes.Buffer
for j := 0; j <= 19; j++ {
buf.Write([]byte{byte(i*20 + j)})
}
err = lcd.ShowMessage(buf.String(), device.SHOW_LINE_1)
checkError(err)
time.Sleep(1 * time.Second)
}
time.Sleep(5 * time.Second)
err = lcd.TestWriteCGRam()
checkError(err)
for i := 0; i <= 12; i++ {
var buf bytes.Buffer
for j := 0; j <= 19; j++ {
buf.Write([]byte{byte(i*20 + j)})
}
err = lcd.ShowMessage(buf.String(), device.SHOW_LINE_1)
checkError(err)
time.Sleep(1 * time.Second)
}
lcd.Clear()
for {
lcd.Home()
t := time.Now()
lcd.SetPosition(1, 0)
fmt.Fprint(lcd, t.Format("Monday Jan 2"))
lcd.SetPosition(2, 1)
fmt.Fprint(lcd, t.Format("15:04:05 2006"))
// lcd.SetPosition(4, 0)
// fmt.Fprint(lcd, "i2c, VGA, and Go")
time.Sleep(666 * time.Millisecond)
}
i2c, err := i2c.NewI2C(0x27, 1)
checkError(err)
defer i2c.Close()
lcd, err := device.NewLcd(i2c, device.LCD_16x2)
checkError(err)
err = lcd.BacklightOn()
checkError(err)
err = lcd.ShowMessage("--=! Let's rock !=--", device.SHOW_LINE_1)
checkError(err)
err = lcd.ShowMessage("Welcome to RPi dude!", device.SHOW_LINE_2)
checkError(err)
// err = lcd.ShowMessage("I'm lazy to be lazy.", device.SHOW_LINE_3)
// checkError(err)
// err = lcd.ShowMessage("R2D2, where are you?", device.SHOW_LINE_4)
// checkError(err)
time.Sleep(5 * time.Second)
for i := 0; i <= 12; i++ {
var buf bytes.Buffer
for j := 0; j <= 19; j++ {
buf.Write([]byte{byte(i*20 + j)})
}
err = lcd.ShowMessage(buf.String(), device.SHOW_LINE_1)
checkError(err)
time.Sleep(1 * time.Second)
}
time.Sleep(5 * time.Second)
err = lcd.TestWriteCGRam()
checkError(err)
for i := 0; i <= 12; i++ {
var buf bytes.Buffer
for j := 0; j <= 19; j++ {
buf.Write([]byte{byte(i*20 + j)})
}
err = lcd.ShowMessage(buf.String(), device.SHOW_LINE_1)
checkError(err)
time.Sleep(1 * time.Second)
}
lcd.Clear()
for {
lcd.Home()
t := time.Now()
lcd.SetPosition(1, 0)
fmt.Fprint(lcd, t.Format("Monday Jan 2"))
lcd.SetPosition(2, 1)
fmt.Fprint(lcd, t.Format("15:04:05 2006"))
// lcd.SetPosition(4, 0)
// fmt.Fprint(lcd, "i2c, VGA, and Go")
time.Sleep(666 * time.Millisecond)
}

}
108 changes: 54 additions & 54 deletions examples/example2/example2.go
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,69 +1,69 @@
package main

import (
"fmt"
"log"
"strings"
"sync"
"time"
"fmt"
"log"
"strings"
"sync"
"time"

sensor "github.com/d2r2/go-dht"
device "github.com/d2r2/go-hd44780"
"github.com/d2r2/go-i2c"
sensor "github.com/d2r2/go-dht"
device "github.com/d2r2/go-hd44780"
"github.com/d2r2/go-i2c"
)

func checkError(err error) {
if err != nil {
log.Fatal(err)
}
if err != nil {
log.Fatal(err)
}
}

func writeTime(lcd *device.Lcd, t time.Time, blink bool, m *sync.Mutex) error {
msg := t.Format("02/1/06 15:04 MST")
if blink {
msg = strings.Replace(msg, ":", " ", 1)
}
m.Lock()
defer m.Unlock()
err := lcd.ShowMessage(msg, device.SHOW_LINE_1|device.SHOW_ELIPSE_IF_NOT_FIT)
if err != nil {
return err
}
return nil
msg := t.Format("02/1/06 15:04 MST")
if blink {
msg = strings.Replace(msg, ":", " ", 1)
}
m.Lock()
defer m.Unlock()
err := lcd.ShowMessage(msg, device.SHOW_LINE_1|device.SHOW_ELIPSE_IF_NOT_FIT)
if err != nil {
return err
}
return nil
}

func main() {
i2c, err := i2c.NewI2C(0x27, 2)
checkError(err)
defer i2c.Close()
lcd, err := device.NewLcd(i2c, device.LCD_20x4)
checkError(err)
err = lcd.BacklightOn()
checkError(err)
m := &sync.Mutex{}
blink := false
go func() {
//writeTime(lcd, time.Now(), blink, m)
c := time.Tick(1 * time.Second)
for t := range c {
writeTime(lcd, t, blink, m)
blink = !blink
}
}()
i2c, err := i2c.NewI2C(0x27, 1)
checkError(err)
defer i2c.Close()
lcd, err := device.NewLcd(i2c, device.LCD_20x4)
checkError(err)
err = lcd.BacklightOn()
checkError(err)
m := &sync.Mutex{}
blink := false
go func() {
//writeTime(lcd, time.Now(), blink, m)
c := time.Tick(1 * time.Second)
for t := range c {
writeTime(lcd, t, blink, m)
blink = !blink
}
}()

for {
temp, hum, _, err := sensor.ReadDHTxxWithRetry(sensor.DHT22, 4, true, 5)
if err == nil {
m.Lock()
lcd.ShowMessage(fmt.Sprintf("T: %0.0f*C Hum: %0.0f%%", temp, hum),
device.SHOW_LINE_2|device.SHOW_ELIPSE_IF_NOT_FIT)
m.Unlock()
} else {
m.Lock()
lcd.ShowMessage("DHTxx read error",
device.SHOW_LINE_2|device.SHOW_ELIPSE_IF_NOT_FIT)
m.Unlock()
}
time.Sleep(10 * time.Second)
}
for {
temp, hum, _, err := sensor.ReadDHTxxWithRetry(sensor.DHT22, 4, true, 5)
if err == nil {
m.Lock()
lcd.ShowMessage(fmt.Sprintf("T: %0.0f*C Hum: %0.0f%%", temp, hum),
device.SHOW_LINE_2|device.SHOW_ELIPSE_IF_NOT_FIT)
m.Unlock()
} else {
m.Lock()
lcd.ShowMessage("DHTxx read error",
device.SHOW_LINE_2|device.SHOW_ELIPSE_IF_NOT_FIT)
m.Unlock()
}
time.Sleep(10 * time.Second)
}
}
Loading