-
Notifications
You must be signed in to change notification settings - Fork 0
/
qwiic_template.py
133 lines (113 loc) · 5.5 KB
/
qwiic_template.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#-------------------------------------------------------------------------------
# qwiic_template.py TODO: replace template
#
# Python library for the SparkFun Qwiic TODO, available here:
# https://www.sparkfun.com/products/TODO
#-------------------------------------------------------------------------------
# Written by SparkFun Electronics, TODO: month and year
#
# This python library supports the SparkFun Electroncis Qwiic ecosystem
#
# More information on Qwiic is at https://www.sparkfun.com/qwiic
#
# Do you like this library? Help support SparkFun. Buy a board!
#===============================================================================
# Copyright (c) 2023 SparkFun Electronics
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#===============================================================================
"""
qwiic_template TODO: replace template
============
Python module for the [SparkFun Qwiic TODO](https://www.sparkfun.com/products/TODO)
This is a port of the existing [Arduino Library](https://github.com/sparkfun/TODO)
This package can be used with the overall [SparkFun Qwiic Python Package](https://github.com/sparkfun/Qwiic_Py)
New to Qwiic? Take a look at the entire [SparkFun Qwiic ecosystem](https://www.sparkfun.com/qwiic).
"""
# The Qwiic_I2C_Py platform driver is designed to work on almost any Python
# platform, check it out here: https://github.com/sparkfun/Qwiic_I2C_Py
import qwiic_i2c
# Define the device name and I2C addresses. These are set in the class defintion
# as class variables, making them avilable without having to create a class
# instance. This allows higher level logic to rapidly create a index of Qwiic
# devices at runtine
_DEFAULT_NAME = "Qwiic Template" # TODO: replace template
# Some devices have multiple available addresses - this is a list of these
# addresses. NOTE: The first address in this list is considered the default I2C
# address for the device.
_AVAILABLE_I2C_ADDRESS = [0x00, 0x01, 0x02] # TODO: set addresses
# Define the class that encapsulates the device being created. All information
# associated with this device is encapsulated by this class. The device class
# should be the only value exported from this module.
class QwiicTemplate(object): # TODO: replace template
# Set default name and I2C address(es)
device_name = _DEFAULT_NAME
available_addresses = _AVAILABLE_I2C_ADDRESS
# TODO: Define constants here
def __init__(self, address=None, i2c_driver=None):
"""
Constructor
:param address: The I2C address to use for the device
If not provided, the default address is used
:type address: int, optional
:param i2c_driver: An existing i2c driver object
If not provided, a driver object is created
:type i2c_driver: I2CDriver, optional
"""
# Use address if provided, otherwise pick the default
if address in self.available_addresses:
self.address = address
else:
self.address = self.available_addresses[0]
# Load the I2C driver if one isn't provided
if i2c_driver is None:
self._i2c = qwiic_i2c.getI2CDriver()
if self._i2c is None:
print("Unable to load I2C driver for this platform.")
return
else:
self._i2c = i2c_driver
# TODO: Initialize any variables used by this driver
def is_connected(self):
"""
Determines if this device is connected
:return: `True` if connected, otherwise `False`
:rtype: bool
"""
# Check if connected by seeing if an ACK is received
# TODO: If the device has a product ID register, that should be
# checked in addition to the ACK
return self._i2c.isDeviceConnected(self.address)
connected = property(is_connected)
def begin(self):
"""
Initializes this device with default parameters
:return: Returns `True` if successful, otherwise `False`
:rtype: bool
"""
# Confirm device is connected before doing anything
if not self.is_connected():
return False
# TODO Perform a reset of the device if possible. This reverts all
# registers to a known state in case the device was reconfigured before
# TODO: Configure device as needed. Once complete, the device should be
# fully ready to use to make it very simple for the user
# TODO: Return True once successful. Template defaults to False!
return False
# TODO: Add features for this device