-
Notifications
You must be signed in to change notification settings - Fork 32
/
__init__.py
154 lines (113 loc) · 3.5 KB
/
__init__.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
# __init__.py
import sys
#----- CONFIG -----------------------------------------------------------------
##DEVICE_NAME = "whaleygeek's awesome microbit"
DEVICE_NAME = "micro:bit"
DEBUG = False
BAUD = 115200
def trace(msg):
if DEBUG:
print(str(msg))
def warn(msg):
print("warning:%s" % str(msg))
def info(msg):
print(msg)
def fail(msg):
print("error:%s" % str(msg))
import sys
sys.exit(-1)
# Allow user to set debug flag on command line
if 'debug' in sys.argv:
DEBUG = True
#----- IMPORTS ----------------------------------------------------------------
try:
import repl
except ImportError:
from . import repl
try:
import portscan
except ImportError:
from . import portscan
try:
import api
except ImportError:
from . import api
import sys, os
SERIAL_PATH = os.path.dirname(os.path.abspath(__file__))
trace("Using path:%s" % str(SERIAL_PATH))
if SERIAL_PATH not in sys.path:
sys.path.insert(0, SERIAL_PATH)
try:
import serial
except ImportError as e:
info("Can't find pyserial on your system")
if DEBUG:
trace(str(e))
import traceback
ex_type, ex, tb = sys.exc_info()
traceback.print_tb(tb)
trace(sys.path)
fail("That's odd, it should be included in this project")
if hasattr(serial, "BITIO"):
trace("Yay, I loaded the BITIO packaged pyserial")
else:
warn("I got the system installed pyserial, that was unexpected")
#----- PORTSCAN ---------------------------------------------------------------
# reuse or scan for a new port
trace("will reuse cache or scan for new port")
name = portscan.getName(DEVICE_NAME)
if name != None:
if DEBUG:
trace("Using port:" + name)
PORT = name
else:
name = portscan.find()
if name == None:
fail("No port selected, giving in")
PORT = name
#----- CONNECT TO SERIAL ------------------------------------------------------
# get the serial port
def get_serial():
s = serial.Serial(PORT)
s.baudrate = BAUD
s.parity = serial.PARITY_NONE
s.databits = serial.EIGHTBITS
s.stopbits = serial.STOPBITS_ONE
s.timeout = 0 # non blocking mode
s.close()
s.port = PORT
s.open()
return s
info("connecting...")
trace("getting active serial port connection to %s" % DEVICE_NAME)
while True:
try:
s = get_serial()
break # got a valid connection
except Exception as e:
warn("Could not open the serial port that was remembered from last time")
portscan.forget()
name = portscan.find(DEVICE_NAME)
if name == None:
fail("Still can't find a port, giving in")
PORT = name
# go round again and try and open serial port
#----- GET RAW REPL -----------------------------------------------------------
# wrap a repl around it
trace("creating a raw REPL connection via serial")
repl = repl.REPL(s)
trace("entering raw repl mode")
repl.to_raw()
#----- CREATE MICROBIT ABSTRACTION --------------------------------------------
trace("creating a MicroBit API class around it")
microbit = api.MicroBit(repl)
#----- TURN THIS MODULE into a microbit instance ------------------------------
# i.e. when people do import microbit
# what they then want is to be able to say microbit.button_a.was_pressed()
# and for it to map directly through to the microbit object above.
# make this module look like a single microbit
me = sys.modules[__name__]
sys.modules[__name__] = microbit
info("Your %s has been detected" % DEVICE_NAME)
info("Now running your program")
# END