-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFishingGame-Joystick.py
65 lines (53 loc) · 1.92 KB
/
FishingGame-Joystick.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python
#import libraries
import sys
import time
import subprocess
#import Spacebrew
from pySpacebrew.spacebrew import Spacebrew
#create Spacebrew object
brew = Spacebrew("MRHT_Micol_Joyfishing", description="The IMU Unit wasn't giving back sensed data so we decided to use the joystick", server="192.168.1.165", port=9000)
#to use Pi as an OUTPUT
#brew.addSubscriber("name", "type")
#to use Pi as an INPUT
brew.addPublisher("up", "boolean")
brew.addPublisher("down", "boolean")
brew.addPublisher("left", "boolean")
brew.addPublisher("left", "boolean")
brew.addPublisher("buttonPress", "boolean")
#define state of the Pi, initially it is not connected
connected = False
#import SenseHat library and create an object
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
#define a frequency to which you want to listent to the Pi
CHECK_FREQ = 1.5
#start connection with spacebrew
try:
brew.start()
print("Press Ctrl-C to quit.")
#loop to get continuous data from Pi's IMU Unit
while True:
#loop function to check for joystick events
for event in sense.stick.get_events():
#if an event is found check what was pressed
print(event.action)
print(event.direction)
#the directions read by Pi are adjusted to the orientation in which the Pi is held
if event.direction == "left":
brew.publish("up", True)
elif (event.direction == "up"):
brew.publish("right", True)
elif (event.direction == "right"):
brew.publish("down", True)
elif (event.direction == "down"):
brew.publish("left", True)
elif (event.direction == "middle"):
brew.publish("buttonPressed", True)
#delay to read again
time.sleep(CHECK_FREQ)
sense.clear
#function that runs once pressed Ctrl-C
finally:
brew.stop()