Raspberry Pi Pico W

Okocer

Decapat
Katılım
26 Mayıs 2021
Mesajlar
23
Daha fazla  
Cinsiyet
Erkek
Merhabalar, daha yeni bir Raspberry Pi Pico W aldım ve bugün Samm Market youtube kanalından şu videodaki projeyi denedim:
Bu içeriği görüntülemek için üçüncü taraf çerezlerini yerleştirmek için izninize ihtiyacımız olacak.
Daha detaylı bilgi için, çerezler sayfamıza bakınız.

Kod da şu:

Kod:
import max7219
import machine
from machine import Pin, SPI
from time import sleep
import utime
import ustruct
import sys


################ ADXL 345'i BASLATIP, OLCUME HAZIR HALE GETIRIYORUZ. ##############
ADXL343_ADDR = 0x53
REG_DEVID = 0x00
REG_POWER_CTL = 0x2D
REG_DATAX0 = 0x32
DEVID = 0xE5
SENSITIVITY_2G = 1.0 / 256
i2c = machine.I2C(0,
scl=machine.Pin(17),
sda=machine.Pin(16),
freq=400000)

def reg_write(i2c, addr, reg, data):

msg = bytearray()
msg.append(data)

i2c.writeto_mem(addr, reg, msg)

def reg_read(i2c, addr, reg, nbytes=1):

if nbytes < 1:
return bytearray()

data = i2c.readfrom_mem(addr, reg, nbytes)

return data

data = reg_read(i2c, ADXL343_ADDR, REG_DEVID)
if (data != bytearray((DEVID,))):
print("ERROR: Could not communicate with ADXL345")
sys.exit()


data = reg_read(i2c, ADXL343_ADDR, REG_POWER_CTL)
data = int.from_bytes(data, "big") | (1 << 3)
reg_write(i2c, ADXL343_ADDR, REG_POWER_CTL, data)
data = reg_read(i2c, ADXL343_ADDR, REG_POWER_CTL)

############# ADXL 345 AYARLAMA BITTI ################

############ MAX7219'U BASLATIP AYARLIYORUZ ###############

spi = SPI(0, baudrate=10000000, polarity=1, phase=0, sck=Pin(2), mosi=Pin(3))
ss = Pin(5, Pin.OUT)

display = max7219.Matrix8x8(spi, ss, 4)
display.brightness(15) # ekran parlakligini ayarliyoruz.

############# MAX7219 AYARLAMA BITTI ##############

#While True içinde kullanacagimiz degiskenleri burada
#sifirlayip baslatmayinca hata aldigim icin bunlar burada...

x_guncel=0
y_guncel=0
z_guncel=0
x_gecmis=0
y_gecmis=0
z_gecmis=0
x_fark=0
y_fark=0
z_fark=0
sure=0

sleep(2.0)

while True:
sleep(0.2)
sure += 1 # Her 200ms'de bir sure degiskeni 1 artiyor. Her seyi buna gore yapiyoruz.
#print (sure)
if sure == 5: # sure 5'e ulasinca (yani 1 saniye gecince 200ms X 5 = 1sn) ADXL345'e bir olcum yaptiriyoruz.
data = reg_read(i2c, ADXL343_ADDR, REG_DATAX0, 6)
x_guncel = ustruct.unpack_from("<h", data, 0)[0]
y_guncel = ustruct.unpack_from("<h", data, 2)[0]
z_guncel = ustruct.unpack_from("<h", data, 4)[0]
#print (x_guncel, " ", y_guncel, " ", z_guncel)

if sure ==10: # aradan 2 saniye gecince bir olcum daha yaptiriyoruz.
data = reg_read(i2c, ADXL343_ADDR, REG_DATAX0, 6)
x_gecmis = ustruct.unpack_from("<h", data, 0)[0]
y_gecmis = ustruct.unpack_from("<h", data, 2)[0]
z_gecmis = ustruct.unpack_from("<h", data, 4)[0]
#print (x_gecmis, " ", y_gecmis, " ", z_gecmis)


x_fark = (x_guncel - x_gecmis) # iki olcum arasindaki farki hesapliyoruz.
y_fark = (y_guncel - y_gecmis)
z_fark = (z_guncel - z_gecmis)
#print (x_fark, " ", y_fark, " ", z_fark)

if -5 < x_fark < 5 and -5 < y_fark < 5 and -5 < z_fark < 5: #eger fark 10'dan kucukse hareket algilanmamis varsayiyoruz.

display.fill(0) # ekranda birsey gosterme
display.show()

else: # eger fark 10'dan buyukse...

if sure == 2 or sure == 4 or sure == 6 or sure == 8 or sure == 10: #ve eger sure 2,4,6,8,10 ise ekranı yak.

display.fill(1)
display.show()

else: #eger sure 1,3,5,7,9 ise ekrani sondur.
display.fill(0)
display.show()

if sure == 10: # sure 10 olunca sifira alip, basa sar.
sure = 0



Ancak çalıştırmaya çalıştığımda şu hatayı alıyorum:

Traceback (most recent call last):
File "<stdin>", line 38, in <module>
File "<stdin>", line 34, in reg_read
OSError: [Errno 5] EIO

Şimdiden teşekkürler.

Tabi bunun yanında bir de dot matrix için max7219.py kodu da var onu da aşağıya ekledim:

"""
MicroPython max7219 cascadable 8x8 LED matrix driver
github.com
GitHub - mcauser/micropython-max7219: MicroPython driver for MAX7219 8x8 LED matrix modules, cascadable and with framebuf
MicroPython driver for MAX7219 8x8 LED matrix modules, cascadable and with framebuf - GitHub - mcauser/micropython-max7219: MicroPython driver for MAX7219 8x8 LED matrix modules, cascadable and wit...
github.com github.com

MIT License
Copyright (c) 2017 Mike Causer

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.
"""

from micropython import const
import framebuf

_NOOP = const(0)
_DIGIT0 = const(1)
_DECODEMODE = const(9)
_INTENSITY = const(10)
_SCANLIMIT = const(11)
_SHUTDOWN = const(12)
_DISPLAYTEST = const(15)

class Matrix8x8:
def __init__(self, spi, cs, num):
"""
Driver for cascading MAX7219 8x8 LED matrices.

>>> import max7219
>>> from machine import Pin, SPI
>>> spi = SPI(1)
>>> display = max7219.Matrix8x8(spi, Pin('X5'), 4)
>>> display.text('1234',0,0,1)
>>> display.show()

"""
self.spi = spi
self.cs = cs
self.cs.init(cs.OUT, True)
self.buffer = bytearray(8 * num)
self.num = num
fb = framebuf.FrameBuffer(self.buffer, 8 * num, 8, framebuf.MONO_HLSB)
self.framebuf = fb
# Provide methods for accessing FrameBuffer graphics primitives. This is a workround
# because inheritance from a native class is currently unsupported.
# framebuf — frame buffer manipulation — MicroPython latest documentation
self.fill = fb.fill # (col)
self.pixel = fb.pixel # (x, y[, c])
self.hline = fb.hline # (x, y, w, col)
self.vline = fb.vline # (x, y, h, col)
self.line = fb.line # (x1, y1, x2, y2, col)
self.rect = fb.rect # (x, y, w, h, col)
self.fill_rect = fb.fill_rect # (x, y, w, h, col)
self.text = fb.text # (string, x, y, col=1)
self.scroll = fb.scroll # (dx, dy)
self.blit = fb.blit # (fbuf, x, y[, key])
self.init()

def _write(self, command, data):
self.cs(0)
for m in range(self.num):
self.spi.write(bytearray([command, data]))
self.cs(1)

def init(self):
for command, data in (
(_SHUTDOWN, 0),
(_DISPLAYTEST, 0),
(_SCANLIMIT, 7),
(_DECODEMODE, 0),
(_SHUTDOWN, 1),
):
self._write(command, data)

def brightness(self, value):
if not 0 <= value <= 15:
raise ValueError("Brightness out of range")
self._write(_INTENSITY, value)

def show(self):
for y in range(8):
self.cs(0)
for m in range(self.num):
self.spi.write(bytearray([_DIGIT0 + y, self.buffer[(y * self.num) + m]]))
self.cs(1)
 

Geri
Yukarı