-
Notifications
You must be signed in to change notification settings - Fork 0
/
py3IDLE.py
129 lines (113 loc) · 3.17 KB
/
py3IDLE.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
from microbit import *
from math import *
import radio
show_error_message = False
loop_error_message = False
num = ':01234567890.'
alpha = '[abcdef ghijkl]' # with a space in the middle
bet = '<Smnopqrstuvwxyz>' # alphabet
operators = 'D,()+-*/="RI'
p = print
d = display.scroll # make typing easier
radio.on()
radio.config(channel=16)
def get_char():
while True:
x = accelerometer.get_x()
y = accelerometer.get_y()
z = accelerometer.get_z()
if x == 0:
theta = 90 # avoid division by zero
else:
theta = atan(z / x) * 180 / pi
if theta < 0:
theta = 180 + theta
# map theta to characters on semisphere (in degrees) using calibrated
# formulae
if y < -300:
i = num[min(int(abs(theta - 30) / 120 * len(num)), len(num) - 1)]
elif y > 700:
i = operators[
min(int(abs(theta - 30) / 120 * len(operators)), len(operators) - 1)]
elif y > 300:
i = bet[int(theta / 180 * len(bet))]
else:
i = alpha[int(theta / 180 * len(alpha))]
display.show(i)
radio.send(i)
if button_b.was_pressed(): # b for execution
return False
if button_a.was_pressed(): # a for typing
display.clear()
radio.send(' ')
while button_a.is_pressed():
pass # feedback: nothing displayed with a pressed
display.show(i)
radio.send(i)
sleep(500)
break
sleep(50)
return i
def code():
s = ''
c = get_char()
caps_lock = False
while c:
if c == 'D':
# delete
s = s[:-1]
elif c == 'S':
caps_lock = True
elif caps_lock:
s += c.upper()
caps_lock = False
elif c == 'I':
# indentation of 4 spaces
s += ' '
elif c == ' ':
if s[-1] == ' ':
# type two spaces for a new line
s = s[:-1]
s += '\n'
else:
s += ' '
elif c == 'R':
s += 'return '
else:
s += c
print('...', s)
c = get_char()
try:
if s[-1] == ' ':
exec(s)
else:
exec("result = " + s)
exec("print('>>>', result)")
exec("radio.send('$' + str(result))") # Scroll output
exec("display.scroll(str(result))")
except:
print('error')
if show_error_message:
radio.send('error')
display.scroll('error', wait=show_error_message, loop=loop_error_message)
idle = """\n\nPython 3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25)
[GCC 4.2.1 (Jason Li build 5666) (dot 3)] on BBC micro:bit\n>>> """
print(idle)
while True:
code()
# for display
'''
from microbit import *
import radio
radio.on()
radio.config(channel=16)
while True:
incoming = radio.receive()
if incoming:
if incoming[0] == '$':
# Scroll output
display.scroll(incoming[1:])
else:
# Sync keyboard display
display.show(incoming)
'''