-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathcalibration.py
48 lines (38 loc) · 994 Bytes
/
calibration.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
reads position from thumb-joystick
needs spidev installed
http://www.raspberrypi-spy.co.uk/2014/08/enabling-the-spi-interface-on-the-raspberry-pi/
"""
import RPi.GPIO as GPIO
import time
import spidev
xPin = 0 # joystick x connected to A0
yPin = 1 # joystick y connected to A1
xzero = 512
yzero = 512
spi = spidev.SpiDev()
spi.open(0,0)
def readadc(adcnum):
# read SPI data from MCP3004 chip, 4 possible adc’s (0 thru 3)
if ((adcnum > 3) or (adcnum < 0)):
return -1
r = spi.xfer2([1,8 + adcnum << 4, 0])
print(r)
adcout = ((r[1] &3) << 8) + r[2]
return adcout
def position(value, zerovalue):
return value - zerovalue
while True:
value = readadc(xPin)
posx = position(value, xzero)
print("x: %4d/1023 => %5d" % (value, posx))
value = readadc(yPin)
posy = position(value, yzero)
print("y: %4d/1023 => %5d" % (value, posy))
if (posx > (1023 * 3/4)):
print("Button pressed!")
print("")
time.sleep(0.5)
print('done.')