Page 1 of 1

How to tell pi to hold and release wiimote buttons python2.7

Posted: Mon Dec 26, 2016 7:38 pm
by The programmer
As your can see at the bottom I tried to tell my pi to switch on a led when pressed and off when released with wiimote. But when_pressed and when_released is only for buttons. So how do aI do this.
Thank you for your help and support.

Code: Select all

#!/usr/bin/python
#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#|R|a|s|p|b|e|r|r|y|P|i|-|S|p|y|.|c|o|.|u|k|
#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#
# wii_remote_1.py
# Connect a Nintendo Wii Remote via Bluetooth
# and  read the button states in Python.
#
# Project URL :
# http://www.raspberrypi-spy.co.uk/?p=1101
#
# Author : Matt Hawkins
# Date   : 30/01/2013

# -----------------------
# Import required Python libraries
# -----------------------
import cwiid
import time
from gpiozero import Button,LED
from time import sleep
#import RPi.GPIO as io

#io.setmode(io.BCM)
#pins = (2,3,4,17)
#for i in pins:
#  io.setup(i,io.OUT)

led=LED(17)
led1=LED(3)
button_delay = 0.1

print 'Press 1 + 2 on your Wii Remote now ...'
time.sleep(1)

# Connect to the Wii Remote. If it times out
# then quit.
try:
  wii=cwiid.Wiimote()
except RuntimeError:
  print "Error opening wiimote connection"
  quit()

print 'Wii Remote connected...\n'
wii.rpt_mode = cwiid.RPT_BTN
while True:

  buttons = wii.state['buttons']

  # If Plus and Minus buttons pressed
  # together then rumble and quit.
  if (buttons - cwiid.BTN_PLUS - cwiid.BTN_MINUS == 0):  
    print '\nClosing connection ...'
    wii.rumble = 1
    time.sleep(1)
    wii.rumble = 0
    exit(wii)  
  
  # Check if other buttons are pressed by
  # doing a bitwise AND of the buttons number
  # and the predefined constant for that button.
  if (buttons & cwiid.BTN_UP):
      print 'Up pressed'        
      time.sleep(button_delay)          
      #io.output(4, True)
      led.when_pressed=led.on()
      led.when_released=led.off()
    
  if (buttons & cwiid.BTN_DOWN):
      print 'Down pressed'      
      time.sleep(button_delay)  
      #io.output(17, True)
      led1.when_pressed=led1.on()
      led1.when_released=led1.off()
      

Re: How to tell pi to hold and release wiimote buttons pytho

Posted: Mon Dec 26, 2016 10:17 pm
by alexeames
You seem to be trying to use GPIO Zero to read a Wiimote button press. But you've already found out you can't do that.

The only way to read button presses on a Wiimote with CWiid is to repeatedly read the button states (poll) and have some logic which switches the LED off when the button is not pressed and on when it is pressed. It's not that hard. Have a go. :D

Something like this (for the down button) would do you...

Code: Select all

  if (buttons & cwiid.BTN_DOWN):
      print 'Down pressed'      
      time.sleep(button_delay)  
      led1.on()
  else:
      led1.off()

Re: How to tell pi to hold and release wiimote buttons pytho

Posted: Tue Dec 27, 2016 1:48 pm
by The programmer
Wow that was so easy.
I don't know why i did not do that.

Re: How to tell pi to hold and release wiimote buttons pytho

Posted: Tue Dec 27, 2016 1:49 pm
by The programmer
Do you know how to use the accelerometer, giroscope.

Re: How to tell pi to hold and release wiimote buttons pytho

Posted: Tue Dec 27, 2016 4:58 pm
by alexeames
The programmer wrote:Do you know how to use the accelerometer, giroscope.
I have done before, but I don't remember how I did it. It's probably not hard to find with google though.

I'd google something like this...

"using CWiid to read accelerometer", and I bet that will show you how to do it. :D