Example One - Basic Read Write

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