Beispiel #1
0
func main() {
	mega := tmega{
		bus:            i2c.NewI2CBus(0),
		Addr:           0x78,
		ReadRetryDelay: 10 * time.Millisecond,
	}
	var err error
	var msg megaMsg
	time.Sleep(mega.ReadRetryDelay)
	if err = mega.SendUART(0x30, true); err != nil {
		log.Printf("SendUART error %v", err)
	}
	time.Sleep(mega.ReadRetryDelay)
	if msg, err = mega.ReadMsg(1 * time.Second); err != nil {
		log.Printf("Read error %v", err)
	}
	log.Printf("ReadMsg %s", msg.String())
	time.Sleep(mega.ReadRetryDelay)
	if err = mega.SendUART(0x30, false); err != nil {
		log.Printf("SendUART error %v", err)
	}
	time.Sleep(mega.ReadRetryDelay)
	if msg, err = mega.ReadMsg(1 * time.Second); err != nil {
		log.Printf("Read error %v", err)
	}
	log.Printf("ReadMsg %s", msg.String())
}
Beispiel #2
0
func main1() {
	addr := byte(0x70)
	twi := i2c.NewI2CBus(0)

	//buf := []byte{0x80, 0x00, 0x74}
	//err := twi.WriteBytes(addr, buf)
	//log.Printf("write err=%v", err)
	//time.Sleep(10 * time.Millisecond)
	//for i := 0; i < len(buf); i++ {
	//  	buf[i] = 0
	//}
	//log.Printf("read err=%v buf=%02x%02x%02x", err, buf[0], buf[1], buf[2])

	for {
		b, err := twi.ReadByte(addr)
		if err == nil {
			log.Printf("read %02x", b)
		} else {
			log.Printf("read err=%v", err)
		}
		time.Sleep(100 * time.Millisecond)
	}
	// twi.
}