Skip to content

Commit 6e255ca

Browse files
authored
Add files via upload
1 parent 30e4c7e commit 6e255ca

File tree

3 files changed

+445
-0
lines changed

3 files changed

+445
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
from micropython import const
2+
from pyb import Pin, SPI
3+
from utime import sleep_ms
4+
from ustruct import pack
5+
import framebuf
6+
7+
8+
PCD8544_DC_pin = 'X2'
9+
PCD8544_CS_pin = 'X3'
10+
PCD8544_RST_pin = 'X1'
11+
PCD8544_SCK_pin = 'X6'
12+
PCD8544_MOSI_pin = 'X8'
13+
14+
PCD8544_FUNCTION_SET = const(0x20)
15+
PCD8544_POWER_DOWN = const(0x04)
16+
PCD8544_ADDRESSING_VERT = const(0x02)
17+
PCD8544_EXTENDED_INSTR = const(0x01)
18+
19+
PCD8544_DISPLAY_BLANK = const(0x08)
20+
PCD8544_DISPLAY_ALL = const(0x09)
21+
PCD8544_DISPLAY_NORMAL = const(0x0C)
22+
PCD8544_DISPLAY_INVERSE = const(0x0D)
23+
24+
PCD8544_TEMP_COEFF_0 = const(0x04)
25+
PCD8544_TEMP_COEFF_1 = const(0x05)
26+
PCD8544_TEMP_COEFF_2 = const(0x06)
27+
PCD8544_TEMP_COEFF_3 = const(0x07)
28+
29+
PCD8544_BIAS_1_100 = const(0x10)
30+
PCD8544_BIAS_1_80 = const(0x11)
31+
PCD8544_BIAS_1_65 = const(0x12)
32+
PCD8544_BIAS_1_48 = const(0x13)
33+
PCD8544_BIAS_1_40 = const(0x14)
34+
PCD8544_BIAS_1_24 = const(0x15)
35+
PCD8544_BIAS_1_18 = const(0x16)
36+
PCD8544_BIAS_1_10 = const(0x17)
37+
PCD8544_SET_VOP = const(0x80)
38+
PCD8544_COL_ADDR = const(0x80)
39+
PCD8544_BANK_ADDR = const(0x40)
40+
41+
CMD = False
42+
DAT = True
43+
44+
LOW = False
45+
HIGH = True
46+
47+
PCD8544_GLCD_WIDTH = const(84)
48+
PCD8544_GLCD_HEIGHT = const(48)
49+
50+
51+
class PCD8544(framebuf.FrameBuffer):
52+
def __init__(self):
53+
self.width = PCD8544_GLCD_WIDTH
54+
self.height = PCD8544_GLCD_HEIGHT
55+
56+
self.BLACK = const(0xFF)
57+
self.WHITE = const(0x00)
58+
59+
self.PCD8544_CS = Pin(PCD8544_CS_pin, Pin.OUT_PP)
60+
self.PCD8544_RST = Pin(PCD8544_RST_pin, Pin.OUT_PP)
61+
self.PCD8544_SCK = Pin(PCD8544_SCK_pin, Pin.OUT_PP)
62+
self.PCD8544_MOSI = Pin(PCD8544_MOSI_pin, Pin.OUT_PP)
63+
64+
self.PCD8544_SPI = SPI(1, SPI.CONTROLLER, baudrate = 4_000_000, polarity = 0, phase = 0)
65+
66+
self.PCD8544_DC = Pin(PCD8544_DC_pin, Pin.OUT_PP)
67+
68+
self.buffer = bytearray((self.height * self.width) // 8) #84 x 48 = 504
69+
super().__init__(self.buffer, self.width, self.height, framebuf.MONO_VLSB)
70+
71+
self.init()
72+
73+
74+
75+
def init(self):
76+
self.function = PCD8544_FUNCTION_SET
77+
self.disp_reset()
78+
self.addressing(True)
79+
self.set_contrast(PCD8544_BIAS_1_40, PCD8544_TEMP_COEFF_2, 63)
80+
self.disp_invert(False)
81+
self.clear()
82+
83+
84+
def disp_reset(self):
85+
self.PCD8544_CS.value(HIGH)
86+
self.PCD8544_DC.value(HIGH)
87+
88+
self.PCD8544_RST.value(HIGH)
89+
sleep_ms(10)
90+
self.PCD8544_RST.value(LOW)
91+
sleep_ms(20)
92+
self.PCD8544_RST.value(HIGH)
93+
94+
95+
def send(self, value, mode):
96+
self.PCD8544_CS.value(LOW)
97+
self.PCD8544_DC.value(mode)
98+
99+
if(mode == DAT):
100+
self.PCD8544_SPI.write(pack('B'*len(value), *value))
101+
else:
102+
self.PCD8544_SPI.write(bytearray([value]))
103+
104+
self.PCD8544_CS.value(HIGH)
105+
106+
107+
def set_xy(self, x_pos, y_pos):
108+
self.send((PCD8544_COL_ADDR | x_pos), CMD)
109+
self.send((PCD8544_BANK_ADDR | y_pos), CMD)
110+
111+
112+
def addressing(self, horizontal_or_vertical = True):
113+
if(horizontal_or_vertical == True):
114+
self.function &= ~PCD8544_ADDRESSING_VERT
115+
else:
116+
self.function |= PCD8544_ADDRESSING_VERT
117+
118+
self.send(self.function, CMD)
119+
120+
121+
def clear(self):
122+
self.send(([0] * 504), DAT) #84 x 48 = 504
123+
self.set_xy(0, 0)
124+
125+
126+
def disp_invert(self, mode):
127+
if(mode == True):
128+
self.send(PCD8544_DISPLAY_INVERSE, CMD)
129+
else:
130+
self.send(PCD8544_DISPLAY_NORMAL, CMD)
131+
132+
133+
def disp_power(self, mode):
134+
if(mode == True):
135+
self.function &= ~PCD8544_POWER_DOWN
136+
else:
137+
self.function |= PCD8544_POWER_DOWN
138+
139+
self.send(self.function, CMD)
140+
141+
142+
def set_contrast(self, bias = PCD8544_BIAS_1_40, t_coff = PCD8544_TEMP_COEFF_2, contrast = 63):
143+
self.send((self.function | PCD8544_EXTENDED_INSTR), CMD)
144+
self.send(t_coff, CMD)
145+
self.send(bias, CMD)
146+
self.send(PCD8544_SET_VOP | contrast, CMD)
147+
self.send((self.function & ~PCD8544_EXTENDED_INSTR), CMD)
148+
149+
150+
151+
def show(self):
152+
self.send(self.buffer, DAT)
153+
Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
from micropython import const
2+
from utime import sleep_ms, ticks_ms
3+
4+
5+
ToF_TX_data_packet_size = const(32)
6+
ToF_RX_data_packet_size = const(32)
7+
8+
ToF_output_frame_header = const(0x57)
9+
ToF_output_function_mark = const(0x00)
10+
11+
ToF_inquire_function_mark = const(0x10)
12+
13+
ToF_settings_frame_header = const(0x54)
14+
ToF_settings_function_mark = const(0x20)
15+
16+
Tof_output_mode_active = const(0x00)
17+
Tof_output_mode_inquire = const(0x02)
18+
19+
Tof_output_range_mode_short = const(0x00)
20+
Tof_output_range_mode_medium = const(0x08)
21+
Tof_output_range_mode_long = const(0x04)
22+
23+
Tof_output_interface_UART = const(0x00)
24+
Tof_output_interface_CAN = const(0x01)
25+
Tof_output_interface_IO = const(0x10)
26+
Tof_output_interface_I2C = const(0x11)
27+
28+
Tof_UART_Baud_115200 = const(115200)
29+
Tof_UART_Baud_230400 = const(230400)
30+
Tof_UART_Baud_460800 = const(460800)
31+
Tof_UART_Baud_921600 = const(921600)
32+
33+
Tof_CAN_Baud_1000000 = const(1000000)
34+
Tof_CAN_Baud_1200000 = const(1200000)
35+
Tof_CAN_Baud_1500000 = const(1500000)
36+
Tof_CAN_Baud_2000000 = const(2000000)
37+
Tof_CAN_Baud_3000000 = const(3000000)
38+
39+
40+
class waveshare_ToF():
41+
42+
def __init__(self, _uart):
43+
self.tx_data_frame = bytearray(ToF_TX_data_packet_size)
44+
self.rx_data_frame = bytearray(ToF_RX_data_packet_size)
45+
46+
self.uart = _uart
47+
48+
49+
def settings(self,
50+
ID = 0,
51+
terminal_time = ticks_ms(),
52+
mode = (Tof_output_mode_inquire | Tof_output_range_mode_long | Tof_output_interface_UART),
53+
baud_rate = Tof_UART_Baud_115200,
54+
band_start = 0,
55+
bandwidth = 0):
56+
57+
chk_sum = 0
58+
current_tick = terminal_time
59+
baud_value = baud_rate
60+
bs = band_start
61+
bw = bandwidth
62+
63+
self.tx_data_frame[0] = ToF_settings_frame_header
64+
self.tx_data_frame[1] = ToF_settings_function_mark
65+
self.tx_data_frame[2] = 0
66+
self.tx_data_frame[3] = 0xFF
67+
self.tx_data_frame[4] = ID
68+
self.tx_data_frame[5] = (current_tick & 0x000F)
69+
self.tx_data_frame[6] = ((current_tick & 0x00F0) >> 8)
70+
self.tx_data_frame[7] = ((current_tick & 0x0F00) >> 16)
71+
self.tx_data_frame[8] = ((current_tick & 0xF000) >> 24)
72+
self.tx_data_frame[9] = mode
73+
self.tx_data_frame[10] = 0xFF
74+
self.tx_data_frame[11] = 0xFF
75+
self.tx_data_frame[12] = (baud_value & 0x000F)
76+
self.tx_data_frame[13] = ((baud_value & 0x00F0) >> 8)
77+
self.tx_data_frame[14] = ((baud_value & 0x0F00) >> 16)
78+
self.tx_data_frame[15] = 0xFF
79+
self.tx_data_frame[16] = 0xFF
80+
self.tx_data_frame[17] = 0xFF
81+
self.tx_data_frame[18] = 0xFF
82+
self.tx_data_frame[19] = (bs & 0x000F)
83+
self.tx_data_frame[20] = ((bs & 0x00F0) >> 8)
84+
self.tx_data_frame[21] = (bw & 0x000F)
85+
self.tx_data_frame[22] = ((bw & 0x00F0) >> 8)
86+
self.tx_data_frame[23] = 0xFF
87+
self.tx_data_frame[24] = 0xFF
88+
self.tx_data_frame[25] = 0xFF
89+
self.tx_data_frame[26] = 0xFF
90+
self.tx_data_frame[27] = 0xFF
91+
self.tx_data_frame[28] = 0xFF
92+
self.tx_data_frame[29] = 0xFF
93+
self.tx_data_frame[30] = 0xFF
94+
95+
for i in range(0, 31):
96+
chk_sum += self.tx_data_frame[i]
97+
98+
self.tx_data_frame[31] = (chk_sum & 0xFF)
99+
100+
self.uart.write(self.tx_data_frame)
101+
sleep_ms(60)
102+
103+
chk_sum = 0
104+
current_tick = 0
105+
baud_value = 0
106+
bs = 0
107+
bw = 0
108+
109+
if(self.uart.any() > 0x00):
110+
self.rx_data_frame = self.uart.read(ToF_RX_data_packet_size)
111+
112+
if(self.rx_data_frame[0] == ToF_settings_frame_header):
113+
114+
if(self.rx_data_frame[1] == ToF_settings_function_mark):
115+
116+
if(self.rx_data_frame[2] == 1):
117+
118+
ID = self.rx_data_frame[4]
119+
current_tick = ((self.rx_data_frame[8] << 24) |
120+
(self.rx_data_frame[7] << 16) |
121+
(self.rx_data_frame[6] << 8) |
122+
self.rx_data_frame[5])
123+
124+
mode = self.rx_data_frame[9]
125+
baud_value = ((self.rx_data_frame[14] << 16) |
126+
(self.rx_data_frame[13] << 8) |
127+
self.rx_data_frame[12])
128+
129+
bs = ((self.rx_data_frame[20] << 8) |
130+
self.rx_data_frame[19])
131+
132+
bw = ((self.rx_data_frame[22] << 8) |
133+
self.rx_data_frame[21])
134+
135+
for i in range(0, 31):
136+
chk_sum += self.rx_data_frame[i]
137+
138+
chk_sum = (chk_sum & 0xFF)
139+
140+
if(chk_sum == self.rx_data_frame[31]):
141+
return ID, current_tick, mode, baud_value, bs, bw
142+
143+
else:
144+
return 0, 0, 0, 0, 0, 0
145+
146+
return -1, -1, -1, -1, -1, -1
147+
148+
149+
def inquire_sensor(self, ID = 0, delay = 60):
150+
chk_sum = 0
151+
152+
self.tx_data_frame[0] = ToF_output_frame_header
153+
self.tx_data_frame[1] = ToF_inquire_function_mark
154+
self.tx_data_frame[2] = 0xFF
155+
self.tx_data_frame[3] = 0xFF
156+
self.tx_data_frame[4] = ID
157+
self.tx_data_frame[5] = 0xFF
158+
self.tx_data_frame[6] = 0xFF
159+
160+
for i in range(0, 7):
161+
chk_sum += self.tx_data_frame[i]
162+
163+
self.tx_data_frame[7] = chk_sum
164+
165+
self.uart.write(self.tx_data_frame)
166+
sleep_ms(delay)
167+
168+
169+
170+
def read_sensor(self, delay = 400):
171+
ID = 0
172+
sys_time = 0
173+
distance = 0
174+
status = 0
175+
sig_strength = 0
176+
precision = 0
177+
chk_sum = 0
178+
179+
if(self.uart.any() > 0x00):
180+
self.rx_data_frame = self.uart.read(ToF_RX_data_packet_size)
181+
182+
if(self.rx_data_frame[0] == ToF_output_frame_header):
183+
184+
if(self.rx_data_frame[1] == ToF_output_function_mark):
185+
186+
if(self.rx_data_frame[2] == 0xFF):
187+
ID = self.rx_data_frame[3]
188+
189+
sys_time = ((self.rx_data_frame[7] << 24) |
190+
(self.rx_data_frame[6] << 16) |
191+
(self.rx_data_frame[5] << 8) |
192+
self.rx_data_frame[4])
193+
194+
distance = ((self.rx_data_frame[10] << 16) |
195+
(self.rx_data_frame[9] << 8) |
196+
self.rx_data_frame[8])
197+
198+
199+
status = self.rx_data_frame[11]
200+
sig_strength = ((self.rx_data_frame[13] << 8) |
201+
self.rx_data_frame[12])
202+
203+
precision = self.rx_data_frame[14]
204+
205+
for i in range(0, 15):
206+
chk_sum += self.rx_data_frame[i]
207+
208+
chk_sum = (chk_sum & 0xFF)
209+
sleep_ms(delay)
210+
211+
if(chk_sum == self.rx_data_frame[15]):
212+
return ID, sys_time, distance, status, sig_strength, precision
213+
214+
else:
215+
return 0, 0, 0, 0, 0, 0
216+
217+
sleep_ms(delay)
218+
return -1, -1, -1, -1, -1, -1
219+
220+
221+
222+
223+
224+

0 commit comments

Comments
 (0)