Skip to content

Commit 82bb6ba

Browse files
committed
M5310A Restructure
1 parent 808d69f commit 82bb6ba

3 files changed

Lines changed: 158 additions & 152 deletions

File tree

07.sensors/M5310A/M5310A.py

Lines changed: 0 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -1,141 +0,0 @@
1-
from machine import UART
2-
import utime
3-
from machine import Pin
4-
5-
class M5310_A():
6-
def send_cmd(self,cmd):
7-
self.uart.write(cmd + '\r\n')
8-
def cheak_ack(self,buf, ack):
9-
if ack in buf:
10-
return True
11-
else:
12-
return False
13-
def __init__(self):
14-
self.rst = Pin(18, Pin.OUT)
15-
self.uart = UART(2)
16-
self.uart.init(9600, bits=8, parity=None, stop=1)
17-
self.RecvMax = 128
18-
self.RecvBuf = bytearray()
19-
self.cmd = ['AT', 'AT+CIMI', 'AT+COPS=1,2,\"46000\"', 'AT+CSQ', 'AT+CEREG?', 'AT+CGATT?',
20-
'AT+NSOCR="DGRAM",17,0,1', 'AT+NSOCFG=0', 'AT+NSOCFG=0,0,0']
21-
self.cmd_len = len(self.cmd)
22-
self.cmd_index = 0
23-
self.set_up()
24-
def set_up(self):
25-
self.rst.value(1)
26-
utime.sleep_ms(200)
27-
self.rst.value(0)
28-
self.send_cmd(self.cmd[self.cmd_index])
29-
send_time = utime.ticks_ms()
30-
while True:
31-
recvlen = self.uart.any()
32-
if recvlen > 0:
33-
buffer = self.uart.read(recvlen)
34-
for data in buffer:
35-
self.RecvBuf.append(data)
36-
if self.cmd[self.cmd_index] == 'AT' or self.cmd[self.cmd_index] == 'AT+CIMI' \
37-
or self.cmd[self.cmd_index] == 'AT+COPS=1,2,\"46000\"' or self.cmd[self.cmd_index] ==\
38-
'AT+NSOCFG=0' or self.cmd[self.cmd_index] == 'AT+NSOCFG=0,0,0':
39-
if self.cheak_ack(self.RecvBuf, b'\r\nOK\r\n'):
40-
print('CHEAK_OK')
41-
print(self.RecvBuf)
42-
self.RecvBuf = bytearray()
43-
self.cmd_index += 1
44-
if self.cmd_index >= self.cmd_len:
45-
break
46-
else:
47-
self.send_cmd(self.cmd[self.cmd_index])
48-
send_time = utime.ticks_ms()
49-
elif self.cmd[self.cmd_index] == 'AT+CSQ':
50-
if self.cheak_ack(self.RecvBuf, b'\r\n+CSQ:'):
51-
print('CHEAK_OK')
52-
print(self.RecvBuf)
53-
strbuf = bytes(self.RecvBuf)
54-
print(strbuf)
55-
num = strbuf.find(b':')
56-
csq = int(strbuf[num + 1:num + 3])
57-
if csq > 12 and csq < 99:
58-
self.RecvBuf = bytearray()
59-
self.cmd_index += 1
60-
if self.cmd_index >= self.cmd_len:
61-
break
62-
else:
63-
self.send_cmd(self.cmd[self.cmd_index])
64-
send_time = utime.ticks_ms()
65-
elif self.cmd[self.cmd_index] == 'AT+CGATT?':
66-
if self.cheak_ack(self.RecvBuf, b'+CGATT:1'):
67-
print('CHEAK_OK')
68-
print(self.RecvBuf)
69-
self.RecvBuf = bytearray()
70-
self.cmd_index += 1
71-
if self.cmd_index >= self.cmd_len:
72-
break
73-
else:
74-
self.send_cmd(self.cmd[self.cmd_index])
75-
send_time = utime.ticks_ms()
76-
elif self.cmd[self.cmd_index] == 'AT+CEREG?':
77-
if self.cheak_ack(self.RecvBuf, b'+CEREG:0'):
78-
print('CHEAK_OK')
79-
print(self.RecvBuf)
80-
81-
strbuf = bytes(self.RecvBuf)
82-
print(strbuf)
83-
num = strbuf.find(b'G:')
84-
reg = int(strbuf[num + 4:num + 5])
85-
if reg == 1 or reg == 5:
86-
self.RecvBuf = bytearray()
87-
self.cmd_index += 1
88-
if self.cmd_index >= self.cmd_len:
89-
break
90-
else:
91-
self.send_cmd(self.cmd[self.cmd_index])
92-
send_time = utime.ticks_ms()
93-
elif self.cmd[self.cmd_index] == 'AT+NSOCR="DGRAM",17,0,1':
94-
if self.cheak_ack(self.RecvBuf, b'\r\nOK\r\n'):
95-
print('CHEAK_OK')
96-
print(self.RecvBuf)
97-
strbuf = bytes(self.RecvBuf)
98-
print(strbuf)
99-
num = strbuf.find(b'\r\nOK\r\n')
100-
reg = int(strbuf[num - 3]) - 48
101-
print(reg)
102-
if reg >= 0 and reg <= 6:
103-
self.RecvBuf = bytearray()
104-
self.cmd_index += 1
105-
if self.cmd_index >= self.cmd_len:
106-
break
107-
else:
108-
self.send_cmd(self.cmd[self.cmd_index])
109-
send_time = utime.ticks_ms()
110-
if (len(self.RecvBuf) > self.RecvMax):
111-
self.RecvBuf = bytearray()
112-
print('OUT LINE')
113-
self.send_cmd(self.cmd[self.cmd_index])
114-
print('resend:', self.cmd[self.cmd_index])
115-
send_time = utime.ticks_ms()
116-
now_time = utime.ticks_ms()
117-
if send_time + 500 < now_time:
118-
self.RecvBuf = bytearray()
119-
self.send_cmd(self.cmd[self.cmd_index])
120-
print('resend:', self.cmd[self.cmd_index])
121-
send_time = utime.ticks_ms()
122-
def send_data(self,data):
123-
data='AT+NSOST=0,zwidas.top,8888,,\"%s\"'%data
124-
self.send_cmd(data)
125-
126-
def unit_test():
127-
ts=M5310_A()
128-
ts.send_data('hello')
129-
while True:
130-
recvlen = ts.uart.any()
131-
if recvlen > 0:
132-
buffer = ts.uart.read(recvlen)
133-
for data in buffer:
134-
ts.RecvBuf.append(data)
135-
print(ts.RecvBuf)
136-
if ts.cheak_ack(ts.RecvBuf, b'+NSONMI'):
137-
ts.RecvBuf=bytearray()
138-
ts.send_cmd('AT+NSORF=0,100')
139-
140-
if __name__=='__main__':
141-
unit_test()

07.sensors/M5310A/M5310A_old.py

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
from machine import UART
2+
import utime
3+
from machine import Pin
4+
5+
class M5310_A():
6+
def send_cmd(self,cmd):
7+
self.uart.write(cmd + '\r\n')
8+
def cheak_ack(self,buf, ack):
9+
if ack in buf:
10+
return True
11+
else:
12+
return False
13+
def __init__(self):
14+
self.rst = Pin(18, Pin.OUT)
15+
self.uart = UART(2)
16+
self.uart.init(9600, bits=8, parity=None, stop=1)
17+
self.RecvMax = 128
18+
self.RecvBuf = bytearray()
19+
self.cmd = ['AT', 'AT+CIMI', 'AT+COPS=1,2,\"46000\"', 'AT+CSQ', 'AT+CEREG?', 'AT+CGATT?',
20+
'AT+NSOCR="DGRAM",17,0,1', 'AT+NSOCFG=0', 'AT+NSOCFG=0,0,0']
21+
self.cmd_len = len(self.cmd)
22+
self.cmd_index = 0
23+
self.set_up()
24+
def set_up(self):
25+
self.rst.value(1)
26+
utime.sleep_ms(200)
27+
self.rst.value(0)
28+
self.send_cmd(self.cmd[self.cmd_index])
29+
send_time = utime.ticks_ms()
30+
while True:
31+
recvlen = self.uart.any()
32+
if recvlen > 0:
33+
buffer = self.uart.read(recvlen)
34+
for data in buffer:
35+
self.RecvBuf.append(data)
36+
if self.cmd[self.cmd_index] == 'AT' or self.cmd[self.cmd_index] == 'AT+CIMI' \
37+
or self.cmd[self.cmd_index] == 'AT+COPS=1,2,\"46000\"' or self.cmd[self.cmd_index] ==\
38+
'AT+NSOCFG=0' or self.cmd[self.cmd_index] == 'AT+NSOCFG=0,0,0':
39+
if self.cheak_ack(self.RecvBuf, b'\r\nOK\r\n'):
40+
print('CHEAK_OK')
41+
print(self.RecvBuf)
42+
self.RecvBuf = bytearray()
43+
self.cmd_index += 1
44+
if self.cmd_index >= self.cmd_len:
45+
break
46+
else:
47+
self.send_cmd(self.cmd[self.cmd_index])
48+
send_time = utime.ticks_ms()
49+
elif self.cmd[self.cmd_index] == 'AT+CSQ':
50+
if self.cheak_ack(self.RecvBuf, b'\r\n+CSQ:'):
51+
print('CHEAK_OK')
52+
print(self.RecvBuf)
53+
strbuf = bytes(self.RecvBuf)
54+
print(strbuf)
55+
num = strbuf.find(b':')
56+
csq = int(strbuf[num + 1:num + 3])
57+
if csq > 12 and csq < 99:
58+
self.RecvBuf = bytearray()
59+
self.cmd_index += 1
60+
if self.cmd_index >= self.cmd_len:
61+
break
62+
else:
63+
self.send_cmd(self.cmd[self.cmd_index])
64+
send_time = utime.ticks_ms()
65+
elif self.cmd[self.cmd_index] == 'AT+CGATT?':
66+
if self.cheak_ack(self.RecvBuf, b'+CGATT:1'):
67+
print('CHEAK_OK')
68+
print(self.RecvBuf)
69+
self.RecvBuf = bytearray()
70+
self.cmd_index += 1
71+
if self.cmd_index >= self.cmd_len:
72+
break
73+
else:
74+
self.send_cmd(self.cmd[self.cmd_index])
75+
send_time = utime.ticks_ms()
76+
elif self.cmd[self.cmd_index] == 'AT+CEREG?':
77+
if self.cheak_ack(self.RecvBuf, b'+CEREG:0'):
78+
print('CHEAK_OK')
79+
print(self.RecvBuf)
80+
81+
strbuf = bytes(self.RecvBuf)
82+
print(strbuf)
83+
num = strbuf.find(b'G:')
84+
reg = int(strbuf[num + 4:num + 5])
85+
if reg == 1 or reg == 5:
86+
self.RecvBuf = bytearray()
87+
self.cmd_index += 1
88+
if self.cmd_index >= self.cmd_len:
89+
break
90+
else:
91+
self.send_cmd(self.cmd[self.cmd_index])
92+
send_time = utime.ticks_ms()
93+
elif self.cmd[self.cmd_index] == 'AT+NSOCR="DGRAM",17,0,1':
94+
if self.cheak_ack(self.RecvBuf, b'\r\nOK\r\n'):
95+
print('CHEAK_OK')
96+
print(self.RecvBuf)
97+
strbuf = bytes(self.RecvBuf)
98+
print(strbuf)
99+
num = strbuf.find(b'\r\nOK\r\n')
100+
reg = int(strbuf[num - 3]) - 48
101+
print(reg)
102+
if reg >= 0 and reg <= 6:
103+
self.RecvBuf = bytearray()
104+
self.cmd_index += 1
105+
if self.cmd_index >= self.cmd_len:
106+
break
107+
else:
108+
self.send_cmd(self.cmd[self.cmd_index])
109+
send_time = utime.ticks_ms()
110+
if (len(self.RecvBuf) > self.RecvMax):
111+
self.RecvBuf = bytearray()
112+
print('OUT LINE')
113+
self.send_cmd(self.cmd[self.cmd_index])
114+
print('resend:', self.cmd[self.cmd_index])
115+
send_time = utime.ticks_ms()
116+
now_time = utime.ticks_ms()
117+
if send_time + 500 < now_time:
118+
self.RecvBuf = bytearray()
119+
self.send_cmd(self.cmd[self.cmd_index])
120+
print('resend:', self.cmd[self.cmd_index])
121+
send_time = utime.ticks_ms()
122+
def send_data(self,data):
123+
data='AT+NSOST=0,zwidas.top,8888,,\"%s\"'%data
124+
self.send_cmd(data)
125+
126+
def unit_test():
127+
ts=M5310_A()
128+
ts.send_data('hello')
129+
while True:
130+
recvlen = ts.uart.any()
131+
if recvlen > 0:
132+
buffer = ts.uart.read(recvlen)
133+
for data in buffer:
134+
ts.RecvBuf.append(data)
135+
print(ts.RecvBuf)
136+
if ts.cheak_ack(ts.RecvBuf, b'+NSONMI'):
137+
ts.RecvBuf=bytearray()
138+
ts.send_cmd('AT+NSORF=0,100')
139+
140+
if __name__=='__main__':
141+
unit_test()

07.sensors/cs5460a/cs5460a.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import utime
2+
from machine import Pin
13
from machine import Pin, SPI
24

35
from micropython import const
@@ -18,15 +20,16 @@
1820

1921
CS5460A_CFG_POWER_UP = const(0xa0) # // power-up/halt
2022

21-
CS5460A_CFG_GAIN = const(0x40) # // reg write: config. PGA Gain 10x, IHPF=1, VHPF=1
23+
# // reg write: config. PGA Gain 10x, IHPF=1, VHPF=1
24+
CS5460A_CFG_GAIN = const(0x40)
2225

2326
CS5460A_CFG_GAIN1 = const(0x01)
2427

2528
CS5460A_CFG_GAIN2 = const(0x00)
2629

2730
CS5460A_CFG_GAIN3 = const(0x61)
2831

29-
CS5460A_CFG_IGN= const(0x44) # // reg write: Ign [current chan gain].
32+
CS5460A_CFG_IGN = const(0x44) # // reg write: Ign [current chan gain].
3033

3134
CS5460A_CFG_IGN1 = const(0x40)
3235

@@ -42,11 +45,12 @@
4245

4346
CS5460A_CFG_VGN3 = const(0xEA)
4447

45-
CS5460A_START_CONV = const(0xe8) #command : start convert
48+
CS5460A_START_CONV = const(0xe8) # command : start convert
4649

4750

4851
class cs5460a(object):
4952
import utime
53+
5054
def __init__(self, spi, cs=2, rst=4):
5155
self.spi = spi
5256
self.cs = Pin(cs, Pin.OUT)
@@ -82,9 +86,12 @@ def cs5460a_setup(self):
8286
self.spi.write(bytearray([CS5460A_CFG_POWER_UP]))
8387
self.cs.value(1) # the command of the power_up
8488

85-
self.write(CS5460A_CFG_GAIN, bytearray([CS5460A_CFG_GAIN1, CS5460A_CFG_GAIN2, CS5460A_CFG_GAIN3])) # set
86-
self.write(CS5460A_CFG_VGN , bytearray([CS5460A_CFG_VGN1, CS5460A_CFG_VGN2, CS5460A_CFG_VGN3]))#V
87-
self.write(CS5460A_CFG_IGN, bytearray([CS5460A_CFG_IGN1, CS5460A_CFG_IGN2, CS5460A_CFG_IGN3]))#A
89+
self.write(CS5460A_CFG_GAIN, bytearray(
90+
[CS5460A_CFG_GAIN1, CS5460A_CFG_GAIN2, CS5460A_CFG_GAIN3])) # set
91+
self.write(CS5460A_CFG_VGN, bytearray(
92+
[CS5460A_CFG_VGN1, CS5460A_CFG_VGN2, CS5460A_CFG_VGN3])) # V
93+
self.write(CS5460A_CFG_IGN, bytearray(
94+
[CS5460A_CFG_IGN1, CS5460A_CFG_IGN2, CS5460A_CFG_IGN3])) # A
8895

8996
self.cs.value(0)
9097
self.spi.write(bytearray([CS5460A_START_CONV]))
@@ -101,7 +108,8 @@ def _conv(self, true_power):
101108
# print(c)
102109
temp = ((c[0] + b[0] * 256 + a[0] * 65536) + 1)
103110
else:
104-
temp = ((true_power[0] + true_power[0] * 256 + true_power[0] * 65536) + 1)
111+
temp = ((true_power[0] + true_power[0] *
112+
256 + true_power[0] * 65536) + 1)
105113
return temp
106114

107115
def read_u(self):
@@ -123,15 +131,13 @@ def read_p(self):
123131
return P
124132

125133

126-
from machine import Pin
127-
import utime
128-
129134
p = Pin(18, Pin.OUT)
130135
p.value(1)
131136

132137

133138
def unit_test():
134-
vspi = SPI(-1, sck=Pin(5), mosi=Pin(23), miso=Pin(19), baudrate=2000000) # -1 software spi
139+
vspi = SPI(-1, sck=Pin(5), mosi=Pin(23), miso=Pin(19),
140+
baudrate=2000000) # -1 software spi
135141
ts = cs5460a(vspi)
136142
ts.cs5460a_setup() # 初始化
137143

0 commit comments

Comments
 (0)