-
Notifications
You must be signed in to change notification settings - Fork 1
/
grove_thumb_joystick.py
78 lines (61 loc) · 1.81 KB
/
grove_thumb_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
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# The MIT License (MIT)
#
# Grove Base Hat for the Raspberry Pi, used to connect grove sensors.
# Copyright (C) 2018 Seeed Technology Co.,Ltd.
'''
This is the code for
- `Grove - Thumb Joystick <https://www.seeedstudio.com/Grove-Thumb-Joystick-p-935.html>`_
Examples:
.. code-block:: python
import time
from grove.grove_thumb_joystick import GroveThumbJoystick
# connect to alalog pin 2(slot A2)
PIN = 2
sensor = GroveThumbJoystick(PIN)
while True:
x, y = sensor.value
if x > 900:
print('Joystick Pressed')
print("X, Y = {0} {1}".format(x, y))
time.sleep(.2)
'''
import math
import sys
import time
from grove.adc import ADC
__all__ = ['GroveThumbJoystick']
class GroveThumbJoystick(object):
'''
Grove Thumb Joystick class
Args:
channel(int): number of analog pin/channel the sensor connected.
'''
def __init__(self, channel):
self.channelX = channel
self.channelY = channel + 1
self.adc = ADC()
@property
def value(self):
'''
Get the water strength value
Returns:
(pair): x-ratio, y-ratio, all are 0(0.0%) - 1000(100.0%)
'''
return self.adc.read(self.channelX), self.adc.read(self.channelY)
Grove = GroveThumbJoystick
def main():
from grove.helper import SlotHelper
sh = SlotHelper(SlotHelper.ADC)
pin = sh.argv2pin()
sensor = GroveThumbJoystick(pin)
while True:
x, y = sensor.value
if x > 900:
print('Joystick Pressed')
print("X, Y = {0} {1}".format(x, y))
time.sleep(.2)
if __name__ == '__main__':
main()