1# !/usr/bin/env python
2# ----------------------------------------------------------------------
3# qwiic_eeprom_ex2_settings.py
4#
5# This example demonstrates how to set the various settings for a given EEPROM.
6# Read the datasheet! Each EEPROM will have specific values for:
7# Overall EEPROM size in bytes (512kbit = 64000, 256kbit = 32000)
8# Bytes per page write (64 and 128 are common)
9# Whether write polling is supported
10# ----------------------------------------------------------------------
11#
12# Written by Priyanka Makin @ SparkFun Electronics, July 2021
13#
14# This python library supports the SparkFun Electronics qwiic sensor/
15# board ecosystem on a Raspberry Pi (and compatable) single board
16# computers.
17#
18# More information on qwiic is at https://www.sparkfun.com/qwiic
19#
20# Do you like this library? Help support SparkFun by buying a board!
21#
22# ======================================================================
23# Copyright (c) 2021 SparkFun Electronics
24#
25# Permission is hereby granted, free of charge, to any person obtaining
26# a copy of this software and associated documentation files (the
27# "Software"), to deal in the Software without restriction, including
28# without limitation the rights to use, copy, modify, merge, publish,
29# distribute, sublicense, and/or sell copies of the Software, and to
30# permit persons to whom the Software is furnished to do so, subject to
31# the following conditions:
32#
33# The above copyright notice and this permission notice shall be
34# included in all copies or substantial portions of the Software.
35#
36# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
37# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
38# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
39# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
40# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
41# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
42# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
43#=======================================================================
44# Example 2
45
46from __future__ import print_function
47import qwiic_eeprom
48import time
49import sys
50
51def run_example():
52
53 print("\nSparkFun Qwiic EEPROM, Example 2\n")
54 my_eeprom = qwiic_eeprom.QwiicEEPROM()
55
56 if my_eeprom.begin() != True:
57 print("\nThe Qwiic EEPROM isn't connected to the system. Please check your connection", \
58 file=sys.stderr)
59 return
60
61 print("\nEEPROM ready!")
62
63 # Set settings for this EEPROM
64 my_eeprom.set_memory_size(512000/8) # In bytes. 512kbit = 64kbyte
65 my_eeprom.set_page_size(128) # in bytes. Has 128 byte page size.
66 my_eeprom.disable_poll_for_write_complete() # Supports I2C polling of write completion
67 my_eeprom.set_page_write_time(3) # 3 ms max write time
68
69 print("\nMem size in bytes: " + str(my_eeprom.get_memory_size()))
70 print("\nPage size in bytes: " + str(my_eeprom.get_page_size()))
71
72 my_value = -7.35
73 my_eeprom.write_float(20, my_value) # (location, data)
74 my_read = my_eeprom.read_float(20) # (location)
75 # Round to 2-decimal point precision
76 my_read = round(my_read, 2)
77 print("\nI read: " + str(my_read))
78
79if __name__ == '__main__':
80 try:
81 run_example()
82 except (KeyboardInterrupt, SystemExit) as exErr:
83 print("\nEnding Example 1")
84 sys.exit(0)