-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
175 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
""" | ||
Copyright (c) 2023 Alan Yorinks All rights reserved. | ||
This program is free software; you can redistribute it and/or | ||
modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE | ||
Version 3 as published by the Free Software Foundation; either | ||
or (at your option) any later version. | ||
This library is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
General Public License for more details. | ||
You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE | ||
along with this library; if not, write to the Free Software | ||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
""" | ||
|
||
import asyncio | ||
import sys | ||
from telemetrix_aio import telemetrix_aio | ||
|
||
|
||
""" | ||
This program reads x, y, and z registers of an ADXL345 using 4 Wire SPI interface | ||
""" | ||
|
||
|
||
async def the_callback(data): | ||
""" | ||
:param data: [pin_type, chip select pin, device read register, x data pair, | ||
y data pair, z data pair, time stamp] | ||
""" | ||
report_type = data[0] | ||
chip_select_pin = data[1] | ||
device_read_register = data[2] & 0x3f # strip off read command bits | ||
number_bytes_read = data[3] | ||
x_msb = data[4] | ||
x_lsb = data[5] | ||
y_msb = data[6] | ||
y_lsb = data[7] | ||
z_msb = data[8] | ||
z_lsb = data[9] | ||
|
||
x_data = (x_msb << 8) + x_lsb | ||
y_data = (y_msb << 8) + y_lsb | ||
z_data = (z_msb << 8) + z_lsb | ||
|
||
# test report type for SPI report | ||
if report_type == 13: | ||
print(f'SPI Report: CS Pin: {chip_select_pin} SPI Register: ' | ||
f'{device_read_register} Number Of Bytes Read: {number_bytes_read} x: ' | ||
f'{x_data} y: {y_data} z: {z_data}') | ||
else: | ||
print(f'unexpected report type: {report_type}') | ||
|
||
|
||
async def adxl345(my_board): | ||
""" | ||
:type my_board: object | ||
""" | ||
# initialize spi mode for chipselect on pin 10 | ||
await my_board.set_pin_mode_spi([10]) | ||
await asyncio.sleep(.3) | ||
|
||
# set the SPI format | ||
# spi speed is FPU frequency divided by 4 | ||
# data order is MSB | ||
# mode is MODE3 | ||
await my_board.spi_set_format(4, 1, 0x0c) | ||
await asyncio.sleep(.3) | ||
|
||
# set up power and control register | ||
await my_board.spi_write_blocking(10, [45, 0]) | ||
await asyncio.sleep(.3) | ||
|
||
await my_board.spi_write_blocking(10, [45, 8]) | ||
await asyncio.sleep(.3) | ||
|
||
# set up data format register for 4 wire spi | ||
await my_board.spi_write_blocking(10, [49, 0]) | ||
await asyncio.sleep(.3) | ||
|
||
# read 6 bytes from the data register | ||
# for a multibyte read, we need to OR in a 0x40 into the register value | ||
while True: | ||
# read 6 bytes from the data register | ||
try: | ||
await my_board.spi_read_blocking(10, 50 | 0x40, 6, the_callback) | ||
|
||
await asyncio.sleep(.5) | ||
|
||
except (KeyboardInterrupt, RuntimeError): | ||
await my_board.shutdown() | ||
sys.exit(0) | ||
|
||
|
||
# get the event loop | ||
loop = asyncio.new_event_loop() | ||
asyncio.set_event_loop(loop) | ||
|
||
# instantiate telemetrix_aio | ||
board = telemetrix_aio.TelemetrixAIO() | ||
|
||
try: | ||
# start the main function | ||
loop.run_until_complete(adxl345(board)) | ||
except KeyboardInterrupt: | ||
loop.run_until_complete(board.shutdown()) | ||
sys.exit(0) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.