Example Five - Interface Test

examples/qwiic_eeprom_ex5_interface_test.py
  1# !/usr/bin/env python
  2# ----------------------------------------------------------------------
  3# qwiic_eeprom_ex5_interface_test.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 5
 41
 42from __future__ import print_function
 43import qwiic_eeprom
 44import time
 45import sys
 46import random
 47
 48def run_example():
 49
 50    print("\nSparkFun Qwiic EEPROM, Example 5\n")
 51    my_eeprom = qwiic_eeprom.QwiicEEPROM()
 52
 53    if my_eeprom.begin() != True:
 54        print("\nThe Qwiic EEPROM isn't connected to the system. Please check your connection", \
 55            file=sys.stderr)
 56        return
 57    
 58    print("\nEEPROM ready!")
 59
 60    my_eeprom.set_memory_size(51200 / 8)    # Qwiic EEPROM is 24512C (512kbit)
 61    # my_eeprom.set_page_size(128)
 62    # my_eeprom.disable_poll_for_write_complete()
 63
 64    all_tests_passed = True
 65
 66    print("\nMem size in bytes: " + str(my_eeprom.length()))
 67
 68    # Erase test
 69    # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 70    print("\nErasing EEPROM, please be patient!")
 71    start_time = time.time()
 72    my_eeprom.erase()
 73    end_time = time.time()
 74    print("\nTime to erase all EEPROM: " + str(end_time  - start_time) + "s")
 75
 76    # Byte sequential test
 77    # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 78    print("\n")
 79    print("\n8 bit tests")
 80    my_value1 = 200
 81    my_value2 = 23
 82    random_location = random.randrange(0, my_eeprom.length())
 83
 84    start_time = time.time()
 85    my_eeprom.write_byte(random_location, my_value1)   # (location, data)
 86    end_time = time.time()
 87    my_eeprom.write_byte(random_location + 1, my_value2)
 88    print("\nTime to record byte: " + str(end_time - start_time) + " s")
 89
 90    start_time = time.time()
 91    my_eeprom.write_byte(random_location, my_value1)    # (location, data)
 92    end_time = time.time()
 93    print("\nTime to write identical byte to same location (should be ~0s): " + str(end_time - start_time) + " s")
 94
 95    start_time = time.time()
 96    response1 = my_eeprom.read_byte(random_location)
 97    end_time = time.time()
 98    print("\nTime to read byte: " + str(end_time - start_time) + " s")
 99
100    response2 = my_eeprom.read_byte(random_location + 1)
101    print("\nLocation " + str(random_location) + " should be " + str(my_value1) + ": " + str(response1))
102    print("\nLocation " + str(random_location + 1) + " should be " + str(my_value2) + ": " + str(response2))
103    
104    if my_value1 != response1:
105        all_tests_passed = False
106    if my_value2 != response2:
107        all_tests_passed = False
108    
109    # 32 bit test
110    # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
111    print("\n")
112    print("\n32 bit tests")
113
114    my_value3 = -245000
115    my_value4 = 400123
116    random_location = random.randrange(0, my_eeprom.length())
117
118    start_time = time.time()
119    my_eeprom.write_int(random_location, my_value3)
120    end_time = time.time()
121    print("\nTime to record int32: " + str(end_time - start_time) + " s")
122    my_eeprom.write_int(random_location + 4, my_value4)
123
124    start_time = time.time()
125    response3 = my_eeprom.read_int(random_location)
126    end_time = time.time()
127    print("\nTime to read 32 bits: " + str(end_time - start_time) + " s")
128
129    response4 = my_eeprom.read_int(random_location + 4)
130    print("\nLocation " + str(random_location) + " should be " + str(my_value3) + ": " + str(response3))
131    print("\nLocation " + str(random_location + 4) + " should be " + str(my_value4) + ": " + str(response4))
132
133    if my_value3 != response3:
134        all_tests_passed = False
135    if my_value4 != response4:
136        all_tests_passed = False
137    
138    # 32 bit sequential test
139    # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
140    my_value5 = -341002
141    my_value6 = 241544
142    random_location = random.randrange(0, my_eeprom.length())
143
144    my_eeprom.write_int(random_location, my_value5)
145    my_eeprom.write_int(random_location + 4, my_value6)
146
147    start_time = time.time()
148    response5 = my_eeprom.read_int(random_location)
149    end_time = time.time()
150    print("\nTime to read 32 bits: " + str(end_time - start_time) + " s")
151
152    response6 = my_eeprom.read_int(random_location + 4)
153    print("\nLocation " + str(random_location) + " should be " + str(my_value5) + ": " + str(response5))
154    print("\nLocation " + str(random_location + 4) + " should be " + str(my_value6) + ": " + str(response6))
155
156    if my_value5 != response5:
157        all_tests_passed = False
158    if my_value6 != response6:
159        all_tests_passed = False
160    
161    # Float sequential test
162    # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
163    my_value7 = -7.35
164    my_value8 = 5.22
165    random_location = random.randrange(0, my_eeprom.length())
166
167    my_eeprom.write_float(random_location, my_value7)
168    my_eeprom.write_float(random_location + 4, my_value8)
169
170    start_time = time.time()
171    response7 = my_eeprom.read_float(random_location)
172    end_time = time.time()
173    print("\nTime to read float: " + str(end_time - start_time) + " s")
174    
175    response8 = my_eeprom.read_float(random_location + 4)
176    
177    # Round floats read to 2-decimal point precision
178    response7 = round(response7, 2)
179    response8 = round(response8, 2)
180
181    print("\nLocation " + str(random_location) + " should be " + str(my_value7) + ": " + str(response7))
182    print("\nLocation " + str(random_location + 4) + " should be " + str(my_value8) + ": " + str(response8))
183
184    if my_value7 != response7:
185        all_tests_passed = False
186    if my_value8 != response8:
187        all_tests_passed = False
188    
189    # 64 bits sequential test
190    # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
191    print("\n")
192    print("\n64 bit tests")
193    
194    # Made up list of 64-bits
195    my_value9 = [0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]
196    random_location = random.randrange(0, my_eeprom.length())
197
198    start_time = time.time()
199    my_eeprom.write(random_location, my_value9)
200    end_time = time.time()
201    print("\nTime to record 64 bits: " + str(end_time - start_time) + " s")
202
203    start_time = time.time()
204    response9 = my_eeprom.read(random_location, len(my_value9))
205    end_time = time.time()
206    print("\nTime to read 64 bits: " + str(end_time - start_time) + " s")
207
208    print("\nLocation " + str(random_location) + " should be " + str(my_value9) + ": " + str(response9))
209 
210    if my_value9 != response9:
211        all_tests_passed = False
212    
213    # Buffer write test
214    # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
215    print("\n")
216    print("\nBuffer write test")
217        
218    my_chars = "Lorem ipsum dolor sit amet, has in verte rem accusamus. Nulla viderer inciderint eum at."
219    random_location = random.randrange(0, my_eeprom.length() - len(my_chars))
220    
221    est_time = len(my_chars) / my_eeprom.get_page_size() * my_eeprom.get_page_write_time()
222    print("\nCalculated time to record array of " + str(len(my_chars)) + " characters: ~" + str(est_time) + " ms")
223
224    start_time = time.time()
225    my_eeprom.write_string(random_location, my_chars)
226    end_time = time.time()
227    print("\nTime to record string: " + str(end_time - start_time) + " s")
228
229    start_time = time.time()
230    read_chars = my_eeprom.read_string(random_location, len(my_chars))
231    end_time = time.time()
232    print("\nTime to read string: " + str(end_time - start_time) + " s")
233
234    print("\nLocation " + str(random_location) + " string should read:\n" + my_chars)
235    print("\n" + read_chars)
236
237    if my_chars != read_chars:
238        print("\nString compare failed")
239        all_tests_passed = False
240
241    # # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
242    if all_tests_passed == True:
243        print("\nAll tests PASSED!")
244    else:
245        print("\nOne or more tests failed. See output")
246
247if __name__ == '__main__':
248    try:
249        run_example()
250    except (KeyboardInterrupt, SystemExit) as exErr:
251        print("\nEnding Example 1")
252        sys.exit(0)