mark.sea...@gmail.com wrote:
I'm trying to make a script environment with datatypes (or classes)
for accessing hardware registers.  At the top level, I would like the
ability to bitwise ops if bit slice brackets are used, but if no
brackets are used, I would like it to write/read the whole value.

For example, if I have something like:

shadow_register = MyRegClass(0xAA)
shadow_register
170
shadow_register[7:4] = 3  # <== changes value to 0x3A
shadow_register
58
shadow_register = 0x89
shadow_register
137
shadow_register[4:1]
4

I have the bitslice part working.  But of course as expected, if I
type
shadow_register
<__main__.boo object at 0x00A130D0>

I wanted to avoid having something like shadow_register.value just
because it's clumsier.  I read about the __new__() class for
overriding the classtype, like:

print 'foo'
class foo(object):
    def __new__(*args): return 0

but if I stick in a __init__(self, val): method, then it chokes saying
val is a global name that's not defined.

Now I know that I have to live with the fact that I can't have
shadow_register = 0x89
Because it will get reassigned from my class value to a newly
intialized memory location (int object).  But can I have it read the
value without the .value extension?  Is this even possible?  Maybe
there's a way to override the = operator?  (Go easy on me - I'm a
hardware guy).

Interesting what you're doing. I've struggled with the same issues, simulating a pic,
never really solved them.
Maybe this is what you're looking for:

class MyRegClass ( object ) :
 def __init__ ( self, value ) :
   self.Value = value
 def __repr__ ( self ) :
   line = hex ( self.Value )
   line = line [:2] + line [2:].upper()
   return line

btw, I'm a hardware guy too, and therefor I've never understood why the hex function returns lowercase ;-)

cheers,
Stef

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to