gobot.io/x/gobot/v2@v2.1.0/examples/raspi_generic.go (about)

     1  //go:build example
     2  // +build example
     3  
     4  //
     5  // Do not build by default.
     6  
     7  package main
     8  
     9  import (
    10  	"fmt"
    11  	"reflect"
    12  	"time"
    13  
    14  	"gobot.io/x/gobot/v2"
    15  	"gobot.io/x/gobot/v2/drivers/i2c"
    16  	"gobot.io/x/gobot/v2/platforms/raspi"
    17  )
    18  
    19  // Wiring
    20  // PWR  Raspi: 1 (+3.3V, VCC), 6, 9, 14, 20 (GND)
    21  // I2C1 Raspi: 3 (SDA), 5 (SCL)
    22  // PCA9501: 20 (VDD, +2.5..3.6V), 10 (VSS, GND), 19 (SDA), 18 (SCL)
    23  // HW address pins: 1 (A0), 2 (A1), 3 (A2), 12 (A3), 11 (A4), 9 (A5)
    24  func main() {
    25  	const (
    26  		defaultAddress = 0x7F // with open address pins (internal pull-up)
    27  		myAddress      = 0x44 // needs to be adjusted for your configuration
    28  		eepromAddr     = uint8(0x02)
    29  		dataLen        = 7
    30  		write          = true
    31  		read           = true
    32  	)
    33  
    34  	board := raspi.NewAdaptor()
    35  	drv := i2c.NewGenericDriver(board, "PCA9501-EEPROM", defaultAddress, i2c.WithAddress(myAddress))
    36  
    37  	var valWr uint8 = 0x09
    38  	var err error
    39  
    40  	wData := make([]byte, dataLen)
    41  	rData := make([]byte, dataLen)
    42  
    43  	work := func() {
    44  		gobot.Every(500*time.Millisecond, func() {
    45  			// write "dataLen" values, starting by EEPROM address
    46  			valWr++
    47  
    48  			if write {
    49  				for i := range wData {
    50  					wData[i] = byte(i) + valWr
    51  				}
    52  				err = drv.WriteBlockData(eepromAddr, wData)
    53  				if err != nil {
    54  					fmt.Println("err write:", err)
    55  				} else if read != write {
    56  					fmt.Printf("EEPROM addr: %d, wr: %v\n", eepromAddr, wData)
    57  				}
    58  			}
    59  
    60  			// write process needs some time, so wait at least 5ms before read a value
    61  			// when decreasing to much, the check below will fail
    62  			time.Sleep(10 * time.Millisecond)
    63  
    64  			if read {
    65  				err = drv.ReadBlockData(eepromAddr, rData)
    66  				if err != nil {
    67  					fmt.Println("err read:", err)
    68  				} else if read != write {
    69  					fmt.Printf("EEPROM addr: %d, rd: %v\n", eepromAddr, rData)
    70  				}
    71  			}
    72  
    73  			// compare read and write
    74  			if read && write {
    75  				if reflect.DeepEqual(wData, rData) {
    76  					fmt.Printf("EEPROM addr: %d equal: %v\n", eepromAddr, rData)
    77  
    78  				} else {
    79  					fmt.Printf("EEPROM addr: %d wr: %v differ rd: %v\n", eepromAddr, wData, rData)
    80  				}
    81  			}
    82  		})
    83  	}
    84  
    85  	robot := gobot.NewRobot("genericDriverI2c",
    86  		[]gobot.Connection{board},
    87  		[]gobot.Device{drv},
    88  		work,
    89  	)
    90  
    91  	err = robot.Start()
    92  	if err != nil {
    93  		fmt.Println(err)
    94  	}
    95  }