1# !/usr/bin/env python
2# ----------------------------------------------------------------------
3# qwiic_eeprom_ex4_user_options.py
4#
5# This example demonstrates how to record various user settings easily
6# to EEPROM.
7# ----------------------------------------------------------------------
8#
9# Written by Priyanka Makin @ SparkFun Electronics, July 2021
10#
11# This python library supports the SparkFun Electronics qwiic sensor/
12# board ecosystem on a Raspberry Pi (and compatable) single board
13# computers.
14#
15# More information on qwiic is at https://www.sparkfun.com/qwiic
16#
17# Do you like this library? Help support SparkFun by buying a board!
18#
19# ======================================================================
20# Copyright (c) 2021 SparkFun Electronics
21#
22# Permission is hereby granted, free of charge, to any person obtaining
23# a copy of this software and associated documentation files (the
24# "Software"), to deal in the Software without restriction, including
25# without limitation the rights to use, copy, modify, merge, publish,
26# distribute, sublicense, and/or sell copies of the Software, and to
27# permit persons to whom the Software is furnished to do so, subject to
28# the following conditions:
29#
30# The above copyright notice and this permission notice shall be
31# included in all copies or substantial portions of the Software.
32#
33# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
34# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
35# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
36# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
37# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
38# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
39# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40#=======================================================================
41# Example 4
42
43from __future__ import print_function
44import qwiic_eeprom
45import time
46import sys
47# TODO: remove this?
48from collections import namedtuple
49from dataclasses import *
50
51LOCATION_SETTINGS = 0
52SETTINGS_SIZE = 10 # 2 ints + 2 bools = 10 bytes total
53
54# Load the current settings from EEPROM into the settings struct
55def load_user_settings(my_mem, settings):
56 # Uncomment these lines to forcibly erase the EEPROM and see how the defaults are set
57 # print("\nErasing EEPROM, be patient please")
58 # my_mem.erase()
59
60 # Check to see if EEPROM is blank. If the first four spots are zeros then we can assume the EEPROM is blank.
61 num_bytes = 4
62 if (my_mem.read_int(LOCATION_SETTINGS) == 0): # (EEPROM address to read, thing to read to)
63 # At power on, settings are set to defaults within the tuple
64 # So go record the tuple as it currently exists so that defaults are set
65 record_user_settings(my_mem, settings)
66
67 print("\nDefault settings applied")
68 else:
69
70 settings.baud_rate == my_mem.read_int(0)
71 settings.log_date == my_mem.read_byte(4)
72 settings.enable_IMU == my_mem.read_byte(5)
73 cal_val_temp = my_mem.read_float(6)
74 # Round to 2-decimal point precision
75 settings.cal_value == round(cal_val_temp, 2)
76
77# Record the current settings into EEPROM
78def record_user_settings(my_mem, settings):
79 # Use our individual write functions to make writing this class easier
80 my_mem.write_int(0, settings.baud_rate)
81 time.sleep(0.1)
82 my_mem.write_byte(4, settings.log_date)
83 time.sleep(0.1)
84 my_mem.write_byte(5, settings.enable_IMU)
85 time.sleep(0.1)
86 my_mem.write_float(6, settings.cal_value)
87 time.sleep(0.1)
88
89def run_example():
90
91 print("\nSparkFun Qwiic EEPROM, Example 4\n")
92 my_eeprom = qwiic_eeprom.QwiicEEPROM()
93
94 if my_eeprom.begin() != True:
95 print("\nThe Qwiic EEPROM isn't connected to the syste. Please check your connection", \
96 file=sys.stderr)
97 return
98
99 print("\nEEPROM ready!")
100
101 @dataclass
102 class Settings:
103 baud_rate: int
104 log_date: bool
105 enable_IMU: bool
106 cal_value: float
107
108 default_settings = Settings(115200, False, True, -5.17)
109
110 load_user_settings(my_eeprom, default_settings)
111
112 print("\nBaud rate: " + str(default_settings.baud_rate))
113
114 log_str = ""
115 if default_settings.log_date == True:
116 log_str = "True"
117 else:
118 log_str = "False"
119 print("\nlog_date: " + log_str)
120
121 print("\ncal_value: " + str(default_settings.cal_value))
122
123 # Now we can change something
124 print("\nEnter a new baud rate (1200 to 115200): ")
125 new_baud = input()
126 if int(new_baud) < 1200 or int(new_baud) > 115200:
127 print("\nError: baud rate out of range!")
128 else:
129 default_settings = replace(default_settings, baud_rate = int(new_baud))
130
131 print("\nThis is the new baud rate: ")
132 print(default_settings.baud_rate)
133
134 record_user_settings(my_eeprom, default_settings)
135
136if __name__ == '__main__':
137 try:
138 run_example()
139 except (KeyboardInterrupt, SystemExit) as exErr:
140 print("\nEnding Example 1")
141 sys.exit(0)
142