-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patht.py
206 lines (185 loc) · 5.51 KB
/
t.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# to test i2 and other funcs on ESP8266
import gc
import sys
import time
import machine
from machine import I2C
from machine import Pin
try:
import msvcrt
def kbhit():
return msvcrt.kbhit()
def getch():
return msvcrt.getwch()
except ImportError:
print("Assuming on ESP")
import uselect
def kbhit():
spoll=uselect.poll()
spoll.register(sys.stdin,uselect.POLLIN)
kbch = sys.stdin.read(1) if spoll.poll(0) else None
spoll.unregister(sys.stdin)
return(kbch)
def getch():
while True:
ch= sys.stdin.read(1)
if ch is not None:
return ch
# detach UART from REPL
from machine import UART
uart = UART(0, 38400) # 0 or 1
import os
#os.dupterm(None, 1)
# some globals
count=2 # number of bytes
start = time.ticks_ms()
devaddr=104
devtyp='?' # 'M' mpu6050 'Q' qmc5883 'P' pcf8574
pinSDA=machine.Pin(4) #green
pinSCL=machine.Pin(5) #yell
con=I2C(scl=pinSCL, sda=pinSDA)
print ("Devices:", con.scan())
try:
from mpu6050 import mpu6050
mpu=mpu6050(con,devaddr)
devtyp='M'
except ImportError:
print("no mpu6050")
devtyp='?'
def intFromBytes(x):
y = int.from_bytes(x)
if devtyp=='M': #signed
if (y >= 0x8000):
return -((65535 - y) + 1)
return y
def info():
gc.collect()
print(" Free",gc.mem_free(),"Dev",devaddr,"Cnt",count,)
def switchdev(n):
global devaddr,devtyp
devaddr=n
if devaddr==104: devtyp='M'
elif devaddr==80: devtyp='Q'
elif devaddr >= 32 and devaddr <=40: devtyp='P'
else: devtyp='?'
def showTups(erg):
if count > 1:
x = intFromBytes(erg[0:2])
else:
x=0
if count >3:
y = intFromBytes(erg[2:4])
else:
y=0
if count >5:
z = intFromBytes(erg[4:6])
else:
z=0;
print("x", x, "y", y, "z", z)
#return {"x": x, "y": y, "z": z}
def durch():
print("durch ebd #")
os.dupterm(None, 1)
uart.write('Hallo')
while kbhit() is None:
if uart.any():
buf=uart.read()
str=buf.decode('utf-8')
print(str,end='')
if str=='#' : break
os.dupterm(uart, 1)
print("done")
def doread(adr):
if devtyp =='M': # read count for memory device
erg=mpu._read(count,adr)
print("Read: ",erg)
if count > 1:
showTups(erg)
else: # read one
erg=con.readfrom(devaddr,1)
print("Read: ",erg)
def dowrite(data):
print(con.writeto(devaddr,bytes([data])))
def menu():
global count
inpAkt=False
inp=0
pu=0
while True:
if not inpAkt: print(devtyp, devaddr,">",end='')
ch = getch()
print(ch,end='')
if ((ch >= '0') and (ch <= '9')):
if (inpAkt) :
inp = inp * 10 + (ord(ch) - 48);
else:
inpAkt = True;
inp = ord(ch) - 48;
else:
inpAkt=False
try:
if ch=="b": #scan (only from 0x08 to 0x77)
print("\bScan:",con.scan())
elif ch=="a": # accel read
count=6
inp=59
while not kbhit():
erg=mpu._read(count,inp)
showTups(erg)
elif ch=="c": #set device
count=inp
info()
elif ch=="d": #set device
switchdev(inp)
info()
elif ch=="g": # gyro read
count=6
inp=67
while not kbhit():
erg=mpu._read(count,inp)
showTups(erg)
elif ch=="h": # pin high
p0 = Pin(inp, Pin.OUT)
p0.on()
elif ch=="i":
p0 = Pin(inp, Pin.IN)
elif ch=="l": # pin low
p0 = Pin(inp, Pin.OUT)
p0.off()
elif ch=="q":
os.dupterm(uart, 1)
print ("\brestart with t.menu() ")
return
elif ch=="r": #read count from
doread(inp)
elif ch=="s":
print ("speed",inp)
con.init(pinSCL,pinSDA,freq=inp*1000)
elif ch=="t":
erg=mpu.readTemp()
print ("temp",erg)
elif ch=="u":
print ("wake")
mpu.wake()
elif ch=="U":
print ("sleep")
mpu.sleep()
elif ch=="w":
dowrite(inp)
elif ch==",":
pu=bytes([inp]);
print(pu)
elif ch=="+":
inp+=1
print ("\b",inp)
elif ch=="-":
inp-=1
print ("\b",inp)
elif ch==" ":
durch()
else:
info()
print("?")
except Exception as inst:
sys.print_exception(inst)
menu()