Skip to content

Commit

Permalink
Merge pull request #6 from MrYsLab/spi
Browse files Browse the repository at this point in the history
Release 1.1
  • Loading branch information
MrYsLab authored Aug 3, 2021
2 parents c96ad70 + 8904292 commit 564db3b
Show file tree
Hide file tree
Showing 6 changed files with 1,416 additions and 30 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

# Telemetrix-RPi-Pico

Interact and monitor a Raspberry Pi Pico remotely from your PC using Python.
It is similar to Arduino Firmata, but written specifically for the Raspberry Pi
Pico.

![](images/tmx.png)

Expand All @@ -17,6 +20,7 @@ The following functionality is implemented in this release:
* PWM output
* Loopback (for client/server link debugging)
* I2C Support
* SPI Support
* NeoPixel Support
* Servo Support
* HC-SR04 Type Sonar Distance Sensor Support
Expand Down
4 changes: 0 additions & 4 deletions examples/hc_sr04.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""





import sys
import time

Expand Down
147 changes: 147 additions & 0 deletions examples/spi_mpu9250.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
# noinspection GrazieInspection
"""
Copyright (c) 2021 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
"""

"""
This example initializes an MPU9250 and then reads the accelerometer
and gyro values and prints them to the screen.
The processing of the data returned from the MPU9250 is done within
the callback functions.
"""

import sys
import time

from telemetrix_rpi_pico import telemetrix_rpi_pico

# Instantiate the TelemetrixRpiPico class accepting all default parameters.
pico = telemetrix_rpi_pico.TelemetrixRpiPico()

"""
CALLBACKS
These functions process the data returned from the MPU9250
"""


def the_device_callback(report):
"""
Verify the device ID
:param report: [SPI_REPORT, SPI_PORT, Number of bytes, device id]
"""
if report[3] == 0x71:
print('MPU9250 Device ID confirmed.')
else:
print(f'Unexpected device ID: {report[3]}')


# noinspection GrazieInspection
def accel_callback(report):
"""
Print the AX, AY and AZ values.
:param report: [SPI_REPORT, SPI_PORT, Number of bytes, AX-msb, AX-lsb
AY-msb, AY-lsb, AX-msb, AX-lsb]
"""
print(f"AX = {int.from_bytes(report[3:5], byteorder='big', signed=True)} "
f"AY = {int.from_bytes(report[5:7], byteorder='big', signed=True)} "
f"AZ = {int.from_bytes(report[7:9], byteorder='big', signed=True)} ")


def gyro_callback(report):
# noinspection GrazieInspection
"""
Print the GX, GY, and GZ values.
:param report: [SPI_REPORT, SPI_PORT, Number of bytes, GX-msb, GX-lsb
GY-msb, GY-lsb, GX-msb, GX-lsb]
"""
print(f"GX = {int.from_bytes(report[3:5], byteorder='big', signed=True)} "
f"GY = {int.from_bytes(report[5:7], byteorder='big', signed=True)} "
f"GZ = {int.from_bytes(report[7:9], byteorder='big', signed=True)} ")


# This is a utility function to read SPI data
def read_data_from_device(register, number_of_bytes, callback):
# noinspection GrazieInspection
"""
This function reads the number of bytes using the register value.
Data is returned via the specified callback.
:param register: register value
:param number_of_bytes: number of bytes to read
:param callback: callback function
"""
# OR in the read bit
data = register | 0x80

# activate chip select
pico.spi_cs_control(5, 0)

# select the register
pico.spi_write_blocking([data], 0)
time.sleep(.1)

# read the data back
pico.spi_read_blocking(number_of_bytes, 0, call_back=callback)

# deactivate chip select
pico.spi_cs_control(5, 1)
time.sleep(.1)


# Convenience values for the pins.
# Note that the CS value is within a list

# These are "non-standard" pin-numbers, and therefore
# the qualify_pins parameter is set to FALSe

SPI_PORT = 0
MISO = 4
MOSI = 7
CLK = 6
CS = [5]
CS_PIN = 5

NUM_BYTES_TO_READ = 6
FREQ = 500000

# initialize the device
pico.set_pin_mode_spi(SPI_PORT, MISO, MOSI, CLK,
FREQ, CS, qualify_pins=False)

# reset the device
pico.spi_cs_control(CS_PIN, 0)
pico.spi_write_blocking([0x6B, 0], SPI_PORT)
pico.spi_cs_control(CS_PIN, 1)

time.sleep(.3)

# get the device ID
read_data_from_device(0x75, 1, the_device_callback)

while True:
try:
# get the acceleration values
read_data_from_device(0x3b | 0x80, 6, accel_callback)

# get the gyro values
read_data_from_device(0x43 | 0x80, 6, gyro_callback)
time.sleep(.1)
except KeyboardInterrupt:
pico.shutdown()
sys.exit(0)
Loading

0 comments on commit 564db3b

Please sign in to comment.