-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathled_button.py
165 lines (148 loc) · 5.29 KB
/
led_button.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# The MIT License (MIT)
# Copyright (C) 2018 Seeed Technology Co.,Ltd.
#
# This is the library for Grove Base Hat
# which used to connect grove sensors for Raspberry Pi.
'''
This is the code for
- `Grove - Red LED Button <https://www.seeedstudio.com/Grove-Red-LED-Button-p-3096.html>`_
- `Grove - Yellow LED Button <https://www.seeedstudio.com/Grove-Yellow-LED-Button-p-3101.html>`_
- `Grove - Blue LED Button <https://www.seeedstudio.com/Grove-Blue-LED-Button-p-3104.html>`_
Examples:
.. code-block:: python
from grove.button import Button
import grove.grove_ryb_led_button.GroveLedButton
import time
# slot/gpio number your device plugin
pin = 12
obj = GroveLedButton(pin)
# the default behavior of led is
# single click - on
# double click - blink
# long press - off
# remove \'\'\' pairs below to begin your experiment
\'\'\'
# define a customized event handle your self
def cust_on_event(index, event, tm):
# obj.led could be used to operate led
print("event with code {}, time {}".format(event, tm))
obj.on_event = cust_on_event
\'\'\'
while True:
time.sleep(1)
'''
import time
from grove.button import Button
from grove.factory import Factory
from mraa import getGpioLookup
from upm import pyupm_buzzer as upmBuzzer
# sphinx autoapi required
__all__ = ["GroveLedButton"]
class GroveLedButton(object):
'''
Grove Red/Yellow/Blue Led Button class
all of them has a gpio button with low valid level of pressing,
and a gpio led with high valid level for lighting.
Args:
pin(int): the number of gpio/slot your grove device connected.
'''
def __init__(self, pin):
# High = light on
self.led = Factory.getOneLed("GPIO-HIGH", pin)
# Low = pressed
self.__btn = Factory.getButton("GPIO-LOW", pin + 1)
self.__on_event = None
self.__btn.on_event(self, GroveLedButton.__handle_event)
@property
def on_event(self):
'''
Property access with
callback -- a callable function/object,
will be called when there is button event
callback prototype:
callback(index, code, time)
callback argument:
Args:
index(int): button index, be in 0 to [button count - 1]
code (int): bits combination of
- Button.EV_LEVEL_CHANGED
- Button.EV_SINGLE_CLICK
- Button.EV_DOUBLE_CLICK
- Button.EV_LONG_PRESS
time(time): event generation time
Returns: none
Examples:
set
.. code-block:: python
obj.on_event = callback
get
.. code-block:: python
callobj = obj.on_event
'''
return self.__on_event
@on_event.setter
def on_event(self, callback):
if not callable(callback):
return
self.__on_event = callback
def __handle_event(self, evt):
#print "event index:{} event:{} pressed:{}".format(evt['index'], evt['code'], evt['presesed'])
if callable(self.__on_event):
# the customized behavior
self.__on_event(evt['index'], evt['code'], evt['time'])
return
# the default behavior
self.led.brightness = self.led.MAX_BRIGHT
event = evt['code']
if event & Button.EV_SINGLE_CLICK:
self.led.light(True)
print("turn on LED")
elif event & Button.EV_DOUBLE_CLICK:
self.led.blink()
print("blink LED")
elif event & Button.EV_LONG_PRESS:
self.led.light(False)
print("turn off LED")
def main():
from grove import helper
from grove.helper import helper
helper.root_check()
from grove.helper import SlotHelper
sh = SlotHelper(SlotHelper.GPIO)
butt_pin = sh.argv2pin()
press = GroveLedButton(butt_pin)
print "button assigned"
# PWM JST SLOT - PWM[12 13 VCC GND]
buzz_pin = 12
#
# Create the buzzer object using RaspberryPi GPIO12
mraa_pin = getGpioLookup("GPIO%02d" % buzz_pin)
buzzer = upmBuzzer.Buzzer(mraa_pin)
print "buzzer assigned"
# define a customized event handle your self
def cust_on_event(index, event, tm):
print "event with code {}, time {}".format(event, tm)
press.led.brightness = press.led.MAX_BRIGHT
if event & Button.EV_SINGLE_CLICK:
press.led.light(True)
print(buzzer.playSound(upmBuzzer.BUZZER_DO, 500000))
print "turn on LED"
elif event & Button.EV_DOUBLE_CLICK:
press.led.blink()
print(buzzer.playSound(upmBuzzer.BUZZER_DO, 250000))
print "blink LED"
elif event & Button.EV_LONG_PRESS:
press.led.light(False)
print(buzzer.playSound(upmBuzzer.BUZZER_SI, 500000))
print "turn off LED"
press.on_event = cust_on_event
i = 0
while True:
print "time = {}".format(i)
i += 1
time.sleep(1)
if __name__ == '__main__':
main()