- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupython_code.py
More file actions
Latest commit
129 lines (105 loc) · 4.06 KB
/
Copy pathupython_code.py
File metadata and controls
129 lines (105 loc) · 4.06 KB
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
frommachineimportPin
importselect
importtime
importsys
import_thread
# 888~-_ e88~-_ 888
# 888 888 888 \ Y88b / d888 \ e88~-_ e88~\888 e88~~8e
# 888 888 888 | Y888/ 8888 d888 i d888 888 d888 88b
# 888 888 888 / Y8/ 8888 8888 | 8888 888 8888__888
# 888 888 888_-~ Y Y888 / Y888 ' Y888 888 Y888 ,
# "88_-888 888 / "88_-~ "88_-~ "88_/888 "88___/
# _/
# Communication back to the GUI is done using a simple print
print('STARTING uPython code...')
# Anti-pattern anywone? Cheating to get global variables so that the code isn't peppered with global statements
classG:
stop_requested=False
blink_led=False
input_pin:Pin=None
pin_map= {} # dictionary of pin to pin num
# 888~-_ ,e,
# 888 \ e88~~8e e88~~\ e88~~8e " Y88b / e88~~8e
# 888 | d888 88b d888 d888 88b 888 Y88b / d888 88b
# 888 / 8888__888 8888 8888__888 888 Y88b/ 8888__888
# 888_-~ Y888 , Y888 Y888 , 888 Y8/ Y888 ,
# 888 ~-_ "88___/ "88__/ "88___/ 888 Y "88___/
#
# ~~~888~~~ 888 888
# 888 888-~88e e88~~8e /~~~8e e88~\888
# 888 888 888 d888 88b 88b d888 888
# 888 888 888 8888__888 e88~-888 8888 888
# 888 888 888 Y888 , C888 888 Y888 888
# 888 888 888 "88___/ "88_-888 "88_/888
defreceive_thread():
print('Serial Receive Thread STARTED')
whileTrue:
# See if chars are available on the serial port
r, _, _=select.select([sys.stdin], [], [], 2.0)
ifr:
line=sys.stdin.readline().strip()
ifnotline:
continue
ifline.startswith('TEST'):
foriinrange(10):
print(f'Test {i}')
time.sleep(.2)
elifline.startswith('STOP'):
print('uPython serial thread EXITING....')
G.stop_requested=True
_thread.exit()
elifline.startswith('LED'):
print('Starting LED blink test')
G.blink_led=True
else:
print(f'Received unknown command: {line}')
# 888 d8 d8
# 888 888-~88e _d88__ e88~~8e 888-~\ 888-~\ 888 888 888-~88e _d88__
# 888 888 888 888 d888 88b 888 888 888 888 888 888b 888
# 888 888 888 888 8888__888 888 888 888 888 888 8888 888
# 888 888 888 888 Y888 , 888 888 888 888 888 888P 888
# 888 888 888 "88_/ "88___/ 888 888 "88_-888 888-_88" "88_/
# 888
@micropython.native
defchip_interrupt(pin:Pin):
try:
pin_num=G.pin_map[pin]
except:
print('Error pin lookup')
pin_num=None
print(f'Interrupt on pin {pin_num}')
defblink_led():
led=Pin(25, Pin.OUT) # use any free GPIO pin number
foriinrange(0,20):
led.value(1) # optimized call
time.sleep(.5)
led.value(0)
time.sleep(.5)
ifG.stop_requested:
return
# e e ,e,
# d8b d8b /~~~8e " 888-~88e
# d888bdY88b 88b 888 888 888
# / Y88Y Y888b e88~-888 888 888 888
# / YY Y888b C888 888 888 888 888
# / Y888b "88_-888 888 888 888
defmain():
print('Started MAIN')
G.input_pin=Pin(2, Pin.IN)
G.pin_map[G.input_pin] =2
G.input_pin.irq(chip_interrupt, Pin.IRQ_FALLING)
_thread.start_new_thread(receive_thread, ())
print('DONE starting uPython code')
# background / work loop
whileTrue:
time.sleep(3)
ifG.stop_requested:
print('uPython code EXITING')
break
elifG.blink_led:
blink_led()
G.blink_led=False
print('LED Blink Test Complete')
sys.exit()
if__name__=='__main__':
main()