Call method and passed as a parameter

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
giants
Posts: 44
Joined: Fri Apr 26, 2019 2:07 pm

Call method and passed as a parameter

Post by giants » Sat Jun 15, 2019 10:50 am

Hi there !

Here i'm for ask a help. I start to wrote my first own class but gave me this error : AttributeError:'fd77? object has no attribute 'scan'
I don't understand where the problem is. I post a little part of the code here .

Library :

class rfd77(object):
def __init__ (self , data, clock):
self.clock = clock
self.data = data
print(clock)
print(data)
device = i2c.scan()
print (device)

def scan(self):
i2c.readfrom_mem_into(0x4C,0x28,buf)
print(buf[1])
print(buf[0])


Code:
from machine import Pin, I2C
import RFD77402
from time import sleep

dist = rfd77 (5,4)
dist.scan()


So if i call dis = fd77(5,4) work no problem when i call the method gave me the problem ,, another thing i don't understand is because
if i wrote this

i2c = I2C(-1, scl=Pin(self.clock), sda=Pin(self.sda), freq=400000)

doesn't work

Thank you so much
Sergio

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Call method and passed as a parameter

Post by jimmo » Sat Jun 15, 2019 11:21 am

Hi,

Can you post a more complete example of the code (ideally the whole file). I'm unsure where you're putting this line

Code: Select all

i2c = I2C(-1, scl=Pin(self.clock), sda=Pin(self.sda), freq=400000) 
The general convention for drivers is to have the constructor take an instance of I2C (or SPI, etc), rather than the pins directly. This gives the user of the class more flexibility, and also makes it easier to support different board types.

For example, https://github.com/micropython/micropyt ... ssd1306.py

Code: Select all

class SSD1306_I2C(SSD1306):
    def __init__(self, width, height, i2c, addr=0x3c, external_vcc=False):
        self.i2c = i2c
Then I use this with:

Code: Select all

from machine import I2C, Pin
from ssd1306 import SSD1306_I2C

i2c =  I2C(scl=Pin(4), sda=Pin(5), freq=400000) 
display = SSD1306_I2C(128, 64, i2c)

giants
Posts: 44
Joined: Fri Apr 26, 2019 2:07 pm

Re: Call method and passed as a parameter

Post by giants » Sun Jun 16, 2019 7:14 am

Hi ,
this is my library just start to grow up:
from machine import Pin, I2C
from micropython import const
from time import sleep
""" Costanti """

ICSR = const(0x00)IER = const(0x02)
Cmd_R = const(0x04)
Dev_Status_R = const(0x06)
Rslt_R = const(0x08)
RsltCnfdR = const(0x0A)
Cmd_CfgR_A = const(0x0C)
Cmd_CfgR_B = const(0x0E)
H2M_mbx = const(0x10)
M2H_mbx = const(0x12)P
MU_Cfg = const(0x14)
I2C_Ad_ptr = const(0x18)
I2C_Data_port = const(0x1A)
I2C_Init_cfg = const(0x1C)
MCPU_PM_Ctrl = const(0x1E)
HFCfg_0 = const(0x20)
HFCfg_1 = const(0x22)
HFCfg_2 = const(0x24)
HFCfg_3 = const(0x26)
Mod_Chip_ID = const(0x28)
Ptch_MCfg = const(0x2A)

""" Variabili """

buf = bytearray(6)



class rfd77(object):
def __init__ (self , data, clock):
self.clock = clock
self.data = data
i2c = I2C(-1, scl=Pin(self.clock), sda=Pin(self.data), freq=400000)
print(clock)
print(data)
device = i2c.scan()
print (device)

def scan(self):
self.i2c.readfrom_mem_into(0x4C,0x28,buf)
print(buf[1])
print(buf[0])
-----------------------------------------------------------------------------------

CODE:

from machine import Pin, I2C
import RFD77402
from time import sleep


dist = rfd77 (5,4)
dist.scan()


This is all my code for the moment, i state to move the first step in python and really some times i have a lot of doubt ., for luck this forum help me a lot. Can you help me to understand because when i call dis.scan() gave me an error i wrote in the fist post ? . I saw your file thank you now i print and i study.
Have a good day
Sergio

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Call method and passed as a parameter

Post by jimmo » Sun Jun 16, 2019 10:48 am

Inside `def scan()`, it refers to self.i2c, but you in `__init__`, the code never sets `self.i2c`.

So you need to change it to:

Code: Select all

class rfd77(object): 
  def __init__ (self , data, clock): 
    self.clock = clock 
    self.data = data
    i2c = I2C(-1, scl=Pin(self.clock), sda=Pin(self.data), freq=400000) 
    print(clock) 
    print(data) 
    device = self.i2c.scan() 
    print (device) 
But I recommend changing it to take the i2c instance, rather than the individual pins

Code: Select all

class rfd77(object): 
  def __init__ (self , i2c): 
    self.i2c = i2c
    device = self.i2c.scan() 
    print (device) 
then when you use your rfd77 class:

Code: Select all

dist = rfd77 (I2C(-1, scl=Pin(self.clock), sda=Pin(self.data), freq=400000) )
dist.scan()

giants
Posts: 44
Joined: Fri Apr 26, 2019 2:07 pm

Re: Call method and passed as a parameter

Post by giants » Sun Jun 16, 2019 3:10 pm

Hi,
i try to did but the first part work the scene doesn't this is the error compiler wrote to me


download ok
exec(open('main.py').read(),globals())
[76]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 15, in <module>
AttributeError: 'rfd77' object has no attribute 'scan'

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Call method and passed as a parameter

Post by jimmo » Sun Jun 16, 2019 5:47 pm

Is your scan method indented the same as __init__ ? (i.e. the same number of spaces in front of it). Sorry I can't tell because the formatting is lost.

When you past code into the forum, select it and then press the </> button so the formatting is preserved.

giants
Posts: 44
Joined: Fri Apr 26, 2019 2:07 pm

Re: Call method and passed as a parameter

Post by giants » Sun Jun 16, 2019 8:37 pm

Code: Select all

from machine import Pin, I2C
from micropython import const
from time import sleep
""" Costanti """

ICSR = const(0x00)IER = const(0x02)
Cmd_R = const(0x04)
Dev_Status_R = const(0x06)
Rslt_R = const(0x08)
RsltCnfdR = const(0x0A)
Cmd_CfgR_A = const(0x0C)
Cmd_CfgR_B = const(0x0E)
H2M_mbx = const(0x10)
M2H_mbx = const(0x12)P
MU_Cfg = const(0x14)
I2C_Ad_ptr = const(0x18)
I2C_Data_port = const(0x1A)
I2C_Init_cfg = const(0x1C)
MCPU_PM_Ctrl = const(0x1E)
HFCfg_0 = const(0x20)
HFCfg_1 = const(0x22)
HFCfg_2 = const(0x24)
HFCfg_3 = const(0x26)
Mod_Chip_ID = const(0x28)
Ptch_MCfg = const(0x2A)

""" Variabili """

buf = bytearray(6)



class rfd77(object): 
  def __init__ (self , i2c): 
    self.i2c = i2c
    device = self.i2c.scan() 
    print (device) 

    def scan(self): 
    self.i2c.readfrom_mem_into(0x4C,0x28,buf) 
    print(buf[1]) 
    print(buf[0])

Code: Select all

dist = rfd77 (I2C(-1, scl=Pin(self.clock), sda=Pin(self.data), freq=400000) )
dist.scan()

giants
Posts: 44
Joined: Fri Apr 26, 2019 2:07 pm

Re: Call method and passed as a parameter

Post by giants » Sun Jun 16, 2019 8:38 pm

and this is the message gave to me

Code: Select all

download ok
exec(open('main.py').read(),globals())
[76]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 15, in <module>
AttributeError: 'rfd77' object has no attribute 'scan'
Thank you

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Call method and passed as a parameter

Post by jimmo » Sun Jun 16, 2019 10:00 pm

Sorry I meant like this:

Code: Select all

class rfd77(object): 
  def __init__ (self , i2c): 
    self.i2c = i2c
    device = self.i2c.scan() 
    print (device) 

  def scan(self): 
    self.i2c.readfrom_mem_into(0x4C,0x28,buf) 
    print(buf[1]) 
    print(buf[0])

giants
Posts: 44
Joined: Fri Apr 26, 2019 2:07 pm

Re: Call method and passed as a parameter

Post by giants » Mon Jun 17, 2019 6:10 am

Please forgive me
i wrote in your same way but still doesn't work :cry: :cry: :cry:

same error

Code: Select all

download ok
exec(open('main.py').read(),globals())
[76]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 15, in <module>
AttributeError: 'rfd77' object has no attribute 'scan'
I use UPyCraftv1.1 do you use other software ? really i don't understand

Sergio

Post Reply