tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/examples/at24cx/main.go (about)

     1  package main
     2  
     3  import (
     4  	"machine"
     5  	"time"
     6  
     7  	"tinygo.org/x/drivers/at24cx"
     8  )
     9  
    10  func main() {
    11  	machine.I2C0.Configure(machine.I2CConfig{})
    12  
    13  	eeprom := at24cx.New(machine.I2C0)
    14  	eeprom.Configure(at24cx.Config{})
    15  
    16  	values := make([]uint8, 100)
    17  	for i := uint16(0); i < 100; i++ {
    18  		values[i] = uint8(65 + i%26)
    19  	}
    20  	_, err := eeprom.WriteAt(values, 0)
    21  	if err != nil {
    22  		println("There was an error in WriteAt:", err)
    23  		return
    24  	}
    25  
    26  	for i := uint16(0); i < 26; i++ {
    27  		err = eeprom.WriteByte(100+i, uint8(90-i))
    28  		if err != nil {
    29  			println("There was an error in WriteByte:", i, err)
    30  			return
    31  		}
    32  		time.Sleep(2 * time.Millisecond)
    33  	}
    34  
    35  	println("\n\r\n\rRead 26 bytes one by one from address 0")
    36  	println("Expected: ABCDEFGHIJKLMNOPQRSTUVWXYZ")
    37  	print("Real: ")
    38  	for i := uint16(0); i < 26; i++ {
    39  		char, err := eeprom.ReadByte(i)
    40  		print(string(char))
    41  		if err != nil {
    42  			println("There was an error in ReadByte:", i, err)
    43  			return
    44  		}
    45  	}
    46  	println("")
    47  
    48  	println("\n\r\n\rRead 100 bytes from address 26")
    49  	println("Expected: ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVZYXWVUTSRQPONMLKJIHGFEDCBA")
    50  	print("Real: ")
    51  	data := make([]byte, 100)
    52  	_, err = eeprom.ReadAt(data, 26)
    53  	if err != nil {
    54  		println("There was an error in ReadAt:", err)
    55  		return
    56  	}
    57  	for i := 0; i < 100; i++ {
    58  		print(string(data[i]))
    59  	}
    60  	println("")
    61  
    62  	// Move to the beginning of memory
    63  	eeprom.Seek(0, 0)
    64  	_, err = eeprom.Write([]uint8{88, 88, 88})
    65  	if err != nil {
    66  		println("There was an error in Write:", err)
    67  		return
    68  	}
    69  
    70  	println("\n\r\n\rRead 3 bytes")
    71  	println("Expected: DEF")
    72  	print("Real: ")
    73  	data = make([]byte, 3)
    74  	_, err = eeprom.Read(data)
    75  	if err != nil {
    76  		println("There was an error in Read:", err)
    77  		return
    78  	}
    79  	for _, char := range data {
    80  		print(string(char))
    81  	}
    82  	println("")
    83  
    84  	println("\n\r\n\rRead another 3 bytes (from the beginning this time)")
    85  	eeprom.Seek(-6, 1)
    86  	println("Expected: XXX")
    87  	print("Real: ")
    88  	data = make([]byte, 3)
    89  	_, err = eeprom.Read(data)
    90  	if err != nil {
    91  		println("There was an error in Read:", err)
    92  		return
    93  	}
    94  	for _, char := range data {
    95  		print(string(char))
    96  	}
    97  	println("")
    98  
    99  	// Move to the end of memory
   100  	eeprom.Seek(-4, 2)
   101  	_, err = eeprom.Write([]uint8{89, 90, 89, 90})
   102  	if err != nil {
   103  		println("There was an error in Write:", err)
   104  		return
   105  	}
   106  
   107  	println("\n\r\n\rRead the last 4 bytes of the memory and the 3 of the beginning")
   108  	eeprom.Seek(-4, 1)
   109  	println("Expected: YZYZXXX")
   110  	print("Real: ")
   111  	data = make([]byte, 7)
   112  	_, err = eeprom.Read(data)
   113  	if err != nil {
   114  		println("There was an error in Read:", err)
   115  		return
   116  	}
   117  	for _, char := range data {
   118  		print(string(char))
   119  	}
   120  	println("")
   121  
   122  }