-
Notifications
You must be signed in to change notification settings - Fork 7
/
callback.py
59 lines (48 loc) · 1.45 KB
/
callback.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
"""Demonstrates the use of callbacks. Press the onboard button S2 to toggle led.
"""
from robovero.LPC17xx import IRQn_Type
from robovero.core import NVIC_EnableIRQ
from robovero.arduino import pinMode, digitalWrite, digitalRead, BTN, LED, OUTPUT
from robovero.extras import heartbeatOff, registerCallback
from robovero.lpc17xx_exti import EXTI_Init, EXTI_ClearEXTIFlag
from robovero.lpc17xx_pinsel import PINSEL_CFG_Type, PINSEL_ConfigPin
from time import sleep
from random import choice
__author__ = "Neil MacMunn"
__email__ = "[email protected]"
__copyright__ = "Copyright 2010, Gumstix Inc."
__license__ = "BSD 2-Clause"
__version__ = "0.1"
responses = (
"Please", "Hey", "OK I surrender, just", "I said", "Ouch",
"I'm afraid I can't let you do that, Dave. Also,",
"Hammer says"
)
def EINT0Callback():
"""Callback function for EINT0.
"""
while not digitalRead(BTN):
sleep(0)
print "%s don't touch that!" % choice(responses)
state = digitalRead(LED)
digitalWrite(LED, state ^ 1)
EXTI_ClearEXTIFlag(0)
# control the LED manually
heartbeatOff()
pinMode(LED, OUTPUT)
# setup EINT0 on pin 2.10
PinCfg = PINSEL_CFG_Type()
PinCfg.Funcnum = 1
PinCfg.OpenDrain = 0
PinCfg.Pinmode = 0
PinCfg.Pinnum = 10
PinCfg.Portnum = 2
PINSEL_ConfigPin(PinCfg.ptr)
EXTI_Init()
# register the callback
registerCallback(IRQn_Type.EINT0_IRQn, EINT0Callback)
# enable EINT0
NVIC_EnableIRQ(IRQn_Type.EINT0_IRQn)
# the callback does everything from here
while True:
sleep(1)