8000 Reading the MCP3008 with Adafruit library · sevvalmehder/RaspiJoy@b637f51 · GitHub
[go: up one dir, main page]

Skip to content

Commit b637f51

Browse files
author
sevvalmehder
committed
Reading the MCP3008 with Adafruit library
1 parent 8e051be commit b637f51

File tree

1 file changed

+24
-59
lines changed

1 file changed

+24
-59
lines changed

src/RaspberrySide/connect.py

Lines changed: 24 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,47 @@
11
#!/usr/bin/python
2-
3-
import RPi.GPIO as GPIO
4-
import spidev
5-
import os
62
import time
73

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+
813
#
914
# Define channels
1015
#
1116
# Left joystick(below one) horizantal movement channel
12-
ch_l_joystick_hor = 0
17+
ch_leftJS_hor = 0
1318
# Left joystick(below one) vertex movement channel
14-
ch_l_joystick_ver = 1
19+
ch_leftJS_ver = 1
1520
# Right joystick(upper one) horizantal movement channel
16-
ch_r_joystick_hor = 6
21+
ch_rightJS_hor = 6
1722
# 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
3124

3225
# Delay in seconds
3326
delay = 0.5
3427

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-
4328
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
5531

5632
def writeReport(report):
5733
with open('/dev/hidg0', 'rb+') as fd:
58-
fd.write(report)
34+
fd.write(report.encode())
5935

6036
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)
8045

8146
# Send the data
8247
writeReport(toSendData)

0 commit comments

Comments
 (0)
0