|
1 | 1 | #!/usr/bin/python
|
2 |
| - |
3 |
| -import RPi.GPIO as GPIO |
4 |
| -import spidev |
5 |
| -import os |
6 | 2 | import time
|
7 | 3 |
|
| 4 | +# Import SPI library (for hardware SPI) and MCP3008 library. |
| 5 | +import Adafruit_GPIO.SPI as SPI |
| 6 | +import Adafruit_MCP3008 |
| 7 | + |
| 8 | +# Hardware SPI configuration: |
| 9 | +SPI_PORT = 0 |
| 10 | +SPI_DEVICE = 0 |
| 11 | +mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE)) |
| 12 | + |
8 | 13 | #
|
9 | 14 | # Define channels
|
10 | 15 | #
|
11 | 16 | # Left joystick(below one) horizantal movement channel
|
12 |
| -ch_l_joystick_hor = 0 |
| 17 | +ch_leftJS_hor = 0 |
13 | 18 | # Left joystick(below one) vertex movement channel
|
14 |
| -ch_l_joystick_ver = 1 |
| 19 | +ch_leftJS_ver = 1 |
15 | 20 | # Right joystick(upper one) horizantal movement channel
|
16 |
| -ch_r_joystick_hor = 6 |
| 21 | +ch_rightJS_hor = 6 |
17 | 22 | # Right joystick(upper one) vertex movement channel
|
18 |
| -ch_r_joystick_ver = 7 |
19 |
| - |
20 |
| -# Set raspberry pi pins |
21 |
| -GPIO.setup(2, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) |
22 |
| -GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) |
23 |
| -GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) |
24 |
| -GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) |
25 |
| - |
26 |
| -# Assign buttons to the variables |
27 |
| -right_button = GPIO.input(2) |
28 |
| -top_button = GPIO.input(3) |
29 |
| -left_button = GPIO.input(4) |
30 |
| -bottom_button = GPIO.input(17) |
| 23 | +ch_rightJS_ver = 7 |
31 | 24 |
|
32 | 25 | # Delay in seconds
|
33 | 26 | delay = 0.5
|
34 | 27 |
|
35 |
| -# SPI constants |
36 |
| -spi_port = 0 |
37 |
| -spi_device = 0 |
38 |
| - |
39 |
| -# Create spi object |
40 |
| -spi = spidev.SpiDev() |
41 |
| -spi.open(spi_port, spi_device) |
42 |
| - |
43 | 28 | def readChannel(channel):
|
44 |
| - # read SPI data from MCP3008 channel |
45 |
| - r = spi.xfer2([1, 8 + channel << 4, 0]) |
46 |
| - data = ((r[1] & 3) << 8) + r[2] |
47 |
| - return data |
48 |
| - |
49 |
| -def decimal2hex(num): |
50 |
| - # convert signed decimal number to hexadeicmal |
51 |
| - if num < 0: |
52 |
| - return hex((1 << 8) + num) |
53 |
| - else: |
54 |
| - return hex(num) |
| 29 | + # read SPI data from MCP3008 given channel |
| 30 | + return mcp.read_adc(channel)/8 |
55 | 31 |
|
56 | 32 | def writeReport(report):
|
57 | 33 | with open('/dev/hidg0', 'rb+') as fd:
|
58 |
| - fd.write(report) |
| 34 | + fd.write(report.encode()) |
59 | 35 |
|
60 | 36 | while True:
|
61 |
| - # Read joysticks positions |
62 |
| - ljs_x = readChannel(ch_l_joystick_hor) |
63 |
| - ljs_y = readChannel(ch_l_joystick_ver) |
64 |
| - rjs_x = readChannel(ch_r_joystick_hor) |
65 |
| - rjs_y = readChannel(ch_r_joystick_ver) |
66 |
| - |
67 |
| - # First 4 bit useless for buttons |
68 |
| - buttons = "0000" |
69 |
| - # Control buttons actions |
70 |
| - buttons = buttons + str(1) if right_button == False else buttons + str(0) |
71 |
| - buttons = buttons + str(1) if left_button == False else buttons + str(0) |
72 |
| - buttons = buttons + str(1) if top_button == False else buttons + str(0) |
73 |
| - buttons = buttons + str(1) if bottom_button == False else buttons + str(0) |
74 |
| - |
75 |
| - # Joystick will 5 byte data |
76 |
| - toSendData = "\{}\{}\{}\{}\{}".format(buttons, |
77 |
| - decimal2hex(ljs_x), decimal2hex(ljs_y), decimal2hex(rjs_x), decimal2hex(rjs_y)) |
78 |
| - |
79 |
| - # print(toSendData) |
| 37 | + # Read joysticks positions from channels |
| 38 | + ljs_x = readChannel(ch_leftJS_hor) |
| 39 | + ljs_y = readChannel(ch_leftJS_ver) |
| 40 | + rjs_x = readChannel(ch_rightJS_hor) |
| 41 | + rjs_y = readChannel(ch_rightJS_ver) |
| 42 | + |
| 43 | + # Joystick will send 5 byte data |
| 44 | + toSendData = chr(127) + chr(ljs_x) + chr(ljs_y) + chr(rjs_x) + chr(rjs_y) |
80 | 45 |
|
81 | 46 | # Send the data
|
82 | 47 | writeReport(toSendData)
|
|
0 commit comments