A couple of months ago I started working on the WiiM8 (http://www.ramdac.be - forum thread). I finally found some time to improve it and add more options like controlling color, scale, ... with the Wii-remote, another big improvement is the addition of value smoothing as the original version was very "twitchy".
At the moment I'm looking into the back-end of the module in Xcode and optimization of my python code in the module as I believe this could be achieved with much less code.
Here's a part of my PeriodicalEvent code
Code: Select all
      x = txt[0]
      x = float(x)
      
      #x signal smoothing
      listx.append(x)
      if len(listx) == 4:
         del listx[0]
      x = (listx[0]+2*listx[1] + listx[2])/4
      #visualize value
      module.setValue ('accX',0,x)
      
      #mode selection
      if mode == 'rotate':
         x -= x0
         x = x/xdiv
         x = asin(x)
         x = degrees(x)
         module.setValue ('degX',0,x)
         modul8.setValue ('direct_layer_rotation_z',x,0)
      elif mode == 'colorize':
         x -= x0
         x = x/xdiv
         x = asin(x)
         modul8.setValue ('direct_layer_color_additiveR',x,0)
      elif mode == 'scale':
         x -= x0
         x = x/xdiv
         x = asin(x)
         modul8.setValue ('direct_layer_scale_x',x,0)
I think this part is actually quite OK, the matter is that I've copied this part 3 times, for the X, Y and Z values of the 3-axis accelerometers. I believe this could be achieved much easier using 'def' so I can define 1 function to use for the X, Y, Z values but this is where it usually goes wrong.
Any help on defining this function would be greatly appreciated.
Here's the full PeriodicalEvent code:
Code: Select all
count += elapsed
if calc == 'done'   :
   if count >= 0.04 : #25fps
      count = 0.0
      resetvalues()
            
      x = txt[0]
      x = float(x)
      
      #x signal smoothing
      listx.append(x)
      if len(listx) == 4:
         del listx[0]
      x = (listx[0]+2*listx[1] + listx[2])/4
      #visualize value
      module.setValue ('accX',0,x)
      
      #mode selection
      if mode == 'rotate':
         x -= x0
         x = x/xdiv
         x = asin(x)
         x = degrees(x)
         module.setValue ('degX',0,x)
         modul8.setValue ('direct_layer_rotation_z',x,0)
      elif mode == 'colorize':
         x -= x0
         x = x/xdiv
         x = asin(x)
         modul8.setValue ('direct_layer_color_additiveR',x,0)
      elif mode == 'scale':
         x -= x0
         x = x/xdiv
         x = asin(x)
         modul8.setValue ('direct_layer_scale_x',x,0)
         
      y = txt[1]
      y = float(y)
      
      #y signal smoothing
      listy.append(y)
      if len(listy) == 4:
         del listy[0]
      y = (listy[0]+2*listy[1] + listy[2])/4
      
      #visualize value
      module.setValue ('accY',0,y)
      #mode selection
      if mode == 'rotate':
         y -= y0
         y = y/ydiv
         y = asin(y)
         y = degrees(y)
         module.setValue ('degY',0,y)
         modul8.setValue ('direct_layer_rotation_x',y,0)
      elif mode == 'colorize':
         y -= y0
         y = y/ydiv
         y = asin(y)
         modul8.setValue ('direct_layer_color_additiveG',y,0)
      elif mode == 'scale':
         y -= y0
         y = y/ydiv
         y = asin(y)
         modul8.setValue ('direct_layer_scale_y',y,0)
      
      z = txt[2]
      z = float(z)
      
      #z signal smoothing
      listz.append(z)      
      if len(listz) == 4:
         del listz[0]
      z = (listz[0]+2*listz[1] + listz[2])/4
      
      #visualize value
      module.setValue ('accZ',0,z)
      
      #mode selection
      if mode == 'rotate':
         z -= z0
         z = z/zdiv
         z = asin(z)
         z = degrees(z)
         module.setValue ('degZ',0,z)
         modul8.setValue ('direct_layer_rotation_z',z,0)
      elif mode == 'colorize':
         z -= z0
         z = z/zdiv
         z = asin(z)
         modul8.setValue ('direct_layer_color_additiveB',z,0)
      elif mode == 'scale':
         z -= z0
         z = z/zdiv
         z = asin(z)
         modul8.setValue ('direct_layer_scale_z',z,0)
Thanks in advance,
Kevin

 .
.
 
