-
Notifications
You must be signed in to change notification settings - Fork 1
/
porygon.py
106 lines (77 loc) · 2.32 KB
/
porygon.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python
import configparser
import os
import subprocess
import tempfile
import time
import PIL
import pytesseract
import smbus2
try:
import RPi.GPIO as GPIO
except RuntimeError:
# If we're not running on a Pi, then we are running a test and
# we can count on the test to stub out a mock GPIO module.
GPIO = None
# <module_config>
__config = configparser.ConfigParser()
__config.read(os.path.join(
os.path.dirname(__file__), 'config.ini'
))
def __make_button(name):
return int(__config['buttons'][name], 0), name
Y = __make_button('Y')
B = __make_button('B')
X = __make_button('X')
A = __make_button('A')
HOME = __make_button('HOME')
I2C_WRITE_COMMAND = int(__config['joystick']['I2C_WRITE_COMMAND'], 0)
I2C_LEFTRIGHT_ADDR = int(__config['joystick']['I2C_LEFTRIGHT_ADDR'], 0)
I2C_UPDOWN_ADDR = int(__config['joystick']['I2C_UPDOWN_ADDR'], 0)
LR_NEUTRAL = int(__config['joystick']['LR_NEUTRAL'], 0)
UD_NEUTRAL = int(__config['joystick']['UD_NEUTRAL'], 0)
# </module_config>
__bus = None
def bus():
global __bus
if not __bus:
__bus = smbus2.SMBus(1)
return __bus
def init_pi():
GPIO.setmode(GPIO.BOARD)
for but in [A, B, X, Y, HOME]:
GPIO.setup(but[0], GPIO.OUT)
GPIO.output(but[0], False)
still()
def press(but, hold_delay=.1, rest_delay=.1):
print('pressing {}'.format(but[1]))
GPIO.output(but[0], True)
time.sleep(hold_delay)
GPIO.output(but[0], False)
time.sleep(rest_delay)
def set_leftright(level):
level &= 0xfff
msg = [(level & 0xff0) >> 4, (level & 0xf)]
bus().write_i2c_block_data(I2C_LEFTRIGHT_ADDR, I2C_WRITE_COMMAND, msg)
def tilt_x(tilt):
set_leftright(LR_NEUTRAL + tilt)
def set_updown(level):
level &= 0xfff
msg = [(level & 0xff0) >> 4, (level & 0xf)]
bus().write_i2c_block_data(I2C_UPDOWN_ADDR, I2C_WRITE_COMMAND, msg)
def tilt_y(tilt):
set_updown(UD_NEUTRAL + tilt)
def still():
tilt_x(0)
tilt_y(0)
def read_cropped_image(coords):
try:
fname = tempfile.NamedTemporaryFile(prefix='pokemon')
subprocess.call(['fswebcam', '-r', '1280x720', fname.name])
image = PIL.Image.open(fname.name)
cropped = image.crop(coords)
retval = pytesseract.image_to_string(cropped)
return retval
except Exception as e:
print(e)
return ''